source: abuse/trunk/src/net/include/tcpip.hpp @ 4

Last change on this file since 4 was 4, checked in by Sam Hocevar, 17 years ago
  • debian patches
File size: 6.7 KB
Line 
1#include "sock.hpp"
2
3#include <stdlib.h>
4#include <fcntl.h>
5#include <sys/ioctl.h>
6#include <sys/stat.h>
7#include <sys/types.h>
8#include <signal.h>
9#include <sys/types.h>
10#include "isllist.hpp"
11
12#ifdef __APPLE__
13typedef int socklen_t;
14#endif
15
16#if (defined(__APPLE__) && !defined(__MACH__))
17#include "GUSI.h"
18#else
19#include <netdb.h>
20#include <netinet/in.h>
21#include <stdio.h>
22#include <string.h>
23#include <sys/time.h>
24#include <sys/ipc.h>
25#include <sys/shm.h>
26#include <sys/socket.h>
27#include <unistd.h>
28#ifdef HAVE_BSTRING_H
29#include <bstring.h>
30#else
31#include <sys/select.h>
32#endif
33#endif
34
35extern fd_set master_set,master_write_set,read_set,exception_set,write_set;
36
37class ip_address : public net_address
38{
39public:
40  sockaddr_in addr;
41
42  virtual protocol protocol_type() const { return net_address::IP; }
43  virtual int equal(const net_address *who) const
44  //{{{
45  {
46    if (who->protocol_type()==IP &&
47        !memcmp(&addr.sin_addr,& ((ip_address *)who)->addr.sin_addr,sizeof(addr.sin_addr)))
48      return 1;
49    else return 0;
50  }
51        //}}}
52  virtual int set_port(int port)  { addr.sin_port=htons(port); return 1; }
53  ip_address(sockaddr_in *Addr) { memcpy(&addr,Addr,sizeof(addr)); }
54  virtual void print()
55  //{{{
56  {
57    unsigned char *c=(unsigned char *) (&addr.sin_addr.s_addr);
58    fprintf(stderr,"%d.%d.%d.%d",c[0],c[1],c[2],c[3]);
59  }
60        //}}}
61  int get_port() { return htons(addr.sin_port); }
62  net_address *copy()  { return new ip_address(&addr); }
63  ip_address() {} ;
64  void store_string(char *st, int st_length)
65  //{{{
66  {
67    char buf[100];
68    unsigned char *c=(unsigned char *) (&addr.sin_addr.s_addr);
69    sprintf(buf,"%d.%d.%d.%d:%d",c[0],c[1],c[2],c[3],htons(addr.sin_port));
70    strncpy(st,buf,st_length);
71    st[st_length-1]=0;   
72  }
73  //}}}
74} ;
75
76class tcpip_protocol : public net_protocol
77{
78protected:
79  // Request Data
80  struct RequestItem
81  {
82        ip_address *addr;
83        char name[256];   //name
84  };
85  typedef isllist<RequestItem *>::iterator p_request;
86  isllist<RequestItem*> servers,returned;
87
88  // Notification Data
89  net_socket *notifier;
90  char notify_data[512];
91  int notify_len;
92
93  net_socket *responder;
94  ip_address *bcast;
95
96  int handle_notification();
97  int handle_responder();
98public :
99  fd_set master_set,master_write_set,read_set,exception_set,write_set;
100
101  tcpip_protocol();
102  net_address *get_local_address();
103  net_address *get_node_address(char *&server_name, int def_port, int force_port);
104  net_socket *connect_to_server(net_address *addr,
105        net_socket::socket_type sock_type=net_socket::SOCKET_SECURE);
106  net_socket *create_listen_socket(int port, net_socket::socket_type sock_type);
107  int installed() { return 1; }  // always part of unix
108  char *name() { return "UNIX generic TCPIP"; }
109  void cleanup();
110  int select(int block);          // return # of sockets available for read & writing
111 
112  // Notification methods
113  virtual net_socket *start_notify(int port, void *data, int len);
114  virtual void end_notify();
115
116  // Find notifiers methods
117  virtual net_address *find_address(int port, char *name);   // name should be a 256 byte buffer
118  virtual void reset_find_list();
119  virtual ~tcpip_protocol() { cleanup(); }
120} ;
121
122extern tcpip_protocol tcpip;
123
124class unix_fd : public net_socket
125{
126  protected :
127  int fd;
128  public :
129  unix_fd(int fd) : fd(fd) { };
130  virtual int error()                             { return FD_ISSET(fd,&tcpip.exception_set); }
131  virtual int ready_to_read()                     { return FD_ISSET(fd,&tcpip.read_set); }
132  virtual int ready_to_write()                   
133  {
134    struct timeval tv={0,0};     // don't wait
135    fd_set write_check; 
136    FD_ZERO(&write_check); 
137    FD_SET(fd,&write_check);     
138    select(FD_SETSIZE,NULL,&write_check,NULL,&tv);
139    return FD_ISSET(fd,&write_check);
140  }
141  virtual int write(void *buf, int size, net_address *addr=NULL);
142  virtual int read(void *buf, int size, net_address **addr);
143
144  virtual ~unix_fd()                            { read_unselectable();  write_unselectable(); close(fd); }
145  virtual void read_selectable()                   { FD_SET(fd,&tcpip.master_set); }
146  virtual void read_unselectable()                 { FD_CLR(fd,&tcpip.master_set); }
147  virtual void write_selectable()                  { FD_SET(fd,&tcpip.master_write_set); }
148  virtual void write_unselectable()                { FD_CLR(fd,&tcpip.master_write_set); }
149  int get_fd() { return fd; }
150 
151  void broadcastable();
152} ;
153
154class tcp_socket : public unix_fd
155{
156  int listening;
157  public :
158  tcp_socket(int fd) : unix_fd(fd) { listening=0; };
159  virtual int listen(int port)
160  {
161    sockaddr_in host;
162    memset( (char*) &host,0, sizeof(host));
163    host.sin_family = AF_INET;
164    host.sin_port = htons(port);
165    host.sin_addr.s_addr = htonl(INADDR_ANY);
166    if (bind(fd, (struct sockaddr *) &host, sizeof(sockaddr_in))==-1)
167    {
168      fprintf(stderr,"net driver : could not bind socket to port %d\n",port);
169      return 0;
170    }
171    if (::listen(fd,5)==-1)
172    {
173      fprintf(stderr,"net driver : could not listen to socket on port %d\n",port);   
174      return 0;
175    }
176    listening=1;
177    return 1;
178  }
179  virtual net_socket *accept(net_address *&addr)
180  {
181    if (listening)
182    {
183      struct sockaddr_in from;
184      socklen_t addr_len=sizeof(from);
185      int new_fd=::accept(fd,(sockaddr *)&from,&addr_len);
186      if (new_fd>=0)
187      {
188        addr=new ip_address(&from);
189        return new tcp_socket(new_fd);
190      }
191      else
192      { addr=NULL; return 0; }
193    }
194    return 0;
195  }
196} ;
197
198class udp_socket : public unix_fd
199{
200  public :
201  udp_socket(int fd) : unix_fd(fd) { };
202  virtual int read(void *buf, int size, net_address **addr)
203  {
204    int tr;
205    if (addr)
206    {
207      *addr=new ip_address;
208      socklen_t addr_size=sizeof(sockaddr_in);
209      tr=recvfrom(fd,buf,size,0, (sockaddr *) &((ip_address *)(*addr))->addr,&addr_size);
210    } else
211      tr=recv(fd,buf,size,0);
212    return tr;
213  }
214  virtual int write(void *buf, int size, net_address *addr=NULL)
215  {
216    if (addr)
217      return sendto(fd,buf,size,0,(sockaddr *)(&((ip_address *)addr)->addr),sizeof(((ip_address *)addr)->addr));
218    else
219      return ::write(fd,(char*)buf,size);     
220  }
221  virtual int listen(int port)
222  {
223    sockaddr_in host;
224    memset( (char*) &host,0, sizeof(host));
225    host.sin_family = AF_INET;
226    host.sin_port = htons(port);
227    host.sin_addr.s_addr = htonl(INADDR_ANY);
228    if (bind(fd, (struct sockaddr *) &host, sizeof(sockaddr_in))==-1)
229    {
230      fprintf(stderr,"net driver : could not bind socket to port %d\n",port);
231      return 0;
232    }
233    return 1;
234  }
235
236} ;
237
Note: See TracBrowser for help on using the repository browser.