exercises/ex04/trout/sock.c
char *sock_ntop_host(const struct sockaddr *sa, socklen_t salen)
{
  static char str[128];

  switch (sa->sa_family) {
  case AF_INET: {
    struct sockaddr_in      *sin = (struct sockaddr_in *) sa;

    if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL) {
      return NULL;
    } else {
      return str;
    }
  }
  case AF_UNIX: {
    struct sockaddr_un      *unp = (struct sockaddr_un *) sa;

    /* OK to have no pathname bound to the socket: happens on
       every connect() unless client calls bind() first. */
    if (unp->sun_path[0] == 0)
      strcpy(str, "(no pathname bound)");
    else
      snprintf(str, sizeof(str), "%s", unp->sun_path);
    return str;
  }
  default:
    snprintf(str, sizeof(str), "sock_ntop_host: unknown AF_xxx: %d, len %d",
	     sa->sa_family, salen);
    return str;
  }
  return NULL;
}
/* A collection of functions for interacting with sockets and network. */
A collection of functions for interacting with sockets and network.
A collection of functions for interacting with sockets and network .
char *sock_ntop_host(const struct sockaddr *sa, socklen_t salen)
{
  static char str[128];
  switch (sa->sa_family) {
  case AF_INET: {
    struct sockaddr_in      *sin = (struct sockaddr_in *) sa;
    if (inet_ntop(AF_INET, &sin->sin_addr, str, sizeof(str)) == NULL) {
      return NULL;
    } else {
      return str;
    }
  }
  case AF_UNIX: {
    struct sockaddr_un      *unp = (struct sockaddr_un *) sa;
    if (unp->sun_path[0] == 0)
      strcpy(str, "(no pathname bound)");
    else
      snprintf(str, sizeof(str), "%s", unp->sun_path);
    return str;
  }
  default:
    snprintf(str, sizeof(str), "sock_ntop_host: unknown AF_xxx: %d, len %d",
	     sa->sa_family, salen);
    return str;
  }
  return NULL;
}
char * sock_ntop_host ( const struct sockaddr * sa , socklen_t salen ) { static char str [ 128 ] ; switch ( sa -> sa_family ) { case AF_INET : { struct sockaddr_in * sin = ( struct sockaddr_in * ) sa ; if ( inet_ntop ( AF_INET , & sin -> sin_addr , str , sizeof ( str ) ) == NULL ) { return NULL ; } else { return str ; } } case AF_UNIX : { struct sockaddr_un * unp = ( struct sockaddr_un * ) sa ; if ( unp -> sun_path [ 0 ] == 0 ) strcpy ( str , " " ) ; else snprintf ( str , sizeof ( str ) , " " , unp -> sun_path ) ; return str ; } default : snprintf ( str , sizeof ( str ) , " " , sa -> sa_family , salen ) ; return str ; } return NULL ; }
A collection of functions for interacting with sockets and network.
A collection of functions for interacting with sockets and network .
/* OK to have no pathname bound to the socket: happens on
       every connect() unless client calls bind() first. */