]>
git.saurik.com Git - apple/syslog.git/blob - syslogd.tproj/udp_in.c
2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
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
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,
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.
21 * @APPLE_LICENSE_HEADER_END@
24 #include <sys/types.h>
26 #include <sys/socket.h>
27 #include <netinet/in.h>
28 #include <arpa/inet.h>
40 #define forever for(;;)
42 #define MY_ID "udp_in"
47 static int ufd
[MAXSOCK
];
49 static char uline
[MAXLINE
+ 1];
55 udp_convert(int fmt
, char *s
, int len
, char *from
)
65 m
= asl_msg_from_string(s
);
66 if (from
!= NULL
) asl_set(m
, ASL_KEY_HOST
, from
);
70 return asl_syslog_input_convert(uline
, len
, from
, 0);
74 udp_in_acceptmsg(int fd
)
76 int format
, status
, x
, fromlen
;
79 struct sockaddr_storage from
;
80 char fromstr
[64], *r
, *p
;
81 struct sockaddr_in
*s4
;
82 struct sockaddr_in6
*s6
;
84 fromlen
= sizeof(struct sockaddr_storage
);
85 memset(&from
, 0, fromlen
);
87 len
= recvfrom(fd
, uline
, MAXLINE
, 0, (struct sockaddr
*)&from
, &fromlen
);
88 if (len
<= 0) return NULL
;
93 if (from
.ss_family
== AF_INET
)
95 s4
= (struct sockaddr_in
*)&from
;
96 inet_ntop(from
.ss_family
, &(s4
->sin_addr
), fromstr
, 64);
98 asldebug("%s: recvfrom %s len %d\n", MY_ID
, fromstr
, len
);
100 else if (from
.ss_family
== AF_INET6
)
102 s6
= (struct sockaddr_in6
*)&from
;
103 inet_ntop(from
.ss_family
, &(s6
->sin6_addr
), fromstr
, 64);
105 asldebug("%s: recvfrom %s len %d\n", MY_ID
, fromstr
, len
);
110 p
= strrchr(uline
, '\n');
111 if (p
!= NULL
) *p
= '\0';
115 * Determine if the input is "old" syslog format or new ASL format.
116 * Old format lines should start with "<", but they can just be
117 * straight text. ASL input starts with a length (10 bytes)
118 * followed by a space and a '['.
123 if ((uline
[0] != '<') && (len
> 11))
125 status
= sscanf(uline
, "%d ", &x
);
128 if ((uline
[10] == ' ') && (uline
[11] == '['))
136 return udp_convert(format
, uline
+off
, len
-off
, r
);
142 struct addrinfo hints
, *gai
, *ai
;
145 asldebug("%s: init\n", MY_ID
);
146 if (nsock
> 0) return 0;
148 memset(&hints
, 0, sizeof(hints
));
149 hints
.ai_flags
= AI_PASSIVE
;
150 hints
.ai_family
= PF_UNSPEC
;
151 hints
.ai_socktype
= SOCK_DGRAM
;
153 status
= getaddrinfo(NULL
, "syslog", &hints
, &gai
);
154 if (status
!= 0) return -1;
156 for (ai
= gai
; (ai
!= NULL
) && (nsock
< MAXSOCK
); ai
= ai
->ai_next
)
158 ufd
[nsock
] = socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
161 asldebug("%s: socket: %s\n", MY_ID
, strerror(errno
));
165 if (bind(ufd
[nsock
], ai
->ai_addr
, ai
->ai_addrlen
) < 0)
167 asldebug("%s: bind: %s\n", MY_ID
, strerror(errno
));
179 asldebug("%s: no input sockets\n", MY_ID
);
183 for (i
= 0; i
< nsock
; i
++) aslevent_addfd(ufd
[i
], udp_in_acceptmsg
, NULL
, NULL
);
198 if (nsock
== 0) return 1;
200 for (i
= 0; i
< nsock
; i
++)