| 1 |
/*
|
| 2 |
* Copyright (C) 2000,2001 Onlyer (onlyer@263.net)
|
| 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 |
|
| 21 |
#ifdef HAVE_STDDEF_H
|
| 22 |
# include <stddef.h>
|
| 23 |
#else
|
| 24 |
# ifndef NULL
|
| 25 |
# define NULL ((void *)0)
|
| 26 |
# endif
|
| 27 |
#endif
|
| 28 |
#include <ctype.h>
|
| 29 |
#ifdef HAVE_STRING_H
|
| 30 |
# include <string.h>
|
| 31 |
#else
|
| 32 |
# ifdef HAVE_STRINGS_H
|
| 33 |
# include <strings.h>
|
| 34 |
# endif
|
| 35 |
# ifdef HAVE_MEMORY_H
|
| 36 |
# include <memory.h>
|
| 37 |
# endif
|
| 38 |
#endif
|
| 39 |
#ifdef STDC_HEADERS
|
| 40 |
# include <stdlib.h>
|
| 41 |
#else
|
| 42 |
# ifdef HAVE_MALLOC_H
|
| 43 |
# include <malloc.h>
|
| 44 |
# endif
|
| 45 |
#endif
|
| 46 |
#include "compat/memset.h"
|
| 47 |
#include <errno.h>
|
| 48 |
#include "compat/strerror.h"
|
| 49 |
#ifdef HAVE_UNISTD_H
|
| 50 |
# include <unistd.h>
|
| 51 |
#endif
|
| 52 |
#ifdef HAVE_FCNTL_H
|
| 53 |
# include <fcntl.h>
|
| 54 |
#else
|
| 55 |
# ifdef HAVE_SYS_FILE_H
|
| 56 |
# include <sys/file.h>
|
| 57 |
# endif
|
| 58 |
#endif
|
| 59 |
#ifdef HAVE_SYS_TYPES_H
|
| 60 |
# include <sys/types.h>
|
| 61 |
#endif
|
| 62 |
#ifdef HAVE_SYS_SOCKET_H
|
| 63 |
# include <sys/socket.h>
|
| 64 |
#endif
|
| 65 |
#include "compat/socket.h"
|
| 66 |
#ifdef HAVE_SYS_PARAM_H
|
| 67 |
# include <sys/param.h>
|
| 68 |
#endif
|
| 69 |
#ifdef HAVE_NETINET_IN_H
|
| 70 |
# include <netinet/in.h>
|
| 71 |
#endif
|
| 72 |
#include "compat/netinet_in.h"
|
| 73 |
#ifdef HAVE_ARPA_INET_H
|
| 74 |
# include <arpa/inet.h>
|
| 75 |
#endif
|
| 76 |
#ifdef HAVE_NETDB_H
|
| 77 |
# include <netdb.h>
|
| 78 |
#endif
|
| 79 |
#include "compat/psock.h"
|
| 80 |
|
| 81 |
#include "net.h"
|
| 82 |
#include "common/eventlog.h"
|
| 83 |
#include "common/setup_after.h"
|
| 84 |
|
| 85 |
|
| 86 |
/* FIXME: use addr.h code to do this and make a separate error
|
| 87 |
* return path so that 255.255.255.255 isn't an error.
|
| 88 |
*/
|
| 89 |
extern unsigned long int net_inet_addr(char const * host)
|
| 90 |
{
|
| 91 |
struct hostent * hp;
|
| 92 |
|
| 93 |
if (isdigit((int)host[0])) {
|
| 94 |
return inet_addr(host);
|
| 95 |
} else {
|
| 96 |
hp=gethostbyname(host);
|
| 97 |
if (!hp || !(hp->h_addr_list)) {
|
| 98 |
return ~0UL;
|
| 99 |
}
|
| 100 |
return *(unsigned long *)hp->h_addr_list[0];
|
| 101 |
}
|
| 102 |
}
|
| 103 |
|
| 104 |
extern int net_socket(int type)
|
| 105 |
{
|
| 106 |
int sock;
|
| 107 |
int val;
|
| 108 |
int ipproto;
|
| 109 |
|
| 110 |
if (type==PSOCK_SOCK_STREAM) {
|
| 111 |
ipproto = PSOCK_IPPROTO_TCP;
|
| 112 |
} else {
|
| 113 |
ipproto = PSOCK_IPPROTO_UDP;
|
| 114 |
}
|
| 115 |
if ((sock=psock_socket(PSOCK_PF_INET, type, ipproto))<0) {
|
| 116 |
eventlog(eventlog_level_error,__FUNCTION__,"error creating socket (psock_socket: %s)", pstrerror(psock_errno()));
|
| 117 |
return -1;
|
| 118 |
}
|
| 119 |
val=1;
|
| 120 |
if (psock_setsockopt(sock,PSOCK_SOL_SOCKET, PSOCK_SO_KEEPALIVE, &val, sizeof(val))<0) {
|
| 121 |
eventlog(eventlog_level_error,__FUNCTION__,"error set socket option KEEPALIVE (psock_setsockopt: %s)",pstrerror(psock_errno()));
|
| 122 |
}
|
| 123 |
if (psock_ctl(sock,PSOCK_NONBLOCK)<0) {
|
| 124 |
eventlog(eventlog_level_error,__FUNCTION__,"error set socket mode to non-block (psock_ctl: %s)",pstrerror(psock_errno()));
|
| 125 |
psock_close(sock);
|
| 126 |
return -1;
|
| 127 |
}
|
| 128 |
return sock;
|
| 129 |
}
|
| 130 |
|
| 131 |
extern int net_check_connected(int sock)
|
| 132 |
{
|
| 133 |
int err;
|
| 134 |
psock_t_socklen errlen;
|
| 135 |
|
| 136 |
err = 0;
|
| 137 |
errlen = sizeof(err);
|
| 138 |
if (psock_getsockopt(sock,PSOCK_SOL_SOCKET, PSOCK_SO_ERROR, &err, &errlen)<0) {
|
| 139 |
eventlog(eventlog_level_error,__FUNCTION__,"error get socket option SO_ERROR (psock_getsockopt: %s)",pstrerror(psock_errno()));
|
| 140 |
return -1;
|
| 141 |
}
|
| 142 |
if (errlen && err)
|
| 143 |
return -1;
|
| 144 |
return 0;
|
| 145 |
}
|
| 146 |
|
| 147 |
|
| 148 |
extern int net_listen(unsigned int ip, unsigned int port, int type)
|
| 149 |
{
|
| 150 |
int sock;
|
| 151 |
int val;
|
| 152 |
struct sockaddr_in addr;
|
| 153 |
int ipproto;
|
| 154 |
|
| 155 |
if (type==PSOCK_SOCK_STREAM) {
|
| 156 |
ipproto = PSOCK_IPPROTO_TCP;
|
| 157 |
} else {
|
| 158 |
ipproto = PSOCK_IPPROTO_UDP;
|
| 159 |
}
|
| 160 |
if ((sock=psock_socket(PSOCK_PF_INET, type, ipproto))<0) {
|
| 161 |
eventlog(eventlog_level_error,__FUNCTION__,"error create listen socket");
|
| 162 |
return -1;
|
| 163 |
}
|
| 164 |
val=1;
|
| 165 |
if (psock_setsockopt(sock,PSOCK_SOL_SOCKET,PSOCK_SO_REUSEADDR,&val,sizeof(int))<0) {
|
| 166 |
eventlog(eventlog_level_error,__FUNCTION__,"error set socket option SO_REUSEADDR");
|
| 167 |
}
|
| 168 |
memset(&addr,0,sizeof(addr));
|
| 169 |
addr.sin_family=PSOCK_AF_INET;
|
| 170 |
addr.sin_port=htons(port);
|
| 171 |
addr.sin_addr.s_addr=htonl(ip);
|
| 172 |
if (psock_bind(sock,(struct sockaddr *)&addr, sizeof(addr))<0) {
|
| 173 |
eventlog(eventlog_level_error,__FUNCTION__,"error bind listen socket");
|
| 174 |
psock_close(sock);
|
| 175 |
return -1;
|
| 176 |
}
|
| 177 |
if (psock_listen(sock,LISTEN_QUEUE)<0) {
|
| 178 |
eventlog(eventlog_level_error,__FUNCTION__,"error listen socket");
|
| 179 |
psock_close(sock);
|
| 180 |
return -1;
|
| 181 |
}
|
| 182 |
return sock;
|
| 183 |
}
|
| 184 |
|
| 185 |
extern int net_send_data(int sock, char * buff, int buffsize, int * pos, int * currsize)
|
| 186 |
{
|
| 187 |
int nsend;
|
| 188 |
|
| 189 |
ASSERT(buff,-1);
|
| 190 |
ASSERT(pos,-1);
|
| 191 |
if (*pos>buffsize) {
|
| 192 |
eventlog(eventlog_level_error,__FUNCTION__,"[%d] send buffer overflow pos=%d buffsize=%d",sock,*pos,buffsize);
|
| 193 |
return -1;
|
| 194 |
}
|
| 195 |
if (*currsize>*pos) {
|
| 196 |
eventlog(eventlog_level_error,__FUNCTION__,"[%d] send buffer error currsize=%d pos=%d",sock,*currsize,*pos);
|
| 197 |
return -1;
|
| 198 |
}
|
| 199 |
nsend=psock_send(sock,buff+*pos-*currsize,*currsize,0);
|
| 200 |
if (nsend==0) {
|
| 201 |
eventlog(eventlog_level_error,__FUNCTION__,"[%d] no data sent (close connection)",sock);
|
| 202 |
return -1;
|
| 203 |
}
|
| 204 |
if (nsend<0) {
|
| 205 |
if (
|
| 206 |
#ifdef PSOCK_EINTR
|
| 207 |
psock_errno()==PSOCK_EINTR ||
|
| 208 |
#endif
|
| 209 |
#ifdef PSOCK_EAGAIN
|
| 210 |
psock_errno()==PSOCK_EAGAIN ||
|
| 211 |
#endif
|
| 212 |
#ifdef PSOCK_EWOULDBLOCK
|
| 213 |
psock_errno()==PSOCK_EWOULDBLOCK ||
|
| 214 |
#endif
|
| 215 |
#ifdef PSOCK_NOBUFS
|
| 216 |
psock_errno()==PSOCK_ENOBUFS ||
|
| 217 |
#endif
|
| 218 |
#ifdef PSOCK_ENOMEM
|
| 219 |
psock_errno()==ENOMEM ||
|
| 220 |
#endif
|
| 221 |
0)
|
| 222 |
return 0;
|
| 223 |
|
| 224 |
eventlog(eventlog_level_error,__FUNCTION__,"[%d] error sent data (closing connection) (psock_send: %s)",sock,pstrerror(psock_errno()));
|
| 225 |
return -1;
|
| 226 |
}
|
| 227 |
*currsize -= nsend;
|
| 228 |
return 1;
|
| 229 |
}
|
| 230 |
|
| 231 |
extern int net_recv_data(int sock, char * buff, int buffsize, int * pos, int * currsize)
|
| 232 |
{
|
| 233 |
int nrecv;
|
| 234 |
|
| 235 |
ASSERT(buff,-1);
|
| 236 |
ASSERT(pos,-1);
|
| 237 |
if (*pos>buffsize) {
|
| 238 |
eventlog(eventlog_level_error,__FUNCTION__,"[%d] recv buffer overflow pos=%d buffsize=%d",sock,*pos,buffsize);
|
| 239 |
return -1;
|
| 240 |
}
|
| 241 |
if (*currsize>*pos) {
|
| 242 |
eventlog(eventlog_level_error,__FUNCTION__,"[%d] recv buffer error currsize=%d pos=%d",sock,*currsize,*pos);
|
| 243 |
return -1;
|
| 244 |
}
|
| 245 |
if (*pos==buffsize) {
|
| 246 |
memmove(buff,buff+*pos,*currsize);
|
| 247 |
*pos=0;
|
| 248 |
}
|
| 249 |
nrecv=psock_recv(sock,buff+*pos,buffsize-*pos,0);
|
| 250 |
if (nrecv==0) {
|
| 251 |
eventlog(eventlog_level_info,__FUNCTION__,"[%d] remote host closed connection",sock);
|
| 252 |
return -1;
|
| 253 |
}
|
| 254 |
if (nrecv<0) {
|
| 255 |
if (
|
| 256 |
#ifdef PSOCK_EINTR
|
| 257 |
psock_errno()==PSOCK_EINTR ||
|
| 258 |
#endif
|
| 259 |
#ifdef PSOCK_EAGAIN
|
| 260 |
psock_errno()==PSOCK_EAGAIN ||
|
| 261 |
#endif
|
| 262 |
#ifdef PSOCK_EWOULDBLOCK
|
| 263 |
psock_errno()==PSOCK_EWOULDBLOCK ||
|
| 264 |
#endif
|
| 265 |
#ifdef PSOCK_ENOMEM
|
| 266 |
psock_errno()==ENOMEM ||
|
| 267 |
#endif
|
| 268 |
0)
|
| 269 |
return 0;
|
| 270 |
|
| 271 |
if (
|
| 272 |
#ifdef PSOCK_ENOTCONN
|
| 273 |
psock_errno()==PSOCK_ENOTCONN ||
|
| 274 |
#endif
|
| 275 |
#ifdef PSOCK_ECONNRESET
|
| 276 |
psock_errno()==PSOCK_ECONNRESET ||
|
| 277 |
#endif
|
| 278 |
0) {
|
| 279 |
eventlog(eventlog_level_error,__FUNCTION__,"[%d] remote host closed connection (psock_recv: %s)",sock,pstrerror(psock_errno()));
|
| 280 |
return -1;
|
| 281 |
}
|
| 282 |
eventlog(eventlog_level_error,__FUNCTION__,"[%d] recv error (closing connection) (psock_recv: %s)",sock,pstrerror(psock_errno()));
|
| 283 |
return -1;
|
| 284 |
}
|
| 285 |
* currsize += nrecv;
|
| 286 |
return 1;
|
| 287 |
}
|