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.8 2009/06/02 18:49:23 herscher
21 <rdar://problem/3948252> Update the .NET code to use the new Bonjour COM component
23 Revision 1.7 2006/08/14 23:23:58 cheshire
24 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
26 Revision 1.6 2005/02/10 22:35:06 cheshire
27 <rdar://problem/3727944> Update name
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
33 Revision 1.4 2004/09/13 19:38:17 shersche
34 Changed code to reflect namespace and type changes to dnssd.NET library
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.
39 Revision 1.2 2004/07/22 23:15:25 shersche
40 Fix service names for teleport, tftp, and bootps
42 Revision 1.1 2004/07/19 07:54:24 shersche
51 using System.Collections;
52 using System.ComponentModel;
53 using System.Windows.Forms;
58 namespace DNSServiceBrowser_NET
61 /// Summary description for Form1.
63 public class Form1 : System.Windows.Forms.Form
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;
72 /// Required designer variable.
74 private System.ComponentModel.Container components = null;
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;
91 // Required for Windows Form Designer support
93 InitializeComponent();
95 this.Load += new System.EventHandler(this.Form1_Load);
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);
103 service = new DNSSDService();
106 private void Form1_Load(object sender, EventArgs e)
108 typeBox.SelectedItem = "_http._tcp";
113 /// Clean up any resources being used.
115 protected override void Dispose( bool disposing )
119 if (components != null)
121 components.Dispose();
124 if (resolver != null)
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);
144 base.Dispose( disposing );
147 #region Windows Form Designer generated code
149 /// Required method for Designer support - do not modify
150 /// the contents of this method with the code editor.
152 private void InitializeComponent()
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();
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);
179 this.typeBox.Items.AddRange(new object[]
189 "_animobserver._tcp",
203 "_contactserver._tcp",
214 "_ecbyesfsgksc._tcp",
224 "_fmpro-internal._tcp",
234 "_ica-networking._tcp",
242 "_ipbroadcaster._tcp",
256 "_macfoh-remote._tcp",
263 "_ncsyncserver._tcp",
264 "_net-assistant._tcp",
267 "_nssocketport._tcp",
269 "_omni-bookmark._tcp",
272 "_pdl-datastream._tcp",
282 "_realplayfavs._tcp",
287 "_sallingclicker._tcp",
303 "_ssscreenshare._tcp",
314 "_ticonnectmgr._tcp",
319 "_vue4rendercow._tcp",
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);
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;
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;
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;
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;
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 = "";
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 = "";
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 = "";
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 = "";
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 = "";
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;
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;
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,
457 this.Text = "DNSServices Browser";
458 this.ResumeLayout(false);
464 /// The main entry point for the application.
469 Application.Run(new Form1());
474 // This class is used to store data associated
475 // with a DNSService.Browse() operation
477 public class BrowseData
479 public uint InterfaceIndex;
482 public String Domain;
484 public override String
497 if ((object) this == other)
501 else if (other is BrowseData)
503 BrowseData otherBrowseData = (BrowseData) other;
505 result = (this.Name == otherBrowseData.Name);
515 return Name.GetHashCode();
523 // This class is used to store data associated
524 // with a DNSService.Resolve() operation
526 public class ResolveData
528 public uint InterfaceIndex;
529 public String FullName;
530 public String HostName;
532 public TXTRecord TxtRecord;
534 public override String
544 // Populate this form with data associated with a
545 // DNSService.Resolve() call
548 Populate(BrowseData browseData, ResolveData resolveData)
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();
556 serviceTextField.Items.Clear();
558 if (resolveData.TxtRecord != null)
560 for (uint idx = 0; idx < resolveData.TxtRecord.GetCount(); idx++)
565 key = resolveData.TxtRecord.GetKeyAtIndex(idx);
566 bytes = (Byte[])resolveData.TxtRecord.GetValueAtIndex(idx);
574 val = Encoding.ASCII.GetString(bytes, 0, bytes.Length);
577 serviceTextField.Items.Add(key + "=" + val);
584 // called when the type field changes
586 private void typeBox_SelectedIndexChanged(object sender, System.EventArgs e)
588 browseList.Items.Clear();
597 domainField.Text = "";
600 serviceTextField.Items.Clear();
604 browser = service.Browse( 0, 0, typeBox.SelectedItem.ToString(), null, eventManager );
608 MessageBox.Show("Browse Failed", "Error");
613 private void listBox1_SelectedIndexChanged(object sender, System.EventArgs e)
615 if (resolver != null)
621 if (browseList.SelectedItem != null)
625 BrowseData data = (BrowseData) browseList.SelectedItem;
627 resolver = service.Resolve(0, data.InterfaceIndex, data.Name, data.Type, data.Domain, eventManager);
631 MessageBox.Show("Resolve Failed", "Error");
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
644 public void ServiceFound
654 BrowseData data = new BrowseData();
656 data.InterfaceIndex = ifIndex;
657 data.Name = serviceName;
659 data.Domain = domain;
661 browseList.Items.Add(data);
662 browseList.Invalidate();
665 public void ServiceLost
675 BrowseData data = new BrowseData();
677 data.InterfaceIndex = ifIndex;
678 data.Name = serviceName;
680 data.Domain = domain;
682 browseList.Items.Remove(data);
683 browseList.Invalidate();
686 public void ServiceResolved
697 ResolveData data = new ResolveData();
699 data.InterfaceIndex = ifIndex;
700 data.FullName = fullName;
701 data.HostName = hostName;
703 data.TxtRecord = txtRecord;
708 Populate((BrowseData) browseList.SelectedItem, data);
711 public void OperationFailed
717 MessageBox.Show("Operation failed: error code: " + error, "Error");