int
init_sockets(void)
{
	int error;

	if ((error = socreate(PF_INET, &socket_afnet, SOCK_DGRAM, 0,
	    curlwp, NULL)) != 0)
		return error;
	if ((error = socreate(PF_ROUTE, &routeso, SOCK_RAW, 0,
	    curlwp, NULL)) != 0)
		return error;
	return 0;
}
Example #2
0
int
pipe1(struct lwp *l, register_t *retval, int flags)
{
	file_t		*rf, *wf;
	struct socket	*rso, *wso;
	int		fd, error;
	proc_t		*p;

	if (flags & ~(O_CLOEXEC|O_NONBLOCK|O_NOSIGPIPE))
		return EINVAL;
	p = curproc;
	if ((error = socreate(AF_LOCAL, &rso, SOCK_STREAM, 0, l, NULL)) != 0)
		return error;
	if ((error = socreate(AF_LOCAL, &wso, SOCK_STREAM, 0, l, rso)) != 0)
		goto free1;
	/* remember this socket pair implements a pipe */
	wso->so_state |= SS_ISAPIPE;
	rso->so_state |= SS_ISAPIPE;
	if ((error = fd_allocfile(&rf, &fd)) != 0)
		goto free2;
	retval[0] = fd;
	rf->f_flag = FREAD | flags;
	rf->f_type = DTYPE_SOCKET;
	rf->f_ops = &socketops;
	rf->f_socket = rso;
	if ((error = fd_allocfile(&wf, &fd)) != 0)
		goto free3;
	wf->f_flag = FWRITE | flags;
	wf->f_type = DTYPE_SOCKET;
	wf->f_ops = &socketops;
	wf->f_socket = wso;
	retval[1] = fd;
	solock(wso);
	error = unp_connect2(wso, rso);
	sounlock(wso);
	if (error != 0)
		goto free4;
	fd_affix(p, wf, (int)retval[1]);
	fd_affix(p, rf, (int)retval[0]);
	return (0);
 free4:
	fd_abort(p, wf, (int)retval[1]);
 free3:
	fd_abort(p, rf, (int)retval[0]);
 free2:
	(void)soclose(wso);
 free1:
	(void)soclose(rso);
	return error;
}
Example #3
0
static int slirp_state_load(QEMUFile *f, void *opaque, int version_id)
{
    struct ex_list *ex_ptr;
    int r;

    while ((r = qemu_get_byte(f))) {
        int ret;
        struct socket *so = socreate();

        if (!so)
            return -ENOMEM;

        ret = slirp_socket_load(f, so);

        if (ret < 0)
            return ret;

        if ((so->so_faddr_ip & 0xffffff00) != special_addr_ip)
            return -EINVAL;

        for (ex_ptr = exec_list; ex_ptr; ex_ptr = ex_ptr->ex_next)
            if (ex_ptr->ex_pty == 3 &&
                    (so->so_faddr_ip & 0xff) == ex_ptr->ex_addr &&
                    so->so_faddr_port == ex_ptr->ex_fport)
                break;

        if (!ex_ptr)
            return -EINVAL;

        so->extra = (void *)ex_ptr->ex_exec;
    }

    return 0;
}
Example #4
0
static int
makesocket(struct lwp *l, file_t **fp, int *fd, int flags, int type,
    int domain, int proto, struct socket *soo)
{
	struct socket *so;
	int error;

	if ((error = socreate(domain, &so, type, proto, l, soo)) != 0) {
		return error;
	}
	if (flags & SOCK_NONBLOCK) {
		so->so_state |= SS_NBIO;
	}

	if ((error = fd_allocfile(fp, fd)) != 0) {
		soclose(so);
		return error;
	}
	fd_set_exclose(l, *fd, (flags & SOCK_CLOEXEC) != 0);
	(*fp)->f_flag = FREAD|FWRITE|
	    ((flags & SOCK_NONBLOCK) ? FNONBLOCK : 0)|
	    ((flags & SOCK_NOSIGPIPE) ? FNOSIGPIPE : 0);
	(*fp)->f_type = DTYPE_SOCKET;
	(*fp)->f_ops = &socketops;
	(*fp)->f_socket = so;
	return 0;
}
Example #5
0
/** Nasty hacky function used to debug default gateways. Prints out the
 * default gateway when called (ish). Definitely only for debugging.
 */
void def_gw_get()
{
    struct socket *so = NULL;
    struct {
        struct rt_msghdr msg;
        unsigned char space[512];
    } rtm_msg;
    char buf[1024];
    size_t buflen = 1024;
    struct rt_msghdr *r;
    struct sockaddr *sa;
    int i, datalen;

#define rtm rtm_msg.msg
    
    assert(                       // IPPROTO_IP
	socreate(PF_ROUTE, &so, SOCK_RAW, AF_INET, proc0.p_ucred, curthread)
	== 0);

    memset(&rtm_msg, 0, sizeof(rtm_msg));
    
    rtm.rtm_msglen = 168; // hmm...
    rtm.rtm_version = RTM_VERSION; 
    rtm.rtm_type = RTM_GET;
    rtm.rtm_flags = 2051; // hmm... what are these???
    rtm.rtm_addrs = 21; // == 21
    rtm.rtm_seq = 2;
    rtm.rtm_inits = 0;
    //rtm.rtm_pid = 0;
    //msg.rtm_index = 0;  
    
    // This is seriously hax
    rtm_msg.space[0] = 10; 
    rtm_msg.space[1] = 2; 
    rtm_msg.space[16] = 2; 
    rtm_msg.space[17] = 2; 
    
    // IP addr
    rtm_msg.space[20] = 36; 
    rtm_msg.space[21] = 12; 
    
    datalen = 168;
    assert(
            nsc_sosend(so, NULL, &rtm_msg, &datalen) == 0
          );

    assert(
            nsc_soreceive_blocking(so, buf, &buflen, NULL) == 0
          );

    r = (struct rt_msghdr *)buf;
    sa = (struct sockaddr *)(r + 1);
    printf("%s: read says:\n", __FUNCTION__);
    for(i = 0; i < (r->rtm_msglen - sizeof(struct rt_msghdr)); i++) {
        printf("%02hhx ", ((unsigned char *)sa)[i]);
    }

    soclose(so);
    sofree(so);
}
Example #6
0
struct socket * soCloneUDPSocketWithForegnAddr(PNATState pData, bool fBindSocket, struct socket *pSo, uint32_t u32ForeignAddr)
{
    struct socket *pNewSocket = NULL;
    LogFlowFunc(("Enter: fBindSocket:%RTbool, so:%R[natsock], u32ForeignAddr:%RTnaipv4\n", fBindSocket, pSo, u32ForeignAddr));
    pNewSocket = socreate();
    if (!pNewSocket)
    {
        LogFunc(("Can't create socket\n"));
        LogFlowFunc(("Leave: NULL\n"));
        return NULL;
    }
    if (fBindSocket)
    {
        if (udp_attach(pData, pNewSocket, 0) <= 0)
        {
            sofree(pData, pNewSocket);
            LogFunc(("Can't attach fresh created socket\n"));
            return NULL;
        }
    }
    else
    {
        pNewSocket->so_cloneOf = (struct socket *)pSo;
        pNewSocket->s = pSo->s;
        insque(pData, pNewSocket, &udb);
    }
    pNewSocket->so_laddr = pSo->so_laddr;
    pNewSocket->so_lport = pSo->so_lport;
    pNewSocket->so_faddr.s_addr = u32ForeignAddr;
    pNewSocket->so_fport = pSo->so_fport;
    pSo->so_cCloneCounter++;
    LogFlowFunc(("Leave: %R[natsock]\n", pNewSocket));
    return pNewSocket;
}
Example #7
0
static int     
bsd_socket(cyg_nstab_entry *nste, int domain, int type,
           int protocol, cyg_file *file)
{
    int error = 0;
    struct socket *so;

    error = socreate(domain, &so, type, protocol, (struct proc *)&proc0);

    if( error == ENOERR)
    {

        cyg_selinit(&so->so_rcv.sb_sel);
        cyg_selinit(&so->so_snd.sb_sel);
        
        file->f_flag   |= CYG_FREAD|CYG_FWRITE;
        file->f_type    = CYG_FILE_TYPE_SOCKET;
        file->f_ops     = &bsd_sock_fileops;
        file->f_offset  = 0;
        file->f_data    = (CYG_ADDRWORD)so;
        file->f_xops    = (CYG_ADDRWORD)&bsd_sockops;
    }
    
    return error;
}
Example #8
0
static int
ff_veth_setaddr(struct ff_veth_softc *sc)
{
    struct in_aliasreq req;
    bzero(&req, sizeof req);
    strcpy(req.ifra_name, sc->ifp->if_dname);

    struct sockaddr_in sa;
    bzero(&sa, sizeof(sa));
    sa.sin_len = sizeof(sa);
    sa.sin_family = AF_INET;
    sa.sin_addr.s_addr = sc->ip;
    bcopy(&sa, &req.ifra_addr, sizeof(sa));

    sa.sin_addr.s_addr = sc->netmask;
    bcopy(&sa, &req.ifra_mask, sizeof(sa));

    sa.sin_addr.s_addr = sc->broadcast;
    bcopy(&sa, &req.ifra_broadaddr, sizeof(sa));

    struct socket *so = NULL;
    socreate(AF_INET, &so, SOCK_DGRAM, 0, curthread->td_ucred, curthread);
    int ret = ifioctl(so, SIOCAIFADDR, (caddr_t)&req, curthread);

    sofree(so);

    return ret;
}
Example #9
0
int
nfs_boot_deladdress(struct ifnet *ifp, struct lwp *lwp, uint32_t addr)
{
	struct socket *so;
	struct ifreq ifr;
	struct sockaddr_in sin;
	struct in_addr ia = {.s_addr = addr};
	int error;

	/*
	 * Get a socket to use for various things in here.
	 * After this, use "goto out" to cleanup and return.
	 */
	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
	if (error) {
		printf("deladdress: socreate, error=%d\n", error);
		return (error);
	}

	memset(&ifr, 0, sizeof(ifr));
	memcpy(ifr.ifr_name, ifp->if_xname, IFNAMSIZ);

	sockaddr_in_init(&sin, &ia, 0);
	ifreq_setaddr(SIOCDIFADDR, &ifr, sintocsa(&sin)); 

	error = ifioctl(so, SIOCDIFADDR, &ifr, lwp);
	if (error) {
		printf("deladdress, error=%d\n", error);
		goto out;
	}

out:
	soclose(so);
	return (error);
}
Example #10
0
/** Creates a default route to the address specified. Creates and uses a
 * routing socket to do such.
 */
int def_gw_init(unsigned int gw_addr)
{
    struct socket *so = NULL;
    struct {
        struct rt_msghdr msg;
        unsigned char space[512];
    } rtm_msg;
    struct sockaddr_in *dst, *gtw, *mask;
    int i, datalen;

#define rtm rtm_msg.msg
    
    assert(                       // IPPROTO_IP
	socreate(PF_ROUTE, &so, SOCK_RAW, AF_INET, proc0.p_ucred, curthread)
	== 0);

    memset(&rtm_msg, 0, sizeof(rtm_msg));
    
    rtm.rtm_msglen = 128; // hmm...
    rtm.rtm_version = RTM_VERSION; 
    rtm.rtm_type = RTM_ADD;
    rtm.rtm_flags = 2051; // hmm... what are these???
    rtm.rtm_addrs = RTA_DST | RTA_GATEWAY | RTA_NETMASK; // == 7
    rtm.rtm_seq = 1;
    rtm.rtm_inits = 0;
    //rtm.rtm_pid = 0;
    //msg.rtm_index = 0;  
    
    dst = (struct sockaddr_in *)rtm_msg.space;
    dst->sin_len = sizeof(struct sockaddr_in);
    dst->sin_family = AF_INET;

    gtw = (struct sockaddr_in *)&rtm_msg.space[ ROUNDUP(dst->sin_len) ];
    gtw->sin_len = sizeof(struct sockaddr_in);
    gtw->sin_family = AF_INET;
    gtw->sin_addr.s_addr = gw_addr;
        
    mask = (struct sockaddr_in *)&rtm_msg.space[ ROUNDUP(dst->sin_len) +
        ROUNDUP(gtw->sin_len) ];
    mask->sin_len = sizeof(struct sockaddr_in);
    mask->sin_family = AF_INET;

    rtm.rtm_msglen = sizeof(struct rt_msghdr) + ROUNDUP(dst->sin_len)*3;

    /*for(i = 0; i < rtm.rtm_msglen - sizeof(struct rt_msghdr); i++)
        printf("%0hhx ", rtm_msg.space[i]);
    printf("\n");*/

    datalen = rtm.rtm_msglen;

    assert(
            nsc_sosend(so, NULL, &rtm_msg, &datalen)
            == 0
          );

    soclose(so); // calls sofree for us
    //def_gw_get();
}
Example #11
0
int
nfs_boot_setaddress(struct ifnet *ifp, struct lwp *lwp,
		uint32_t addr, uint32_t netmask, uint32_t braddr)
{
	struct socket *so;
	struct ifaliasreq iareq;
	struct sockaddr_in *sin;
	int error;

	/*
	 * Get a socket to use for various things in here.
	 * After this, use "goto out" to cleanup and return.
	 */
	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
	if (error) {
		printf("setaddress: socreate, error=%d\n", error);
		return (error);
	}

	memset(&iareq, 0, sizeof(iareq));
	memcpy(iareq.ifra_name, ifp->if_xname, IFNAMSIZ);

	/* Set the I/F address */
	sin = (struct sockaddr_in *)&iareq.ifra_addr;
	sin->sin_len = sizeof(*sin);
	sin->sin_family = AF_INET;
	sin->sin_addr.s_addr = addr;

	/* Set the netmask */
	if (netmask != INADDR_ANY) {
		sin = (struct sockaddr_in *)&iareq.ifra_mask;
		sin->sin_len = sizeof(*sin);
		sin->sin_family = AF_INET;
		sin->sin_addr.s_addr = netmask;
	} /* else leave subnetmask unspecified (len=0) */

	/* Set the broadcast addr. */
	if (braddr != INADDR_ANY) {
		sin = (struct sockaddr_in *)&iareq.ifra_broadaddr;
		sin->sin_len = sizeof(*sin);
		sin->sin_family = AF_INET;
		sin->sin_addr.s_addr = braddr;
	} /* else leave broadcast addr unspecified (len=0) */

	error = ifioctl(so, SIOCAIFADDR, (void *)&iareq, lwp);
	if (error) {
		printf("setaddress, error=%d\n", error);
		goto out;
	}

	/* give the link some time to get up */
	tsleep(nfs_boot_setaddress, PZERO, "nfsbtd", 3 * hz);
out:
	soclose(so);
	return (error);
}
Example #12
0
/*
 * XXX This should really be tcp_listen
 */
struct socket *
solisten(u_int port, u_int32_t laddr, u_int lport, int flags)
{
	SockAddress  addr;
	uint32_t     addr_ip;
	struct socket *so;
	int s;

	DEBUG_CALL("solisten");
	DEBUG_ARG("port = %d", port);
	DEBUG_ARG("laddr = %x", laddr);
	DEBUG_ARG("lport = %d", lport);
	DEBUG_ARG("flags = %x", flags);

	if ((so = socreate()) == NULL) {
	  /* free(so);      Not sofree() ??? free(NULL) == NOP */
	  return NULL;
	}

	/* Don't tcp_attach... we don't need so_snd nor so_rcv */
	if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
		free(so);
		return NULL;
	}
	insque(so,&tcb);

	/*
	 * SS_FACCEPTONCE sockets must time out.
	 */
	if (flags & SS_FACCEPTONCE)
	   so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;

	so->so_state      = (SS_FACCEPTCONN|flags);
	so->so_laddr_port = lport; /* Kept in host format */
    so->so_laddr_ip   = laddr; /* Ditto */
    so->so_haddr_port = port;

    s = socket_loopback_server( port, SOCKET_STREAM );
    if (s < 0)
        return NULL;

    socket_get_address(s, &addr);

	so->so_faddr_port = sock_address_get_port(&addr);

    addr_ip = (uint32_t) sock_address_get_ip(&addr);

    if (addr_ip == 0 || addr_ip == loopback_addr_ip)
        so->so_faddr_ip = alias_addr_ip;
    else
        so->so_faddr_ip = addr_ip;

	so->s = s;
	return so;
}
Example #13
0
static int
icl_conn_connect_tcp(struct icl_conn *ic, int domain, int socktype,
    int protocol, struct sockaddr *from_sa, struct sockaddr *to_sa)
{
	struct socket *so;
	int error;
	int interrupted = 0;

	error = socreate(domain, &so, socktype, protocol,
	    curthread->td_ucred, curthread);
	if (error != 0)
		return (error);

	if (from_sa != NULL) {
		error = sobind(so, from_sa, curthread);
		if (error != 0) {
			soclose(so);
			return (error);
		}
	}

	error = soconnect(so, to_sa, curthread);
	if (error != 0) {
		soclose(so);
		return (error);
	}

	SOCK_LOCK(so);
	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
		error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
		    "icl_connect", 0);
		if (error) {
			if (error == EINTR || error == ERESTART)
				interrupted = 1;
			break;
		}
	}
	if (error == 0) {
		error = so->so_error;
		so->so_error = 0;
	}
	SOCK_UNLOCK(so);

	if (error != 0) {
		soclose(so);
		return (error);
	}

	error = icl_conn_handoff_sock(ic, so);
	if (error != 0)
		soclose(so);

	return (error);
}
Example #14
0
struct socket* listenon(unsigned short port)
{
  struct socket* so = NULL;
  socreate(AF_INET, &so, SOCK_STREAM, 0);
  struct sockaddr_in addr;
  bzero(&addr, sizeof addr);
  addr.sin_len = sizeof addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  struct mbuf* nam = m_devget((caddr_t)&addr, sizeof addr, 0, NULL, NULL);
  sobind(so, nam);
  solisten(so, 5);
  return so;
}
Example #15
0
/*
 * XXX Need to implement reconnecting as necessary.  If that were to be
 *     needed, most likely all current vnodes would have to be renegotiated
 *     or otherwise invalidated (a la NFS "stale file handle").
 */
static int
p9fs_connect(struct mount *mp)
{
	struct p9fsmount *p9mp = VFSTOP9(mp);
	struct p9fs_session *p9s = &p9mp->p9_session;
	struct socket *so;
	int error;

	error = socreate(p9s->p9s_sockaddr.sa_family, &p9s->p9s_sock,
	    p9s->p9s_socktype, p9s->p9s_proto, curthread->td_ucred, curthread);
	if (error != 0) {
		vfs_mount_error(mp, "socreate");
		goto out;
	}

	so = p9s->p9s_sock;
	error = soconnect(so, &p9s->p9s_sockaddr, curthread);
	SOCK_LOCK(so);
	while ((so->so_state & SS_ISCONNECTING) && so->so_error == 0) {
		error = msleep(&so->so_timeo, SOCK_MTX(so), PSOCK | PCATCH,
		    "connec", 0);
		if (error)
			break;
	}
	if (error == 0) {
		error = so->so_error;
		so->so_error = 0;
	}
	SOCK_UNLOCK(so);
	if (error) {
		vfs_mount_error(mp, "soconnect");
		if (error == EINTR)
			so->so_state &= ~SS_ISCONNECTING;
		goto out;
	}

	if (so->so_proto->pr_flags & PR_CONNREQUIRED)
		p9fs_setsockopt(so, SO_KEEPALIVE);
	if (so->so_proto->pr_protocol == IPPROTO_TCP)
		p9fs_setsockopt(so, TCP_NODELAY);

	SOCKBUF_LOCK(&so->so_rcv);
	soupcall_set(so, SO_RCV, p9fs_client_upcall, p9mp);
	SOCKBUF_UNLOCK(&so->so_rcv);

	error = 0;

out:
	return (error);
}
Example #16
0
errno_t	xi_sock_socket(int domain, int type, int protocol, sock_upcall callback, void *cookie, xi_socket_t *new_so)
{
#ifdef __KPI_SOCKET__
	return sock_socket(domain, type, protocol, callback, cookie, new_so);
#else
	thread_funnel_set(network_flock, TRUE);
	errno_t	error;
	
	error = socreate(domain, new_so, type, protocol); 
	
	(void)thread_funnel_set(network_flock, FALSE);
	
	return error;
#endif	
}
Example #17
0
/*
 * Create a socket and call ifioctl() to configure the interface.
 * This trickles down to virtif_ioctl().
 */
static int
configaddr(struct ifnet *ifp, struct ifaliasreq *ia)
{
	struct socket *so;
	int error;

	strcpy(ia->ifra_name, ifp->if_xname);
	error = socreate(ia->ifra_addr.sa_family, &so, SOCK_DGRAM,
	    0, curlwp, NULL);
	if (error)
		return error;
	error = ifioctl(so, SIOCAIFADDR, ia, curlwp);
	soclose(so);

	return error;
}
Example #18
0
void handshake()
{
  int port = 1234;
  listenon(port);
  struct socket* so = NULL;
  socreate(AF_INET, &so, SOCK_STREAM, 0);
  struct sockaddr_in addr;
  bzero(&addr, sizeof addr);
  addr.sin_len = sizeof addr;
  addr.sin_family = AF_INET;
  addr.sin_port = htons(port);
  addr.sin_addr.s_addr = htonl(0x7f000001);
  struct mbuf* nam = m_devget((caddr_t)&addr, sizeof addr, 0, NULL, NULL);
  soconnect(so, nam);
  ipintr();
}
Example #19
0
int
nfs_boot_ifupdown(struct ifnet *ifp, struct lwp *lwp, int up)
{
	struct socket *so;
	struct ifreq ireq;
	int error;

	memset(&ireq, 0, sizeof(ireq));
	memcpy(ireq.ifr_name, ifp->if_xname, IFNAMSIZ);

	/*
	 * Get a socket to use for various things in here.
	 * After this, use "goto out" to cleanup and return.
	 */
	error = socreate(AF_INET, &so, SOCK_DGRAM, 0, lwp, NULL);
	if (error) {
		printf("ifupdown: socreate, error=%d\n", error);
		return (error);
	}

	/*
	 * Bring up the interface. (just set the "up" flag)
	 * Get the old interface flags and or IFF_UP into them so
	 * things like media selection flags are not clobbered.
	 */
	error = ifioctl(so, SIOCGIFFLAGS, (void *)&ireq, lwp);
	if (error) {
		printf("ifupdown: GIFFLAGS, error=%d\n", error);
		goto out;
	}
	if (up)
		ireq.ifr_flags |= IFF_UP;
	else
		ireq.ifr_flags &= ~IFF_UP;
	error = ifioctl(so, SIOCSIFFLAGS, &ireq, lwp);
	if (error) {
		printf("ifupdown: SIFFLAGS, error=%d\n", error);
		goto out;
	}

	if (up)
		/* give the link some time to get up */
		tsleep(nfs_boot_ifupdown, PZERO, "nfsbif", 3 * hz);
out:
	soclose(so);
	return (error);
}
/**
 * Returns: 0 on success, a positive value on error
 *
 *		EACCES (from socreate)
 *		EPERM (from socreate)
 *		EADDRNOTAVAIL (from connect)
 *		ETIMEDOUT (from connect)
 *		...
 *
 *  Rare errors:
 *		EPROTONOSUPPORT (from socreate)
 *		EPROTOTYPE (from socreate)
 *		ENOBUFS (from socreate)
 *		EALREADY (from connect)
 *
 */
int sendMessageToSite(struct sockaddr *site, char *message,
		struct sockaddr *sender, struct thread *td) {
	int error, wrote;
	struct socket *so = NULL;

	if (site->sa_family == AF_INET) {
		struct sockaddr_in* site_in = (struct sockaddr_in*) site;
		log_debug("Sending message %s to %s:%d", message,
				inet_ntoa(site_in->sin_addr), ntohs(site_in->sin_port));
	} else {
		struct sockaddr_un* site_un = (struct sockaddr_un*) site;
		log_debug("Sending message %s to %s", message, site_un->sun_path);
	}

	error = socreate(site->sa_family, &so, SOCK_STREAM, 0, td->td_ucred, td);
	if (error) {
		log_warn("error in socreate in sendMessageToSite");
		goto bad;
	}
	error = my_kern_connect(so, (struct sockaddr *) site, td);
	if (error) {
		log_warn("error in my_kern_connect in sendMessageToSite");
		goto bad;
	}
	error = my_kern_send(so, message, strlen(message), &wrote, td);
	if (error || wrote != strlen(message)) {
		error = error || EPIPE;
		log_warn("error in my_kern_send in sendMessageToSite(1)");
		goto bad;
	}
	error = my_kern_send(so, sender, sizeof(struct sockaddr), &wrote, td);
	if (error || wrote != sizeof(struct sockaddr)) {
		error = error || EPIPE;
		log_warn("error in my_kern_send in sendMessageToSite(2)");
		goto bad;
	}

	bad: //on error
	if (error)
		log_error("Error in sendMessageToSite [%d]", error);
	if (so != NULL)
		soclose(so);
	return error;
}
Example #21
0
int
sys_socket(struct thread *td, struct socket_args *uap)
{
	struct socket *so;
	struct file *fp;
	int fd, error, type, oflag, fflag;

	AUDIT_ARG_SOCKET(uap->domain, uap->type, uap->protocol);

	type = uap->type;
	oflag = 0;
	fflag = 0;
	if ((type & SOCK_CLOEXEC) != 0) {
		type &= ~SOCK_CLOEXEC;
		oflag |= O_CLOEXEC;
	}
	if ((type & SOCK_NONBLOCK) != 0) {
		type &= ~SOCK_NONBLOCK;
		fflag |= FNONBLOCK;
	}

#ifdef MAC
	error = mac_socket_check_create(td->td_ucred, uap->domain, type,
	    uap->protocol);
	if (error != 0)
		return (error);
#endif
	error = falloc(td, &fp, &fd, oflag);
	if (error != 0)
		return (error);
	/* An extra reference on `fp' has been held for us by falloc(). */
	error = socreate(uap->domain, &so, type, uap->protocol,
	    td->td_ucred, td);
	if (error != 0) {
		fdclose(td, fp, fd);
	} else {
		finit(fp, FREAD | FWRITE | fflag, DTYPE_SOCKET, so, &socketops);
		if ((fflag & FNONBLOCK) != 0)
			(void) fo_ioctl(fp, FIONBIO, &fflag, td->td_ucred, td);
		td->td_retval[0] = fd;
	}
	fdrop(fp, td);
	return (error);
}
Example #22
0
struct socket *
udp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
           u_int lport, int flags)
{
	struct sockaddr_in addr;
	struct socket *so;
	socklen_t addrlen = sizeof(struct sockaddr_in);

	so = socreate(slirp);
	if (!so) {
	    return NULL;
	}
	so->s = qemu_socket(AF_INET,SOCK_DGRAM,0);
	so->so_expire = curtime + SO_EXPIRE;
	insque(so, &slirp->udb);

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = haddr;
	addr.sin_port = hport;

	if (bind(so->s,(struct sockaddr *)&addr, addrlen) < 0) {
		udp_detach(so);
		return NULL;
	}
	socket_set_fast_reuse(so->s);

	getsockname(so->s,(struct sockaddr *)&addr,&addrlen);
	so->so_fport = addr.sin_port;
	if (addr.sin_addr.s_addr == 0 ||
	    addr.sin_addr.s_addr == loopback_addr.s_addr) {
	   so->so_faddr = slirp->vhost_addr;
	} else {
	   so->so_faddr = addr.sin_addr;
	}
	so->so_lport = lport;
	so->so_laddr.s_addr = laddr;
	if (flags != SS_FACCEPTONCE)
	   so->so_expire = 0;

	so->so_state &= SS_PERSISTENT_MASK;
	so->so_state |= SS_ISFCONNECTED | flags;

	return so;
}
Example #23
0
long
t_socket(int family, 
   int   type, 
   int   proto)
{
   struct socket *   so;

   INET_TRACE (INETM_SOCKET, ("SOCK:sock:family %d, typ %d, proto %d\n",
    family, type, proto));
   LOCK_NET_RESOURCE(NET_RESID);
   if ((so = socreate (family, type, proto)) == NULL) 
   {  /* can't really return error info since no socket.... */
      UNLOCK_NET_RESOURCE(NET_RESID);
      return SOCKET_ERROR;
   }
   SOC_RANGE(so);
   so->so_error = 0;
   UNLOCK_NET_RESOURCE(NET_RESID);
   return SO2LONG(so);
}
Example #24
0
/*
 *********************************************************************
 *                       BSD-style entry points                      *
 *********************************************************************
 */
int
socket (int domain, int type, int protocol)
{
	int fd;
	int error;
	struct socket *so;

	rtems_bsdnet_semaphore_obtain ();
	error = socreate(domain, &so, type, protocol, NULL);
	if (error == 0) {
		fd = rtems_bsdnet_makeFdForSocket (so);
		if (fd < 0)
			soclose (so);
	}
	else {
		errno = error;
		fd = -1;
	}
	rtems_bsdnet_semaphore_release ();
	return fd;
}
Example #25
0
/* 
 * Connect to specified server via IP
 */
int
ncp_sock_connect_in(struct ncp_conn *conn) {
	struct sockaddr_in sin;
	struct thread *td = conn->td;
	int addrlen=sizeof(sin), error;

	conn->flags = 0;
	bzero(&sin,addrlen);
	conn->ncp_so = conn->wdg_so = NULL;
	checkbad(socreate(AF_INET, &conn->ncp_so, SOCK_DGRAM, IPPROTO_UDP, td));
	sin.sin_family = AF_INET;
	sin.sin_len = addrlen;
	checkbad(sobind(conn->ncp_so, (struct sockaddr *)&sin, td));
	checkbad(ncp_soconnect(conn->ncp_so,(struct sockaddr*)&conn->li.addr, td));
	if  (!error)
		conn->flags |= NCPFL_SOCONN;
	return error;
bad:
	ncp_sock_disconnect(conn);
	return (error);
}
Example #26
0
struct socket *
udp_listen(u_int port, u_int32_t laddr, u_int lport, int flags)
{
	struct socket *so;
	SockAddress    addr;
	uint32_t       addr_ip;

	if ((so = socreate()) == NULL) {
		free(so);
		return NULL;
	}
	so->s = socket_anyaddr_server( port, SOCKET_DGRAM );
	so->so_expire = curtime + SO_EXPIRE;
    so->so_haddr_port = port;
	insque(so,&udb);

	if (so->s < 0) {
		udp_detach(so);
		return NULL;
	}

        socket_get_address(so->s, &addr);

	so->so_faddr_port = sock_address_get_port(&addr);
	addr_ip = sock_address_get_ip(&addr);

	if (addr_ip == 0 || addr_ip == loopback_addr_ip)
	   so->so_faddr_ip = alias_addr_ip;
	else
	   so->so_faddr_ip = addr_ip;

	so->so_laddr_port = lport;
	so->so_laddr_ip   = laddr;
	if (flags != SS_FACCEPTONCE)
	   so->so_expire = 0;

	so->so_state = SS_ISFCONNECTED;

	return so;
}
Example #27
0
static int
uinet_ifconfig_begin(struct socket **so, struct ifreq *ifr, const char *name)
{
	struct thread *td = curthread;
	struct uinet_config_if *ifcfg;
	int error;

	ifcfg = uinet_iffind_byname(name);
	if (NULL == ifcfg) {
		printf("could not find interface %s\n", name);
		return (EINVAL);
	}

	error = socreate(PF_INET, so, SOCK_DGRAM, 0, td->td_ucred, td);
	if (0 != error) {
		printf("ifconfig socket creation failed (%d)\n", error);
		return (error);
	}

	snprintf(ifr->ifr_name, sizeof(ifr->ifr_name), "%s", ifcfg->name);
	
	return (0);
}
Example #28
0
int
sys_socket(struct proc *p, void *v, register_t *retval)
{
	struct sys_socket_args /* {
		syscallarg(int) domain;
		syscallarg(int) type;
		syscallarg(int) protocol;
	} */ *uap = v;
	struct filedesc *fdp = p->p_fd;
	struct socket *so;
	struct file *fp;
	int fd, error;

	fdplock(fdp);
	error = falloc(p, &fp, &fd);
	fdpunlock(fdp);
	if (error != 0)
		goto out;

	fp->f_flag = FREAD|FWRITE;
	fp->f_type = DTYPE_SOCKET;
	fp->f_ops = &socketops;
	error = socreate(SCARG(uap, domain), &so, SCARG(uap, type),
			 SCARG(uap, protocol));
	if (error) {
		fdplock(fdp);
		fdremove(fdp, fd);
		closef(fp, p);
		fdpunlock(fdp);
	} else {
		fp->f_data = so;
		FILE_SET_MATURE(fp, p);
		*retval = fd;
	}
out:
	return (error);
}
int waitForMessages(struct sockaddr_in *site_2, struct thread *td) {
	waiting_sockaddr = *site_2;
	struct sockaddr_in *site = &waiting_sockaddr;
	log_info("waiting for messages on %s:%d", inet_ntoa(site->sin_addr), ntohs(site->sin_port));

	int error = 0;
	struct socket *so = NULL;

	error = socreate(AF_INET, &so, SOCK_STREAM, 0, td->td_ucred, td);
	if (error) {
		log_warn("error in socreate in waitForMessages");
		goto bad;
	}
	error = sobind(so, (struct sockaddr *) site, td);
	if (error) {
		log_warn("error in sobind in waitForMessages");
		goto bad;
	}
	error = solisten(so, 5, td);
	if (error) {
		log_warn("error in solisten in waitForMessages");
		goto bad;
	}

	error = kthread_add(accept_loop, so, NULL, &accept_kthread, 0, 0,
			"raymond_accept_loop");
	if (error) {
		log_warn("error creating thread: %d\n", error);
		goto bad;
	}
	return 0;

	bad: // on error
	if (so != NULL)
		soclose(so);
	return error;
}
Example #30
0
File: socket.c Project: djs55/qemu
/*
 * Listen for incoming TCP connections
 */
struct socket *
tcp_listen(Slirp *slirp, uint32_t haddr, u_int hport, uint32_t laddr,
           u_int lport, int flags)
{
	struct sockaddr_in addr;
	struct socket *so;
	int s, opt = 1;
	socklen_t addrlen = sizeof(addr);
	memset(&addr, 0, addrlen);

	DEBUG_CALL("tcp_listen");
	DEBUG_ARG("haddr = %x", haddr);
	DEBUG_ARG("hport = %d", hport);
	DEBUG_ARG("laddr = %x", laddr);
	DEBUG_ARG("lport = %d", lport);
	DEBUG_ARG("flags = %x", flags);

	so = socreate(slirp);
	if (!so) {
	  return NULL;
	}

	/* Don't tcp_attach... we don't need so_snd nor so_rcv */
	if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) {
		free(so);
		return NULL;
	}
	insque(so, &slirp->tcb);

	/*
	 * SS_FACCEPTONCE sockets must time out.
	 */
	if (flags & SS_FACCEPTONCE)
	   so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2;

	so->so_state &= SS_PERSISTENT_MASK;
	so->so_state |= (SS_FACCEPTCONN | flags);
	so->so_lport = lport; /* Kept in network format */
	so->so_laddr.s_addr = laddr; /* Ditto */

	addr.sin_family = AF_INET;
	addr.sin_addr.s_addr = haddr;
	addr.sin_port = hport;

	if (((s = qemu_socket(AF_INET,SOCK_STREAM,0)) < 0) ||
	    (setsockopt(s,SOL_SOCKET,SO_REUSEADDR,(char *)&opt,sizeof(int)) < 0) ||
	    (bind(s,(struct sockaddr *)&addr, sizeof(addr)) < 0) ||
	    (listen(s,1) < 0)) {
		int tmperrno = errno; /* Don't clobber the real reason we failed */

		close(s);
		sofree(so);
		/* Restore the real errno */
#ifdef _WIN32
		WSASetLastError(tmperrno);
#else
		errno = tmperrno;
#endif
		return NULL;
	}
	setsockopt(s,SOL_SOCKET,SO_OOBINLINE,(char *)&opt,sizeof(int));

	getsockname(s,(struct sockaddr *)&addr,&addrlen);
	so->so_fport = addr.sin_port;
	if (addr.sin_addr.s_addr == 0 || addr.sin_addr.s_addr == loopback_addr.s_addr)
	   so->so_faddr = slirp->vhost_addr;
	else
	   so->so_faddr = addr.sin_addr;

	so->s = s;
	return so;
}