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