diff --git a/src/tests/kits/net/Jamfile b/src/tests/kits/net/Jamfile index 5352cdb034..4067a9f2e3 100644 --- a/src/tests/kits/net/Jamfile +++ b/src/tests/kits/net/Jamfile @@ -10,7 +10,9 @@ SubDir HAIKU_TOP src tests kits net ; #UsePrivateHeaders kernel ; #UseArchHeaders $(TARGET_ARCH) ; -# SimpleTest at_client : at_client.c : ; +SimpleTest udp_client : udp_client.c : $(NETWORK_LIBS) ; +SimpleTest udp_echo : udp_echo.c : $(NETWORK_LIBS) ; +SimpleTest udp_server : udp_server.c : $(NETWORK_LIBS) ; SubInclude HAIKU_TOP src tests kits net DialUpPreflet ; SubInclude HAIKU_TOP src tests kits net netperf ; diff --git a/src/tests/kits/net/udp_client.c b/src/tests/kits/net/udp_client.c new file mode 100644 index 0000000000..6d44f42bfb --- /dev/null +++ b/src/tests/kits/net/udp_client.c @@ -0,0 +1,77 @@ +/* + * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Oliver Tappe, zooey@hirschkaefer.de + */ + + +#include +#include +#include +#include +#include +#include + +#define MAXLEN 32768 + + +static void +udp_client(int sockFD, const struct sockaddr_in* serverAddr) +{ + char sendbuf[MAXLEN]; + long status; + + while (!feof(stdin)) { + status = fread(sendbuf, 1, MAXLEN, stdin); + if (status < 0) { + printf("fread(): %lx (%s)\n", status, strerror(status)); + exit(5); + } +printf("trying to send %ld bytes...\n", status); + status = sendto(sockFD, sendbuf, status, 0, + (struct sockaddr*)serverAddr, sizeof(struct sockaddr_in)); + if (status < 0) { + printf("sendto(): %lx (%s)\n", status, strerror(status)); + exit(5); + } + } +} + + +int +main(int argc, char** argv) +{ + long status; + int sockFD; + struct sockaddr_in serverAddr, clientAddr; + + if (argc < 3) { + printf("usage: %s [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 = inet_addr(argv[1]); + printf("addr=%lx port=%u\n", serverAddr.sin_addr.s_addr, ntohs(serverAddr.sin_port)); + + sockFD = socket(AF_INET, SOCK_DGRAM, 0); + + if (argc > 3) { + memset(&clientAddr, 0, sizeof(struct sockaddr_in)); + clientAddr.sin_family = AF_INET; + clientAddr.sin_port = htons(atoi(argv[3])); + printf("binding to port %u\n", ntohs(clientAddr.sin_port)); + status = bind(sockFD, (struct sockaddr *)&clientAddr, sizeof(struct sockaddr_in)); + if (status < 0) { + printf("bind(): %lx (%s)\n", status, strerror(status)); + exit(5); + } + } + + udp_client(sockFD, &serverAddr); + return 0; +} diff --git a/src/tests/kits/net/udp_echo.c b/src/tests/kits/net/udp_echo.c new file mode 100644 index 0000000000..36cc7457b2 --- /dev/null +++ b/src/tests/kits/net/udp_echo.c @@ -0,0 +1,144 @@ +/* + * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Oliver Tappe, zooey@hirschkaefer.de + */ + + +#include +#include +#include +#include +#include +#include +#include + +#define MAXLEN 65535 + + +static void +udp_echo_client(int sockFD, const struct sockaddr_in* serverAddr) +{ + char buf[MAXLEN]; + unsigned int len; + long status; + + while (fgets(buf, MAXLEN, stdin) != NULL) { + len = strlen(buf); + if (len > 0) + len--; +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)); + exit(5); + } + len = 0; + status = recvfrom(sockFD, buf, MAXLEN-1, 0, NULL, NULL); + if (status < 0) { + printf("recvfrom(): %lx (%s)\n", status, strerror(status)); + exit(5); + } + buf[status] = 0; + printf("-> %s\n", buf); + } +} + + +static void +udp_echo_server(int sockFD) +{ + char buf[MAXLEN]; + long status; + socklen_t len; + long i; + struct sockaddr_in clientAddr; + + while (1) { + 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)); + exit(5); + } + buf[status] = 0; + printf("got <%s> from client(%lx:%u)\n", buf, clientAddr.sin_addr.s_addr, clientAddr.sin_port); + for (i = 0; i < status; ++i) { + if (islower(buf[i])) + buf[i] = toupper(buf[i]); + else if (isupper(buf[i])) + buf[i] = tolower(buf[i]); + } + printf("sending <%s>\n", buf); + status = sendto(sockFD, buf, status, 0, + (struct sockaddr*)&clientAddr, sizeof(clientAddr)); + if (status < 0) { + printf("sendto(): %lx (%s)\n", status, strerror(status)); + exit(5); + } + } +} + + +int +main(int argc, char** argv) +{ + long status; + int sockFD; + struct sockaddr_in serverAddr, localAddr; + int clientMode; + unsigned short bindPort = 0; + + if (argc < 2) { + printf("usage: %s client [local-port]\n", argv[0]); + printf("or %s server \n", argv[0]); + exit(5); + } + + if (!strcmp(argv[1], "client")) { + clientMode = 1; + if (argc < 4) { + printf("usage: %s client [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[3])); + serverAddr.sin_addr.s_addr = inet_addr(argv[2]); + if (argc > 4) + 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], "server")) { + clientMode = 0; + if (argc < 3) { + printf("usage: %s server \n", argv[0]); + exit(5); + } + bindPort = atoi(argv[2]); + } + + sockFD = socket(AF_INET, SOCK_DGRAM, 0); + + if (bindPort > 0) { + memset(&localAddr, 0, sizeof(struct sockaddr_in)); + localAddr.sin_family = AF_INET; + localAddr.sin_port = htons(bindPort); + 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)); + exit(5); + } + } + + if (clientMode) + udp_echo_client(sockFD, &serverAddr); + else + udp_echo_server(sockFD); + + return 0; +} diff --git a/src/tests/kits/net/udp_server.c b/src/tests/kits/net/udp_server.c new file mode 100644 index 0000000000..73ee068e54 --- /dev/null +++ b/src/tests/kits/net/udp_server.c @@ -0,0 +1,64 @@ +/* + * Copyright 2006, Haiku, Inc. All Rights Reserved. + * Distributed under the terms of the MIT License. + * + * Authors: + * Oliver Tappe, zooey@hirschkaefer.de + */ + + +#include +#include +#include +#include +#include +#include + +#define MAXLEN 65536 + + +static void +udp_server(int sockFD) +{ + char buf[MAXLEN]; + long status; + + while (1) { + status = recvfrom(sockFD, buf, MAXLEN-1, 0, NULL, NULL); + if (status < 0) { + printf("recvfrom(): %lx (%s)\n", status, strerror(status)); + exit(5); + } + buf[status] = 0; + printf("%s", buf); + } +} + + +int +main(int argc, char** argv) +{ + long status; + int sockFD; + struct sockaddr_in localAddr; + + if (argc < 2) { + printf("usage: %s \n", argv[0]); + exit(5); + } + + sockFD = socket(AF_INET, SOCK_DGRAM, 0); + + memset(&localAddr, 0, sizeof(struct sockaddr_in)); + localAddr.sin_family = AF_INET; + localAddr.sin_port = htons(atoi(argv[1])); + printf("binding to port %u\n", ntohs(localAddr.sin_port)); + status = bind(sockFD, (struct sockaddr *)&localAddr, sizeof(struct sockaddr_in)); + if (status < 0) { + printf("bind(): %lx (%s)\n", status, strerror(status)); + exit(5); + } + + udp_server(sockFD); + return 0; +}