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

Last change on this file since 49 was 49, checked in by Sam Hocevar, 12 years ago
  • Imported original public domain release, for future reference.
  • Property svn:keywords set to Id
File size: 4.5 KB
Line 
1#include "tcpip.hpp"
2#include <ctype.h>
3
4tcpip_protocol tcpip;
5
6/*
7FILE *log_file=NULL;
8extern int net_start();
9void net_log(char *st, void *buf, long size)
10{
11   
12  if (!log_file)
13  {
14    if (net_start())
15      log_file=fopen("client.log","wb");
16    else
17      log_file=fopen("server.log","wb");
18  }
19
20
21    fprintf(log_file,"%s%d - ",st,size);
22    int i;
23    for (i=0;i<size;i++)
24      if (isprint(*((unsigned char *)buf+i)))
25        fprintf(log_file,"%c",*((unsigned char *)buf+i));
26      else fprintf(log_file,"~");
27
28    fprintf(log_file," : ");
29
30    for (i=0;i<size;i++)
31    fprintf(log_file,"%02x, ",*((unsigned char *)buf+i),*((unsigned char *)buf+i));
32    fprintf(log_file,"\n");
33    fflush(log_file);
34
35} */
36
37
38int unix_fd::read(void *buf, int size, net_address **addr)
39{
40
41  SLOW();
42  int tr=::read(fd,buf,size);
43  if (addr) *addr=NULL;
44  return tr;
45}
46
47int unix_fd::write(void *buf, int size, net_address *addr)
48{
49  SLOW();
50  if (addr) fprintf(stderr,"Cannot change address for this socket type\n");
51  return ::write(fd,buf,size);
52}
53
54
55net_address *tcpip_protocol::get_local_address()
56{
57  char my_name[100];                              // now check to see if this address is 'hostname'
58  gethostname(my_name,100);
59  struct hostent *l_hn=gethostbyname(my_name); 
60  ip_address *a=new ip_address();
61  memset(&a->addr,0,sizeof(a->addr));
62  memcpy(&a->addr.sin_addr,*l_hn->h_addr_list,4); 
63}
64
65net_address *tcpip_protocol::get_node_address(char *&server_name, int def_port, int force_port)
66{
67  char name[256],*np;
68  np=name;
69  while (*server_name && *server_name!=':' && *server_name!='/')
70    *(np++)=*(server_name)++;
71  *np=0;
72  if (*server_name==':')
73  {
74    server_name++;
75    char port[256],*p;
76    p=port;
77    while (*server_name && *server_name!='/')
78      *(p++)=*(server_name++);
79    *p=0;
80    int x;
81    if (!force_port)
82    {
83      if (sscanf(port,"%d",&x)==1) def_port=x;
84      else return 0;
85    }
86  }
87
88  if (*server_name=='/') server_name++;
89
90  hostent *hp=gethostbyname(name);
91  if (!hp)
92  {
93    fprintf(stderr,"unable to locate server named '%s'\n",name);
94    return 0;
95  }
96 
97
98  sockaddr_in host;
99  memset( (char*) &host,0, sizeof(host));
100  host.sin_family = AF_INET;
101  host.sin_port = htons(def_port);
102  host.sin_addr.s_addr = htonl(INADDR_ANY);
103  memcpy(&host.sin_addr,hp->h_addr,hp->h_length);
104  return new ip_address(&host);
105}
106
107net_socket *tcpip_protocol::connect_to_server(net_address *addr, net_socket::socket_type sock_type)
108{
109  if (addr->protocol_type()!=net_address::IP)
110  {
111    fprintf(stderr,"Procotol type not supported in the executable\n");
112    return NULL;
113  }
114
115  int socket_fd=socket(AF_INET,sock_type==net_socket::SOCKET_SECURE ? SOCK_STREAM : SOCK_DGRAM,0);
116  if (socket_fd<0)
117  {
118    fprintf(stderr,"unable to create socket (too many open files?)\n");
119    return 0;
120  }
121
122  int zz;
123  if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&zz,sizeof(zz))<0)
124  {
125    fprintf(stderr,"could not set socket option reuseaddr");
126    return 0;
127  }
128
129   
130  if (connect(socket_fd, (struct sockaddr *) &((ip_address *)addr)->addr, sizeof( ((ip_address *)addr)->addr ))==-1)
131  {
132    fprintf(stderr,"unable to connect\n");
133    close(socket_fd);
134    return 0;
135  }
136
137  if (sock_type==net_socket::SOCKET_SECURE)
138    return new tcp_socket(socket_fd);
139  else
140    return new udp_socket(socket_fd);
141}
142
143
144net_socket *tcpip_protocol::create_listen_socket(int &port, net_socket::socket_type sock_type)
145{
146  int socket_fd=socket(AF_INET,sock_type==net_socket::SOCKET_SECURE ? SOCK_STREAM : SOCK_DGRAM,0);
147  if (socket_fd<0)
148  {
149    fprintf(stderr,"unable to create socket (too many open files?)\n");
150    return 0;
151  }
152/*  int zz;
153  if (setsockopt(socket_fd,SOL_SOCKET,SO_REUSEADDR,(char *)&zz,sizeof(zz))<0)
154  {
155    fprintf(stderr,"could not set socket option reuseaddr");
156    return 0;
157  } */
158
159
160  net_socket *s;
161  if (sock_type==net_socket::SOCKET_SECURE)
162    s=new tcp_socket(socket_fd);
163  else s=new udp_socket(socket_fd);
164  if (s->listen(port)==0)
165  {   
166    delete s;
167    return 0;
168  }
169
170  return s;
171}
172
173
174tcpip_protocol::tcpip_protocol()
175{
176  FD_ZERO(&master_set); 
177  FD_ZERO(&master_write_set); 
178  FD_ZERO(&read_set);
179  FD_ZERO(&exception_set);
180  FD_ZERO(&write_set);
181}
182
183
184int tcpip_protocol::select(int block)
185{
186  memcpy(&read_set,&master_set,sizeof(master_set));
187  memcpy(&exception_set,&master_set,sizeof(master_set));
188  memcpy(&write_set,&master_write_set,sizeof(master_set));
189  if (block)
190    return ::select(FD_SETSIZE,&read_set,&write_set,&exception_set,NULL);
191  else
192  {
193    timeval tv={0,0};
194    return ::select(FD_SETSIZE,&read_set,&write_set,&exception_set,&tv);
195  }
196}
197
198
Note: See TracBrowser for help on using the repository browser.