Date: Tue, 21 Dec 2010 18:53:42 +0330 From: Mohammad Hedayati <hedayati.mo@gmail.com> To: freebsd-net@freebsd.org Subject: Re: SOCK_STREAM socket in kernel space Message-ID: <AANLkTikEbXKVRc5wGf1TvEMACHopeX4rAwwxwhfO2cTK@mail.gmail.com> In-Reply-To: <201012210753.43916.jhb@freebsd.org> References: <AANLkTinL8HRex1FBi5fmbi216rW256mJc7nBu9J1%2B=OC@mail.gmail.com> <201012210753.43916.jhb@freebsd.org>
next in thread | previous in thread | raw e-mail | index | archive | help
On Tue, Dec 21, 2010 at 4:23 PM, John Baldwin <jhb@freebsd.org> wrote: > On Tuesday, December 21, 2010 6:39:26 am Mohammad Hedayati wrote: >> I'm about to use a char device for a kind of distributed processing, >> so I've coded the open function as follows. The problem is that >> soaccept returns 0 without populating the raddr. I've checked netstat, >> everything seems to be fine, the socket is created, bound and the >> state is LISTENING. Even the remote is connection is ESTABLISHED. But, >> it cannot receive anything. it says that socket =C2=A0(sock variable) is >> not connected. > > Yes, you are calling soaccept() on the wrong socket. =C2=A0You need to wa= it for a > connection and dequeue the socket that connected and then call soaccept()= on > that new socket. =C2=A0Look at kern_accept() in sys/kern/uipc_syscalls.c. > > I wonder though if you wouldn't rather be calling soconnect instead? =C2= =A0Do you > really need to listen for new connections? > > -- > John Baldwin > Thanks, I have corrected it as follows. It now works fine. int open(struct cdev *dev, int flag, int otyp, struct thread *td) { uprintf("in open...\n"); =09 int error =3D -1;=09 socktd =3D td; =09 error =3D socreate(AF_INET, &sock, SOCK_STREAM, IPPROTO_TCP, td->td_proc->p_ucred, socktd); if(error !=3D 0) return error; =09 sockaddr.sin_len =3D sizeof(struct sockaddr_in); sockaddr.sin_family =3D AF_INET; sockaddr.sin_port =3D htons(1234); sockaddr.sin_addr.s_addr =3D INADDR_ANY; error =3D sobind(sock, (struct sockaddr *)&sockaddr, socktd); uprintf("sobind error =3D %d\n", error); =09 error =3D solisten(sock, 5, socktd); uprintf("solisten error =3D %d\n", error); =09 /* Accepting the connection */=09 ACCEPT_LOCK(); // Block, waiting for connection ... while (TAILQ_EMPTY(&sock->so_comp) && sock->so_error =3D=3D 0) { // Check if the connection is already aborted? if (sock->so_rcv.sb_state & SBS_CANTRCVMORE) { sock->so_error =3D ECONNABORTED; error =3D sock->so_error; sock->so_error =3D 0; ACCEPT_UNLOCK(); return(error); } error =3D msleep(&sock->so_timeo, &accept_mtx, PSOCK | PCATCH, "accept", = 0); if (error) { ACCEPT_UNLOCK(); return(error); } } rsock =3D TAILQ_FIRST(&sock->so_comp); SOCK_LOCK(rsock); soref(rsock); TAILQ_REMOVE(&sock->so_comp, rsock, so_list); sock->so_qlen--; rsock->so_state |=3D (sock->so_state & SS_NBIO); rsock->so_qstate &=3D ~SQ_COMP; rsock->so_head =3D NULL; SOCK_UNLOCK(rsock); ACCEPT_UNLOCK();=09 error =3D soaccept(rsock, (struct sockaddr **)&raddr); uprintf("soaccept error =3D %d, ip=3D%s\n", error, inet_ntoa(raddr->sin_ad= dr)); =09 uprintf("out open...\n"); =09 return(error); }
Want to link to this message? Use this URL: <https://mail-archive.FreeBSD.org/cgi/mid.cgi?AANLkTikEbXKVRc5wGf1TvEMACHopeX4rAwwxwhfO2cTK>