]>
Commit | Line | Data |
---|---|---|
b16a592a | 1 | /* |
57b0aad2 | 2 | * Copyright (c) 2004-2008 Apple Inc. All rights reserved. |
b16a592a A |
3 | * |
4 | * @APPLE_LICENSE_HEADER_START@ | |
5 | * | |
5dd30d76 A |
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. | |
b16a592a A |
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, | |
5dd30d76 A |
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. | |
b16a592a A |
20 | * |
21 | * @APPLE_LICENSE_HEADER_END@ | |
22 | */ | |
23 | ||
24 | #include <sys/types.h> | |
25 | #include <sys/stat.h> | |
26 | #include <sys/socket.h> | |
27 | #include <netinet/in.h> | |
28 | #include <arpa/inet.h> | |
29 | #include <sys/un.h> | |
30 | #include <sys/uio.h> | |
31 | #include <stdio.h> | |
32 | #include <stdlib.h> | |
33 | #include <string.h> | |
34 | #include <fcntl.h> | |
35 | #include <errno.h> | |
36 | #include <unistd.h> | |
37 | #include <netdb.h> | |
38 | #include "daemon.h" | |
39 | ||
40 | #define forever for(;;) | |
41 | ||
5dd30d76 | 42 | #define UDP_SOCKET_NAME "NetworkListener" |
b16a592a A |
43 | #define MY_ID "udp_in" |
44 | #define MAXLINE 4096 | |
45 | ||
46 | #define MAXSOCK 16 | |
47 | static int nsock = 0; | |
48 | static int ufd[MAXSOCK]; | |
49 | ||
50 | static char uline[MAXLINE + 1]; | |
51 | ||
52 | #define FMT_LEGACY 0 | |
53 | #define FMT_ASL 1 | |
54 | ||
b16a592a A |
55 | asl_msg_t * |
56 | udp_in_acceptmsg(int fd) | |
57 | { | |
5dd30d76 | 58 | socklen_t fromlen; |
b16a592a A |
59 | ssize_t len; |
60 | struct sockaddr_storage from; | |
61 | char fromstr[64], *r, *p; | |
62 | struct sockaddr_in *s4; | |
63 | struct sockaddr_in6 *s6; | |
64 | ||
65 | fromlen = sizeof(struct sockaddr_storage); | |
66 | memset(&from, 0, fromlen); | |
67 | ||
68 | len = recvfrom(fd, uline, MAXLINE, 0, (struct sockaddr *)&from, &fromlen); | |
69 | if (len <= 0) return NULL; | |
70 | ||
71 | fromstr[0] = '\0'; | |
72 | r = NULL; | |
73 | ||
74 | if (from.ss_family == AF_INET) | |
75 | { | |
76 | s4 = (struct sockaddr_in *)&from; | |
77 | inet_ntop(from.ss_family, &(s4->sin_addr), fromstr, 64); | |
78 | r = fromstr; | |
79 | asldebug("%s: recvfrom %s len %d\n", MY_ID, fromstr, len); | |
80 | } | |
81 | else if (from.ss_family == AF_INET6) | |
82 | { | |
83 | s6 = (struct sockaddr_in6 *)&from; | |
84 | inet_ntop(from.ss_family, &(s6->sin6_addr), fromstr, 64); | |
85 | r = fromstr; | |
86 | asldebug("%s: recvfrom %s len %d\n", MY_ID, fromstr, len); | |
87 | } | |
88 | ||
89 | uline[len] = '\0'; | |
90 | ||
91 | p = strrchr(uline, '\n'); | |
92 | if (p != NULL) *p = '\0'; | |
93 | ||
5dd30d76 | 94 | return asl_input_parse(uline, len, r, 0); |
b16a592a A |
95 | } |
96 | ||
97 | int | |
98 | udp_in_init(void) | |
99 | { | |
5dd30d76 A |
100 | int i, rbufsize, len; |
101 | launch_data_t sockets_dict, fd_array, fd_dict; | |
b16a592a A |
102 | |
103 | asldebug("%s: init\n", MY_ID); | |
104 | if (nsock > 0) return 0; | |
57b0aad2 | 105 | if (global.launch_dict == NULL) |
5dd30d76 | 106 | { |
c4fdb7d1 | 107 | asldebug("%s: launchd dict is NULL\n", MY_ID); |
5dd30d76 A |
108 | return -1; |
109 | } | |
b16a592a | 110 | |
57b0aad2 | 111 | sockets_dict = launch_data_dict_lookup(global.launch_dict, LAUNCH_JOBKEY_SOCKETS); |
5dd30d76 A |
112 | if (sockets_dict == NULL) |
113 | { | |
c4fdb7d1 | 114 | asldebug("%s: launchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID); |
5dd30d76 A |
115 | return -1; |
116 | } | |
57b0aad2 | 117 | |
5dd30d76 A |
118 | fd_array = launch_data_dict_lookup(sockets_dict, UDP_SOCKET_NAME); |
119 | if (fd_array == NULL) | |
120 | { | |
c4fdb7d1 | 121 | asldebug("%s: launchd lookup of UDP_SOCKET_NAME failed\n", MY_ID); |
5dd30d76 A |
122 | return -1; |
123 | } | |
57b0aad2 | 124 | |
5dd30d76 A |
125 | nsock = launch_data_array_get_count(fd_array); |
126 | if (nsock <= 0) | |
127 | { | |
c4fdb7d1 | 128 | asldebug("%s: launchd fd array is empty\n", MY_ID); |
5dd30d76 A |
129 | return -1; |
130 | } | |
57b0aad2 | 131 | |
5dd30d76 A |
132 | for (i = 0; i < nsock; i++) |
133 | { | |
134 | fd_dict = launch_data_array_get_index(fd_array, i); | |
135 | if (fd_dict == NULL) | |
136 | { | |
c4fdb7d1 | 137 | asldebug("%s: launchd file discriptor array element 0 is NULL\n", MY_ID); |
5dd30d76 A |
138 | return -1; |
139 | } | |
57b0aad2 | 140 | |
5dd30d76 | 141 | ufd[i] = launch_data_get_fd(fd_dict); |
b16a592a | 142 | |
5dd30d76 A |
143 | rbufsize = 128 * 1024; |
144 | len = sizeof(rbufsize); | |
b16a592a | 145 | |
5dd30d76 | 146 | if (setsockopt(ufd[i], SOL_SOCKET, SO_RCVBUF, &rbufsize, len) < 0) |
b16a592a | 147 | { |
5dd30d76 A |
148 | asldebug("%s: couldn't set receive buffer size for socket %d: %s\n", MY_ID, ufd[i], strerror(errno)); |
149 | close(ufd[i]); | |
150 | ufd[i] = -1; | |
b16a592a A |
151 | continue; |
152 | } | |
153 | ||
5dd30d76 | 154 | if (fcntl(ufd[i], F_SETFL, O_NONBLOCK) < 0) |
b16a592a | 155 | { |
5dd30d76 A |
156 | asldebug("%s: couldn't set O_NONBLOCK for socket %d: %s\n", MY_ID, ufd[i], strerror(errno)); |
157 | close(ufd[i]); | |
158 | ufd[i] = -1; | |
b16a592a A |
159 | continue; |
160 | } | |
b16a592a A |
161 | } |
162 | ||
c4fdb7d1 A |
163 | for (i = 0; i < nsock; i++) if (ufd[i] != -1) aslevent_addfd(SOURCE_UDP_SOCKET, ufd[i], 0, udp_in_acceptmsg, NULL, NULL); |
164 | ||
b16a592a A |
165 | return 0; |
166 | } | |
167 | ||
168 | int | |
169 | udp_in_reset(void) | |
170 | { | |
171 | return 0; | |
172 | } | |
173 | ||
174 | int | |
175 | udp_in_close(void) | |
176 | { | |
177 | int i; | |
178 | ||
179 | if (nsock == 0) return 1; | |
180 | ||
181 | for (i = 0; i < nsock; i++) | |
182 | { | |
5dd30d76 | 183 | if (ufd[i] != -1) close(ufd[i]); |
b16a592a A |
184 | ufd[i] = -1; |
185 | } | |
186 | ||
187 | nsock = 0; | |
188 | ||
189 | return 0; | |
190 | } |