- struct hostent *hostent;
- struct servent *servent;
- char *endptr;
- long port_long;
- uint16_t port_short;
-
- addr->sin_family = AF_INET;
-
- hostent = gethostbyname (host);
- if (hostent == NULL)
- {
- fprintf (stderr, "Error: Lookup failed for host %s: %s\n",
- host, hstrerror (h_errno));
- return TTT_STATUS_FAILURE;
- }
- memcpy (&addr->sin_addr.s_addr, hostent->h_addr_list[0],
- sizeof (addr->sin_addr.s_addr));
-
- assert (*port != '\0');
- port_long = strtol (port, &endptr, 10);
- if (*endptr == '\0')
- {
- /* Numeric port*/
- if (port_long <= 0 || port_long >= (1 << 16)) {
- fprintf (stderr, "Error: Port %ld out of range.\n", port_long);
- return TTT_STATUS_FAILURE;
- }
- port_short = port_long;
- addr->sin_port = htons (port_short);
- } else {
- /* Named port */
- servent = getservbyname (port, "tcp");
- if (port == NULL) {
- fprintf (stderr, "Error: Lookup failed for port %s: %s\n",
- port, hstrerror (h_errno));
- return TTT_STATUS_FAILURE;
- }
- addr->sin_port = servent->s_port;
- }