Exemplo n.º 1
0
static void netreceive_connectpoll(t_netreceive *x)
{
    int fd = accept(x->x_connectsocket, 0, 0);
    if (fd < 0) post("netreceive: accept failed");
    else
    {
        t_socketreceiver *y = socketreceiver_new((void *)x, 
            (t_socketnotifier)netreceive_notify,
                (x->x_msgout ? netreceive_doit : 0), 0);
        sys_addpollfn(fd, (t_fdpollfn)socketreceiver_read, y);
        outlet_float(x->x_connectout, ++x->x_nconnections);
    }
}
Exemplo n.º 2
0
static void netreceive_connectpoll(t_netreceive *x)
{
    int fd = accept(x->x_ns.x_sockfd, 0, 0);
    if (fd < 0) post("netreceive: accept failed");
    else
    {
        int nconnections = x->x_nconnections+1;
        
        x->x_connections = (int *)t_resizebytes(x->x_connections,
            x->x_nconnections * sizeof(int), nconnections * sizeof(int));
        x->x_connections[x->x_nconnections] = fd;
        if (x->x_ns.x_bin)
            sys_addpollfn(fd, (t_fdpollfn)netsend_readbin, x);
        else
        {
            t_socketreceiver *y = socketreceiver_new((void *)x, 
            (t_socketnotifier)netreceive_notify,
                (x->x_ns.x_msgout ? netsend_doit : 0), 0);
            sys_addpollfn(fd, (t_fdpollfn)socketreceiver_read, y);
        }
        outlet_float(x->x_ns.x_connectout, (x->x_nconnections = nconnections));
    }
}
Exemplo n.º 3
0
static void netserver_connectpoll(t_netserver *x)
{
   struct sockaddr_in incomer_address;
   int sockaddrl = (int) sizeof( struct sockaddr );
   int fd = accept(x->x_connectsocket, (struct sockaddr*)&incomer_address, &sockaddrl);
   if (fd < 0) post("netserver: accept failed");
   else
   {
	  t_netserver_socketreceiver *y = netserver_socketreceiver_new((void *)x, 
																   (t_netserver_socketnotifier)netserver_notify,
																   (x->x_msgout ? netserver_doit : 0));
	  sys_addpollfn(fd, (t_fdpollfn)netserver_socketreceiver_read, y);
	  x->x_nconnections++;
	  x->x_host[x->x_nconnections - 1] = gensym(inet_ntoa(incomer_address.sin_addr));
	  x->x_fd[x->x_nconnections - 1] = fd;

	  if (x->x_log_pri >= LOG_NOTICE) 
		 post("netserver: ** accepted connection from %s on socket %d", 
			  x->x_host[x->x_nconnections - 1]->s_name, x->x_fd[x->x_nconnections - 1]);
	  outlet_float(x->x_connectout, x->x_nconnections);
   }
}
Exemplo n.º 4
0
static void popen_ropen(t_popen *x, t_symbol *s,int argc, t_atom *argv)
{
    char cmd[512],*text;
    int cmd_len;

    t_binbuf *bb=binbuf_new();

    popen_close(x);

    //post("argc=%d",argc);
    //post("argv[0]=%s",atom_getsymbol(&argv[0])->s_name);

    binbuf_add(bb,argc,argv);
    binbuf_gettext(bb, &text,&cmd_len);
    binbuf_free(bb);

    strncpy(cmd,text,cmd_len);
    cmd[cmd_len]=0;
    //post("cmd=%s",cmd);

    x->x_file=popen(cmd,"r");
	sys_addpollfn(fileno(x->x_file),(t_fdpollfn)popen_read,(void*)x);
	x->x_ropened=1;
}
Exemplo n.º 5
0
static int udpreceive_new_socket(t_udpreceive *x, char *address, int port)
{
    // return nonzero if successful in creating and binding a socket
    int                 sockfd;
    int                 intarg;
    int                 multicast_joined = 0;
    struct sockaddr_in6  server;
    struct hostent      *hp;
#if defined __APPLE__ || defined _WIN32
    struct ipv6_mreq      mreq;
#else
    struct ipv6_mreqn     mreq;
#endif
    
    if (x->x_connectsocket >= 0)
    {
        // close the existing socket first
        sys_rmpollfn(x->x_connectsocket);
        sys_closesocket(x->x_connectsocket);
    }
    /* create a socket */
    sockfd = socket(AF_INET6, SOCK_DGRAM, 0);
#ifdef DEBUG
    post("udpreceive_new: socket %d port %d", sockfd, portno);
#endif
    if (sockfd < 0)
    {
        udpreceive_sock_err(x, "udpreceive: socket");
        return 0;
    }
    server.sin6_family = AF_INET6;
    if (address[0] == 0)
        server.sin6_addr = in6addr_any;
    else
    {

        hp = gethostbyname2(address, AF_INET6);
        if (hp == 0)
        {
            pd_error(x, "udpreceive: bad host?\n");
            return 0;
        }
        //memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
        inet_pton(AF_INET6, address, &server.sin6_addr);
        
    }
    /* enable delivery of all multicast or broadcast (but not unicast)
     * UDP datagrams to all sockets bound to the same port */
    intarg = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
                   (char *)&intarg, sizeof(intarg)) < 0)
    {
        udpreceive_sock_err(x, "udpreceive: setsockopt (SO_REUSEADDR) failed");
        return 0;
    }
    /* assign server port number */
    server.sin6_port = htons((u_short)port);
    
    if(IN6_IS_ADDR_MULTICAST(&server.sin6_addr))
        post("need to impletment mulitcast");
    
    /* if a multicast address was specified, join the multicast group */
    /* hop count defaults to 1 so we won't leave the subnet*/
/*
    if (0xE0000000 == (ntohl(server.sin_addr.s_addr) & 0xF0000000))
    {
        server.sin_addr.s_addr = INADDR_ANY;
        // first bind the socket to INADDR_ANY
        if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
        {
            udpreceive_sock_err(x, "udpreceive: bind");
            sys_closesocket(sockfd);
            return 0;
        }
        // second join the multicast group
        memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);
        
#if defined __APPLE__ || defined _WIN32
        mreq.imr_multiaddr.s_addr = server.sin_addr.s_addr;
        mreq.imr_interface.s_addr = INADDR_ANY;// can put a specific local IP address here if host is multihomed
#else
        mreq.imr_multiaddr.s_addr = server.sin_addr.s_addr;
        mreq.imr_address.s_addr = INADDR_ANY;
        mreq.imr_ifindex = 0;
#endif //__APPLE__ || _WIN32
        if (setsockopt(sockfd, IPPROTO_IP, IP_ADD_MEMBERSHIP,
                       (char *)&mreq, sizeof(mreq)) < 0)
            udpreceive_sock_err(x, "udpreceive: setsockopt IP_ADD_MEMBERSHIP");
        else
        {
            multicast_joined = 1;
            post ("udpreceive: added to multicast group");
        }
    }
    else
    {*/
        /* name the socket */
        if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
        {
            udpreceive_sock_err(x, "udpreceive: bind");
            sys_closesocket(sockfd);
            return 0;
        }
  //  }
    x->x_multicast_joined = multicast_joined;
    x->x_connectsocket = sockfd;
    x->x_total_received = 0L;
    sys_addpollfn(x->x_connectsocket, (t_fdpollfn)udpreceive_read, x);
    return 1;
}
Exemplo n.º 6
0
static void *netserver_new(t_floatarg fportno)
{
   t_netserver *x;
   int i;
   struct sockaddr_in server;
   int sockfd, portno = fportno;

   x = (t_netserver *)pd_new(netserver_class);
/* set default debug message level */
   x->x_log_pri = LOG_ERR;
/* create a socket */
   sockfd = socket(AF_INET, SOCK_STREAM, 0);
   if (x->x_log_pri >= LOG_NOTICE)
	  post("netserver: receive socket %d", sockfd);
   if (sockfd < 0)
   {
	  sys_sockerror("socket");
	  return (0);
   }
   server.sin_family = AF_INET;
   server.sin_addr.s_addr = INADDR_ANY;

#ifdef IRIX
   /* this seems to work only in IRIX but is unnecessary in
	  Linux.  Not sure what NT needs in place of this. */
   if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 0, 0) < 0)
	  post("setsockopt failed\n");
#endif

   /* assign server port number */
   server.sin_port = htons((u_short)portno);

   /* name the socket */
   if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
   {
	  sys_sockerror("bind");
	  sys_closesocket(sockfd);
	  return (0);
   }
   x->x_msgout = outlet_new(&x->x_obj, &s_anything);

   /* streaming protocol */
   if (listen(sockfd, 5) < 0)
   {
	  sys_sockerror("listen");
	  sys_closesocket(sockfd);
	  sockfd = -1;
   }
   else
   {
	  sys_addpollfn(sockfd, (t_fdpollfn)netserver_connectpoll, x);
	  x->x_connectout = outlet_new(&x->x_obj, &s_float);
	  x->x_clientno = outlet_new(&x->x_obj, &s_float);
	  x->x_connectionip = outlet_new(&x->x_obj, &s_symbol);
	  inbinbuf = binbuf_new();
   }
   x->x_connectsocket = sockfd;
   x->x_nconnections = 0;
   for(i = 0; i < MAX_CONNECT; i++)x->x_fd[i] = -1;

   return (x);
}
Exemplo n.º 7
0
static void netreceive_listen(t_netreceive *x, t_floatarg fportno)
{
    int portno = fportno, intarg;
    struct sockaddr_in server;
    netreceive_closeall(x);
    if (portno <= 0)
        return;
    x->x_ns.x_sockfd = socket(AF_INET, x->x_ns.x_protocol, 0);
    if (x->x_ns.x_sockfd < 0)
    {
        sys_sockerror("socket");
        return;
    }
#if 0
    fprintf(stderr, "receive socket %d\n", x->x_ sockfd);
#endif

#if 1
        /* ask OS to allow another Pd to repoen this port after we close it. */
    intarg = 1;
    if (setsockopt(x->x_ns.x_sockfd, SOL_SOCKET, SO_REUSEADDR,
        (char *)&intarg, sizeof(intarg)) < 0)
            post("netreceive: setsockopt (SO_REUSEADDR) failed\n");
#endif
#if 0
    intarg = 0;
    if (setsockopt(x->x_ns.x_sockfd, SOL_SOCKET, SO_RCVBUF,
        &intarg, sizeof(intarg)) < 0)
            post("setsockopt (SO_RCVBUF) failed\n");
#endif
    intarg = 1;
    if (setsockopt(x->x_ns.x_sockfd, SOL_SOCKET, SO_BROADCAST, 
        (const void *)&intarg, sizeof(intarg)) < 0)
            post("netreceive: failed to sett SO_BROADCAST");
        /* Stream (TCP) sockets are set NODELAY */
    if (x->x_ns.x_protocol == SOCK_STREAM)
    {
        intarg = 1;
        if (setsockopt(x->x_ns.x_sockfd, IPPROTO_TCP, TCP_NODELAY,
            (char *)&intarg, sizeof(intarg)) < 0)
                post("setsockopt (TCP_NODELAY) failed\n");
    }
        /* assign server port number etc */
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;
    server.sin_port = htons((u_short)portno);

        /* name the socket */
    if (bind(x->x_ns.x_sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        sys_sockerror("bind");
        sys_closesocket(x->x_ns.x_sockfd);
        x->x_ns.x_sockfd = -1;
        return;
    }

    if (x->x_ns.x_protocol == SOCK_DGRAM)        /* datagram protocol */
    {
        if (x->x_ns.x_bin)
            sys_addpollfn(x->x_ns.x_sockfd, (t_fdpollfn)netsend_readbin, x);
        else
        {
            t_socketreceiver *y = socketreceiver_new((void *)x, 
                (t_socketnotifier)netreceive_notify,
                    (x->x_ns.x_msgout ? netsend_doit : 0), 1);
            sys_addpollfn(x->x_ns.x_sockfd, (t_fdpollfn)socketreceiver_read, y);
            x->x_ns.x_connectout = 0;
        }
    }
    else        /* streaming protocol */
    {
        if (listen(x->x_ns.x_sockfd, 5) < 0)
        {
            sys_sockerror("listen");
            sys_closesocket(x->x_ns.x_sockfd);
            x->x_ns.x_sockfd = -1;
        }
        else
        {
            sys_addpollfn(x->x_ns.x_sockfd, (t_fdpollfn)netreceive_connectpoll, x);
            x->x_ns.x_connectout = outlet_new(&x->x_ns.x_obj, &s_float);
        }
    }
}
Exemplo n.º 8
0
static void netsend_connect(t_netsend *x, t_symbol *hostname,
    t_floatarg fportno)
{
    struct sockaddr_in server;
    struct hostent *hp;
    int sockfd;
    int portno = fportno;
    int intarg;
    if (x->x_sockfd >= 0)
    {
        error("netsend_connect: already connected");
        return;
    }

        /* create a socket */
    sockfd = socket(AF_INET, x->x_protocol, 0);
#if 0
    fprintf(stderr, "send socket %d\n", sockfd);
#endif
    if (sockfd < 0)
    {
        sys_sockerror("socket");
        return;
    }
    /* connect socket using hostname provided in command line */
    server.sin_family = AF_INET;
    hp = gethostbyname(hostname->s_name);
    if (hp == 0)
    {
        post("bad host?\n");
        return;
    }
#if 0
    intarg = 0;
    if (setsockopt(sockfd, SOL_SOCKET, SO_SNDBUF,
        &intarg, sizeof(intarg)) < 0)
            post("setsockopt (SO_RCVBUF) failed\n");
#endif
    intarg = 1;
    if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, 
                  (const void *)&intarg, sizeof(intarg)) < 0)
        post("setting SO_BROADCAST");
        /* for stream (TCP) sockets, specify "nodelay" */
    if (x->x_protocol == SOCK_STREAM)
    {
        intarg = 1;
        if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
            (char *)&intarg, sizeof(intarg)) < 0)
                post("setsockopt (TCP_NODELAY) failed\n");
    }
    memcpy((char *)&server.sin_addr, (char *)hp->h_addr, hp->h_length);

    /* assign client port number */
    server.sin_port = htons((u_short)portno);

    post("connecting to port %d", portno);
        /* try to connect.  LATER make a separate thread to do this
        because it might block */
    if (connect(sockfd, (struct sockaddr *) &server, sizeof (server)) < 0)
    {
        sys_sockerror("connecting stream socket");
        sys_closesocket(sockfd);
        return;
    }
    x->x_sockfd = sockfd;
    if (x->x_bin)
        sys_addpollfn(sockfd, (t_fdpollfn)netsend_readbin, x);
    else
    {
        t_socketreceiver *y =
            socketreceiver_new((void *)x, 0, netsend_doit, 0);
        sys_addpollfn(sockfd, (t_fdpollfn)socketreceiver_read, y);
    }
    outlet_float(x->x_obj.ob_outlet, 1);
}
Exemplo n.º 9
0
static void *netrec_new(t_symbol *compatflag,
    t_floatarg fportno, t_floatarg udpflag)
{
    t_netrec *x;
	int i;
    struct sockaddr_in server;
    int sockfd, portno = fportno, udp = (udpflag != 0);
    int old = !strcmp(compatflag->s_name , "old");
    	/* create a socket */
    sockfd = socket(AF_INET, (udp ? SOCK_DGRAM : SOCK_STREAM), 0);
#if 1
    post("netrec: receive socket %d\n", sockfd);
#endif
    if (sockfd < 0)
    {
    	sys_sockerror("socket");
    	return (0);
    }
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;

#ifdef IRIX
    	/* this seems to work only in IRIX but is unnecessary in
	Linux.  Not sure what NT needs in place of this. */
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 0, 0) < 0)
    	post("setsockopt failed\n");
#endif

    	/* assign server port number */
    server.sin_port = htons((u_short)portno);

    	/* name the socket */
    if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
    	sys_sockerror("bind");
    	sys_closesocket(sockfd);
    	return (0);
    }
    x = (t_netrec *)pd_new(netrec_class);
    if (old)
    {
    	/* old style, nonsecure version */
		x->x_msgout = 0;
    }
    else x->x_msgout = outlet_new(&x->x_obj, &s_anything);

    if (udp)	    /* datagram protocol */
    {
    	t_netrec_socketreceiver *y = netrec_socketreceiver_new((void *)x, 
    	    (t_netrec_socketnotifier)netrec_notify,
	    	(x->x_msgout ? netrec_doit : 0), 1);
		sys_addpollfn(sockfd, (t_fdpollfn)netrec_socketreceiver_read, y);
		x->x_connectout = 0;
    }
    else    	/* streaming protocol */
    {
		if (listen(sockfd, 5) < 0)
		{
    		sys_sockerror("listen");
    		sys_closesocket(sockfd);
			sockfd = -1;
		}
    	else
		{
			sys_addpollfn(sockfd, (t_fdpollfn)netrec_connectpoll, x);
    		x->x_connectout = outlet_new(&x->x_obj, &s_float);
			x->x_clientno = outlet_new(&x->x_obj, &s_float);
			x->x_connectionip = outlet_new(&x->x_obj, &s_symbol);
			inbinbuf = binbuf_new();
		}
    }
    x->x_connectsocket = sockfd;
    x->x_nconnections = 0;
    x->x_udp = udp;
	for(i = 0; i < MAX_CONNECT; i++)x->x_fd[i] = -1;

    return (x);
}
Exemplo n.º 10
0
static void *netreceive_new(t_symbol *compatflag,
    t_floatarg fportno, t_floatarg udpflag)
{
    t_netreceive *x;
    struct sockaddr_in server;
    int sockfd, portno = fportno, udp = (udpflag != 0);
    int old = !strcmp(compatflag->s_name , "old");
    int intarg;
        /* create a socket */
    sockfd = socket(AF_INET, (udp ? SOCK_DGRAM : SOCK_STREAM), 0);
#if 0
    fprintf(stderr, "receive socket %d\n", sockfd);
#endif
    if (sockfd < 0)
    {
        sys_sockerror("socket");
        return (0);
    }
    server.sin_family = AF_INET;
    server.sin_addr.s_addr = INADDR_ANY;

#if 1
        /* ask OS to allow another Pd to repoen this port after we close it. */
    intarg = 1;
    if (setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
        (char *)&intarg, sizeof(intarg)) < 0)
            post("setsockopt (SO_REUSEADDR) failed\n");
#endif
#if 0
    intarg = 0;
    if (setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF,
        &intarg, sizeof(intarg)) < 0)
            post("setsockopt (SO_RCVBUF) failed\n");
#endif
    intarg = 1;
    if(setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, 
                  (const void *)&intarg, sizeof(intarg)) < 0)
        post("setting SO_BROADCAST");
        /* Stream (TCP) sockets are set NODELAY */
    if (!udp)
    {
        intarg = 1;
        if (setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY,
            (char *)&intarg, sizeof(intarg)) < 0)
                post("setsockopt (TCP_NODELAY) failed\n");
    }
        /* assign server port number */
    server.sin_port = htons((u_short)portno);

        /* name the socket */
    if (bind(sockfd, (struct sockaddr *)&server, sizeof(server)) < 0)
    {
        sys_sockerror("bind");
        sys_closesocket(sockfd);
        return (0);
    }
    x = (t_netreceive *)pd_new(netreceive_class);
    if (old)
    {
        /* old style, nonsecure version */
        x->x_msgout = 0;
    }
    else x->x_msgout = outlet_new(&x->x_obj, &s_anything);

    if (udp)        /* datagram protocol */
    {
        t_socketreceiver *y = socketreceiver_new((void *)x, 
            (t_socketnotifier)netreceive_notify,
                (x->x_msgout ? netreceive_doit : 0), 1);
        sys_addpollfn(sockfd, (t_fdpollfn)socketreceiver_read, y);
        x->x_connectout = 0;
    }
    else        /* streaming protocol */
    {
        if (listen(sockfd, 5) < 0)
        {
            sys_sockerror("listen");
            sys_closesocket(sockfd);
            sockfd = -1;
        }
        else
        {
            sys_addpollfn(sockfd, (t_fdpollfn)netreceive_connectpoll, x);
            x->x_connectout = outlet_new(&x->x_obj, &s_float);
        }
    }
    x->x_connectsocket = sockfd;
    x->x_nconnections = 0;
    x->x_udp = udp;

    return (x);
}