[56] | 1 | /* |
---|
| 2 | * Abuse - dark 2D side-scrolling platform game |
---|
| 3 | * Copyright (c) 1995 Crack dot Com |
---|
[494] | 4 | * Copyright (c) 2005-2011 Sam Hocevar <sam@hocevar.net> |
---|
[56] | 5 | * |
---|
| 6 | * This software was released into the Public Domain. As with most public |
---|
[555] | 7 | * domain software, no warranty is made or implied by Crack dot Com, by |
---|
| 8 | * Jonathan Clark, or by Sam Hocevar. |
---|
[56] | 9 | */ |
---|
[2] | 10 | |
---|
[555] | 11 | #if defined HAVE_CONFIG_H |
---|
| 12 | # include "config.h" |
---|
| 13 | #endif |
---|
[2] | 14 | |
---|
[651] | 15 | #if HAVE_NETWORK |
---|
| 16 | |
---|
[2] | 17 | #include <stdio.h> |
---|
| 18 | #include <stdlib.h> |
---|
| 19 | #include <fcntl.h> |
---|
| 20 | #include <unistd.h> |
---|
| 21 | #include <sys/ioctl.h> |
---|
| 22 | #include <sys/stat.h> |
---|
| 23 | #include <sys/types.h> |
---|
| 24 | #include <sys/time.h> |
---|
| 25 | #include <string.h> |
---|
| 26 | #include <signal.h> |
---|
| 27 | #include <sys/socket.h> |
---|
| 28 | #include <netinet/in.h> |
---|
| 29 | #include <sys/types.h> |
---|
| 30 | #include <sys/ipc.h> |
---|
| 31 | #include <sys/shm.h> |
---|
| 32 | #include <netdb.h> |
---|
| 33 | |
---|
[524] | 34 | #include "common.h" |
---|
| 35 | |
---|
[481] | 36 | #include "undrv.h" |
---|
[524] | 37 | #include "../include/netface.h" // net interface structures the engine will use |
---|
[56] | 38 | |
---|
[481] | 39 | //#include "netdrv.h" |
---|
| 40 | #include "gserver.h" |
---|
| 41 | #include "gclient.h" |
---|
| 42 | #include "fileman.h" |
---|
| 43 | #include "sock.h" |
---|
| 44 | #include "tcpip.h" |
---|
[2] | 45 | |
---|
| 46 | |
---|
| 47 | |
---|
| 48 | static int comm_fd=-1,game_fd=-1; |
---|
| 49 | net_socket *game_sock=NULL,*comm_sock=NULL; |
---|
| 50 | |
---|
| 51 | void undrv_cleanup() |
---|
| 52 | { |
---|
| 53 | if (game_sock) { delete game_sock; game_sock=NULL; } |
---|
| 54 | if (comm_sock) { delete comm_sock; comm_sock=NULL; } |
---|
| 55 | } |
---|
| 56 | |
---|
| 57 | |
---|
| 58 | void setup_ports(int comm_port, int game_port) |
---|
| 59 | { |
---|
| 60 | comm_sock=tcpip.create_listen_socket(comm_port,net_socket::SOCKET_SECURE); |
---|
| 61 | if (!comm_sock) mdie("net driver : could not setup comminication socket"); |
---|
| 62 | comm_sock->read_selectable(); |
---|
| 63 | |
---|
| 64 | game_sock=tcpip.create_listen_socket(game_port,net_socket::SOCKET_FAST); |
---|
| 65 | if (!game_sock) mdie("net driver : could not setup data socket"); |
---|
| 66 | game_sock->read_selectable(); |
---|
| 67 | } |
---|
| 68 | |
---|
| 69 | int kill_old_driver(int argc, char **argv) |
---|
| 70 | { |
---|
| 71 | FILE *fp=fopen(DLOCK_NAME,"rb"); |
---|
| 72 | if (fp) |
---|
| 73 | { |
---|
| 74 | int pid; |
---|
| 75 | if (fscanf(fp,"%d",&pid)==1) |
---|
| 76 | { |
---|
| 77 | struct stat st; |
---|
| 78 | char proc_path[50]; |
---|
| 79 | sprintf(proc_path,"/proc/%d",pid); |
---|
| 80 | if (!stat(proc_path,&st)) |
---|
| 81 | { |
---|
[124] | 82 | fprintf(stderr,"net driver : warning, %s already running, attempting to kill...\n",argv[0]); |
---|
| 83 | if (kill(pid,SIGKILL)) |
---|
| 84 | { |
---|
| 85 | fprintf(stderr,"net driver : unable to kill process %d, cannot run net-abuse\n",pid); |
---|
| 86 | fclose(fp); |
---|
| 87 | return 0; |
---|
| 88 | } |
---|
| 89 | fprintf(stderr,"killed process %d\n",pid); |
---|
[2] | 90 | } |
---|
| 91 | } |
---|
| 92 | fclose(fp); |
---|
[124] | 93 | unlink(DLOCK_NAME); |
---|
[2] | 94 | } |
---|
[643] | 95 | unlink(DIN_NAME); // remove any previous files if they existss |
---|
[2] | 96 | unlink(DOUT_NAME); |
---|
| 97 | return 1; |
---|
| 98 | } |
---|
| 99 | |
---|
| 100 | main(int argc, char **argv) |
---|
| 101 | { |
---|
| 102 | if (!kill_old_driver(argc,argv)) |
---|
| 103 | return 0; |
---|
| 104 | |
---|
| 105 | int no_fork=0,i; |
---|
[494] | 106 | for (i=1; i<argc; i++) |
---|
[2] | 107 | if (!strcmp(argv[i],"-no_fork")) // use this to debug easier |
---|
| 108 | no_fork=1; |
---|
[124] | 109 | |
---|
[2] | 110 | if (!no_fork) // use this for debugging |
---|
| 111 | { |
---|
| 112 | int child_pid=fork(); |
---|
| 113 | if (child_pid) |
---|
| 114 | { |
---|
| 115 | FILE *fp=fopen(DLOCK_NAME,"wb"); |
---|
| 116 | if (!fp) |
---|
[124] | 117 | { |
---|
| 118 | fprintf(stderr,"Unable to open %s for writing, killing child\n",DLOCK_NAME); |
---|
| 119 | kill(child_pid,SIGUSR2); |
---|
| 120 | return 0; |
---|
[2] | 121 | } |
---|
| 122 | fprintf(fp,"%d\n",child_pid); |
---|
| 123 | fclose(fp); |
---|
| 124 | printf("%d\n",child_pid); // tell parent the sound driver's process number |
---|
| 125 | return 0; // exit, child will continue |
---|
| 126 | } |
---|
[124] | 127 | } |
---|
[2] | 128 | |
---|
| 129 | fman=new file_manager(argc,argv,&tcpip); |
---|
| 130 | |
---|
| 131 | int comm_port=DEFAULT_COMM_PORT; |
---|
| 132 | int game_port=-1; |
---|
| 133 | int debug=0; |
---|
| 134 | |
---|
[494] | 135 | for (i=1; i<argc-1; i++) |
---|
[2] | 136 | if (!strcmp(argv[i],"-port")) |
---|
| 137 | { |
---|
| 138 | comm_port=atoi(argv[i+1]); |
---|
| 139 | if (game_port==-1) |
---|
| 140 | game_port=comm_port+1; |
---|
| 141 | } |
---|
| 142 | else if (!strcmp(argv[i],"-game_port")) |
---|
| 143 | game_port=atoi(argv[i+1]); |
---|
| 144 | else if (!strcmp(argv[i],"-debug")) |
---|
| 145 | debug=1; |
---|
| 146 | |
---|
| 147 | |
---|
| 148 | if (game_port==-1) game_port=DEFAULT_GAME_PORT; |
---|
[124] | 149 | |
---|
[2] | 150 | // make sure this program was run by the abuse engine |
---|
[124] | 151 | if (argc<2 || strcmp(argv[1],"runme")) |
---|
| 152 | { |
---|
[2] | 153 | fprintf(stderr,"%s is normally run by abuse, running stand-alone file server\n" |
---|
[124] | 154 | "Server will be killed by running abuse\n",argv[0]); |
---|
[2] | 155 | } else driver=new net_driver(argc,argv,comm_port,game_port,&tcpip); |
---|
| 156 | |
---|
| 157 | setup_ports(comm_port,game_port); |
---|
| 158 | |
---|
| 159 | while (1) |
---|
| 160 | { |
---|
[124] | 161 | tcpip.select_sockets(); |
---|
[2] | 162 | if (driver) |
---|
| 163 | driver->check_commands(); |
---|
| 164 | |
---|
| 165 | if (comm_sock->ready_to_read()) |
---|
| 166 | { |
---|
| 167 | net_address *addr; |
---|
| 168 | |
---|
| 169 | net_socket *new_sock=comm_sock->accept(addr); |
---|
| 170 | if (debug) |
---|
| 171 | { |
---|
[124] | 172 | if (new_sock) |
---|
| 173 | { |
---|
| 174 | fprintf(stderr,"accepting new connection from \n"); |
---|
| 175 | addr->print(); |
---|
| 176 | } else |
---|
| 177 | fprintf(stderr,"accept failed\n"); |
---|
[2] | 178 | } |
---|
| 179 | |
---|
[124] | 180 | |
---|
[2] | 181 | if (new_sock) |
---|
| 182 | { |
---|
[124] | 183 | uchar client_type; |
---|
| 184 | if (new_sock->read(&client_type,1)!=1) |
---|
| 185 | { |
---|
[494] | 186 | delete addr; |
---|
[124] | 187 | delete new_sock; |
---|
| 188 | } |
---|
| 189 | else if (client_type==CLIENT_NFS) |
---|
| 190 | { |
---|
[494] | 191 | delete addr; |
---|
[124] | 192 | fman->add_nfs_client(new_sock); |
---|
| 193 | } |
---|
| 194 | else if (!driver || !driver->add_client(client_type,new_sock,addr)) |
---|
| 195 | { |
---|
[494] | 196 | delete addr; |
---|
[124] | 197 | delete new_sock; |
---|
| 198 | } |
---|
[2] | 199 | } |
---|
[124] | 200 | } |
---|
[2] | 201 | |
---|
| 202 | fman->process_net(); |
---|
| 203 | } |
---|
[651] | 204 | } |
---|
[2] | 205 | |
---|
[651] | 206 | #endif // HAVE_NETWORK |
---|
[2] | 207 | |
---|