|
| iMatix home page | << | < | > | >> |
SFLVersion 2.11 |
#include "sflsock.h"
int
address_end_point (
const char *host, /* Name of host, "" = localhost */
const char *service, /* Service name or port as string */
const char *protocol, /* Protocol "tcp" or "udp" */
struct sockaddr_in *sin /* Block for formatted address */
)
Formats an address block (struct sockaddr_in) for the specified host and service (port) information. Returns 0 if okay, SOCKET_ERROR if there was an error, in which case you can call connect error () to get the reason for the error. This may be one of:
| IP NOSOCKETS | Sockets not supported on this system |
| IP BADHOST | Host is not known |
{
#if (defined (DOES_SOCKETS))
struct hostent
*phe; /* Host information entry */
struct servent
*pse; /* Service information entry */
char
hostname [MAXHOSTNAMELEN + 1]; /* Name of this system */
int
feedback = 0; /* Assume everything works */
ASSERT (service && *service);
ASSERT (protocol && *protocol);
ASSERT (sin);
connect_error_value = IP_NOERROR; /* Assume no errors */
memset ((void *) sin, 0, sizeof (*sin));
sin-> sin_family = AF_INET;
/* Map service name to a port number */
pse = getservbyname (service, protocol);
if (pse)
sin-> sin_port = htons ((short) (ntohs (pse-> s_port)));
else
sin-> sin_port = htons ((short) (atoi (service)));
/* Map host name to IP address, allowing for dotted decimal */
if (host && strused (host))
strcpy (hostname, host);
else
strcpy (hostname, "127.0.0.1");
/* Check if it's a valid IP address first */
sin-> sin_addr.s_addr = inet_addr (hostname);
if (sin-> sin_addr.s_addr == INADDR_NONE)
{
/* Not a dotted address -- try to translate the name */
phe = gethostbyname (hostname);
if (phe)
memcpy ((void *) &sin-> sin_addr, phe-> h_addr, phe-> h_length);
else
{ /* Cannot map to host */
connect_error_value = IP_BADHOST;
feedback = (int) SOCKET_ERROR;
}
}
return (feedback);
#else
connect_error_value = IP_NOSOCKETS;
return ((int) SOCKET_ERROR); /* Sockets not supported */
#endif
}
| | << | < | > | >> |
|