| iMatix home page
| << | < | > | >>
SFL Logo SFL
Version 2.11

 

passive_socket

#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     */
)

Synopsis

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
By default, opens a socket on all available IP addresses. You can open the socket on a specific address, by setting the global variable ip_passive to the address (in network order). This variable is reset to INADDR_ANY after each call to passive_socket or one of the functions that calls it.

Source Code - (sflsock.c)

{
#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
}

| << | < | > | >> iMatix Copyright © 1996-2000 iMatix Corporation