All pastes #965028 Raw Edit

Something

public c v1 · immutable
#965028 ·published 2008-03-31 18:53 UTC
rendered paste body
#include <unistd.h>#include <stdio.h>#include <stdlib.h>#include <string.h>#include <sys/socket.h>#include <sys/ioctl.h>#include <arpa/inet.h>#include <netinet/ip.h>#include <netinet/tcp.h>#include <linux/if.h>#include <linux/if_packet.h>#include <linux/if_ether.h>#include <sys/wait.h>#include <sys/time.h>void printError(char msg[128]);void transmit(int port, int protocol, int requests, int buffersize);struct sockaddr_in remote_addr;struct ifreq ifreq;socklen_t s_size;FILE* logfile;int main(int argc, char* argv[]) {	pid_t pid;	int i, port, clients, requests, buffersize, protocol;	if (argc < 7) printError("usage: testcli <ip> <port> <protocol> <clients> <requests> <bytes>\n");	logfile = fopen("log.txt","w");	if (inet_addr(argv[1]) == -1) printError("invalid ip address\n");	if ((port = atoi(argv[2])) < 1) printError("port < 1\n");	if ((clients = atoi(argv[4])) < 1) printError("clients < 1\n");	if ((requests = atoi(argv[5])) < 1) printError("requests < 1\n");	if ((buffersize = atoi(argv[6])) < 1 || buffersize > 8192) printError("bytes parameter must be between 1 and 8192\n");	if (!strcmp(argv[3],"UDP")) protocol = SOCK_DGRAM;	else if (!strcmp(argv[3],"TCP")) protocol = SOCK_STREAM;	else printError("invalid protocol specified");	remote_addr.sin_family = AF_INET;	remote_addr.sin_port = htons(port);	inet_pton(AF_INET, argv[1], &remote_addr.sin_addr.s_addr);	strcpy (ifreq.ifr_name, "eth0");	s_size = sizeof(remote_addr);	if (getpid() == getppid()) {		fprintf(logfile,"LOG: %i bytes\n", buffersize);		fprintf(logfile,"LOG: %i clients\n", clients);		fprintf(logfile,"-------------------\n");	}	for (i = 0; i < clients; i++) {		pid = fork();		switch(pid) {			case -1: printError("Fork error\n");			case 0: {				transmit(port, protocol, requests, buffersize);				break;			}		}	}	for (i = 0; i < clients; i++) wait(0);	fclose(logfile);	return 0;}void transmit(int port, int protocol, int requests, int buffersize) {	int i, sock, msec;	char buffer[buffersize], rbuffer[buffersize];		for (i = 0; i < buffersize; i++) { buffer[i] = (char)(rand() % 92 + 70); }	struct timeval start, finish;  	gettimeofday (&start, NULL);	switch (protocol) {		case SOCK_STREAM:{			if ((sock = socket(AF_INET, protocol, 0)) == -1) printError("Can't create socket\n");			if (connect(sock, (struct sockaddr *)&remote_addr, s_size) == -1) printError("Can't connect\n");			printf("Forked TCP process (pid: %i) connected\n", getpid());			for (i = 0; i < requests; i++) {				if (send(sock, buffer, buffersize, 0) == -1) printf("Forked process, Send error (pid: %i)\n", getpid());				if (recv(sock, rbuffer, buffersize, 0) == -1) printf("Forked process, Recv error (pid: %i)\n", getpid());				else printf("Forked process %i sent:	%s\n		received:	%s\n", getpid(), buffer, rbuffer);			}			close(sock);			gettimeofday (&finish, NULL);			msec = finish.tv_sec * 1000 + finish.tv_usec / 1000;	  		msec -= start.tv_sec * 1000 + start.tv_usec / 1000;			printf("Forked process (pid: %i) killed\n", getpid());			fprintf(logfile," - Duration: %5.3fs\n", (double)msec/1000);			exit(0);			break;		}		case SOCK_DGRAM:{			if ((sock = socket(AF_INET, protocol, 0)) == -1) printError("Can't create socket\n");			printf("Forked UDP process (pid: %i) created\n", getpid());			for (i = 0; i < requests; i++) {				if (sendto(sock, buffer, buffersize, 0, (struct sockaddr*)&remote_addr, s_size) == -1) printf("Forked process, Sendto error (pid: %i)\n", getpid());				if (recvfrom(sock, rbuffer, buffersize, 0, (struct sockaddr*)&remote_addr, &s_size) == -1) printf("Forked process, Recvfrom error (pid: %i)\n", getpid());				else printf("Forked process %i sent:	%s\n		received:	%s\n", getpid(), buffer, rbuffer);			}			close(sock);			gettimeofday (&finish, NULL);			msec = finish.tv_sec * 1000 + finish.tv_usec / 1000;	  		msec -= start.tv_sec * 1000 + start.tv_usec / 1000;			printf("Forked process (pid: %i) killed\n", getpid());			fprintf(logfile," - Duration: %5.3fs\n", (double)msec/1000);			exit(0);			break;		}	}}void printError(char msg[128]) {	printf(msg);	exit(1);}