From owner-freebsd-net@FreeBSD.ORG Tue Dec 21 15:24:23 2010 Return-Path: Delivered-To: freebsd-net@freebsd.org Received: from mx1.freebsd.org (mx1.freebsd.org [IPv6:2001:4f8:fff6::34]) by hub.freebsd.org (Postfix) with ESMTP id C06D01065679 for ; Tue, 21 Dec 2010 15:24:23 +0000 (UTC) (envelope-from hedayati.mo@gmail.com) Received: from mail-wy0-f196.google.com (mail-wy0-f196.google.com [74.125.82.196]) by mx1.freebsd.org (Postfix) with ESMTP id 548F98FC22 for ; Tue, 21 Dec 2010 15:24:22 +0000 (UTC) Received: by wyb40 with SMTP id 40so1524401wyb.7 for ; Tue, 21 Dec 2010 07:24:22 -0800 (PST) DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=gmail.com; s=gamma; h=domainkey-signature:received:mime-version:received:in-reply-to :references:from:date:message-id:subject:to:content-type :content-transfer-encoding; bh=YLbat6/4ndC+aIT5vu1xno9Z5oVFSgu8/GRRBqOAC0Q=; b=EoPpVaFQLrHLPWFNlt1ICHes1l+qgoI06fS3LCwlgmTxzlKwQ35Efnxaqg/lSEtIol 2n3MKfuOs9nvVKP02IEjoNdV4WYB2MwJnPt4r9oIjRl4VR0r+GMpf+OHHjRA5zER1723 fS3/Yw0M98TmTj1wQpvToJHAQP7tqKm1edjnY= DomainKey-Signature: a=rsa-sha1; c=nofws; d=gmail.com; s=gamma; h=mime-version:in-reply-to:references:from:date:message-id:subject:to :content-type:content-transfer-encoding; b=PVFvhUR3BzLFcjPUvHmmChCsyrRARx1XHZQOS+tW7kGQwfqTOq+b3PPQ2IjBS518Q5 THaARCWnQYmqVBK4q2XTRxx4QwncBghrpZp52uuig7i2rP8OW+kXBhWY4+bfegZ5nhAM frZiFxxG/p68nxBTswtyfwVk1NDYxCJ2kliOg= Received: by 10.227.146.145 with SMTP id h17mr3469655wbv.60.1292945062098; Tue, 21 Dec 2010 07:24:22 -0800 (PST) MIME-Version: 1.0 Received: by 10.227.143.202 with HTTP; Tue, 21 Dec 2010 07:23:42 -0800 (PST) In-Reply-To: <201012210753.43916.jhb@freebsd.org> References: <201012210753.43916.jhb@freebsd.org> From: Mohammad Hedayati Date: Tue, 21 Dec 2010 18:53:42 +0330 Message-ID: To: freebsd-net@freebsd.org Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: quoted-printable Subject: Re: SOCK_STREAM socket in kernel space X-BeenThere: freebsd-net@freebsd.org X-Mailman-Version: 2.1.5 Precedence: list List-Id: Networking and TCP/IP with FreeBSD List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Tue, 21 Dec 2010 15:24:23 -0000 On Tue, Dec 21, 2010 at 4:23 PM, John Baldwin 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); }