File: snetconnect.c

package info (click to toggle)
xscorch 0.2.1~pre2-2
  • links: PTS
  • area: main
  • in suites: squeeze
  • size: 4,848 kB
  • ctags: 3,514
  • sloc: ansic: 30,552; sh: 9,843; makefile: 472; perl: 16
file content (219 lines) | stat: -rw-r--r-- 6,644 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
/* $Header: /fridge/cvs/xscorch/snet/snetconnect.c,v 1.15 2009-04-26 17:39:56 jacob Exp $ */
/*
   
   xscorch - snetconnect.c    Copyright(c) 2000-2003 Justin David Smith
                              Copyright(c) 2001-2003 Jacob Luna Lundberg
   justins(at)chaos2.org      http://chaos2.org/
   jacob(at)chaos2.org        http://chaos2.org/~jacob

   Network connection open, close


   This program is free software; you can redistribute it and/or modify 
   it under the terms of the GNU General Public License as published by 
   the Free Software Foundation, version 2 of the License ONLY. 

   This program is distributed in the hope that it will be useful, 
   but WITHOUT ANY WARRANTY; without even the implied warranty of
   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU 
   General Public License for more details.

   You should have received a copy of the GNU General Public License along
   with this program; if not, write to the Free Software Foundation, Inc.,
   51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA

*/
#include <errno.h>
#include <fcntl.h>
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include <snetint.h>

#include <sgame/splayer.h>

#include <libj/jstr/libjstr.h>
#include <snet/tcpnet/tcpnet.h>

/* TEMP: This fixup is done in netdb.h ... but only if __USE_GNU or __USE_MISC. */
#define h_addr h_addr_list[0]



sc_server *sc_net_server_new(sc_config *c, int port) {

   addr socket_info;    /* Information about us */
   sc_server *srv;      /* Server object */
   int i;               /* Iterator */

   /* Construct a new server object and initialise it */   
   srv = (sc_server *)malloc(sizeof(sc_server));
   if(srv == NULL) {
      sc_net_set_error("server_new", "malloc failed");
      return(NULL);
   }
   srv->connections = 0;

   /* Open a new TCP socket */
   srv->linein = socket(PF_INET, SOCK_STREAM, DEFAULT_PROTOCOL);
   if(srv->linein == -1) {
      sc_net_set_error("server_new, socket", strerror(errno));
      free(srv);
      return(NULL);
   }

   /* Setup socket information about us; attempt to bind to a port */
   memset((char *)&socket_info, '\0', sizeof(socket_info));
   socket_info.sin_family = AF_INET;
   socket_info.sin_port = htons(port);
   socket_info.sin_addr.s_addr = INADDR_ANY;
   if(bind(srv->linein, (struct sockaddr *)&socket_info, sizeof(socket_info)) < 0) {
      sc_net_set_error("server_new, bind", strerror(errno));
      free(srv);
      return(NULL);
   }

   /* setup server socket not to block */
   sc_net_set_nonblocking(srv->linein);

   /* Game state initalisation */
   c->numplayers = 0;
   for(i = 0; i < SC_MAX_PLAYERS; ++i) {
      c->players[i]->aitype = SC_AI_RANDOM;
   }

   /* Prepare to listen for incoming packets */
   if(listen(srv->linein, 5) < 0) {
      sc_net_set_error("server_new, listen", strerror(errno));
      free(srv);
      return(NULL);
   }

   /* Display informative message */
   sc_net_set_info("server_new", "Network server established");

   /* Return the new socket object */
   return(srv);

}



void sc_net_server_free(sc_server **srv, const char *msg) {
/* sc_net_server_free
   Clean out a stale server struct. */

   int i;
   if(srv == NULL || *srv == NULL) return;
   for(i = 0; i < (*srv)->connections; ++i) {
      sc_net_send_message(&(*srv)->clients[i], SC_NET_SVR_QUIT, msg);
      tn_terminate(&(*srv)->clients[i].connection);
      sc_net_shutdown(&(*srv)->clients[i].socket);
   }
   sc_net_shutdown(&(*srv)->linein);
   free(*srv);
   *srv = NULL;
   return;

}



sc_client *sc_net_client_new(const char *name, const char *hostname, int port) {

   char versionstr[SC_NET_BUFFER_SIZE];  /* version info */
   sc_packet packet;    /* Transmitted data packet */
   host *host_info;     /* Remote host information */
   sc_client *cli;      /* Client object */

   /* Get my version data */
   sc_net_version_info(versionstr, sizeof(versionstr));

   /* Construct a new client object and initialise it */   
   cli = (sc_client *)malloc(sizeof(sc_client));
   if(cli == NULL) {
      sc_net_set_error("client_new", "malloc failed");
      return(NULL);
   }
   strcopyb(cli->name, name, sizeof(cli->name));

   /* Open a new TCP socket */
   cli->server.socket = socket(PF_INET, SOCK_STREAM, DEFAULT_PROTOCOL);
   if(cli->server.socket == -1) {
      sc_net_set_error("client_new, socket", strerror(errno));
      free(cli);
      return(NULL);
   }

   /* Get information about the remote host */
   host_info = gethostbyname(hostname);
   if(host_info == NULL) {
      sc_net_set_error("client_new, gethostbyname", "Cannot resolve hostname.");
      free(cli);
      return(NULL);
   }

   /* setup client destination address */
   memset((char *)&cli->server.address, '\0', sizeof(addr));
   memcpy((char *)&cli->server.address.sin_addr, host_info->h_addr, host_info->h_length);
   cli->server.address.sin_family = host_info->h_addrtype;
   cli->server.address.sin_port = htons(port);

   /* Below we initiate a connection request to the server */

   /* Initialise server-client data */
   cli->server.flags = SC_CONN_CLI_IFLAGS;
   cli->server.syncarg = 0;

   /* Attempt a connect request. We want this call to block */
   if(connect(cli->server.socket, (const struct sockaddr *)&cli->server.address, sizeof(addr)) < 0) {
      sc_net_set_error("client_new, connect", strerror(errno));
      free(cli);
      return(NULL);
   }

   /* setup client socket not to block from now on */
   sc_net_set_nonblocking(cli->server.socket);

   /* Register the new connection with the TCP NET packet engine. */
   if(!tn_instantiate(&cli->server.connection, cli->server.socket)) {
      sc_net_set_error("client_new, TCP NET", strerror(errno));
      free(cli);
      return(NULL);
   }

   /* Initiate a connection request to the server */
   sc_net_packet_init(&packet, SC_NET_CLI_CONNECT, strlenn(versionstr) + sizeof(dword));
   *(dword *)packet.data = htonl(SC_NET_VERSION);
   memcpy(packet.data + sizeof(dword), versionstr, strlenn(versionstr));

   /* Attempt to send the packet */
   if(!sc_net_send_packet_now(&cli->server, &packet)) {
      free(cli);
      return(NULL);
   }
   sc_net_packet_release(&packet);
   
   /* Initialise the status data */
   sc_net_status_init(cli);

   /* Return the client connection */
   return(cli);

}



void sc_net_client_free(sc_client **cli, const char *msg) {
/* sc_net_client_free
   Let them run free! */

   if(cli == NULL || *cli == NULL) return;
   sc_net_send_message(&(*cli)->server, SC_NET_CLI_DISCONNECT, msg);
   sc_net_shutdown(&(*cli)->server.socket);
   free(*cli);
   *cli = NULL;
   return;

}