Debian Codegraph @

src/d2cs/s2s.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 "s2s.h"
21
22 #include <cstring>
23 #include <cstdlib>
24
25 #include "compat/psock.h"
26 #include "compat/strerror.h"
27 #include "common/eventlog.h"
28 #include "common/xalloc.h"
29 #include "bnetd.h"
30 #include "net.h"
31 #include "server.h"
32 #include "common/setup_after.h"
33
34 namespace pvpgn
35 {
36
37 namespace d2cs
38 {
39
40 extern int s2s_check(void)
41 {
42 bnetd_check();
43 return 0;
44 }
45
46 extern int s2s_init(void)
47 {
48 bnetd_init();
49 return 0;
50 }
51
52 extern t_connection * s2s_create(char const * server, unsigned short def_port, t_conn_class cclass)
53 {
54 struct sockaddr_in addr, laddr;
55 psock_t_socklen laddr_len;
56 unsigned int ip;
57 unsigned short port;
58 int sock, connected;
59 t_connection * c;
60 char * p, * tserver;
61
62 ASSERT(server,NULL);
63 tserver=xstrdup(server);
64 p=std::strchr(tserver,':');
65 if (p) {
66 port=(unsigned short)std::strtoul(p+1,NULL,10);
67 *p='\0';
68 } else {
69 port=def_port;
70 }
71
72 if ((sock=net_socket(PSOCK_SOCK_STREAM))<0) {
73 eventlog(eventlog_level_error,__FUNCTION__,"error creating s2s socket");
74 xfree(tserver);
75 return NULL;
76 }
77
78 std::memset(&addr,0,sizeof(addr));
79 addr.sin_family = PSOCK_AF_INET;
80 addr.sin_port = htons(port);
81 addr.sin_addr.s_addr= net_inet_addr(tserver);
82 xfree(tserver);
83
84 eventlog(eventlog_level_info,__FUNCTION__,"try make s2s connection to {}",server);
85 if (psock_connect(sock,(struct sockaddr *)&addr,sizeof(addr))<0) {
86 if (psock_errno()!=PSOCK_EWOULDBLOCK && psock_errno() != PSOCK_EINPROGRESS) {
87 eventlog(eventlog_level_error,__FUNCTION__,"error connecting to {} (psock_connect: {})",server,pstrerror(psock_errno()));
88 psock_close(sock);
89 return NULL;
90 }
91 connected=0;
92 eventlog(eventlog_level_info,__FUNCTION__,"connection to s2s server {} is in progress",server);
93 } else {
94 connected=1;
95 eventlog(eventlog_level_info,__FUNCTION__,"connected to s2s server {}",server);
96 }
97 laddr_len=sizeof(laddr);
98 std::memset(&laddr,0,sizeof(laddr));
99 ip=port=0;
100 if (psock_getsockname(sock,(struct sockaddr *)&laddr,&laddr_len)<0) {
101 eventlog(eventlog_level_error,__FUNCTION__,"unable to get local socket info");
102 } else {
103 if (laddr.sin_family != PSOCK_AF_INET) {
104 eventlog(eventlog_level_error,__FUNCTION__,"got bad socket family {}",laddr.sin_family);
105 } else {
106 ip=ntohl(laddr.sin_addr.s_addr);
107 port=ntohs(laddr.sin_port);
108 }
109 }
110 if (!(c=d2cs_conn_create(sock,ip,port, ntohl(addr.sin_addr.s_addr), ntohs(addr.sin_port)))) {
111 eventlog(eventlog_level_error,__FUNCTION__,"error create s2s connection");
112 psock_close(sock);
113 return NULL;
114 }
115 if (connected) {
116 if (conn_add_fd(c,fdwatch_type_read, d2cs_server_handle_tcp)<0) {
117 eventlog(eventlog_level_error, __FUNCTION__, "error adding socket {} to fdwatch pool (max sockets?)",sock);
118 d2cs_conn_set_state(c,conn_state_destroy);
119 return NULL;
120 }
121 d2cs_conn_set_state(c,conn_state_init);
122 } else {
123 if (conn_add_fd(c, fdwatch_type_write, d2cs_server_handle_tcp)<0) {
124 eventlog(eventlog_level_error, __FUNCTION__, "error adding socket {} to fdwatch pool (max sockets?)",sock);
125 d2cs_conn_set_state(c,conn_state_destroy);
126 return NULL;
127 }
128 d2cs_conn_set_state(c,conn_state_connecting);
129 }
130 d2cs_conn_set_class(c,cclass);
131 return c;
132 }
133
134 extern int s2s_destroy(t_connection * c)
135 {
136 ASSERT(c,-1);
137 switch (d2cs_conn_get_class(c)) {
138 case conn_class_bnetd:
139 bnetd_destroy(c);
140 break;
141 default:
142 eventlog(eventlog_level_error,__FUNCTION__,"got bad s2s connection class {}",d2cs_conn_get_class(c));
143 return -1;
144 }
145 return 0;
146 }
147
148 }
149
150 }