#include #include #include #include #include #include #include "sa.h" static void die(char *fmt, ...) { va_list ap; va_start(ap, fmt); fprintf(stderr, "test:ERROR: "); vfprintf(stderr, fmt, ap); fprintf(stderr, "\n"); va_end(ap); exit(1); } int main(int argc, char *argv[]) { sa_addr_t *ra; sa_addr_t *la; sa_t *sa; char caBuf[1024]; int nBuf; char caTime[15+1]; time_t now; struct tm *tm; struct utsname uts; char *cp; char caTag[32+1]; char *cpHost; int n; /* create remote address */ if (sa_addr_create(&ra) != SA_OK) die("sa_addr_create (ra)"); if (sa_addr_u2a(ra, "inet://141.1.129.1:514") != SA_OK) die("sa_addr_u2a (ra)"); /* create local address */ if (sa_addr_create(&la) != SA_OK) die("sa_addr_create (la)"); if (sa_addr_u2a(la, "inet://0.0.0.0:0") != SA_OK) die("sa_addr_u2a (la)"); /* create datagram socket */ if (sa_create(&sa) != SA_OK) die("sa_create"); if (sa_type(sa, SA_TYPE_DATAGRAM) != SA_OK) die("sa_type"); /* bind socket to local address */ if (sa_bind(sa, la) != SA_OK) die("sa_bind"); /* RFC3164: The BSD syslog Protocol; C. Lonvick; August 2001. */ now = time(NULL); tm = localtime(&now); strftime(caTime, sizeof(caTime), "%b %e %H:%M:%S", tm); if (uname(&uts) == -1) die("uname"); cpHost = strdup(uts.nodename); if ((cp = strchr(cpHost, '.')) != NULL) *cp = '\0'; strcpy(caTag, "progname[12]: "); if (strlen(caTag) > 32) caTag[32] = '\0'; sprintf(caBuf, "<16>%s %s %s[%ld]: %s", caTime, cpHost, argv[0], (long)getpid(), "test for Internet Datagram socket"); fprintf(stderr, "Send to syslog: \"%s\"\n", caBuf); /* send message to syslogd(8) */ nBuf = strlen(caBuf); if (sa_send(sa, caBuf, nBuf, (size_t *)&n, ra) != SA_OK) die("sa_writeto"); /* destroy socket and address objects */ if (sa_destroy(sa) != SA_OK) die("sa_destroy"); if (sa_addr_destroy(la) != SA_OK) die("sa_addr_destroy (la)"); if (sa_addr_destroy(ra) != SA_OK) die("sa_addr_destroy (ra)"); return 0; }