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