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