2 * Copyright (c) 2004 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * "Portions Copyright (c) 2004 Apple Computer, Inc. All Rights
7 * Reserved. This file contains Original Code and/or Modifications of
8 * Original Code as defined in and that are subject to the Apple Public
9 * Source License Version 1.0 (the 'License'). You may not use this file
10 * except in compliance with the License. Please obtain a copy of the
11 * License at http://www.apple.com/publicsource and read it before using
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 OR NON-INFRINGEMENT. Please see the
19 * License for the specific language governing rights and limitations
22 * @APPLE_LICENSE_HEADER_END@
25 #include <sys/types.h>
27 #include <sys/socket.h>
28 #include <netinet/in.h>
29 #include <arpa/inet.h>
41 #define forever for(;;)
43 #define MY_ID "udp_in"
48 static int ufd
[MAXSOCK
];
50 static char uline
[MAXLINE
+ 1];
56 udp_convert(int fmt
, char *s
, int len
, char *from
)
66 m
= asl_msg_from_string(s
);
67 if (from
!= NULL
) asl_set(m
, ASL_KEY_HOST
, from
);
71 return asl_syslog_input_convert(uline
, len
, from
, 0);
75 udp_in_acceptmsg(int fd
)
77 int format
, status
, x
, fromlen
;
80 struct sockaddr_storage from
;
81 char fromstr
[64], *r
, *p
;
82 struct sockaddr_in
*s4
;
83 struct sockaddr_in6
*s6
;
85 fromlen
= sizeof(struct sockaddr_storage
);
86 memset(&from
, 0, fromlen
);
88 len
= recvfrom(fd
, uline
, MAXLINE
, 0, (struct sockaddr
*)&from
, &fromlen
);
89 if (len
<= 0) return NULL
;
94 if (from
.ss_family
== AF_INET
)
96 s4
= (struct sockaddr_in
*)&from
;
97 inet_ntop(from
.ss_family
, &(s4
->sin_addr
), fromstr
, 64);
99 asldebug("%s: recvfrom %s len %d\n", MY_ID
, fromstr
, len
);
101 else if (from
.ss_family
== AF_INET6
)
103 s6
= (struct sockaddr_in6
*)&from
;
104 inet_ntop(from
.ss_family
, &(s6
->sin6_addr
), fromstr
, 64);
106 asldebug("%s: recvfrom %s len %d\n", MY_ID
, fromstr
, len
);
111 p
= strrchr(uline
, '\n');
112 if (p
!= NULL
) *p
= '\0';
116 * Determine if the input is "old" syslog format or new ASL format.
117 * Old format lines should start with "<", but they can just be
118 * straight text. ASL input starts with a length (10 bytes)
119 * followed by a space and a '['.
124 if ((uline
[0] != '<') && (len
> 11))
126 status
= sscanf(uline
, "%d ", &x
);
129 if ((uline
[10] == ' ') && (uline
[11] == '['))
137 return udp_convert(format
, uline
+off
, len
-off
, r
);
143 struct addrinfo hints
, *gai
, *ai
;
146 asldebug("%s: init\n", MY_ID
);
147 if (nsock
> 0) return 0;
149 memset(&hints
, 0, sizeof(hints
));
150 hints
.ai_flags
= AI_PASSIVE
;
151 hints
.ai_family
= PF_UNSPEC
;
152 hints
.ai_socktype
= SOCK_DGRAM
;
154 status
= getaddrinfo(NULL
, "syslog", &hints
, &gai
);
155 if (status
!= 0) return -1;
157 for (ai
= gai
; (ai
!= NULL
) && (nsock
< MAXSOCK
); ai
= ai
->ai_next
)
159 ufd
[nsock
] = socket(ai
->ai_family
, ai
->ai_socktype
, ai
->ai_protocol
);
162 asldebug("%s: socket: %s\n", MY_ID
, strerror(errno
));
166 if (bind(ufd
[nsock
], ai
->ai_addr
, ai
->ai_addrlen
) < 0)
168 asldebug("%s: bind: %s\n", MY_ID
, strerror(errno
));
180 asldebug("%s: no input sockets\n", MY_ID
);
184 for (i
= 0; i
< nsock
; i
++) aslevent_addfd(ufd
[i
], udp_in_acceptmsg
, NULL
, NULL
);
199 if (nsock
== 0) return 1;
201 for (i
= 0; i
< nsock
; i
++)