Ejemplo n.º 1
0
// Open Socket to server.
static int muroar_open_socket_bsd (const char * server) {
 struct hostent     * he;
 struct sockaddr_in   in;
#ifdef HAVE_AF_UNIX
 struct sockaddr_un   un;
#endif
 int fh = -1;
 char * buf = NULL;
 char * object;
#ifdef HAVE_LIB_DNET
 char * node;
 static char localnode[16] = {0};
 struct dn_naddr      *binaddr;
 struct nodeent       *dp;
#endif

#ifdef __WIN32
 muroar_init_win32();
#endif

 if ( !strcmp(server, MUROAR_ABSTRACT) || (strstr(server, "/") != NULL && strstr(server, "::") == NULL) ) {
// Handle AF_UNIX Sockets,
// do not build on broken systems like win32 not
// supporting the AF_UNIX sockets.
#ifdef HAVE_AF_UNIX
  if ( (fh = socket(AF_UNIX, SOCK_STREAM, 0)) == -1 )
   return -1;

  un.sun_family = AF_UNIX;

  if ( !strcmp(server, MUROAR_ABSTRACT) ) {
   memset(un.sun_path, 0, sizeof(un.sun_path));
   strncpy(un.sun_path + 1, "RoarAudio/UNIX/Abstract/1", sizeof(un.sun_path) - 2);
  } else {
   strncpy(un.sun_path, server, sizeof(un.sun_path) - 1);
  }

  if ( connect(fh, (struct sockaddr *)&un, sizeof(struct sockaddr_un)) == -1 ) {
   __CLOSE(fh);
   return -1;
  }

  return fh;
#else
  return -1;
#endif
 } else if ( strstr(server, "::") != NULL ) {
#ifdef HAVE_LIB_DNET
  // alloc a temp buffer so we can change the string at will:
  buf = strdup(server);

  // cut node::object into buf and object
  object  = strstr(buf, "::");
  *object = 0;
   object += 2;

  // use default if we have a zero-size node name:
  if ( *buf == 0 ) {
   if ( !localnode[0] ) {
    if ( (binaddr=getnodeadd()) == NULL) {
     free(buf);
     return -1;
    }

    if ( (dp = getnodebyaddr((char*)binaddr->a_addr, binaddr->a_len, AF_DECnet)) == NULL ) {
     free(buf);
     return -1;
    }

    strncpy(localnode, dp->n_name, sizeof(localnode)-1);
    localnode[sizeof(localnode)-1] = 0;
   }

   node = localnode;
  } else {
   node = buf;
  }

  // use default if we have a zero size object name:
  if ( *object == 0 ) {
   object = MUROAR_OBJECT;
  }

  fh = dnet_conn(node, object, SOCK_STREAM, NULL, 0, NULL, 0);

  // free buffer when we are done.
  free(buf);

  return fh;
#else
  return -1;
#endif
 }

 if ( strstr(server, ":") != NULL ) {
  buf = strdup(server);
  server = buf;
  object = strstr(buf, ":");
  *object = 0;
  object++;
  if ( !*object ) /* finnaly check if this is just a tailing :, in that case we assume the default port */
   object = NULL;
 } else {
  object = NULL;
 }

 if ( (he = gethostbyname(server)) == NULL ) {
  if ( buf != NULL )
   free(buf);
  return -1;
 }

 memcpy((struct in_addr *)&(in.sin_addr), he->h_addr, sizeof(struct in_addr));

 in.sin_family = AF_INET;
 if ( object != NULL ) {
  in.sin_port   = htons(atoi(object));
 } else {
  in.sin_port   = htons(MUROAR_PORT);
 }

 if ( buf != NULL )
  free(buf);

 if ( (fh = socket(AF_INET, SOCK_STREAM, 0)) == -1 )
  return -1;

 if ( connect(fh, (const struct sockaddr *)&in, sizeof(in)) == -1 ) {
  __CLOSE(fh);
  return -1;
 }

 return fh;
}
Ejemplo n.º 2
0
char *
get_hostname (Xauth *auth)
{
    static struct hostent *hp;
    int af;
#ifdef DNETCONN
    struct nodeent *np;
    static char nodeaddr[4 + 2 * DN_MAXADDL];
#endif /* DNETCONN */

    hp = NULL;
    if (auth->address_length == 0)
	return "Illegal Address";
#ifdef TCPCONN
    if (auth->family == FamilyInternet
#if defined(IPv6) && defined(AF_INET6)
      || auth->family == FamilyInternet6
#endif 
	)
    {
#if defined(IPv6) && defined(AF_INET6)
	if (auth->family == FamilyInternet6)
	    af = AF_INET6;
	else
#endif
	    af = AF_INET;
	if (no_name_lookups == False) {
#ifdef SIGALRM
	/* gethostbyaddr can take a LONG time if the host does not exist.
	   Assume that if it does not respond in NAMESERVER_TIMEOUT seconds
	   that something is wrong and do not make the user wait.
	   gethostbyaddr will continue after a signal, so we have to
	   jump out of it. 
	   */
	nameserver_timedout = False;
	signal (SIGALRM, nameserver_lost);
	alarm (4);
	if (setjmp(env) == 0) {
#endif
	    hp = gethostbyaddr (auth->address, auth->address_length, af);
#ifdef SIGALRM
	}
	alarm (0);
#endif
	}
	if (hp)
	  return (hp->h_name);
#if defined(IPv6) && defined(AF_INET6)
	else if (af == AF_INET6) {
	  static char addr[INET6_ADDRSTRLEN+2];
	  /* Add [] for clarity to distinguish between address & display,
	     like RFC 2732 for URL's.  Not required, since X display syntax
	     always ends in :<display>, but makes it easier for people to read
	     and less confusing to those who expect the RFC 2732 style. */
	  addr[0] = '[';
	  if (inet_ntop(af, auth->address, addr + 1, INET6_ADDRSTRLEN) == NULL)
	    return NULL;
	  strcat(addr, "]");
          return addr;
	}
#endif
	else {
	  return (inet_ntoa(*((struct in_addr *)(auth->address))));
	}
    }
#endif
#ifdef DNETCONN
    if (auth->family == FamilyDECnet) {
	struct dn_naddr *addr_ptr = (struct dn_naddr *) auth->address;

	if ((no_name_lookups == False) &&
	    (np = getnodebyaddr(addr_ptr->a_addr, addr_ptr->a_len, AF_DECnet))) {
	    sprintf(nodeaddr, "%s:", np->n_name);
	} else {
	    sprintf(nodeaddr, "%s:", dnet_htoa(auth->address));
	}
	return(nodeaddr);
    }
#endif

    return (NULL);
}
Ejemplo n.º 3
0
char *
TRANS(GetPeerNetworkId) (XtransConnInfo ciptr)

{
    int		family = ciptr->family;
    char	*peer_addr = ciptr->peeraddr;
    char	*hostname;
    char	addrbuf[256];
    const char	*addr = NULL;

    switch (family)
    {
    case AF_UNSPEC:
#if defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
    case AF_UNIX:
    {
	if (gethostname (addrbuf, sizeof (addrbuf)) == 0)
	    addr = addrbuf;
	break;
    }
#endif /* defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) || defined(OS2PIPECONN) */

#if defined(TCPCONN) || defined(STREAMSCONN)
    case AF_INET:
#if defined(IPv6) && defined(AF_INET6)
    case AF_INET6:
#endif
    {
	struct sockaddr_in *saddr = (struct sockaddr_in *) peer_addr;
#if defined(IPv6) && defined(AF_INET6)
	struct sockaddr_in6 *saddr6 = (struct sockaddr_in6 *) peer_addr;
#endif
	char *address;
	int addresslen;
#ifdef XTHREADS_NEEDS_BYNAMEPARAMS
	_Xgethostbynameparams hparams;
#endif
	struct hostent * volatile hostp = NULL;

#if defined(IPv6) && defined(AF_INET6)
	if (family == AF_INET6)
	{
	    address = (char *) &saddr6->sin6_addr;
	    addresslen = sizeof (saddr6->sin6_addr);
	}
	else
#endif
	{
	    address = (char *) &saddr->sin_addr;
	    addresslen = sizeof (saddr->sin_addr);
	}

#ifdef SIGALRM
	/*
	 * gethostbyaddr can take a LONG time if the host does not exist.
	 * Assume that if it does not respond in NAMESERVER_TIMEOUT seconds
	 * that something is wrong and do not make the user wait.
	 * gethostbyaddr will continue after a signal, so we have to
	 * jump out of it. 
	 */

	nameserver_timedout = 0;
	signal (SIGALRM, nameserver_lost);
	alarm (4);
	if (setjmp(env) == 0) {
#endif
	    hostp = _XGethostbyaddr (address, addresslen, family, hparams);
#ifdef SIGALRM
	}
	alarm (0);
#endif
	if (hostp != NULL)
	  addr = hostp->h_name;
	else
#if defined(IPv6) && defined(AF_INET6)
	  addr = inet_ntop (family, address, addrbuf, sizeof (addrbuf));
#else
	  addr = inet_ntoa (saddr->sin_addr);
#endif
	break;
    }

#endif /* defined(TCPCONN) || defined(STREAMSCONN) */

#if defined(DNETCONN)
    case AF_DECnet:
    {
	struct sockaddr_dn *saddr = (struct sockaddr_dn *) peer_addr;
	struct nodeent *np;

	if (np = getnodebyaddr(saddr->sdn_add.a_addr,
	    saddr->sdn_add.a_len, AF_DECnet)) {
	    sprintf(addrbuf, "%s:", np->n_name);
	} else {
	    sprintf(addrbuf, "%s:", dnet_htoa(&saddr->sdn_add));
	}
	addr = addrbuf;
	break;
    }
#endif /* defined(DNETCONN) */

    default:
	return (NULL);
    }


    hostname = (char *) xalloc (
	strlen (ciptr->transptr->TransName) + strlen (addr) + 2);
    strcpy (hostname, ciptr->transptr->TransName);
    strcat (hostname, "/");
    if (addr)
	strcat (hostname, addr);

    return (hostname);
}
Ejemplo n.º 4
0
char *
TRANS(GetPeerNetworkId) (XtransConnInfo ciptr)

{
    int		family = ciptr->family;
    char	*hostname;
    char	addrbuf[256];
    char	*addr = NULL;

    switch (family)
    {
    case AF_UNSPEC:
#if defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
    case AF_UNIX:
    {
	if (gethostname (addrbuf, sizeof (addrbuf)) == 0) {
	    addrbuf[sizeof(addrbuf)-1] = '\0';
	    addr = addrbuf;
	}
	break;
    }
#endif /* defined(UNIXCONN) || defined(STREAMSCONN) || defined(LOCALCONN) || defined(OS2PIPECONN)
*/

#if defined(TCPCONN) || defined(STREAMSCONN) || defined(MNX_TCPCONN)
    case AF_INET:
    {
    char	*peer_addr = ciptr->peeraddr;
	struct sockaddr_in *saddr = (struct sockaddr_in *) peer_addr;
	struct hostent * hostp = NULL;

#if defined(SIGALRM) && !defined(_WIN32)
	/*
	 * gethostbyaddr can take a LONG time if the host does not exist.
	 * Assume that if it does not respond in NAMESERVER_TIMEOUT seconds
	 * that something is wrong and do not make the user wait.
	 * gethostbyaddr will continue after a signal, so we have to
	 * jump out of it.
	 */

	nameserver_timedout = 0;
	signal (SIGALRM, nameserver_lost);
	alarm (4);
	if (setjmp(env) == 0) {
#endif
	    hostp = gethostbyaddr ((char *) &saddr->sin_addr,
		sizeof (saddr->sin_addr), AF_INET);
#if defined(SIGALRM) && !defined(_WIN32)
	}
	alarm (0);
#endif
	if (hostp != NULL)
	  addr = hostp->h_name;
	else
	  addr = inet_ntoa (saddr->sin_addr);
	break;
    }

#endif /* defined(TCPCONN) || defined(STREAMSCONN) || MNX_TCPCONN */

#if defined(DNETCONN)
    case AF_DECnet:
    {
    char	*peer_addr = ciptr->peeraddr;
	struct sockaddr_dn *saddr = (struct sockaddr_dn *) peer_addr;
	struct nodeent *np;

	if (np = getnodebyaddr(saddr->sdn_add.a_addr,
	    saddr->sdn_add.a_len, AF_DECnet)) {
	    snprintf(addrbuf, sizeof(addrbuf), "%s:", np->n_name);
	} else {
	    snprintf(addrbuf, sizeof(addrbuf), "%s:", dnet_htoa(&saddr->sdn_add));
	}
	addr = addrbuf;
	break;
    }
#endif /* defined(DNETCONN) */

#if defined(AMRPCCONN)
    case AF_AMOEBA:
    {
	addr = "Amoeba"; /* not really used */
	break;
    }
#endif
#if defined(AMTCPCONN) && !(defined(TCPCONN) || defined(STREAMSCONN))
    case AF_INET:
    {
	if (gethostname (addrbuf, sizeof (addrbuf)) == 0) {
	    addrbuf[sizeof(addrbuf)-1] = '\0';
	    addr = addrbuf;
	} else {
	    addr = "";
	}
	break;
    }
#endif

    default:
	return (NULL);
    }


    hostname = (char *) xalloc (
	strlen (ciptr->transptr->TransName) + strlen (addr) + 2);
    strcpy (hostname, ciptr->transptr->TransName);
    strcat (hostname, "/");
    if (addr)
	strcat (hostname, addr);

    return (hostname);
}