]> git.saurik.com Git - apple/syslog.git/blame - syslogd.tproj/udp_in.c
syslog-385.tar.gz
[apple/syslog.git] / syslogd.tproj / udp_in.c
CommitLineData
b16a592a 1/*
db78b1bd 2 * Copyright (c) 2004-2011 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
81582353
A
24#include <TargetConditionals.h>
25
86a8bcf5 26#if TARGET_OS_SIMULATOR
81582353
A
27struct _not_empty;
28#else
29
b16a592a
A
30#include <sys/types.h>
31#include <sys/stat.h>
32#include <sys/socket.h>
33#include <netinet/in.h>
34#include <arpa/inet.h>
35#include <sys/un.h>
36#include <sys/uio.h>
37#include <stdio.h>
38#include <stdlib.h>
39#include <string.h>
40#include <fcntl.h>
41#include <errno.h>
42#include <unistd.h>
43#include <netdb.h>
44#include "daemon.h"
45
46#define forever for(;;)
47
5dd30d76 48#define UDP_SOCKET_NAME "NetworkListener"
b16a592a
A
49#define MY_ID "udp_in"
50#define MAXLINE 4096
51
52#define MAXSOCK 16
53static int nsock = 0;
54static int ufd[MAXSOCK];
db78b1bd 55static dispatch_source_t ufd_src[MAXSOCK];
b16a592a
A
56
57static char uline[MAXLINE + 1];
58
db78b1bd
A
59static dispatch_source_t in_src[MAXSOCK];
60static dispatch_queue_t in_queue;
61
b16a592a
A
62#define FMT_LEGACY 0
63#define FMT_ASL 1
64
db78b1bd 65void
b16a592a
A
66udp_in_acceptmsg(int fd)
67{
5dd30d76 68 socklen_t fromlen;
b16a592a
A
69 ssize_t len;
70 struct sockaddr_storage from;
71 char fromstr[64], *r, *p;
72 struct sockaddr_in *s4;
73 struct sockaddr_in6 *s6;
f3df4c03 74 asl_msg_t *m;
b16a592a
A
75
76 fromlen = sizeof(struct sockaddr_storage);
77 memset(&from, 0, fromlen);
78
79 len = recvfrom(fd, uline, MAXLINE, 0, (struct sockaddr *)&from, &fromlen);
db78b1bd 80 if (len <= 0) return;
b16a592a
A
81
82 fromstr[0] = '\0';
83 r = NULL;
84
85 if (from.ss_family == AF_INET)
86 {
87 s4 = (struct sockaddr_in *)&from;
88 inet_ntop(from.ss_family, &(s4->sin_addr), fromstr, 64);
89 r = fromstr;
db78b1bd 90 asldebug("%s: fd %d recvfrom %s len %d\n", MY_ID, fd, fromstr, len);
b16a592a
A
91 }
92 else if (from.ss_family == AF_INET6)
93 {
94 s6 = (struct sockaddr_in6 *)&from;
95 inet_ntop(from.ss_family, &(s6->sin6_addr), fromstr, 64);
96 r = fromstr;
db78b1bd 97 asldebug("%s: fd %d recvfrom %s len %d\n", MY_ID, fd, fromstr, len);
b16a592a
A
98 }
99
100 uline[len] = '\0';
101
102 p = strrchr(uline, '\n');
103 if (p != NULL) *p = '\0';
104
db78b1bd 105 m = asl_input_parse(uline, len, r, SOURCE_UDP_SOCKET);
81582353 106 process_message(m, SOURCE_UDP_SOCKET);
b16a592a
A
107}
108
109int
db78b1bd 110udp_in_init()
b16a592a 111{
db78b1bd 112 int i, rbufsize, len, fd;
5dd30d76 113 launch_data_t sockets_dict, fd_array, fd_dict;
db78b1bd
A
114 static dispatch_once_t once;
115
116 dispatch_once(&once, ^{
117 in_queue = dispatch_queue_create(MY_ID, NULL);
118 });
b16a592a
A
119
120 asldebug("%s: init\n", MY_ID);
121 if (nsock > 0) return 0;
db78b1bd 122
57b0aad2 123 if (global.launch_dict == NULL)
5dd30d76 124 {
c4fdb7d1 125 asldebug("%s: launchd dict is NULL\n", MY_ID);
5dd30d76
A
126 return -1;
127 }
b16a592a 128
57b0aad2 129 sockets_dict = launch_data_dict_lookup(global.launch_dict, LAUNCH_JOBKEY_SOCKETS);
5dd30d76
A
130 if (sockets_dict == NULL)
131 {
c4fdb7d1 132 asldebug("%s: launchd lookup of LAUNCH_JOBKEY_SOCKETS failed\n", MY_ID);
5dd30d76
A
133 return -1;
134 }
57b0aad2 135
5dd30d76
A
136 fd_array = launch_data_dict_lookup(sockets_dict, UDP_SOCKET_NAME);
137 if (fd_array == NULL)
138 {
c4fdb7d1 139 asldebug("%s: launchd lookup of UDP_SOCKET_NAME failed\n", MY_ID);
5dd30d76
A
140 return -1;
141 }
57b0aad2 142
5dd30d76
A
143 nsock = launch_data_array_get_count(fd_array);
144 if (nsock <= 0)
145 {
c4fdb7d1 146 asldebug("%s: launchd fd array is empty\n", MY_ID);
5dd30d76
A
147 return -1;
148 }
57b0aad2 149
5dd30d76
A
150 for (i = 0; i < nsock; i++)
151 {
db78b1bd
A
152 ufd[i] = -1;
153
5dd30d76
A
154 fd_dict = launch_data_array_get_index(fd_array, i);
155 if (fd_dict == NULL)
156 {
c4fdb7d1 157 asldebug("%s: launchd file discriptor array element 0 is NULL\n", MY_ID);
5dd30d76
A
158 return -1;
159 }
57b0aad2 160
db78b1bd 161 fd = launch_data_get_fd(fd_dict);
b16a592a 162
5dd30d76
A
163 rbufsize = 128 * 1024;
164 len = sizeof(rbufsize);
b16a592a 165
db78b1bd 166 if (setsockopt(fd, SOL_SOCKET, SO_RCVBUF, &rbufsize, len) < 0)
b16a592a 167 {
db78b1bd 168 asldebug("%s: couldn't set receive buffer size for file descriptor %d: %s\n", MY_ID, fd, strerror(errno));
b16a592a
A
169 }
170
db78b1bd 171 if (fcntl(fd, F_SETFL, O_NONBLOCK) < 0)
b16a592a 172 {
db78b1bd 173 asldebug("%s: couldn't set O_NONBLOCK for file descriptor %d: %s\n", MY_ID, fd, strerror(errno));
b16a592a 174 }
b16a592a 175
db78b1bd
A
176 ufd[i] = fd;
177
178 in_src[i] = dispatch_source_create(DISPATCH_SOURCE_TYPE_READ, (uintptr_t)fd, 0, in_queue);
179 dispatch_source_set_event_handler(in_src[i], ^{ udp_in_acceptmsg(fd); });
180
181 dispatch_resume(in_src[i]);
182 }
c4fdb7d1 183
b16a592a
A
184 return 0;
185}
186
b16a592a
A
187int
188udp_in_close(void)
189{
190 int i;
191
db78b1bd 192 if (nsock == 0) return -1;
b16a592a
A
193
194 for (i = 0; i < nsock; i++)
195 {
db78b1bd 196 if (ufd_src[i] != NULL)
a83ff38a 197 {
db78b1bd
A
198 dispatch_source_cancel(in_src[i]);
199 dispatch_release(in_src[i]);
200 in_src[i] = NULL;
a83ff38a
A
201 }
202
db78b1bd
A
203 if (ufd[i] != -1)
204 {
5222c21d 205 close(ufd[i]);
db78b1bd
A
206 ufd[i] = -1;
207 }
b16a592a
A
208 }
209
210 nsock = 0;
211
212 return 0;
213}
a83ff38a
A
214
215int
216udp_in_reset(void)
217{
db78b1bd
A
218 if (udp_in_close() != 0) return -1;
219 return udp_in_init();
a83ff38a 220}
81582353 221
86a8bcf5 222#endif /* !TARGET_OS_SIMULATOR */