From e192628c47608e6e61f98b9496250b4b5b3681ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Axel=20D=C3=B6rfler?= Date: Fri, 11 Aug 2006 14:56:16 +0000 Subject: [PATCH] * In case of an error, status is always -1, only errno shows the actual error. * Added test mode for UDP broadcasting. git-svn-id: file:///srv/svn/repos/haiku/haiku/trunk@18485 a95241bf-73f2-0310-859d-f6bbb57e9c96 --- src/tests/kits/net/udp_echo.c | 84 +++++++++++++++++++++++++++++------ 1 file changed, 71 insertions(+), 13 deletions(-) diff --git a/src/tests/kits/net/udp_echo.c b/src/tests/kits/net/udp_echo.c index 36cc7457b2..01319bc537 100644 --- a/src/tests/kits/net/udp_echo.c +++ b/src/tests/kits/net/udp_echo.c @@ -4,11 +4,13 @@ * * Authors: * Oliver Tappe, zooey@hirschkaefer.de + * Axel Dörfler, axeld@pinc-software.de */ #include #include +#include #include #include #include @@ -33,13 +35,13 @@ printf("trying to send %u bytes...\n", len); status = sendto(sockFD, buf, len, 0, (struct sockaddr*)serverAddr, sizeof(struct sockaddr_in)); if (status < 0) { - printf("sendto(): %lx (%s)\n", status, strerror(status)); + printf("sendto(): %x (%s)\n", errno, strerror(errno)); exit(5); } len = 0; status = recvfrom(sockFD, buf, MAXLEN-1, 0, NULL, NULL); if (status < 0) { - printf("recvfrom(): %lx (%s)\n", status, strerror(status)); + printf("recvfrom(): %x (%s)\n", errno, strerror(errno)); exit(5); } buf[status] = 0; @@ -48,6 +50,36 @@ printf("trying to send %u bytes...\n", len); } +static void +udp_broadcast(int sockFD, const struct sockaddr_in* serverAddr) +{ + char buf[MAXLEN]; + int option = 1; + int len; + int status; + + strcpy(buf, "broadcast"); + len = strlen(buf); + + setsockopt(sockFD, SOL_SOCKET, SO_BROADCAST, &option, sizeof(option)); + + status = sendto(sockFD, buf, len, 0, + (struct sockaddr*)serverAddr, sizeof(struct sockaddr_in)); + if (status < 0) { + printf("sendto(): %s\n", strerror(errno)); + exit(5); + } + + status = recvfrom(sockFD, buf, MAXLEN-1, 0, NULL, NULL); + if (status < 0) { + printf("recvfrom(): %x (%s)\n", errno, strerror(errno)); + exit(5); + } + buf[status] = 0; + printf("-> %s\n", buf); +} + + static void udp_echo_server(int sockFD) { @@ -61,7 +93,7 @@ udp_echo_server(int sockFD) len = sizeof(clientAddr); status = recvfrom(sockFD, buf, MAXLEN-1, 0, (struct sockaddr*)&clientAddr, &len); if (status < 0) { - printf("recvfrom(): %lx (%s)\n", status, strerror(status)); + printf("recvfrom(): %x (%s)\n", errno, strerror(errno)); exit(5); } buf[status] = 0; @@ -76,7 +108,7 @@ udp_echo_server(int sockFD) status = sendto(sockFD, buf, status, 0, (struct sockaddr*)&clientAddr, sizeof(clientAddr)); if (status < 0) { - printf("sendto(): %lx (%s)\n", status, strerror(status)); + printf("sendto(): %x (%s)\n", errno, strerror(errno)); exit(5); } } @@ -86,20 +118,25 @@ udp_echo_server(int sockFD) int main(int argc, char** argv) { - long status; + unsigned long status; int sockFD; struct sockaddr_in serverAddr, localAddr; - int clientMode; + enum { + CLIENT_MODE, + BROADCAST_MODE, + SERVER_MODE, + } mode; unsigned short bindPort = 0; if (argc < 2) { printf("usage: %s client [local-port]\n", argv[0]); + printf("or %s broadcast \n", argv[0]); printf("or %s server \n", argv[0]); exit(5); } if (!strcmp(argv[1], "client")) { - clientMode = 1; + mode = CLIENT_MODE; if (argc < 4) { printf("usage: %s client [local-port]\n", argv[0]); exit(5); @@ -112,8 +149,22 @@ main(int argc, char** argv) bindPort = atoi(argv[4]); printf("client connected to server(%lx:%u)\n", serverAddr.sin_addr.s_addr, ntohs(serverAddr.sin_port)); + } else if (!strcmp(argv[1], "broadcast")) { + mode = BROADCAST_MODE; + if (argc < 3) { + printf("usage: %s broadcast [local-port]\n", argv[0]); + exit(5); + } + + memset(&serverAddr, 0, sizeof(struct sockaddr_in)); + serverAddr.sin_family = AF_INET; + serverAddr.sin_port = htons(atoi(argv[2])); + serverAddr.sin_addr.s_addr = INADDR_BROADCAST; + + if (argc > 3) + bindPort = atoi(argv[3]); } else if (!strcmp(argv[1], "server")) { - clientMode = 0; + mode = SERVER_MODE; if (argc < 3) { printf("usage: %s server \n", argv[0]); exit(5); @@ -130,15 +181,22 @@ main(int argc, char** argv) printf("binding to port %u\n", bindPort); status = bind(sockFD, (struct sockaddr *)&localAddr, sizeof(localAddr)); if (status < 0) { - printf("bind(): %lx (%s)\n", status, strerror(status)); + printf("bind(): %x (%s)\n", errno, strerror(errno)); exit(5); } } - if (clientMode) - udp_echo_client(sockFD, &serverAddr); - else - udp_echo_server(sockFD); + switch (mode) { + case CLIENT_MODE: + udp_echo_client(sockFD, &serverAddr); + break; + case BROADCAST_MODE: + udp_broadcast(sockFD, &serverAddr); + break; + case SERVER_MODE: + udp_echo_server(sockFD); + break; + } return 0; }