| 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 |
#include "compat/memset.h"
|
| 40 |
#include "compat/strdup.h"
|
| 41 |
#ifdef STDC_HEADERS
|
| 42 |
# include <stdlib.h>
|
| 43 |
#else
|
| 44 |
# ifdef HAVE_MALLOC_H
|
| 45 |
# include <malloc.h>
|
| 46 |
# endif
|
| 47 |
#endif
|
| 48 |
#include <errno.h>
|
| 49 |
#include "compat/strerror.h"
|
| 50 |
#ifdef HAVE_UNISTD_H
|
| 51 |
# include <unistd.h>
|
| 52 |
#endif
|
| 53 |
#ifdef HAVE_SYS_TYPES_H
|
| 54 |
# include <sys/types.h>
|
| 55 |
#endif
|
| 56 |
#ifdef HAVE_SYS_SOCKET_H
|
| 57 |
# include <sys/socket.h>
|
| 58 |
#endif
|
| 59 |
#include "compat/psock.h"
|
| 60 |
#ifdef HAVE_NETINET_IN_H
|
| 61 |
# include <netinet/in.h>
|
| 62 |
#endif
|
| 63 |
#include "compat/netinet_in.h"
|
| 64 |
#include <ctype.h>
|
| 65 |
|
| 66 |
#include "compat/psock.h"
|
| 67 |
#include "compat/strtoul.h"
|
| 68 |
#include "connection.h"
|
| 69 |
#include "bnetd.h"
|
| 70 |
#include "net.h"
|
| 71 |
#include "s2s.h"
|
| 72 |
#include "common/fdwatch.h"
|
| 73 |
#include "server.h"
|
| 74 |
#include "common/eventlog.h"
|
| 75 |
#include "common/xalloc.h"
|
| 76 |
#include "common/setup_after.h"
|
| 77 |
|
| 78 |
extern int s2s_check(void)
|
| 79 |
{
|
| 80 |
bnetd_check();
|
| 81 |
return 0;
|
| 82 |
}
|
| 83 |
|
| 84 |
extern int s2s_init(void)
|
| 85 |
{
|
| 86 |
bnetd_init();
|
| 87 |
return 0;
|
| 88 |
}
|
| 89 |
|
| 90 |
extern t_connection * s2s_create(char const * server, unsigned short def_port, t_conn_class class)
|
| 91 |
{
|
| 92 |
struct sockaddr_in addr, laddr;
|
| 93 |
psock_t_socklen laddr_len;
|
| 94 |
unsigned int ip;
|
| 95 |
unsigned short port;
|
| 96 |
int sock, connected;
|
| 97 |
t_connection * c;
|
| 98 |
char * p, * tserver;
|
| 99 |
|
| 100 |
ASSERT(server,NULL);
|
| 101 |
tserver=xstrdup(server);
|
| 102 |
p=strchr(tserver,':');
|
| 103 |
if (p) {
|
| 104 |
port=(unsigned short)strtoul(p+1,NULL,10);
|
| 105 |
*p='\0';
|
| 106 |
} else {
|
| 107 |
port=def_port;
|
| 108 |
}
|
| 109 |
|
| 110 |
if ((sock=net_socket(PSOCK_SOCK_STREAM))<0) {
|
| 111 |
eventlog(eventlog_level_error,__FUNCTION__,"error creating s2s socket");
|
| 112 |
xfree(tserver);
|
| 113 |
return NULL;
|
| 114 |
}
|
| 115 |
|
| 116 |
memset(&addr,0,sizeof(addr));
|
| 117 |
addr.sin_family = PSOCK_AF_INET;
|
| 118 |
addr.sin_port = htons(port);
|
| 119 |
addr.sin_addr.s_addr= net_inet_addr(tserver);
|
| 120 |
xfree(tserver);
|
| 121 |
|
| 122 |
eventlog(eventlog_level_info,__FUNCTION__,"try make s2s connection to %s",server);
|
| 123 |
if (psock_connect(sock,(struct sockaddr *)&addr,sizeof(addr))<0) {
|
| 124 |
if (psock_errno()!=PSOCK_EWOULDBLOCK && psock_errno() != PSOCK_EINPROGRESS) {
|
| 125 |
eventlog(eventlog_level_error,__FUNCTION__,"error connecting to %s (psock_connect: %s)",server,pstrerror(psock_errno()));
|
| 126 |
psock_close(sock);
|
| 127 |
return NULL;
|
| 128 |
}
|
| 129 |
connected=0;
|
| 130 |
eventlog(eventlog_level_info,__FUNCTION__,"connection to s2s server %s is in progress",server);
|
| 131 |
} else {
|
| 132 |
connected=1;
|
| 133 |
eventlog(eventlog_level_info,__FUNCTION__,"connected to s2s server %s",server);
|
| 134 |
}
|
| 135 |
laddr_len=sizeof(laddr);
|
| 136 |
memset(&laddr,0,sizeof(laddr));
|
| 137 |
ip=port=0;
|
| 138 |
if (psock_getsockname(sock,(struct sockaddr *)&laddr,&laddr_len)<0) {
|
| 139 |
eventlog(eventlog_level_error,__FUNCTION__,"unable to get local socket info");
|
| 140 |
} else {
|
| 141 |
if (laddr.sin_family != PSOCK_AF_INET) {
|
| 142 |
eventlog(eventlog_level_error,__FUNCTION__,"got bad socket family %d",laddr.sin_family);
|
| 143 |
} else {
|
| 144 |
ip=ntohl(laddr.sin_addr.s_addr);
|
| 145 |
port=ntohs(laddr.sin_port);
|
| 146 |
}
|
| 147 |
}
|
| 148 |
if (!(c=d2cs_conn_create(sock,ip,port, ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port)))) {
|
| 149 |
eventlog(eventlog_level_error,__FUNCTION__,"error create s2s connection");
|
| 150 |
psock_close(sock);
|
| 151 |
return NULL;
|
| 152 |
}
|
| 153 |
if (connected) {
|
| 154 |
if (conn_add_fd(c,fdwatch_type_read, d2cs_server_handle_tcp)<0) {
|
| 155 |
eventlog(eventlog_level_error, __FUNCTION__, "error adding socket %d to fdwatch pool (max sockets?)",sock);
|
| 156 |
d2cs_conn_set_state(c,conn_state_destroy);
|
| 157 |
return NULL;
|
| 158 |
}
|
| 159 |
d2cs_conn_set_state(c,conn_state_init);
|
| 160 |
} else {
|
| 161 |
if (conn_add_fd(c, fdwatch_type_write, d2cs_server_handle_tcp)<0) {
|
| 162 |
eventlog(eventlog_level_error, __FUNCTION__, "error adding socket %d to fdwatch pool (max sockets?)",sock);
|
| 163 |
d2cs_conn_set_state(c,conn_state_destroy);
|
| 164 |
return NULL;
|
| 165 |
}
|
| 166 |
d2cs_conn_set_state(c,conn_state_connecting);
|
| 167 |
}
|
| 168 |
d2cs_conn_set_class(c,class);
|
| 169 |
return c;
|
| 170 |
}
|
| 171 |
|
| 172 |
extern int s2s_destroy(t_connection * c)
|
| 173 |
{
|
| 174 |
ASSERT(c,-1);
|
| 175 |
switch (d2cs_conn_get_class(c)) {
|
| 176 |
case conn_class_bnetd:
|
| 177 |
bnetd_destroy(c);
|
| 178 |
break;
|
| 179 |
default:
|
| 180 |
eventlog(eventlog_level_error,__FUNCTION__,"got bad s2s connection class %d",d2cs_conn_get_class(c));
|
| 181 |
return -1;
|
| 182 |
}
|
| 183 |
return 0;
|
| 184 |
}
|