PERFORCE change 165334 for review
Fang Wang
fangwang at FreeBSD.org
Sun Jun 28 01:25:20 UTC 2009
http://perforce.freebsd.org/chv.cgi?CH=165334
Change 165334 by fangwang at fangwang_utobsd on 2009/06/28 01:25:08
Impelement tcputo regression test code, not finish yet.
Affected files ...
.. //depot/projects/soc2009/tcputo/src/tools/regression/netinet/tcputo/Makefile#1 add
.. //depot/projects/soc2009/tcputo/src/tools/regression/netinet/tcputo/tcputo.c#2 edit
Differences ...
==== //depot/projects/soc2009/tcputo/src/tools/regression/netinet/tcputo/tcputo.c#2 (text+ko) ====
@@ -1,0 +1,179 @@
+#include <sys/types.h>
+#include <sys/socket.h>
+
+#include <netinet/in.h>
+#include <netinet/tcp.h>
+
+#include <arpa/inet.h>
+
+#include <errno.h>
+#include <fcntl.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+#include <unistd.h>
+#include <time.h>
+
+static void
+usage(void)
+{
+
+ fprintf(stderr, "tcpconnect server port\n");
+ fprintf(stderr, "tcpconnect client ip port\n");
+ exit(-1);
+}
+
+static int
+tcputo_timer(void)
+{
+ static time_t start_time = 0;
+ time_t end_time;
+ time_t interval;
+
+ if (start_time == 0) {
+ time(&start_time);
+ interval = 0;
+ }
+ else {
+ time(&end_time);
+ interval = end_time - start_time;
+ start_time = 0;
+ }
+
+ return (int)(interval);
+}
+
+static void
+tcputo_server(int argc, char *argv[])
+{
+ int listen_sock, accept_sock;
+ struct sockaddr_in sin;
+ char *dummy;
+ char buf[16*1024];
+ long port;
+ int user_timeout;
+ int optval = 4*1024;
+
+ if (argc != 1)
+ usage();
+
+ bzero(&sin, sizeof(sin));
+ sin.sin_len = sizeof(sin);
+ sin.sin_family = AF_INET;
+ sin.sin_addr.s_addr = htonl(INADDR_ANY);
+
+ port = strtoul(argv[0], &dummy, 10);
+ if (port < 1 || port > 65535 || *dummy != '\0')
+ usage();
+ sin.sin_port = htons(port);
+
+ listen_sock = socket(PF_INET, SOCK_STREAM, 0);
+ if (listen_sock == -1)
+ err(-1, "socket");
+
+ if (bind(listen_sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
+ err(-1, "bind");
+
+ if (listen(listen_sock, -1) == -1)
+ err(-1, "listen");
+
+ accept_sock = accept(listen_sock, NULL, NULL);
+ close(listen_sock);
+ if (setsockopt(accept_sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof(optval)) == -1)
+ err(-1, "setsockopt");
+ while(1) {
+ sleep(1);
+ printf("server again %d\n", optval++);
+ (void)recv(accept_sock, buf, 16*1024, MSG_DONTWAIT);
+ (void)tcputo_timer();
+ if (send(accept_sock, buf, 16*1024, MSG_NOSIGNAL) >= 0) {
+ (void)tcputo_timer();
+ continue;
+ }
+ puts(strerror(errno));
+ user_timeout = tcputo_timer();
+ printf("Connection timeout, %d seconds.\n", user_timeout);
+ break;
+ }
+
+ close(accept_sock);
+}
+
+static void
+tcputo_client(int argc, char *argv[])
+{
+ struct sockaddr_in sin;
+ long count, i, port;
+ char *dummy;
+ char buf[16*1024];
+ int sock;
+ int nonblock = 0, md5enable = 0;
+ int user_timeout;
+ int optval = 4*1024;
+
+ if (argc != 2)
+ usage();
+
+ bzero(&sin, sizeof(sin));
+ sin.sin_len = sizeof(sin);
+ sin.sin_family = AF_INET;
+ if (inet_aton(argv[0], &sin.sin_addr) == 0)
+ err(-1, "convert address");
+
+ port = strtoul(argv[1], &dummy, 10);
+ if (port < 1 || port > 65535 || *dummy != '\0')
+ usage();
+ sin.sin_port = htons(port);
+
+ sock = socket(PF_INET, SOCK_STREAM, 0);
+ if (sock == -1)
+ err(-1, "socket");
+ if (setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &optval, sizeof(optval)) == -1)
+ err(-1, "setsockopt");
+
+ /* No warning in default case on ENOPROTOOPT. */
+ /* if (setsockopt(sock, IPPROTO_TCP, TCP_MD5SIG,
+ &md5enable, sizeof(md5enable)) != 0) {
+ if (errno == ENOPROTOOPT && md5enable > 0)
+ err(-1, "setsockopt(TCP_MD5SIG)");
+ else if (errno != ENOPROTOOPT)
+ warn("setsockopt(TCP_MD5SIG)");
+ } */
+
+ if (connect(sock, (struct sockaddr *)&sin, sizeof(sin)) == -1)
+ err(-1, "connect");
+
+ while (1) {
+ sleep(1);
+ printf("client again %d\n", optval++);
+ (void)recv(sock, buf, 16*1024, MSG_DONTWAIT);
+ (void)tcputo_timer();
+ if (send(sock, buf, 16*1024, MSG_NOSIGNAL) > 0) {
+ (void)tcputo_timer();
+ continue;
+ }
+ puts(strerror(errno));
+ user_timeout = tcputo_timer();
+ printf("Connection timeout, %d seconds.\n", user_timeout);
+ break;
+ }
+
+ close(sock);
+}
+
+int
+main(int argc, char *argv[])
+{
+
+ if (argc < 2)
+ usage();
+
+ if (strcmp(argv[1], "server") == 0)
+ tcputo_server(argc - 2, argv + 2);
+ else if (strcmp(argv[1], "client") == 0)
+ tcputo_client(argc - 2, argv + 2);
+ else
+ usage();
+
+ exit(0);
+}
More information about the p4-projects
mailing list