--- src/logfile.c 2003-01-20 10:57:50.000000000 -0800 +++ src/logfile.c 2005-06-09 08:43:31.000000000 -0700 @@ -41,6 +41,72 @@ { int fd; Logfile *lf; + + int rc,len; + char hostbuf[256]; + unsigned short int port; + char * position; + struct hostent *h; + struct sockaddr_in cliAddr; + + if (strncmp(path,"udp://",6) == 0) { /* The notation for a udp network socket is udp://hostname:port */ + if ((position = rindex(path + 6,':')) != NULL) { + len = position - path - 6; + if (len > 255) { + fatalf("Cannot open socket '%s' because\n" + "\ta port number was not specified.\n", path); + } + strncpy(hostbuf,path + 6,len); + hostbuf[len] = 0; /* We don't want buffer overflows */ + + port = atoi(position + 1); /* At worst this should give us a pointer to the end of string + character which will be detected below */ + + + /* get server IP address (no check if input is IP address or DNS name */ + h = gethostbyname(hostbuf); + if (h==NULL) { + fatalf("Could not resolve host '%s' while\n" + "\tattempting to open logging socket.\n",hostbuf); + } + /* make sure a port # is specified */ + if (port == 0) { + fatalf("Invalid port specified while opening socket '%s'\n" + "\tplease specify a port number from 1-65536.\n",path); + } + /* allocate memory now, we're going to need it right away */ + lf = xcalloc(1, sizeof(*lf)); + /* bufsize should never be > 65000, which is a little less than the maximum UDP packet size */ + //lf->bufsz = 65000; + + /* some voodoo to set up the remote socket address */ + lf->remoteServer.sin_family = h->h_addrtype; + memcpy( (char *) &(lf->remoteServer.sin_addr.s_addr), + h->h_addr_list[0], h->h_length); + lf->remoteServer.sin_port = htons(port); + /* socket creation */ + lf->socket = socket(AF_INET,SOCK_DGRAM,0); + + if(lf->socket < 0) { + fatalf("Cannot open udp socket to host '%s' on port '%u'\n", hostbuf, port); + } + + /* bind any port */ + cliAddr.sin_family = AF_INET; + cliAddr.sin_addr.s_addr = htonl(INADDR_ANY); + cliAddr.sin_port = htons(0); + + rc = bind(lf->socket, (struct sockaddr *) &cliAddr, sizeof(cliAddr)); + if(rc < 0) { + fatalf("Cannot bind a random local udp porte\n"); + } + } else { + fatalf("Cannot open socket '%s' because\n" + "\ta port number was not specified.\n", path); + } + return lf; + } + fd = file_open(path, O_WRONLY | O_CREAT | O_TEXT); if (DISK_ERROR == fd) { if (ENOENT == errno && fatal_flag) { @@ -73,7 +139,11 @@ logfileClose(Logfile * lf) { logfileFlush(lf); - file_close(lf->fd); + if (lf->socket) { + close(lf->socket); + } else { + file_close(lf->fd); + } if (lf->buf) xfree(lf->buf); xfree(lf); @@ -88,6 +158,9 @@ int i; char from[MAXPATHLEN]; char to[MAXPATHLEN]; + if (lf->socket) { + return; /* You can't rotate a socket */ + } assert(lf->path); #ifdef S_ISREG if (stat(lf->path, &sb) == 0) @@ -185,6 +258,11 @@ logfileWriteWrapper(Logfile * lf, const void *buf, size_t len) { int s; + if (lf->socket) { + s = sendto(lf->socket, buf, len, 0, &(lf->remoteServer), sizeof(struct sockaddr_in)); + /* We're going to lose packets with UDP, so who cares how many actually got sent. */ + return; + } s = FD_WRITE_METHOD(lf->fd, buf, len); fd_bytes(lf->fd, s, FD_WRITE); if (s == len) --- src/structs.h 2005-02-22 16:06:35.000000000 -0800 +++ src/structs.h 2005-06-09 08:40:53.000000000 -0700 @@ -2180,6 +2180,8 @@ struct _Logfile { int fd; + int socket; /* socket descriptor */ + struct sockaddr_in remoteServer; /* Host to log to */ char path[MAXPATHLEN]; char *buf; size_t bufsz;