/* A simple snmp trap daemon
** version 2
** by Pegasus Epsilon
**
** History:
**
** version 1: first functional version
** version 2: timestamps added
*/
#include <netinet/in.h> /* ??? INADDR_ANY, struct sockaddr_in */
#include <sys/types.h> /* socket(), bind() */
#include <sys/socket.h> /* socket(), bind() */
#include <stdlib.h> /* exit(), malloc() */
#include <unistd.h> /* write() */
#include <stdio.h> /* perror() */
#include <string.h> /* strcmp(), memset() */
#include <time.h> /* ctime(), time(), time_t */
#ifndef TRUE
# define TRUE -1
#endif
#define PORT 162
#define TBUFLEN 30
void perr(char *err) {
perror(err);
exit(0);
}
void usage(char *err) {
if (err) fprintf(stderr, "%s.\n", err);
/* usage help */
exit(0);
}
int main (int argc, char *argv[]) {
unsigned int sock, i = 0, sz_sin;
struct sockaddr_in sin;
char *buffer, timebuf[TBUFLEN];
time_t now;
if ((sock = socket(PF_INET, SOCK_DGRAM, 0)) == TRUE)
perr("failed to create socket");
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = htonl(INADDR_ANY);
sin.sin_port = htons(PORT);
if (bind(sock, (struct sockaddr *)&sin, sizeof(sin)) != 0)
perr("failed to bind socket to port");
buffer = malloc((size_t)1025);
memset(buffer, 0, 1025);
sz_sin = sizeof(sin);
for (;;) {
if((i = recvfrom(
sock, buffer, 1024, 0, (struct sockaddr *)&sin, &sz_sin
)) == TRUE) perr("cannot read data");
now = time(NULL);
strftime(timebuf, TBUFLEN, "%d %b %Y %T %z", localtime(&now));
printf("%s: %s", timebuf, buffer+74);
fflush(stdout);
memset(buffer, 0, 1024);
}
return 0;
}