]> git.saurik.com Git - apple/mdnsresponder.git/blob - mDNSPosix/ExampleClientApp.c
mDNSResponder-1310.60.4.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
18 #include <stdio.h> // For printf()
19 #include <stdlib.h> // For exit() etc.
20 #include <string.h> // For strlen() etc.
21 #include <unistd.h> // For select()
22 #include <errno.h> // For errno, EINTR
23 #include <netinet/in.h> // For INADDR_NONE
24 #include <arpa/inet.h> // For inet_addr()
25 #include <netdb.h> // For gethostbyname()
26 #include <signal.h> // For SIGINT, etc.
27
28 #include "mDNSEmbeddedAPI.h" // Defines the interface to the client layer above
29 #include "mDNSPosix.h" // Defines the specific types needed to run mDNS on this platform
30
31 //*******************************************************************************************
32 // Main
33
34 static volatile mDNSBool StopNow;
35
36 mDNSlocal void HandleSIG(int signal)
37 {
38 (void)signal; // Unused
39 debugf("%s","");
40 debugf("HandleSIG");
41 StopNow = mDNStrue;
42 }
43
44 mDNSexport void ExampleClientEventLoop(mDNS *const m)
45 {
46 signal(SIGINT, HandleSIG); // SIGINT is what you get for a Ctrl-C
47 signal(SIGTERM, HandleSIG);
48
49 while (!StopNow)
50 {
51 int nfds = 0;
52 fd_set readfds;
53 fd_set writefds;
54 struct timeval timeout;
55 int result;
56
57 // 1. Set up the fd_set as usual here.
58 // This example client has no file descriptors of its own,
59 // but a real application would call FD_SET to add them to the set here
60 FD_ZERO(&readfds);
61 FD_ZERO(&writefds);
62
63 // 2. Set up the timeout.
64 // This example client has no other work it needs to be doing,
65 // so we set an effectively infinite timeout
66 timeout.tv_sec = FutureTime;
67 timeout.tv_usec = 0;
68
69 // 3. Give the mDNSPosix layer a chance to add its information to the fd_set and timeout
70 mDNSPosixGetFDSet(m, &nfds, &readfds, &writefds, &timeout);
71
72 // 4. Call select as normal
73 verbosedebugf("select(%d, %d.%06d)", nfds, timeout.tv_sec, timeout.tv_usec);
74 result = select(nfds, &readfds, &writefds, NULL, &timeout);
75
76 if (result < 0)
77 {
78 verbosedebugf("select() returned %d errno %d", result, errno);
79 if (errno != EINTR) StopNow = mDNStrue;
80 }
81 else
82 {
83 // 5. Call mDNSPosixProcessFDSet to let the mDNSPosix layer do its work
84 mDNSPosixProcessFDSet(m, &readfds, &writefds);
85
86 // 6. This example client has no other work it needs to be doing,
87 // but a real client would do its work here
88 // ... (do work) ...
89 }
90 }
91
92 debugf("Exiting");
93 }