source: abuse/tags/pd/macabuse/src/net/unix/undrv.c @ 49

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