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