Esempio 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;
}
Esempio n. 2
0
static int
TRANS(DNETConnect) (XtransConnInfo ciptr, char *host, char *port)

{
    char objname[OBJBUFSIZE];

    extern int dnet_conn();
    
    PRMSG (2,"DNETConnect(%d,%s,%s)\n", ciptr->fd, host, port);

#ifdef X11_t
    /*
     * X has a well known port, that is transport dependent. It is easier
     * to handle it here, than try and come up with a transport independent
     * representation that can be passed in and resolved the usual way.
     *
     * The port that is passed here is really a string containing the idisplay
     * from ConnectDisplay().
     */

    if (is_numeric (port))
    {
	short tmpport = (short) atoi (port);

	sprintf (objname, "X$X%d", tmpport);
    }
    else
#endif
	strncpy(objname, port, OBJBUFSIZE);


    /*
     * Do the connect
     */

    if (!host) host = "0";

    if ((ciptr->fd = dnet_conn (host, objname, SOCK_STREAM, 0, 0, 0, 0)) < 0)
    {
	return TRANS_CONNECT_FAILED;
    }


    /*
     * Sync up the address fields of ciptr.
     */

    if (TRANS(DNETGetAddr) (ciptr) < 0)
    {
	PRMSG (1,
	      "DNETConnect: ...DNETGetAddr() failed:\n", 0, 0, 0);
	return TRANS_CONNECT_FAILED;
    }

    if (TRANS(DNETGetPeerAddr) (ciptr) < 0)
    {
	PRMSG (1,
	      "DNETConnect: ...DNETGetPeerAddr() failed:\n",
	      0, 0, 0);
	return TRANS_CONNECT_FAILED;
    }

    return 0;
}