00001 #include <sys/socket.h>
00002 #include <netinet/in.h>
00003 #include <sys/types.h>
00004 #include <arpa/inet.h>
00005 #include <stdio.h>
00006 #include <stdlib.h>
00007 #include <unistd.h>
00008 #include "helpers.h"
00009 #include "udpmon.h"
00010
00011 struct udpmon_pkt buff;
00012 char magic[] = UDPMON_MAGIC;
00013
00014 int main(int argc, char **argv)
00015 {
00016 if (argc != 2) { fprintf(stderr, "usage: udpmonserv <port_to_listen_on>\n"); return 1; }
00017
00018 int udp_socket = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
00019 struct sockaddr_in port;
00020 int len;
00021
00022 if (udp_socket == -1) { perror("socket"); return 1; }
00023
00024 port.sin_family = AF_INET;
00025 port.sin_port = htons(atoi(argv[1]));
00026 port.sin_addr.s_addr = INADDR_ANY;
00027
00028 if (bind(udp_socket, (struct sockaddr *) &port, sizeof(port))) { perror("bind"); return 1; }
00029
00030 while (1)
00031 {
00032 struct sockaddr src_addr;
00033 socklen_t addrlen = sizeof(src_addr);
00034
00035 if ((len = recvfrom(udp_socket, &buff, sizeof(buff), MSG_WAITALL, &src_addr, &addrlen)) == -1) { perror("recv"); return 1; }
00036
00037 if (*((int *) &magic) == (*((int *) &buff.magic)))
00038 buff.echoed = gettime();
00039 else
00040 continue;
00041
00042
00043
00044 sendto(udp_socket, &buff, len, 0, &src_addr, addrlen);
00045 }
00046
00047 return 0;
00048 }