|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sflsock.h"
sock_t
passive_socket (
const char *service, /* Service name or port as string */
const char *protocol, /* Protocol "tcp" or "udp" */
int queue_length /* Queue length for TCP sockets */
)
Creates a passive TCP or UDP socket. This function allows a server program to create a master socket, so that connections can be accepted. Used by the passive_TCP and passive_UDP functions. Returns a socket number or INVALID_SOCKET. If it returns INVALID_SOCKET, you can get the reason for the error by calling connect error (). This may be one of:
| IP NOSOCKETS | Sockets not supported on this system |
| IP BADSERVICE | Service cannot be converted to port number |
| IP BADPROTOCOL | Cannot understand protocol name |
| IP SOCKETERROR | Cannot create the passive socket |
| IP BINDERROR | Cannot bind to the port |
| IP LISTENERROR | Cannot listen to port |
{
#if (defined (DOES_SOCKETS))
struct servent
*pse; /* Service information entry */
struct sockaddr_in
sin; /* Internet end-point address */
sock_t
handle; /* Socket from socket() call */
ASSERT (service && *service);
ASSERT (protocol && *protocol);
connect_error_value = IP_NOERROR; /* Assume no errors */
memset ((void *) &sin, 0, sizeof (sin));
sin.sin_family = AF_INET;
sin.sin_addr.s_addr = ip_passive;
ip_passive = INADDR_ANY; /* Reset passive address */
/* To allow privileged operations, if possible */
set uid root ();
/* Map service name to port number */
pse = getservbyname (service, protocol);
if (pse)
sin.sin_port = htons ((dbyte) (ntohs (pse-> s_port) + ip_portbase));
else
{
sin.sin_port = atoi (service);
if (sin.sin_port + ip_portbase > 0)
sin.sin_port = htons ((dbyte) (sin.sin_port + ip_portbase));
else
{
connect_error_value = IP_BADSERVICE;
set uid user ();
return (INVALID_SOCKET);
}
}
handle = create socket (protocol);
if (handle == INVALID_SOCKET) /* Cannot create the socket */
{
set uid user ();
return (INVALID_SOCKET);
}
/* Bind the socket */
if (bind ((SOCKET) handle, (struct sockaddr *) &sin,
sizeof (sin)) == SOCKET_ERROR)
{
connect_error_value = IP_BINDERROR;
set uid user ();
return (INVALID_SOCKET); /* Cannot bind to port */
}
set uid user ();
/* Specify incoming queue length for stream socket */
if (streq (protocol, "tcp")
&& listen ((SOCKET) handle, queue_length) == SOCKET_ERROR)
{
connect_error_value = IP_LISTENERROR;
return (INVALID_SOCKET); /* Cannot listen on port */
}
return (handle);
#elif (defined (FAKE_SOCKETS))
return (1); /* Return dummy handle */
#else
connect_error_value = IP_NOSOCKETS;
return (INVALID_SOCKET); /* Sockets not supported */
#endif
}
| | << | < | > | >> |
|