]> git.saurik.com Git - apple/mdnsresponder.git/blob - Clients/DNSServiceBrowser.NET/DNSServiceBrowser.cs
18aa3e71280c0d6623722d77d7dd5928dcccad9a
[apple/mdnsresponder.git] / Clients / DNSServiceBrowser.NET / DNSServiceBrowser.cs
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
4 *
5 * Licensed under the Apache License, Version 2.0 (the "License");
6 * you may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
8 *
9 * http://www.apache.org/licenses/LICENSE-2.0
10 *
11 * Unless required by applicable law or agreed to in writing, software
12 * distributed under the License is distributed on an "AS IS" BASIS,
13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 * See the License for the specific language governing permissions and
15 * limitations under the License.
16 */
17
18 using System;
19 using System.Drawing;
20 using System.Collections;
21 using System.ComponentModel;
22 using System.Windows.Forms;
23 using System.Data;
24 using System.Text;
25 using Bonjour;
26
27 namespace DNSServiceBrowser_NET
28 {
29 /// <summary>
30 /// Summary description for Form1.
31 /// </summary>
32 public class Form1 : System.Windows.Forms.Form
33 {
34 private System.Windows.Forms.ComboBox typeBox;
35 private System.Windows.Forms.ListBox browseList;
36 private Bonjour.DNSSDEventManager eventManager = null;
37 private Bonjour.DNSSDService service = null;
38 private Bonjour.DNSSDService browser = null;
39 private Bonjour.DNSSDService resolver = null;
40 /// <summary>
41 /// Required designer variable.
42 /// </summary>
43 private System.ComponentModel.Container components = null;
44
45 private System.Windows.Forms.Label label1;
46 private System.Windows.Forms.Label label2;
47 private System.Windows.Forms.Label label3;
48 private System.Windows.Forms.Label label4;
49 private System.Windows.Forms.TextBox nameField;
50 private System.Windows.Forms.TextBox typeField;
51 private System.Windows.Forms.TextBox domainField;
52 private System.Windows.Forms.TextBox hostField;
53 private System.Windows.Forms.TextBox portField;
54 private System.Windows.Forms.Label label5;
55 private System.Windows.Forms.ListBox serviceTextField;
56
57 public Form1()
58 {
59 //
60 // Required for Windows Form Designer support
61 //
62 InitializeComponent();
63
64 this.Load += new System.EventHandler(this.Form1_Load);
65
66 eventManager = new DNSSDEventManager();
67 eventManager.ServiceFound += new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
68 eventManager.ServiceLost += new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
69 eventManager.ServiceResolved += new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
70 eventManager.OperationFailed += new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
71
72 service = new DNSSDService();
73 }
74
75 private void Form1_Load(object sender, EventArgs e)
76 {
77 typeBox.SelectedItem = "_http._tcp";
78 }
79
80
81 /// <summary>
82 /// Clean up any resources being used.
83 /// </summary>
84 protected override void Dispose( bool disposing )
85 {
86 if( disposing )
87 {
88 if (components != null)
89 {
90 components.Dispose();
91 }
92
93 if (resolver != null)
94 {
95 resolver.Stop();
96 }
97
98 if (browser != null)
99 {
100 browser.Stop();
101 }
102
103 if (service != null)
104 {
105 service.Stop();
106 }
107
108 eventManager.ServiceFound -= new _IDNSSDEvents_ServiceFoundEventHandler(this.ServiceFound);
109 eventManager.ServiceLost -= new _IDNSSDEvents_ServiceLostEventHandler(this.ServiceLost);
110 eventManager.ServiceResolved -= new _IDNSSDEvents_ServiceResolvedEventHandler(this.ServiceResolved);
111 eventManager.OperationFailed -= new _IDNSSDEvents_OperationFailedEventHandler(this.OperationFailed);
112 }
113 base.Dispose( disposing );
114 }
115
116 #region Windows Form Designer generated code
117 /// <summary>
118 /// Required method for Designer support - do not modify
119 /// the contents of this method with the code editor.
120 /// </summary>
121 private void InitializeComponent()
122 {
123 this.browseList = new System.Windows.Forms.ListBox();
124 this.typeBox = new System.Windows.Forms.ComboBox();
125 this.label1 = new System.Windows.Forms.Label();
126 this.label2 = new System.Windows.Forms.Label();
127 this.label3 = new System.Windows.Forms.Label();
128 this.label4 = new System.Windows.Forms.Label();
129 this.nameField = new System.Windows.Forms.TextBox();
130 this.typeField = new System.Windows.Forms.TextBox();
131 this.domainField = new System.Windows.Forms.TextBox();
132 this.hostField = new System.Windows.Forms.TextBox();
133 this.portField = new System.Windows.Forms.TextBox();
134 this.label5 = new System.Windows.Forms.Label();
135 this.serviceTextField = new System.Windows.Forms.ListBox();
136 this.SuspendLayout();
137 //
138 // browseList
139 //
140 this.browseList.Location = new System.Drawing.Point(8, 48);
141 this.browseList.Name = "browseList";
142 this.browseList.Size = new System.Drawing.Size(488, 147);
143 this.browseList.TabIndex = 0;
144 this.browseList.SelectedIndexChanged += new System.EventHandler(this.listBox1_SelectedIndexChanged);
145 //
146 // typeBox
147 //
148 this.typeBox.Items.AddRange(new object[]
149 {
150 "_accessone._tcp",
151 "_accountedge._tcp",
152 "_actionitems._tcp",
153 "_addressbook._tcp",
154 "_aecoretech._tcp",
155 "_afpovertcp._tcp",
156 "_airport._tcp",
157 "_animolmd._tcp",
158 "_animobserver._tcp",
159 "_apple-sasl._tcp",
160 "_aquamon._tcp",
161 "_async._tcp",
162 "_auth._tcp",
163 "_beep._tcp",
164 "_bfagent._tcp",
165 "_bootps._udp",
166 "_bousg._tcp",
167 "_bsqdea._tcp",
168 "_cheat._tcp",
169 "_chess._tcp",
170 "_clipboard._tcp",
171 "_collection._tcp",
172 "_contactserver._tcp",
173 "_cvspserver._tcp",
174 "_cytv._tcp",
175 "_daap._tcp",
176 "_difi._tcp",
177 "_distcc._tcp",
178 "_dossier._tcp",
179 "_dpap._tcp",
180 "_earphoria._tcp",
181 "_ebms._tcp",
182 "_ebreg._tcp",
183 "_ecbyesfsgksc._tcp",
184 "_eheap._tcp",
185 "_embrace._tcp",
186 "_eppc._tcp",
187 "_eventserver._tcp",
188 "_exec._tcp",
189 "_facespan._tcp",
190 "_faxstfx._tcp",
191 "_fish._tcp",
192 "_fjork._tcp",
193 "_fmpro-internal._tcp",
194 "_ftp._tcp",
195 "_ftpcroco._tcp",
196 "_gbs-smp._tcp",
197 "_gbs-stp._tcp",
198 "_grillezvous._tcp",
199 "_h323._tcp",
200 "_http._tcp",
201 "_hotwayd._tcp",
202 "_hydra._tcp",
203 "_ica-networking._tcp",
204 "_ichalkboard._tcp",
205 "_ichat._tcp",
206 "_iconquer._tcp",
207 "_ifolder._tcp",
208 "_ilynx._tcp",
209 "_imap._tcp",
210 "_imidi._tcp",
211 "_ipbroadcaster._tcp",
212 "_ipp._tcp",
213 "_isparx._tcp",
214 "_ispq-vc._tcp",
215 "_ishare._tcp",
216 "_isticky._tcp",
217 "_istorm._tcp",
218 "_iwork._tcp",
219 "_lan2p._tcp",
220 "_ldap._tcp",
221 "_liaison._tcp",
222 "_login._tcp",
223 "_lontalk._tcp",
224 "_lonworks._tcp",
225 "_macfoh-remote._tcp",
226 "_macminder._tcp",
227 "_moneyworks._tcp",
228 "_mp3sushi._tcp",
229 "_mttp._tcp",
230 "_ncbroadcast._tcp",
231 "_ncdirect._tcp",
232 "_ncsyncserver._tcp",
233 "_net-assistant._tcp",
234 "_newton-dock._tcp",
235 "_nfs._udp",
236 "_nssocketport._tcp",
237 "_odabsharing._tcp",
238 "_omni-bookmark._tcp",
239 "_openbase._tcp",
240 "_p2pchat._udp",
241 "_pdl-datastream._tcp",
242 "_poch._tcp",
243 "_pop3._tcp",
244 "_postgresql._tcp",
245 "_presence._tcp",
246 "_printer._tcp",
247 "_ptp._tcp",
248 "_quinn._tcp",
249 "_raop._tcp",
250 "_rce._tcp",
251 "_realplayfavs._tcp",
252 "_riousbprint._tcp",
253 "_rfb._tcp",
254 "_rtsp._tcp",
255 "_safarimenu._tcp",
256 "_sallingclicker._tcp",
257 "_scone._tcp",
258 "_sdsharing._tcp",
259 "_see._tcp",
260 "_seeCard._tcp",
261 "_serendipd._tcp",
262 "_servermgr._tcp",
263 "_shell._tcp",
264 "_shout._tcp",
265 "_shoutcast._tcp",
266 "_soap._tcp",
267 "_spike._tcp",
268 "_spincrisis._tcp",
269 "_spl-itunes._tcp",
270 "_spr-itunes._tcp",
271 "_ssh._tcp",
272 "_ssscreenshare._tcp",
273 "_strateges._tcp",
274 "_sge-exec._tcp",
275 "_sge-qmaster._tcp",
276 "_stickynotes._tcp",
277 "_sxqdea._tcp",
278 "_sybase-tds._tcp",
279 "_teamlist._tcp",
280 "_teleport._udp",
281 "_telnet._tcp",
282 "_tftp._udp",
283 "_ticonnectmgr._tcp",
284 "_tinavigator._tcp",
285 "_tryst._tcp",
286 "_upnp._tcp",
287 "_utest._tcp",
288 "_vue4rendercow._tcp",
289 "_webdav._tcp",
290 "_whamb._tcp",
291 "_wired._tcp",
292 "_workstation._tcp",
293 "_wormhole._tcp",
294 "_workgroup._tcp",
295 "_ws._tcp",
296 "_xserveraid._tcp",
297 "_xsync._tcp",
298 "_xtshapro._tcp"
299 });
300
301 this.typeBox.Location = new System.Drawing.Point(8, 16);
302 this.typeBox.Name = "typeBox";
303 this.typeBox.Size = new System.Drawing.Size(192, 21);
304 this.typeBox.Sorted = true;
305 this.typeBox.TabIndex = 3;
306 this.typeBox.SelectedIndexChanged += new System.EventHandler(this.typeBox_SelectedIndexChanged);
307 //
308 // label1
309 //
310 this.label1.Location = new System.Drawing.Point(8, 208);
311 this.label1.Name = "label1";
312 this.label1.Size = new System.Drawing.Size(48, 16);
313 this.label1.TabIndex = 4;
314 this.label1.Text = "Name:";
315 this.label1.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
316 //
317 // label2
318 //
319 this.label2.Location = new System.Drawing.Point(8, 240);
320 this.label2.Name = "label2";
321 this.label2.Size = new System.Drawing.Size(48, 16);
322 this.label2.TabIndex = 5;
323 this.label2.Text = "Type:";
324 this.label2.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
325 //
326 // label3
327 //
328 this.label3.Location = new System.Drawing.Point(8, 272);
329 this.label3.Name = "label3";
330 this.label3.Size = new System.Drawing.Size(48, 16);
331 this.label3.TabIndex = 6;
332 this.label3.Text = "Domain:";
333 this.label3.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
334 //
335 // label4
336 //
337 this.label4.Location = new System.Drawing.Point(8, 304);
338 this.label4.Name = "label4";
339 this.label4.Size = new System.Drawing.Size(48, 16);
340 this.label4.TabIndex = 7;
341 this.label4.Text = "Host:";
342 this.label4.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
343 //
344 // nameField
345 //
346 this.nameField.Location = new System.Drawing.Point(56, 208);
347 this.nameField.Name = "nameField";
348 this.nameField.ReadOnly = true;
349 this.nameField.Size = new System.Drawing.Size(168, 20);
350 this.nameField.TabIndex = 8;
351 this.nameField.Text = "";
352 //
353 // typeField
354 //
355 this.typeField.Location = new System.Drawing.Point(56, 240);
356 this.typeField.Name = "typeField";
357 this.typeField.ReadOnly = true;
358 this.typeField.Size = new System.Drawing.Size(168, 20);
359 this.typeField.TabIndex = 9;
360 this.typeField.Text = "";
361 //
362 // domainField
363 //
364 this.domainField.Location = new System.Drawing.Point(56, 272);
365 this.domainField.Name = "domainField";
366 this.domainField.ReadOnly = true;
367 this.domainField.Size = new System.Drawing.Size(168, 20);
368 this.domainField.TabIndex = 10;
369 this.domainField.Text = "";
370 //
371 // hostField
372 //
373 this.hostField.Location = new System.Drawing.Point(56, 304);
374 this.hostField.Name = "hostField";
375 this.hostField.ReadOnly = true;
376 this.hostField.Size = new System.Drawing.Size(168, 20);
377 this.hostField.TabIndex = 11;
378 this.hostField.Text = "";
379
380 //
381 // portField
382 //
383 this.portField.Location = new System.Drawing.Point(56, 336);
384 this.portField.Name = "portField";
385 this.portField.ReadOnly = true;
386 this.portField.Size = new System.Drawing.Size(168, 20);
387 this.portField.TabIndex = 12;
388 this.portField.Text = "";
389 //
390 // label5
391 //
392 this.label5.Location = new System.Drawing.Point(8, 336);
393 this.label5.Name = "label5";
394 this.label5.Size = new System.Drawing.Size(48, 16);
395 this.label5.TabIndex = 14;
396 this.label5.Text = "Port:";
397 this.label5.TextAlign = System.Drawing.ContentAlignment.MiddleLeft;
398 //
399 // serviceTextField
400 //
401 this.serviceTextField.HorizontalScrollbar = true;
402 this.serviceTextField.Location = new System.Drawing.Point(264, 208);
403 this.serviceTextField.Name = "serviceTextField";
404 this.serviceTextField.Size = new System.Drawing.Size(232, 147);
405 this.serviceTextField.TabIndex = 16;
406 //
407 // Form1
408 //
409 this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
410 this.ClientSize = new System.Drawing.Size(512, 365);
411 this.Controls.AddRange(new System.Windows.Forms.Control[] {
412 this.serviceTextField,
413 this.label5,
414 this.portField,
415 this.hostField,
416 this.domainField,
417 this.typeField,
418 this.nameField,
419 this.label4,
420 this.label3,
421 this.label2,
422 this.label1,
423 this.typeBox,
424 this.browseList});
425 this.Name = "Form1";
426 this.Text = "DNSServices Browser";
427 this.ResumeLayout(false);
428
429 }
430 #endregion
431
432 /// <summary>
433 /// The main entry point for the application.
434 /// </summary>
435 [STAThread]
436 static void Main()
437 {
438 Application.Run(new Form1());
439 }
440 //
441 // BrowseData
442 //
443 // This class is used to store data associated
444 // with a DNSService.Browse() operation
445 //
446 public class BrowseData
447 {
448 public uint InterfaceIndex;
449 public String Name;
450 public String Type;
451 public String Domain;
452
453 public override String
454 ToString()
455 {
456 return Name;
457 }
458
459 public override bool
460 Equals(object other)
461 {
462 bool result = false;
463
464 if (other != null)
465 {
466 if ((object) this == other)
467 {
468 result = true;
469 }
470 else if (other is BrowseData)
471 {
472 BrowseData otherBrowseData = (BrowseData) other;
473
474 result = (this.Name == otherBrowseData.Name);
475 }
476 }
477
478 return result;
479 }
480
481 public override int
482 GetHashCode()
483 {
484 return Name.GetHashCode();
485 }
486 };
487
488
489 //
490 // ResolveData
491 //
492 // This class is used to store data associated
493 // with a DNSService.Resolve() operation
494 //
495 public class ResolveData
496 {
497 public uint InterfaceIndex;
498 public String FullName;
499 public String HostName;
500 public int Port;
501 public TXTRecord TxtRecord;
502
503 public override String
504 ToString()
505 {
506 return FullName;
507 }
508 };
509
510 //
511 // Populate()
512 //
513 // Populate this form with data associated with a
514 // DNSService.Resolve() call
515 //
516 public void
517 Populate(BrowseData browseData, ResolveData resolveData)
518 {
519 nameField.Text = browseData.Name;
520 typeField.Text = browseData.Type;
521 domainField.Text = browseData.Domain;
522 hostField.Text = resolveData.HostName;
523 portField.Text = resolveData.Port.ToString();
524
525 serviceTextField.Items.Clear();
526
527 if (resolveData.TxtRecord != null)
528 {
529 for (uint idx = 0; idx < resolveData.TxtRecord.GetCount(); idx++)
530 {
531 String key;
532 Byte[] bytes;
533
534 key = resolveData.TxtRecord.GetKeyAtIndex(idx);
535 bytes = (Byte[])resolveData.TxtRecord.GetValueAtIndex(idx);
536
537 if (key.Length > 0)
538 {
539 String val = "";
540
541 if (bytes != null)
542 {
543 val = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
544 }
545
546 serviceTextField.Items.Add(key + "=" + val);
547 }
548 }
549 }
550 }
551
552 //
553 // called when the type field changes
554 //
555 private void typeBox_SelectedIndexChanged(object sender, System.EventArgs e)
556 {
557 browseList.Items.Clear();
558
559 if (browser != null)
560 {
561 browser.Stop();
562 }
563
564 nameField.Text = "";
565 typeField.Text = "";
566 domainField.Text = "";
567 hostField.Text = "";
568 portField.Text = "";
569 serviceTextField.Items.Clear();
570
571 try
572 {
573 browser = service.Browse( 0, 0, typeBox.SelectedItem.ToString(), null, eventManager );
574 }
575 catch
576 {
577 MessageBox.Show("Browse Failed", "Error");
578 Application.Exit();
579 }
580 }
581
582 private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
583 {
584 if (resolver != null)
585 {
586 resolver.Stop();
587 resolver = null;
588 }
589
590 if (browseList.SelectedItem != null)
591 {
592 try
593 {
594 BrowseData data = (BrowseData) browseList.SelectedItem;
595
596 resolver = service.Resolve(0, data.InterfaceIndex, data.Name, data.Type, data.Domain, eventManager);
597 }
598 catch
599 {
600 MessageBox.Show("Resolve Failed", "Error");
601 Application.Exit();
602 }
603 }
604 }
605
606 //
607 // ServiceFound
608 //
609 // This call is invoked by the DNSService core. We create
610 // a BrowseData object and invoked the appropriate method
611 // in the GUI thread so we can update the UI
612 //
613 public void ServiceFound
614 (
615 DNSSDService sref,
616 DNSSDFlags flags,
617 uint ifIndex,
618 String serviceName,
619 String regType,
620 String domain
621 )
622 {
623 BrowseData data = new BrowseData();
624
625 data.InterfaceIndex = ifIndex;
626 data.Name = serviceName;
627 data.Type = regType;
628 data.Domain = domain;
629
630 browseList.Items.Add(data);
631 browseList.Invalidate();
632 }
633
634 public void ServiceLost
635 (
636 DNSSDService sref,
637 DNSSDFlags flags,
638 uint ifIndex,
639 String serviceName,
640 String regType,
641 String domain
642 )
643 {
644 BrowseData data = new BrowseData();
645
646 data.InterfaceIndex = ifIndex;
647 data.Name = serviceName;
648 data.Type = regType;
649 data.Domain = domain;
650
651 browseList.Items.Remove(data);
652 browseList.Invalidate();
653 }
654
655 public void ServiceResolved
656 (
657 DNSSDService sref,
658 DNSSDFlags flags,
659 uint ifIndex,
660 String fullName,
661 String hostName,
662 ushort port,
663 TXTRecord txtRecord
664 )
665 {
666 ResolveData data = new ResolveData();
667
668 data.InterfaceIndex = ifIndex;
669 data.FullName = fullName;
670 data.HostName = hostName;
671 data.Port = port;
672 data.TxtRecord = txtRecord;
673
674 resolver.Stop();
675 resolver = null;
676
677 Populate((BrowseData) browseList.SelectedItem, data);
678 }
679
680 public void OperationFailed
681 (
682 DNSSDService sref,
683 DNSSDError error
684 )
685 {
686 MessageBox.Show("Operation failed: error code: " + error, "Error");
687 }
688 }
689 }