]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/ExampleClientApp.c
mDNSResponder-214.tar.gz
[apple/mdnsresponder.git] / mDNSPosix / ExampleClientApp.c
1 /* -*- Mode: C; tab-width: 4 -*-
2 *
3 * Copyright (c) 2002-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: ExampleClientApp.c,v $
20 Revision 1.14 2006/08/14 23:24:46 cheshire
21 Re-licensed mDNSResponder daemon source code under Apache License, Version 2.0
22
23 Revision 1.13 2006/02/23 23:38:43 cheshire
24 <rdar://problem/4427969> On FreeBSD 4 "arpa/inet.h" requires "netinet/in.h" be included first
25
26 Revision 1.12 2004/11/30 22:37:00 cheshire
27 Update copyright dates and add "Mode: C; tab-width: 4" headers
28
29 Revision 1.11 2004/09/17 01:08:53 cheshire
30 Renamed mDNSClientAPI.h to mDNSEmbeddedAPI.h
31 The name "mDNSClientAPI.h" is misleading to new developers looking at this code. The interfaces
32 declared in that file are ONLY appropriate to single-address-space embedded applications.
33 For clients on general-purpose computers, the interfaces defined in dns_sd.h should be used.
34
35 Revision 1.10 2004/09/16 01:58:22 cheshire
36 Fix compiler warnings
37
38 Revision 1.9 2003/08/12 19:56:26 cheshire
39 Update to APSL 2.0
40
41 Revision 1.8 2003/07/02 21:19:58 cheshire
42 <rdar://problem/3313413> Update copyright notices, etc., in source code comments
43
44 Revision 1.7 2003/06/18 05:48:41 cheshire
45 Fix warnings
46
47 Revision 1.6 2003/03/31 22:44:36 cheshire
48 Add log header
49
50 */
51
52 #include <stdio.h> // For printf()
53 #include <stdlib.h> // For exit() etc.
54 #include <string.h> // For strlen() etc.
55 #include <unistd.h> // For select()
56 #include <errno.h> // For errno, EINTR
57 #include <netinet/in.h> // For INADDR_NONE
58 #include <arpa/inet.h> // For inet_addr()
59 #include <netdb.h> // For gethostbyname()
60 #include <signal.h> // For SIGINT, etc.
61
62 #include "mDNSEmbeddedAPI.h" // Defines the interface to the client layer above
63 #include "mDNSPosix.h" // Defines the specific types needed to run mDNS on this platform
64
65 //*******************************************************************************************
66 // Main
67
68 static volatile mDNSBool StopNow;
69
70 mDNSlocal void HandleSIG(int signal)
71 {
72 (void)signal; // Unused
73 debugf("%s","");
74 debugf("HandleSIG");
75 StopNow = mDNStrue;
76 }
77
78 mDNSexport void ExampleClientEventLoop(mDNS *const m)
79 {
80 signal(SIGINT, HandleSIG); // SIGINT is what you get for a Ctrl-C
81 signal(SIGTERM, HandleSIG);
82
83 while (!StopNow)
84 {
85 int nfds = 0;
86 fd_set readfds;
87 struct timeval timeout;
88 int result;
89
90 // 1. Set up the fd_set as usual here.
91 // This example client has no file descriptors of its own,
92 // but a real application would call FD_SET to add them to the set here
93 FD_ZERO(&readfds);
94
95 // 2. Set up the timeout.
96 // This example client has no other work it needs to be doing,
97 // so we set an effectively infinite timeout
98 timeout.tv_sec = 0x3FFFFFFF;
99 timeout.tv_usec = 0;
100
101 // 3. Give the mDNSPosix layer a chance to add its information to the fd_set and timeout
102 mDNSPosixGetFDSet(m, &nfds, &readfds, &timeout);
103
104 // 4. Call select as normal
105 verbosedebugf("select(%d, %d.%06d)", nfds, timeout.tv_sec, timeout.tv_usec);
106 result = select(nfds, &readfds, NULL, NULL, &timeout);
107
108 if (result < 0)
109 {
110 verbosedebugf("select() returned %d errno %d", result, errno);
111 if (errno != EINTR) StopNow = mDNStrue;
112 }
113 else
114 {
115 // 5. Call mDNSPosixProcessFDSet to let the mDNSPosix layer do its work
116 mDNSPosixProcessFDSet(m, &readfds);
117
118 // 6. This example client has no other work it needs to be doing,
119 // but a real client would do its work here
120 // ... (do work) ...
121 }
122 }
123
124 debugf("Exiting");
125 }