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 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354
|
/**********************************************************************
* vanessa_socket_server.c September 1999
* Horms horms@verge.net.au
*
* Open accpet a tcp connection from a client (we are the server)
*
* vanessa_socket
* Library to simplify handling of TCP sockets
* Copyright (C) 1999-2004 Horms
*
* This library is free software; you can redistribute it and/or
* modify it under the terms of the GNU Lesser General Public License
* as published by the Free Software Foundation; either version 2 of
* the License, or (at your option) any later version.
*
* This library 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
* Lesser General Public License for more details.
*
* You should have received a copy of the GNU Lesser General Public
* License along with this library; if not, write to the Free Software
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
* 02111-1307 USA
*
**********************************************************************/
#include "vanessa_socket.h"
/*Keep track of the total number of connections in the parent process*/
unsigned int noconnection;
/**********************************************************************
* vanessa_socket_server_bind
* Open a socket and bind it to a port and address
* pre: port: port to listen to, an ASCII representation of a number
* or an entry from /etc/services
* interface_address: If NULL bind to 0.0.0.0, else
* bind to interface(es) with this address.
* flag: If VANESSA_SOCKET_NO_LOOKUP then no host and port lookups
* will be performed
* post: Bound socket is returned
* return: socket
* -1 on error
**********************************************************************/
int vanessa_socket_server_bind(const char *port,
const char *interface_address,
vanessa_socket_flag_t flag)
{
int s;
struct sockaddr_in from;
/* Fill in informtaion for 'from' */
if (vanessa_socket_host_port_sockaddr_in(interface_address,
port, &from, flag) < 0) {
VANESSA_LOGGER_DEBUG("vanessa_socket_host_port_sockaddr_in");
return (-1);
}
/* Make the connection */
s = vanessa_socket_server_bind_sockaddr_in(from, flag);
if (s < 0) {
VANESSA_LOGGER_DEBUG("vanessa_socket_server_bind");
return (-1);
}
return (s);
}
/**********************************************************************
* vanessa_socket_server_bind_sockaddr_in
* Open a socket and bind it to a port and address
* pre: from: sockaddr_in to bind to
* flag: ignored
* post: Bound socket
* return: socket
* -1 on error
**********************************************************************/
int vanessa_socket_server_bind_sockaddr_in(struct sockaddr_in from,
vanessa_socket_flag_t flag)
{
int s;
int g;
int addrlen;
if ((s = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
VANESSA_LOGGER_DEBUG_ERRNO("socket");
return (-1);
}
/*
* Set SO_REUSEADDR on the server socket s. Variable g is used
* as a scratch varable.
*/
g = 1;
if (setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (void *) &g, sizeof g)
< 0) {
VANESSA_LOGGER_DEBUG_ERRNO("setsockopt");
close(s);
return (-1);
}
#ifdef SO_BINDANY
g = 1;
if (setsockopt(s, SOL_SOCKET, SO_BINDANY, (void *) &g, sizeof g) <
0) {
VANESSA_LOGGER_DEBUG_ERRNO("setsockopt");
close(s);
return (-1);
}
#endif
addrlen = sizeof(struct sockaddr_in);
if (bind(s, (struct sockaddr *) &from, addrlen) < 0) {
VANESSA_LOGGER_DEBUG_ERRNO("bind");
close(s);
return (-1);
}
#ifdef SOMAXCONN
listen(s, SOMAXCONN);
#else
listen(s, 5);
#endif
return(s);
}
/**********************************************************************
* vanessa_socket_server_accept
* Accept connections on a bound socket.
* vanessa_socket_server_bind or vanessa_socket_server_bind_sockaddr_in
* may be used to open the bound socket.
* When one is received fork
* In the Child: close the listening file descriptor
* return the file descriptor that is
* a socket connection to the client
* In the Server: close the socket to the client and loop
* pre: port: port to listen to, an ASCII representation of a number
* or an entry from /etc/services
* interface_address: If NULL bind to all interfaces, else
* bind to interface(es) with this address.
* maximum_connections: maximum number of active connections
* to handle. If 0 then an number of connections
* is unlimited.
* Not used if flat is VANESSA_SOCKET_NO_FORK
* return_from: pointer to a struct_in addr where the
* connecting client's IP address will
* be placed. Ignored if NULL
* return_to: pointer to a in addr where the IP address the
* server accepted the connection on will be placed.
* Ignored if NULL
* flag: If VANESSA_SOCKET_NO_FORK then the process does not for
* when a connection is recieved.
* post: Client sockets are returned in child processes
* In the parent process the function doesn't exit, other
* than on error.
* if return_from is non-null, it is seeded with cleints address
* return: client socket, if connection is accepted.
* -1 on error
**********************************************************************/
int vanessa_socket_server_accept(int listen_socket,
const unsigned int maximum_connections,
struct sockaddr_in *return_from,
struct sockaddr_in *return_to,
vanessa_socket_flag_t flag)
{
int g;
int addrlen;
struct sockaddr_in from;
extern unsigned int noconnection;
for(;;) {
addrlen = sizeof(from);
g = accept(listen_socket, (struct sockaddr *) &from, &addrlen);
if (g < 0) {
if(errno == EINTR || errno == ECONNABORTED) {
continue; /* Ignore EINTR and ECONNABORTED */
}
VANESSA_LOGGER_DEBUG_ERRNO("accept");
return(-1);
}
if ( !(flag&VANESSA_SOCKET_NO_FORK) && maximum_connections &&
noconnection >= maximum_connections) {
close(g);
g=-1;
if(flag&VANESSA_SOCKET_NO_FORK) {
break;
}
}
if (!(flag&VANESSA_SOCKET_NO_FORK)) {
if(fork() == 0) {
if(close(listen_socket) < 0) {
VANESSA_LOGGER_DEBUG_ERRNO("close");
return(-1);
}
}
else {
noconnection++;
close(g);
continue;
}
}
break;
}
if (return_to) {
addrlen = sizeof(struct sockaddr_in);
if (getsockname (g, (struct sockaddr *) return_to,
&addrlen) < 0) {
VANESSA_LOGGER_DEBUG_ERRNO ("warning: getsockname");
/* This is usually (always) a transient error so
* close the connection and soldier on */
vanessa_socket_daemon_exit_cleanly(0);
}
}
if (return_from) {
memcpy(return_from, &from, sizeof(from));
}
return(g);
}
/**********************************************************************
* vanessa_socket_server_connect
* Listen on a tcp port for incoming client connections
* When one is received fork
* In the Child: close the listening file descriptor
* return the file descriptor that is
* a socket connection to the client
* In the Server: close the socket to the client and loop
* pre: port: port to listen to, an ASCII representation of a number
* or an entry from /etc/services
* interface_address: If NULL bind to all interfaces, else
* bind to interface(es) with this address.
* maximum_connections: maximum number of active connections to
* handle. If 0 then an number of connections
* is unlimited.
* return_from: pointer to a struct_in addr where the
* connecting client's IP address will
* be placed. Ignored if NULL
* return_to: pointer to a in addr where the IP address the
* server accepted the connection on will be placed.
* Ignored if NULL
* flag: If VANESSA_SOCKET_NO_LOOKUP then no host and port lookups
* will be performed
* post: Client sockets are returned in child processes
* In the parent process the function doesn't exit, other
* than on error.
* if return_from is non-null, it is seeded with clients address
* return: open client socket, if connection is accepted.
* -1 on error
**********************************************************************/
int vanessa_socket_server_connect(const char *port,
const char *interface_address,
const unsigned int maximum_connections,
struct sockaddr_in *return_from,
struct sockaddr_in *return_to,
vanessa_socket_flag_t flag)
{
struct sockaddr_in from;
int s;
/* Fill in informtaion for 'from' */
if (vanessa_socket_host_port_sockaddr_in(interface_address,
port, &from, flag) < 0) {
VANESSA_LOGGER_DEBUG
("vanessa_socket_host_port_sockaddr_in");
return (-1);
}
/* Make the connection */
if ((s = vanessa_socket_server_connect_sockaddr_in(from,
maximum_connections,
return_from,
return_to,
flag)) < 0) {
VANESSA_LOGGER_DEBUG
("vanessa_socket_server_connect_sockaddr_in");
return (-1);
}
return (s);
}
/**********************************************************************
* vanessa_socket_server_connect_sockaddr_in
* Listen on a tcp port for incoming client connections
* When one is received fork
* In the Child: close the listening file descriptor
* return the file descriptor that is
* a socket connection to the client
* In the Server: close the socket to the client and loop
* pre: from: sockaddr_in to bind to
* maximum_connections: maximum number of active connections to
* handle. If 0 then an number of connections
* is unlimited.
* return_from: pointer to a struct_in addr where the
* connecting client's IP address will
* be placed. Ignored if NULL
* return_to: pointer to a in addr where the IP address the
* server accepted the connection on will be placed.
* Ignored if NULL
* flag: ignored
* post: Client sockets are returned in child processes
* In the parent process the function doesn't exit, other
* than on error.
* if return_from is non-null, it is seeded with clients address
* return: open client socket, if connection is accepted.
* -1 on error
**********************************************************************/
int vanessa_socket_server_connect_sockaddr_in(struct sockaddr_in from,
const unsigned int
maximum_connections,
struct sockaddr_in
*return_from,
struct sockaddr_in
*return_to,
vanessa_socket_flag_t flag)
{
int s;
int g;
s = vanessa_socket_server_bind_sockaddr_in(from, flag);
if(s < 0) {
VANESSA_LOGGER_DEBUG("vanessa_socket_server_bind_sockaddr_in");
return (-1);
}
g = vanessa_socket_server_accept(s, maximum_connections,
return_from, return_to, 0);
if(g < 0) {
VANESSA_LOGGER_DEBUG("vanessa_socket_server_accept");
return(-1);
}
return(g);
}
|