1 /* -*- Mode: C; tab-width: 4 -*-
3 * Copyright (c) 1997-2004 Apple Computer, Inc. All rights reserved.
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
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 Change History (most recent first):
19 $Log: DNSServiceBrowser.cs,v $
20 Revision 1.7 2006/08/14 23:23:58 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
23 Revision 1.6 2005/02/10 22:35:06 cheshire
24 <rdar://problem/3727944> Update name
26 Revision 1.5 2004/09/21 16:26:58 shersche
27 Check to make sure browse list selected item is not null before resolving
28 Submitted by: prepin@gmail.com
30 Revision 1.4 2004/09/13 19:38:17 shersche
31 Changed code to reflect namespace and type changes to dnssd.NET library
33 Revision 1.3 2004/09/11 00:38:14 shersche
34 DNSService APIs now assume port in host format. Check for null text record in resolve callback.
36 Revision 1.2 2004/07/22 23:15:25 shersche
37 Fix service names for teleport, tftp, and bootps
39 Revision 1.1 2004/07/19 07:54:24 shersche
48 using System.Collections;
49 using System.ComponentModel;
50 using System.Windows.Forms;
55 namespace DNSServiceBrowser_NET
58 /// Summary description for Form1.
60 public class Form1 : System.Windows.Forms.Form
62 private System.Windows.Forms.ComboBox typeBox;
63 private System.Windows.Forms.ListBox browseList;
64 private ServiceRef browser = null;
65 private ServiceRef resolver = null;
67 /// Required designer variable.
69 private System.ComponentModel.Container components = null;
73 // These delegates are invoked as a result of DNSService
76 delegate void AddServiceCallback(BrowseData data);
77 delegate void RemoveServiceCallback(BrowseData data);
78 delegate void ResolveServiceCallback(ResolveData data);
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;
98 // Required for Windows Form Designer support
100 InitializeComponent();
102 addServiceCallback = new AddServiceCallback(OnAddService);
103 removeServiceCallback = new RemoveServiceCallback(OnRemoveService);
104 resolveServiceCallback = new ResolveServiceCallback(OnResolveService);
106 this.Load += new System.EventHandler(this.Form1_Load);
109 private void Form1_Load(object sender, EventArgs e)
113 typeBox.SelectedItem = "_spike._tcp";
119 /// Clean up any resources being used.
121 protected override void Dispose( bool disposing )
125 if (components != null)
127 components.Dispose();
135 base.Dispose( disposing );
138 #region Windows Form Designer generated code
140 /// Required method for Designer support - do not modify
141 /// the contents of this method with the code editor.
143 private void InitializeComponent()
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();
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);
170 this.typeBox.Items.AddRange(new object[]
180 "_animobserver._tcp",
194 "_contactserver._tcp",
205 "_ecbyesfsgksc._tcp",
215 "_fmpro-internal._tcp",
225 "_ica-networking._tcp",
233 "_ipbroadcaster._tcp",
247 "_macfoh-remote._tcp",
254 "_ncsyncserver._tcp",
255 "_net-assistant._tcp",
258 "_nssocketport._tcp",
260 "_omni-bookmark._tcp",
263 "_pdl-datastream._tcp",
273 "_realplayfavs._tcp",
278 "_sallingclicker._tcp",
294 "_ssscreenshare._tcp",
305 "_ticonnectmgr._tcp",
310 "_vue4rendercow._tcp",
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);
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;
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;
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;
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;
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 = "";
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 = "";
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 = "";
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 = "";
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 = "";
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;
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;
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,
448 this.Text = "DNSServices Browser";
449 this.ResumeLayout(false);
455 /// The main entry point for the application.
460 Application.Run(new Form1());
465 // This class is used to store data associated
466 // with a DNSService.Browse() operation
468 public class BrowseData
470 public int InterfaceIndex;
473 public String Domain;
475 public override String
488 if ((object) this == other)
492 else if (other is BrowseData)
494 BrowseData otherBrowseData = (BrowseData) other;
496 result = (this.Name == otherBrowseData.Name);
506 return Name.GetHashCode();
514 // This class is used to store data associated
515 // with a DNSService.Resolve() operation
517 public class ResolveData
519 public int InterfaceIndex;
520 public String FullName;
521 public String HostName;
523 public Byte[] TxtRecord;
525 public override String
535 // Populate this form with data associated with a
536 // DNSService.Resolve() call
539 Populate(BrowseData browseData, ResolveData resolveData)
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();
547 serviceTextField.Items.Clear();
549 if (resolveData.TxtRecord != null)
551 for (int idx = 0; idx < TextRecord.GetCount(resolveData.TxtRecord); idx++)
556 bytes = TextRecord.GetItemAtIndex(resolveData.TxtRecord, idx, out key);
564 val = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
567 serviceTextField.Items.Add(key + "=" + val);
574 // called when the type field changes
576 private void typeBox_SelectedIndexChanged(object sender, System.EventArgs e)
578 browseList.Items.Clear();
587 domainField.Text = "";
590 serviceTextField.Items.Clear();
594 browser = DNSService.Browse(0, 0, typeBox.SelectedItem.ToString(), null, new DNSService.BrowseReply(OnBrowseReply));
598 MessageBox.Show("Browse Failed", "Error");
603 private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
605 if (resolver != null)
610 if (browseList.SelectedItem != null)
614 BrowseData data = (BrowseData) browseList.SelectedItem;
616 resolver = DNSService.Resolve(0, 0, data.Name, data.Type, data.Domain, new DNSService.ResolveReply(OnResolveReply));
620 MessageBox.Show("Resolve Failed", "Error");
629 // This method is "Invoked" by OnBrowseReply. This call
630 // executes in the context of the main thread
632 private void OnAddService
637 browseList.Items.Add(data);
638 browseList.Invalidate();
644 // This method is "Invoked" by OnBrowseReply. This call
645 // executes in the context of the main thread
647 private void OnRemoveService
652 browseList.Items.Remove(data);
653 browseList.Invalidate();
659 // This method is "Invoked" by OnResolveReply. This call
660 // executes in the context of the main thread
662 private void OnResolveService
670 Populate((BrowseData) browseList.SelectedItem, data);
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
682 private void OnBrowseReply
693 if (errorCode == ErrorCode.NoError)
695 BrowseData data = new BrowseData();
697 data.InterfaceIndex = interfaceIndex;
700 data.Domain = domain;
702 if ((flags & ServiceFlags.Add) != 0)
704 Invoke(addServiceCallback, new Object[]{data});
707 else if ((flags == 0) || ((flags & ServiceFlags.MoreComing) != 0))
709 Invoke(removeServiceCallback, new Object[]{data});
714 MessageBox.Show("OnBrowseReply returned an error code: " + errorCode, "Error");
718 private void OnResolveReply
730 if (errorCode == ErrorCode.NoError)
732 ResolveData data = new ResolveData();
734 data.InterfaceIndex = interfaceIndex;
735 data.FullName = fullName;
736 data.HostName = hostName;
738 data.TxtRecord = txtRecord;
740 Invoke(resolveServiceCallback, new Object[]{data});
744 MessageBox.Show("OnResolveReply returned an error code: " + errorCode, "Error");