roblem related to sockets in FreeBSD Kernel
Julian Elischer
julian at elischer.org
Mon Dec 20 11:35:51 PST 2004
look at the netgraph ng_ksocket code..
In fact you may find that you can use it directly.
Some people have written their own in-kernel httpd using this and netgraph.
aditya eipl wrote:
> Hi,
> I am writing a socket server in the kernel mode in FreeBSD 4.6.2. This
> program accepts connection and receives data on the socket. But we are
> not getting the data.In the code: We come to know that the we are
> connected we also get the notification for receipt of data but are
> unable to fetch the data. Can anybody please let me know what is wrong
> in my code? Here are the code snippets:
>
> After calling soaccept()
> I have:
> so->so_upcallarg = (caddr_t)upcallargs;
> so->so_upcall = socket_incoming;
> so->so_rcv.sb_flags |= SB_UPCALL;
> so->so_snd.sb_flags |= SB_UPCALL;
>
> I dont know whether this is required. But as I saw this in the FreeBSD
> source I put it.
>
> I have written socket_incoming as follows:
> void socket_incoming(struct socket *so, void *arg, int waitflag)
> {
> log(LOG_ALERT, "hello: socket_incoming%x \n",so->so_state);
> if(so->so_state & SS_ISCONNECTED)
> server_receive(so);
> return;
> }
> I have written server receiveas follows:
> int server_receive(struct socket *so)
> {
> struct uio uio;
> struct iovec uiovec[2];
> struct mbuf *mp;
> int err,i;
> int flags, s;
> struct socket *head = so;
> struct sockaddr *sa = NULL;
> char buff[MAX_BYTES_TO_PROCESS];
> log(LOG_ALERT, "hello: connected now try to read data (%x %x)
> \n", so, socket_server);
> flags = MSG_DONTWAIT ;
>
> uiovec[0].iov_base = buff;
> uiovec[0].iov_len = MAX_BYTES_TO_PROCESS;
> uiovec[1].iov_base = NULL;
> uiovec[1].iov_len = 0;
>
> uio.uio_iov = &uiovec[0];
> uio.uio_iovcnt = 1;
> uio.uio_offset = -1;
> uio.uio_resid = MAX_BYTES_TO_PROCESS;
> uio.uio_segflg = UIO_SYSSPACE;
> uio.uio_rw = UIO_READ;
> uio.uio_procp = p;
> err = -1;
> {
> log(LOG_ALERT, "hello: Calling soreceive\n");
> err = soreceive(so, (so->so_state & SS_ISCONNECTED) ? NULL :
> &sa, &uio, &mp, NULL, &flags);
> /* err = so->so_proto->pr_usrreqs->pru_soreceive(so,
> (so->so_state & SS_ISCONNECTED) ? NULL : &sa,
> &uio, NULL, 0, &flags); */
> /* one of the above should work but both of them do not work :( */
> if (err < 0) {
> log(LOG_ALERT, "hello: socket receive err \n");
> goto sock_server_exit;
> }
> log(LOG_ALERT, "hello: socket receive err = %d \n", err);
>
> }
> log(LOG_ALERT, "hello: iolen = %d \n data :
> ",uio.uio_iov[0].iov_len);
> for(i=0;i< uio.uio_resid;i++) {
> log(LOG_ALERT, "%c
> ",uio.uio_iov[0].iov_base[MAX_BYTES_TO_PROCESS - i]);
> }
> /* we see garbage here :( */
> log(LOG_ALERT, "hello: data = %c
> \n",uio.uio_iov[0].iov_base[uio.uio_resid-3]);
> log(LOG_ALERT, "hello: iovcnt = %d \n",uio.uio_iovcnt);
> log(LOG_ALERT, "hello: offset = %d \n",uio.uio_offset);
> log(LOG_ALERT, "hello: resid = %d \n",uio.uio_resid);
> log(LOG_ALERT, "hello: rw = %d \n",uio.uio_rw);
>
> log(LOG_ALERT, "hello: msg header iovcnt = %s \n",(char *)mp->m_dat);
> sock_server_exit:
> return 0;
> }
>
> Any help will be much appreciated.
>
> _________________________________________________________________
> Searching for your soulmate? Zero in on the perfect choice.
> http://www.astroyogi.com/newmsn/astrodate/ Try MSN Astrodate now!
>
> _______________________________________________
> freebsd-hackers at freebsd.org mailing list
> http://lists.freebsd.org/mailman/listinfo/freebsd-hackers
> To unsubscribe, send any mail to
> "freebsd-hackers-unsubscribe at freebsd.org"
More information about the freebsd-hackers
mailing list