libpsyc/test/testServer.c

194 lines
5.4 KiB
C
Raw Normal View History

2011-04-27 17:18:17 +00:00
/**
* test server for packet parsing & rendering
2011-04-28 13:20:07 +00:00
*
2011-04-27 17:18:17 +00:00
* based on selectserver.c from http://beej.us/guide/bgnet/
2011-04-28 13:20:07 +00:00
* "The C source code presented in this document is hereby granted to the public domain, and is completely free of any license restriction."
2011-04-27 17:18:17 +00:00
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <sys/time.h>
2011-04-27 17:18:17 +00:00
#include <netinet/in.h>
#include <arpa/inet.h>
#define __USE_POSIX
2011-04-27 17:18:17 +00:00
#include <netdb.h>
#include "testServer.h"
#include "testServerPsyc.c"
2011-04-27 17:18:17 +00:00
// get sockaddr, IPv4 or IPv6:
void *get_in_addr (struct sockaddr *sa) {
if (sa->sa_family == AF_INET)
2011-04-27 17:18:17 +00:00
return &(((struct sockaddr_in*)sa)->sin_addr);
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
int main (int argc, char **argv) {
2011-04-27 17:18:17 +00:00
char *port = argc > 1 ? argv[1] : "4440";
2011-05-06 11:32:12 +00:00
char *opts = argc > 2 ? argv[2] : NULL;
char *v, *w;
verbose = opts && (v = memchr(opts, (int)'v', strlen(opts))) ?
2011-05-06 11:32:12 +00:00
v - opts > 0 && (w = memchr(v+1, (int)'v', strlen(opts) - (v - opts))) ?
w - v > 0 && memchr(w+1, (int)'v', strlen(opts) - (w - opts)) ? 3 : 2 : 1 : 0;
routing_only = opts && memchr(opts, (int)'r', strlen(opts));
parse_multiple = opts && memchr(opts, (int)'m', strlen(opts));
no_render = opts && memchr(opts, (int)'n', strlen(opts));
progress = opts && memchr(opts, (int)'p', strlen(opts));
stats = opts && memchr(opts, (int)'s', strlen(opts));
size_t recv_buf_size = argc > 3 ? atoi(argv[3]) : 0;
if (recv_buf_size <= 0)
recv_buf_size = RECV_BUF_SIZE;
2011-04-27 17:18:17 +00:00
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
int fdmax; // maximum file descriptor number
int listener; // listening socket descriptor
int newfd; // newly accept()ed socket descriptor
struct sockaddr_storage remoteaddr; // client address
socklen_t addrlen;
char remoteIP[INET6_ADDRSTRLEN];
int yes = 1; // for setsockopt() SO_REUSEADDR, below
int i, rv, ret;
2011-04-27 17:18:17 +00:00
struct addrinfo hints, *ai, *p;
struct timeval start[NUM_PARSERS], end[NUM_PARSERS];
2011-04-27 17:18:17 +00:00
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);
// get us a socket and bind it
memset(&hints, 0, sizeof hints);
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_STREAM;
hints.ai_flags = AI_PASSIVE;
if ((rv = getaddrinfo(NULL, port, &hints, &ai)) != 0) {
2011-04-27 17:18:17 +00:00
fprintf(stderr, "error: %s\n", gai_strerror(rv));
exit(1);
}
for (p = ai; p != NULL; p = p->ai_next) {
2011-04-27 17:18:17 +00:00
listener = socket(p->ai_family, p->ai_socktype, p->ai_protocol);
if (listener < 0)
2011-04-27 17:18:17 +00:00
continue;
// lose the pesky "address already in use" error message
setsockopt(listener, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(int));
if (bind(listener, p->ai_addr, p->ai_addrlen) < 0) {
2011-04-27 17:18:17 +00:00
close(listener);
continue;
}
break;
}
// if we got here, it means we didn't get bound
if (p == NULL) {
2011-04-27 17:18:17 +00:00
fprintf(stderr, "failed to bind\n");
exit(2);
}
freeaddrinfo(ai); // all done with this
// listen
if (listen(listener, 10) == -1) {
2011-04-27 17:18:17 +00:00
perror("listen");
exit(3);
}
// add the listener to the master set
FD_SET(listener, &master);
// keep track of the biggest file descriptor
fdmax = listener; // so far, it's this one
// main loop
for (;;) {
2011-04-27 17:18:17 +00:00
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
2011-04-27 17:18:17 +00:00
perror("select");
exit(4);
}
// run through the existing connections looking for data to read
for (i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) { // we got one!!
if (i == listener) {
2011-04-27 17:18:17 +00:00
// handle new connections
if (fdmax == NUM_PARSERS - 1)
continue; // ignore if there's too many
addrlen = sizeof remoteaddr;
newfd = accept(listener, (struct sockaddr *)&remoteaddr, &addrlen);
if (newfd == -1) {
2011-04-27 17:18:17 +00:00
perror("accept");
} else {
2011-04-27 17:18:17 +00:00
FD_SET(newfd, &master); // add to master set
if (newfd > fdmax) // keep track of the max
2011-04-27 17:18:17 +00:00
fdmax = newfd;
test_init(newfd);
2011-04-27 17:18:17 +00:00
2011-05-06 11:32:12 +00:00
if (verbose)
printf("# New connection from %s on socket %d\n",
inet_ntop(remoteaddr.ss_family,
get_in_addr((struct sockaddr*)&remoteaddr),
remoteIP, INET6_ADDRSTRLEN),
newfd);
if (stats)
gettimeofday(&start[newfd], NULL);
2011-04-27 17:18:17 +00:00
}
} else {
2011-04-27 17:18:17 +00:00
// handle data from a client
if ((nbytes = recv(i, recvbuf, recv_buf_size, 0)) <= 0) {
if (stats)
printf("%ld ms\n", (end[i].tv_sec * 1000000 + end[i].tv_usec - start[i].tv_sec * 1000000 - start[i].tv_usec) / 1000);
2011-04-27 17:18:17 +00:00
// got error or connection closed by client
if (nbytes == 0) // connection closed
2011-05-06 11:32:12 +00:00
printf("# Socket %d hung up\n", i);
else
2011-04-27 17:18:17 +00:00
perror("recv");
2011-04-27 17:18:17 +00:00
close(i); // bye!
FD_CLR(i, &master); // remove from master set
} else {
2011-05-08 16:45:41 +00:00
if (verbose >= 2)
printf("> %ld bytes\n", nbytes);
if (verbose >= 3)
printf("> [%.*s]", (int)nbytes, recvbuf);
ret = test_input(i);
if (progress)
write(1, " ", 1);
if (stats)
gettimeofday(&end[i], NULL);
if (ret < 0) {
2011-05-06 11:32:12 +00:00
if (verbose)
printf("# Closing connection: %i\n", i);
2011-04-27 17:18:17 +00:00
close(i); // bye!
FD_CLR(i, &master); // remove from master set
}
}
} // END handle data from client
} // END got new incoming connection
} // END looping through file descriptors
} // END for(;;)--and you thought it would never end!
return 0;
}