1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
|
/*
* MudName Server v. 1.6 - A character name generator
* Copyright (C) 1997-2001 Ragnar Hojland Espinosa <ragnar@ragnar-hojland.com>
*
* 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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/time.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <time.h>
#include "names.h"
#include "globals.h"
#include "server.h"
int serverfd;
int connection_count;
void name_generator (int fd, void *args)
{
int i;
srand (time(NULL) ^ getpid());
write (fd, text_version, strlen (text_version));
write (fd, text_ad, strlen (text_ad));
for (i = 0; i < 10; ++i) {
char buf[8192];
generate_name (rrand(0,dictionary_count - 1), buf, 8190);
strcat (buf, "\n\r");
buf[0] -= 32;
write (fd, buf, strlen (buf));
}
write (fd, text_footer, strlen (text_footer));
}
/* obviously at least gettimeofday shouldn't be called inside a signal
* handler. it'll do, tho */
void handler_sigterm (int signo)
{
struct timeval tv;
char *now;
close (serverfd);
gettimeofday (&tv, NULL);
now = ctime (&tv.tv_sec);
now[strlen(now) -1] = '\0';
fprintf (logfile, "%s: killed, processed %d connections\n", now, connection_count);
exit(0);
}
int init_server (int port)
{
struct sockaddr_in server_addr;
serverfd = socket (AF_INET, SOCK_STREAM, 0);
if (serverfd < 0) {
perror ("socket");
return 1;
}
memset (&server_addr, 0, sizeof(server_addr));
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = htonl (INADDR_ANY);
server_addr.sin_port = htons(port);
if (bind (serverfd, (struct sockaddr*) &server_addr, sizeof(server_addr)) < 0) {
perror ("bind");
return 1;
}
if (listen (serverfd, 5) < 0) {
perror ("listen");
return 1;
}
return 0;
}
int run_server (void (*function)(int fd, void *args), void *args)
{
struct sockaddr_in client_addr;
struct timeval tv;
char *now;
gettimeofday (&tv, NULL);
now = ctime (&tv.tv_sec);
now[strlen(now) -1] = '\0';
fprintf (logfile, "%s: %s started\n", now, text_fullid);
while (1) {
pid_t child;
unsigned int n = sizeof (client_addr);
int clientfd = accept (serverfd, (struct sockaddr*) &client_addr, &n);
if (clientfd < 0) {
perror ("accept");
}
++connection_count;
if ( (child = fork()) < 0) {
perror ("fork");
} else if (child == 0) {
if ( (child = fork()) < 0) {
perror ("fork");
} else if (child > 0) {
exit(0);
}
close (serverfd);
function (clientfd, args);
close (clientfd);
exit(0);
}
close (clientfd);
waitpid (child, NULL, 0);
}
}
|