Commit 1d5d6d55 by Michael Pastushkov

cleanup

parent 5a381fad
......@@ -304,31 +304,6 @@ int build() {
return 0;
}
int add_udp_client(struct sockaddr_in client_addr, pid_t pid) {
int i;
for (i=0; i<MAX_UDP_CLIENTS; i++) {
if (clients[i].pid == 0) {
clients[i].addr = client_addr;
clients[i].pid = pid;
return 0;
}
}
return -1; /* No space for new clients */
}
void remove_udp_client(pid_t pid) {
int i;
for (i = 0; i<MAX_UDP_CLIENTS; i++) {
if (clients[i].pid == pid) {
if (options.log)
printf("removing client %s, pid: %d\n", inet_ntoa(clients[i].addr.sin_addr), pid);
clients[i].pid = 0;
memset(&clients[i].addr, 0, sizeof(clients[i].addr));
break;
}
}
}
int compare_clients(struct sockaddr_in client1, struct sockaddr_in client2) {
return (client1.sin_addr.s_addr == client2.sin_addr.s_addr &&
client1.sin_port == client2.sin_port);
......@@ -343,13 +318,13 @@ int find_udp_client(struct sockaddr_in client_addr) {
return -1;
}
int use(void)
int use()
{
fd_set io;
unsigned char buffer[options.buffer_size];
struct sockaddr_in client_addr;
socklen_t addr_len = sizeof(client_addr);
int count_recv; //, count_sent;
int count_recv;
for (;;)
{
......@@ -374,7 +349,7 @@ int use(void)
if (rc.my_addr.sin_port == 0) {
rc.my_addr.sin_addr.s_addr = client_addr.sin_addr.s_addr;
rc.my_addr.sin_port = client_addr.sin_port;
if (options.log)
if (options.log > 2)
printf("my address %s:%d\tpid: %d\n", inet_ntoa(client_addr.sin_addr), ntohs(client_addr.sin_port), getpid());
} else {
if (!compare_clients(rc.my_addr, client_addr)) {
......@@ -406,7 +381,7 @@ int use(void)
decode(buffer, count_recv) :
encode(buffer, count_recv);
/* count_sent = */(options.proto == SOCK_DGRAM) ?
(options.proto == SOCK_DGRAM) ?
sendto(rc.remote_socket, buffer, count_recv, 0, (struct sockaddr *)&rc.remote_addr, sizeof(rc.remote_addr)) :
send(rc.remote_socket, buffer, count_recv, 0);
......@@ -456,26 +431,11 @@ void run() {
void fork_udp() {
#ifndef __MINGW32__
pid_t child_pid;
int client_index = find_udp_client(rc.client_addr);
if (client_index == -1) {
if ((child_pid = fork()) == 0) {
if (options.log)
printf("request from %s, forking %d ...\n", inet_ntoa(rc.client_addr.sin_addr), getpid());
run();
exit(0);
}
if (add_udp_client(rc.client_addr, child_pid) < 0) {
printf("maximum clients %d reached: \n", MAX_UDP_CLIENTS);
}
/* Reap any zombie processes (clients that exited) */
while ((child_pid = waitpid(-1, NULL, WNOHANG)) > 0) {
remove_udp_client(child_pid);
}
} else {
if (options.log > 2)
printf("existing udp client %s, handled by %d ...\n", inet_ntoa(rc.client_addr.sin_addr), clients[client_index].pid);
if ((child_pid = fork()) == 0) {
if (options.log)
printf("request from %s, forking %d ...\n", inet_ntoa(rc.client_addr.sin_addr), getpid());
run();
exit(0);
}
#endif
}
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment