From: Razvan Deaconescu Date: Fri, 15 Oct 2010 15:40:40 +0000 (+0300) Subject: add test-socket-signal folder X-Git-Url: http://p2p-next.cs.pub.ro/gitweb/?a=commitdiff_plain;h=295b3a8d8118a8a91fe8d7dfff7a0fa4ef4af7c6;p=p2p-testing-infrastructure.git add test-socket-signal folder Test impact of using SIGSTOP/SIGCONT on TCP network connections. --- diff --git a/Utils/test-socket-signal/.gitignore b/Utils/test-socket-signal/.gitignore new file mode 100644 index 0000000..d6ff91a --- /dev/null +++ b/Utils/test-socket-signal/.gitignore @@ -0,0 +1,2 @@ +*~ +*.o diff --git a/Utils/test-socket-signal/Makefile b/Utils/test-socket-signal/Makefile new file mode 100644 index 0000000..ca8ac9e --- /dev/null +++ b/Utils/test-socket-signal/Makefile @@ -0,0 +1,19 @@ +CFLAGS = -Wall -g +CPPFLAGS = -Isock_util + +.PHONY: all clean + +all: server client + +server: server.o sock_util/sock_util.o + +server.o: server.c sock_util/sock_util.h + +client: client.o sock_util/sock_util.o + +client.o: client.c sock_util/sock_util.h + +sock_util/sock_util.o: sock_util/sock_util.c sock_util/sock_util.h + +clean: + -rm -f *~ *.o server client sock_util/*.o sock_util/*~ diff --git a/Utils/test-socket-signal/client.c b/Utils/test-socket-signal/client.c new file mode 100644 index 0000000..da1b491 --- /dev/null +++ b/Utils/test-socket-signal/client.c @@ -0,0 +1,67 @@ +/* + * inet (BSD) socket client application + * 2010, Razvan Deaconescu + */ + +#include +#include +#include +#include +#include +#include +#include + +#include "sock_util.h" + +#define DEFAULT_SERVER_LISTEN_PORT 43210 +#define DEFAULT_SERVER_HOST "localhost" + +#define DATA_SIZE 120 + + +static char data[DATA_SIZE]; + +static void init_buffer(void) +{ + size_t i; + + srand(time(NULL)); + + for (i = 0; i < DATA_SIZE-1; i++) + data[i] = (char) (rand() % 26) + 'a'; + data[i] = '\0'; +} + +static void print_buffer(void) +{ + printf("data: %s\n\n", data); +} + +static int send_buffer(int sockfd) +{ + return send(sockfd, data, DATA_SIZE, 0); +} + +int main(void) +{ + int connectfd; + + connectfd = tcp_connect_to_server(DEFAULT_SERVER_HOST, + DEFAULT_SERVER_LISTEN_PORT); + if (connectfd < 0) { + exit(EXIT_FAILURE); + } + + init_buffer(); + print_buffer(); + if (send_buffer(connectfd) < 0) { + perror("send_buffer"); + exit(EXIT_FAILURE); + } + + printf("\n--- data sent!\n"); + + close(connectfd); + + return 0; +} diff --git a/Utils/test-socket-signal/server.c b/Utils/test-socket-signal/server.c new file mode 100644 index 0000000..4d6f321 --- /dev/null +++ b/Utils/test-socket-signal/server.c @@ -0,0 +1,64 @@ +/* + * inet (BSD) socket client application + * 2010, Razvan Deaconescu + */ + +#include +#include +#include +#include +#include +#include +#include +#include + +#include "sock_util.h" + +#define DEFAULT_SERVER_LISTEN_PORT 43210 +#define SERVER_BACKLOG 5 + +#define DATA_SIZE 120 + +static char data[DATA_SIZE]; + +static int receive_buffer(int sockfd) +{ + return recv(sockfd, data, DATA_SIZE, 0); +} + +static void print_buffer(void) +{ + printf("data: %s\n\n", data); +} + +int main(void) +{ + int listenfd; + int sockfd; + struct sockaddr_in addr; + socklen_t addrlen = 0; + + listenfd = tcp_listen_connections(DEFAULT_SERVER_LISTEN_PORT, + SERVER_BACKLOG); + if (listenfd < 0) { + exit(EXIT_FAILURE); + } + + sockfd = accept(listenfd, (SSA *) &addr, &addrlen); + if (sockfd < 0) { + perror("sockfd"); + exit(EXIT_FAILURE); + } + + if (receive_buffer(sockfd) < 0) { + perror("readv"); + exit(EXIT_FAILURE); + } + printf("\n-- data received!\n\n"); + print_buffer(); + + close(sockfd); + close(listenfd); + + return 0; +} diff --git a/Utils/test-socket-signal/sock_util/sock_util.c b/Utils/test-socket-signal/sock_util/sock_util.c new file mode 100644 index 0000000..5fe967a --- /dev/null +++ b/Utils/test-socket-signal/sock_util/sock_util.c @@ -0,0 +1,159 @@ +/* + * sock_util.c: useful socket functions + * + * Copyright (C) 2008 Razvan Deaconescu + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#include +#include +#include +#include + +#include +#include +#include +#include +#include +#include +#include + +#include "sock_util.h" + +/* + * parse string to size_t (using strtoul) + * + * returns 0 on success and -1 on error + * error occurs when conversion number overflows or when string + * is invalid (i.e. first non-numeric character is _not_ '\0') + */ + +int parse_size(const char *nptr, size_t *val) +{ + char *endp; + unsigned long tmp; + + tmp = strtoul(nptr, &endp, 0); + if (*endp != '\0') { + fprintf(stderr, "Invalid conversion string\n"); + return -1; + } + if ((errno == ERANGE && tmp == ULONG_MAX) + || (errno != 0 && tmp == 0)) { + fprintf(stderr, "Out of range value\n"); + return -1; + } + + *val = (size_t) tmp; + + return 0; +} + +/* + * connect to a TCP server identified by name (DNS name or dotted decimal + * string) and port + */ + +int tcp_connect_to_server(const char *name, unsigned short port) +{ + struct hostent *hent; + struct sockaddr_in server_addr; + int s; + + hent = gethostbyname(name); + if (hent == NULL) { + perror("gethostbyname"); + return -1; + } + + s = socket(PF_INET, SOCK_STREAM, 0); + if (s < 0) { + perror("socket"); + return -1; + } + + memset(&server_addr, 0, sizeof(server_addr)); + server_addr.sin_family = AF_INET; + server_addr.sin_port = htons(port); + memcpy(&server_addr.sin_addr.s_addr, hent->h_addr, + sizeof(server_addr.sin_addr.s_addr)); + + if (connect(s, (struct sockaddr *) &server_addr, + sizeof(server_addr)) < 0) { + perror("connect"); + return -1; + } + + return s; +} + +int tcp_close_connection(int sockfd) +{ + if (shutdown(sockfd, SHUT_RDWR) < 0) { + perror("shutdown"); + return -1; + } + return close(sockfd); +} + +/* + * create a server socket + */ + +int tcp_listen_connections(unsigned short port, int backlog) +{ + struct sockaddr_in address; + int listenfd; + int sock_opt; + + listenfd = socket(PF_INET, SOCK_STREAM, 0); + if (listenfd < 0) { + perror("socket"); + return -1; + } + + sock_opt = 1; + if (setsockopt(listenfd, SOL_SOCKET, SO_REUSEADDR, + &sock_opt, sizeof(int)) < 0) { + perror("setsockopt"); + exit(EXIT_FAILURE); + } + + memset(&address, 0, sizeof(address)); + address.sin_family = AF_INET; + address.sin_port = htons(port); + address.sin_addr.s_addr = INADDR_ANY; + + if (bind(listenfd, (SSA *) &address, sizeof(address)) < 0) { + perror("bind"); + close(listenfd); + return -1; + } + + if (listen(listenfd, backlog) < 0) { + perror("listen"); + close(listenfd); + return -1; + } + + return listenfd; +} + +void print_connection(struct sockaddr_in *address) +{ + printf("Connected with %s:%d\n", inet_ntoa(address->sin_addr), + ntohs(address->sin_port)); +} diff --git a/Utils/test-socket-signal/sock_util/sock_util.h b/Utils/test-socket-signal/sock_util/sock_util.h new file mode 100644 index 0000000..7dece04 --- /dev/null +++ b/Utils/test-socket-signal/sock_util/sock_util.h @@ -0,0 +1,37 @@ +/* + * sock_util.h: useful socket macros and structures + * + * Copyright (C) 2008 Razvan Deaconescu + * + * This program is free software; you can redistribute it and/or modify + * it under the terms of the GNU General Public License as published by + * the Free Software Foundation; either version 2 of the License, or + * (at your option) any later version. + * + * This program is distributed in the hope that it will be useful, + * but WITHOUT ANY WARRANTY; without even the implied warranty of + * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + * GNU General Public License for more details. + * + * You should have received a copy of the GNU General Public License + * along with this program; if not, write to the Free Software + * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA + */ + +#ifndef SOCK_UTIL_H_ +#define SOCK_UTIL_H_ 1 + +#include +#include +#include + +#define SSA struct sockaddr + +int parse_size(const char *nptr, size_t *val); + +int tcp_connect_to_server(const char *name, unsigned short port); +int tcp_close_connection(int s); +int tcp_listen_connections(unsigned short port, int backlog); +void print_connection(struct sockaddr_in *address); + +#endif