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