Debian Codegraph @

src/d2cs/net.cpp

raw symbols SCIP
1 /*
2 * Copyright (C) 2000,2001 Onlyer ([email protected])
3 *
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version 2
7 * of the License, or (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 */
18 #include "common/setup_before.h"
19 #include "setup.h"
20 #include "net.h"
21
22 #include <cctype>
23 #include <cstring>
24
25 #include "compat/strerror.h"
26 #include "common/eventlog.h"
27 #include "compat/psock.h"
28 #include "common/setup_after.h"
29
30 namespace pvpgn
31 {
32
33 namespace d2cs
34 {
35
36
37 /* FIXME: use addr.h code to do this and make a separate error
38 * return path so that 255.255.255.255 isn't an error.
39 */
40 extern unsigned long int net_inet_addr(char const * host)
41 {
42 struct hostent * hp;
43
44 if (std::isdigit((int)host[0])) {
45 return inet_addr(host);
46 } else {
47 hp=gethostbyname(host);
48 if (!hp || !(hp->h_addr_list)) {
49 return ~0UL;
50 }
51 return *(unsigned long *)hp->h_addr_list[0];
52 }
53 }
54
55 extern int net_socket(int type)
56 {
57 int sock;
58 int val;
59 int ipproto;
60
61 if (type==PSOCK_SOCK_STREAM) {
62 ipproto = PSOCK_IPPROTO_TCP;
63 } else {
64 ipproto = PSOCK_IPPROTO_UDP;
65 }
66 if ((sock=psock_socket(PSOCK_PF_INET, type, ipproto))<0) {
67 eventlog(eventlog_level_error,__FUNCTION__,"error creating socket (psock_socket: {})", pstrerror(psock_errno()));
68 return -1;
69 }
70 val=1;
71 if (psock_setsockopt(sock,PSOCK_SOL_SOCKET, PSOCK_SO_KEEPALIVE, &val, sizeof(val))<0) {
72 eventlog(eventlog_level_error,__FUNCTION__,"error set socket option KEEPALIVE (psock_setsockopt: {})",pstrerror(psock_errno()));
73 }
74 if (psock_ctl(sock,PSOCK_NONBLOCK)<0) {
75 eventlog(eventlog_level_error,__FUNCTION__,"error set socket mode to non-block (psock_ctl: {})",pstrerror(psock_errno()));
76 psock_close(sock);
77 return -1;
78 }
79 return sock;
80 }
81
82 extern int net_check_connected(int sock)
83 {
84 int err;
85 psock_t_socklen errlen;
86
87 err = 0;
88 errlen = sizeof(err);
89 if (psock_getsockopt(sock,PSOCK_SOL_SOCKET, PSOCK_SO_ERROR, &err, &errlen)<0) {
90 eventlog(eventlog_level_error,__FUNCTION__,"error get socket option SO_ERROR (psock_getsockopt: {})",pstrerror(psock_errno()));
91 return -1;
92 }
93 if (errlen && err)
94 return -1;
95 return 0;
96 }
97
98
99 extern int net_listen(unsigned int ip, unsigned int port, int type)
100 {
101 int sock;
102 int val;
103 struct sockaddr_in addr;
104 int ipproto;
105
106 if (type==PSOCK_SOCK_STREAM) {
107 ipproto = PSOCK_IPPROTO_TCP;
108 } else {
109 ipproto = PSOCK_IPPROTO_UDP;
110 }
111 if ((sock=psock_socket(PSOCK_PF_INET, type, ipproto))<0) {
112 eventlog(eventlog_level_error,__FUNCTION__,"error create listen socket");
113 return -1;
114 }
115 val=1;
116 if (psock_setsockopt(sock,PSOCK_SOL_SOCKET,PSOCK_SO_REUSEADDR,&val,sizeof(int))<0) {
117 eventlog(eventlog_level_error,__FUNCTION__,"error set socket option SO_REUSEADDR");
118 }
119 std::memset(&addr,0,sizeof(addr));
120 addr.sin_family=PSOCK_AF_INET;
121 addr.sin_port=htons(port);
122 addr.sin_addr.s_addr=htonl(ip);
123 if (psock_bind(sock,(struct sockaddr *)&addr, sizeof(addr))<0) {
124 eventlog(eventlog_level_error,__FUNCTION__,"error bind listen socket");
125 psock_close(sock);
126 return -1;
127 }
128 if (psock_listen(sock,LISTEN_QUEUE)<0) {
129 eventlog(eventlog_level_error,__FUNCTION__,"error listen socket");
130 psock_close(sock);
131 return -1;
132 }
133 return sock;
134 }
135
136 extern int net_send_data(int sock, char * buff, int buffsize, int * pos, int * currsize)
137 {
138 int nsend;
139
140 ASSERT(buff,-1);
141 ASSERT(pos,-1);
142 if (*pos>buffsize) {
143 eventlog(eventlog_level_error,__FUNCTION__,"[{}] send buffer overflow pos={} buffsize={}",sock,*pos,buffsize);
144 return -1;
145 }
146 if (*currsize>*pos) {
147 eventlog(eventlog_level_error,__FUNCTION__,"[{}] send buffer error currsize={} pos={}",sock,*currsize,*pos);
148 return -1;
149 }
150 nsend=psock_send(sock,buff+*pos-*currsize,*currsize,0);
151 if (nsend==0) {
152 eventlog(eventlog_level_error,__FUNCTION__,"[{}] no data sent (close connection)",sock);
153 return -1;
154 }
155 if (nsend<0) {
156 if (
157 #ifdef PSOCK_EINTR
158 psock_errno()==PSOCK_EINTR ||
159 #endif
160 #ifdef PSOCK_EAGAIN
161 psock_errno()==PSOCK_EAGAIN ||
162 #endif
163 #ifdef PSOCK_EWOULDBLOCK
164 psock_errno()==PSOCK_EWOULDBLOCK ||
165 #endif
166 #ifdef PSOCK_NOBUFS
167 psock_errno()==PSOCK_ENOBUFS ||
168 #endif
169 #ifdef PSOCK_ENOMEM
170 psock_errno()==ENOMEM ||
171 #endif
172 0)
173 return 0;
174
175 eventlog(eventlog_level_error,__FUNCTION__,"[{}] error sent data (closing connection) (psock_send: {})",sock,pstrerror(psock_errno()));
176 return -1;
177 }
178 *currsize -= nsend;
179 return 1;
180 }
181
182 extern int net_recv_data(int sock, char * buff, int buffsize, int * pos, int * currsize)
183 {
184 int nrecv;
185
186 ASSERT(buff,-1);
187 ASSERT(pos,-1);
188 if (*pos>buffsize) {
189 eventlog(eventlog_level_error,__FUNCTION__,"[{}] recv buffer overflow pos={} buffsize={}",sock,*pos,buffsize);
190 return -1;
191 }
192 if (*currsize>*pos) {
193 eventlog(eventlog_level_error,__FUNCTION__,"[{}] recv buffer error currsize={} pos={}",sock,*currsize,*pos);
194 return -1;
195 }
196 if (*pos==buffsize) {
197 std::memmove(buff,buff+*pos,*currsize);
198 *pos=0;
199 }
200 nrecv=psock_recv(sock,buff+*pos,buffsize-*pos,0);
201 if (nrecv==0) {
202 eventlog(eventlog_level_info,__FUNCTION__,"[{}] remote host closed connection",sock);
203 return -1;
204 }
205 if (nrecv<0) {
206 if (
207 #ifdef PSOCK_EINTR
208 psock_errno()==PSOCK_EINTR ||
209 #endif
210 #ifdef PSOCK_EAGAIN
211 psock_errno()==PSOCK_EAGAIN ||
212 #endif
213 #ifdef PSOCK_EWOULDBLOCK
214 psock_errno()==PSOCK_EWOULDBLOCK ||
215 #endif
216 #ifdef PSOCK_ENOMEM
217 psock_errno()==ENOMEM ||
218 #endif
219 0)
220 return 0;
221
222 if (
223 #ifdef PSOCK_ENOTCONN
224 psock_errno()==PSOCK_ENOTCONN ||
225 #endif
226 #ifdef PSOCK_ECONNRESET
227 psock_errno()==PSOCK_ECONNRESET ||
228 #endif
229 0) {
230 eventlog(eventlog_level_error,__FUNCTION__,"[{}] remote host closed connection (psock_recv: {})",sock,pstrerror(psock_errno()));
231 return -1;
232 }
233 eventlog(eventlog_level_error,__FUNCTION__,"[{}] recv error (closing connection) (psock_recv: {})",sock,pstrerror(psock_errno()));
234 return -1;
235 }
236 * currsize += nrecv;
237 return 1;
238 }
239
240 }
241
242 }