1 | #include "sock.hpp" |
---|
2 | |
---|
3 | #include <stdio.h> |
---|
4 | #include <stdlib.h> |
---|
5 | #include <fcntl.h> |
---|
6 | #include <unistd.h> |
---|
7 | #include <sys/ioctl.h> |
---|
8 | #include <sys/stat.h> |
---|
9 | #include <sys/types.h> |
---|
10 | #include <sys/time.h> |
---|
11 | #include <string.h> |
---|
12 | #include <signal.h> |
---|
13 | #include <sys/socket.h> |
---|
14 | #include <netinet/in.h> |
---|
15 | #include <sys/types.h> |
---|
16 | #include <sys/ipc.h> |
---|
17 | #include <sys/shm.h> |
---|
18 | #include <bstring.h> |
---|
19 | #include <netdb.h> |
---|
20 | |
---|
21 | extern fd_set master_set,master_write_set,read_set,exception_set,write_set; |
---|
22 | |
---|
23 | |
---|
24 | class ip_address : public net_address |
---|
25 | { |
---|
26 | |
---|
27 | public : |
---|
28 | sockaddr_in addr; |
---|
29 | |
---|
30 | virtual protocol protocol_type() { return net_address::IP; } |
---|
31 | virtual equal(net_address *who) |
---|
32 | { |
---|
33 | if (who->protocol_type()==IP && |
---|
34 | !memcmp(&addr.sin_addr,& ((ip_address *)who)->addr.sin_addr,sizeof(addr.sin_addr))) |
---|
35 | return 1; |
---|
36 | else return 0; |
---|
37 | } |
---|
38 | virtual int set_port(int port) { addr.sin_port=htons(port); } |
---|
39 | ip_address(sockaddr_in *Addr) { memcpy(&addr,Addr,sizeof(addr)); } |
---|
40 | virtual void print() |
---|
41 | { |
---|
42 | unsigned char *c=(unsigned char *) (&addr.sin_addr.s_addr); |
---|
43 | fprintf(stderr,"%d.%d.%d.%d",c[0],c[1],c[2],c[3]); |
---|
44 | } |
---|
45 | int get_port() { return htons(addr.sin_port); } |
---|
46 | net_address *copy() { return new ip_address(&addr); } |
---|
47 | ip_address() {} ; |
---|
48 | void store_string(char *st, int st_length) |
---|
49 | { |
---|
50 | char buf[100]; |
---|
51 | unsigned char *c=(unsigned char *) (&addr.sin_addr.s_addr); |
---|
52 | sprintf(buf,"%d.%d.%d.%d:%d",c[0],c[1],c[2],c[3],htons(addr.sin_port)); |
---|
53 | strncpy(st,buf,st_length); |
---|
54 | st[st_length-1]=0; |
---|
55 | } |
---|
56 | } ; |
---|
57 | |
---|
58 | class tcpip_protocol : public net_protocol |
---|
59 | { |
---|
60 | |
---|
61 | public : |
---|
62 | fd_set master_set,master_write_set,read_set,exception_set,write_set; |
---|
63 | |
---|
64 | tcpip_protocol(); |
---|
65 | net_address *get_local_address(); |
---|
66 | net_address *get_node_address(char *&server_name, int def_port, int force_port); |
---|
67 | net_socket *connect_to_server(net_address *addr, |
---|
68 | net_socket::socket_type sock_type=net_socket::SOCKET_SECURE); |
---|
69 | net_socket *create_listen_socket(int port, net_socket::socket_type sock_type, char *name); |
---|
70 | int installed() { return 1; } // always part of unix |
---|
71 | char *name() { return "UNIX generic TCPIP"; } |
---|
72 | void cleanup() { ; } |
---|
73 | int select(int block); // return # of sockets available for read & writing |
---|
74 | } ; |
---|
75 | |
---|
76 | extern tcpip_protocol tcpip; |
---|
77 | |
---|
78 | class unix_fd : public net_socket |
---|
79 | { |
---|
80 | protected : |
---|
81 | int fd; |
---|
82 | public : |
---|
83 | unix_fd(int fd) : fd(fd) { }; |
---|
84 | virtual int error() { return FD_ISSET(fd,&tcpip.exception_set); } |
---|
85 | virtual int ready_to_read() { return FD_ISSET(fd,&tcpip.read_set); } |
---|
86 | virtual int ready_to_write() |
---|
87 | { |
---|
88 | struct timeval tv={0,0}; // don't wait |
---|
89 | fd_set write_check; |
---|
90 | FD_ZERO(&write_check); |
---|
91 | FD_SET(fd,&write_check); |
---|
92 | select(FD_SETSIZE,NULL,&write_check,NULL,&tv); |
---|
93 | return FD_ISSET(fd,&write_check); |
---|
94 | } |
---|
95 | virtual int write(void *buf, int size, net_address *addr=NULL); |
---|
96 | virtual int read(void *buf, int size, net_address **addr); |
---|
97 | |
---|
98 | virtual ~unix_fd() { read_unselectable(); write_unselectable(); close(fd); } |
---|
99 | virtual void read_selectable() { FD_SET(fd,&tcpip.master_set); } |
---|
100 | virtual void read_unselectable() { FD_CLR(fd,&tcpip.master_set); } |
---|
101 | virtual void write_selectable() { FD_SET(fd,&tcpip.master_write_set); } |
---|
102 | virtual void write_unselectable() { FD_CLR(fd,&tcpip.master_write_set); } |
---|
103 | int get_fd() { return fd; } |
---|
104 | } ; |
---|
105 | |
---|
106 | class tcp_socket : public unix_fd |
---|
107 | { |
---|
108 | int listening; |
---|
109 | public : |
---|
110 | tcp_socket(int fd) : unix_fd(fd) { listening=0; }; |
---|
111 | virtual int listen(int port) |
---|
112 | { |
---|
113 | sockaddr_in host; |
---|
114 | memset( (char*) &host,0, sizeof(host)); |
---|
115 | host.sin_family = AF_INET; |
---|
116 | host.sin_port = htons(port); |
---|
117 | host.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
118 | if (bind(fd, (struct sockaddr *) &host, sizeof(sockaddr_in))==-1) |
---|
119 | { |
---|
120 | fprintf(stderr,"net driver : could not bind socket to port %d\n",port); |
---|
121 | return 0; |
---|
122 | } |
---|
123 | if (::listen(fd,5)==-1) |
---|
124 | { |
---|
125 | fprintf(stderr,"net driver : could not listen to socket on port %d\n",port); |
---|
126 | return 0; |
---|
127 | } |
---|
128 | listening=1; |
---|
129 | return 1; |
---|
130 | } |
---|
131 | virtual net_socket *accept(net_address *&addr) |
---|
132 | { |
---|
133 | if (listening) |
---|
134 | { |
---|
135 | struct sockaddr_in from; |
---|
136 | int addr_len=sizeof(from); |
---|
137 | int new_fd=::accept(fd,(sockaddr *)&from,&addr_len); |
---|
138 | if (new_fd>=0) |
---|
139 | { |
---|
140 | addr=new ip_address(&from); |
---|
141 | return new tcp_socket(new_fd); |
---|
142 | } |
---|
143 | else |
---|
144 | { addr=NULL; return 0; } |
---|
145 | } |
---|
146 | } |
---|
147 | } ; |
---|
148 | |
---|
149 | class udp_socket : public unix_fd |
---|
150 | { |
---|
151 | public : |
---|
152 | udp_socket(int fd) : unix_fd(fd) { }; |
---|
153 | virtual int read(void *buf, int size, net_address **addr) |
---|
154 | { |
---|
155 | int tr; |
---|
156 | if (addr) |
---|
157 | { |
---|
158 | *addr=new ip_address; |
---|
159 | int addr_size=sizeof(ip_address::addr); |
---|
160 | tr=recvfrom(fd,buf,size,0, (sockaddr *) &((ip_address *)(*addr))->addr,&addr_size); |
---|
161 | } else |
---|
162 | tr=recv(fd,buf,size,0); |
---|
163 | return tr; |
---|
164 | } |
---|
165 | virtual int write(void *buf, int size, net_address *addr=NULL) |
---|
166 | { |
---|
167 | if (addr) |
---|
168 | return sendto(fd,buf,size,0,(sockaddr *)(&((ip_address *)addr)->addr),sizeof(((ip_address *)addr)->addr)); |
---|
169 | else |
---|
170 | return ::write(fd,buf,size); |
---|
171 | } |
---|
172 | virtual int listen(int port) |
---|
173 | { |
---|
174 | sockaddr_in host; |
---|
175 | memset( (char*) &host,0, sizeof(host)); |
---|
176 | host.sin_family = AF_INET; |
---|
177 | host.sin_port = htons(port); |
---|
178 | host.sin_addr.s_addr = htonl(INADDR_ANY); |
---|
179 | if (bind(fd, (struct sockaddr *) &host, sizeof(sockaddr_in))==-1) |
---|
180 | { |
---|
181 | fprintf(stderr,"net driver : could not bind socket to port %d\n",port); |
---|
182 | return 0; |
---|
183 | } |
---|
184 | return 1; |
---|
185 | } |
---|
186 | |
---|
187 | } ; |
---|
188 | |
---|