]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/ExampleClientApp.c
mDNSResponder-58.8.1.tar.gz
[apple/mdnsresponder.git] / mDNSPosix / ExampleClientApp.c
1 /*
2 * Copyright (c) 2002-2003 Apple Computer, Inc. All rights reserved.
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22
23 Change History (most recent first):
24
25 $Log: ExampleClientApp.c,v $
26 Revision 1.9 2003/08/12 19:56:26 cheshire
27 Update to APSL 2.0
28
29 Revision 1.8 2003/07/02 21:19:58 cheshire
30 <rdar://problem/3313413> Update copyright notices, etc., in source code comments
31
32 Revision 1.7 2003/06/18 05:48:41 cheshire
33 Fix warnings
34
35 Revision 1.6 2003/03/31 22:44:36 cheshire
36 Add log header
37
38 */
39
40 #include <stdio.h> // For printf()
41 #include <stdlib.h> // For exit() etc.
42 #include <string.h> // For strlen() etc.
43 #include <unistd.h> // For select()
44 #include <errno.h> // For errno, EINTR
45 #include <arpa/inet.h> // For inet_addr()
46 #include <netinet/in.h> // For INADDR_NONE
47 #include <netdb.h> // For gethostbyname()
48 #include <signal.h> // For SIGINT, etc.
49
50 #include "mDNSClientAPI.h" // Defines the interface to the client layer above
51 #include "mDNSPosix.h" // Defines the specific types needed to run mDNS on this platform
52
53 //*******************************************************************************************
54 // Main
55
56 static volatile mDNSBool StopNow;
57
58 mDNSlocal void HandleSIG(int signal)
59 {
60 (void)signal; // Unused
61 debugf("");
62 debugf("HandleSIG");
63 StopNow = mDNStrue;
64 }
65
66 mDNSexport void ExampleClientEventLoop(mDNS *const m)
67 {
68 signal(SIGINT, HandleSIG); // SIGINT is what you get for a Ctrl-C
69 signal(SIGTERM, HandleSIG);
70
71 while (!StopNow)
72 {
73 int nfds = 0;
74 fd_set readfds;
75 struct timeval timeout;
76 int result;
77
78 // 1. Set up the fd_set as usual here.
79 // This example client has no file descriptors of its own,
80 // but a real application would call FD_SET to add them to the set here
81 FD_ZERO(&readfds);
82
83 // 2. Set up the timeout.
84 // This example client has no other work it needs to be doing,
85 // so we set an effectively infinite timeout
86 timeout.tv_sec = 0x3FFFFFFF;
87 timeout.tv_usec = 0;
88
89 // 3. Give the mDNSPosix layer a chance to add its information to the fd_set and timeout
90 mDNSPosixGetFDSet(m, &nfds, &readfds, &timeout);
91
92 // 4. Call select as normal
93 verbosedebugf("select(%d, %d.%06d)", nfds, timeout.tv_sec, timeout.tv_usec);
94 result = select(nfds, &readfds, NULL, NULL, &timeout);
95
96 if (result < 0)
97 {
98 verbosedebugf("select() returned %d errno %d", result, errno);
99 if (errno != EINTR) StopNow = mDNStrue;
100 }
101 else
102 {
103 // 5. Call mDNSPosixProcessFDSet to let the mDNSPosix layer do its work
104 mDNSPosixProcessFDSet(m, &readfds);
105
106 // 6. This example client has no other work it needs to be doing,
107 // but a real client would do its work here
108 // ... (do work) ...
109 }
110 }
111
112 debugf("Exiting");
113 }