From nobody@FreeBSD.org  Mon Sep 17 10:30:57 2001
Return-Path: <nobody@FreeBSD.org>
Received: from freefall.freebsd.org (freefall.freebsd.org [216.136.204.21])
	by hub.freebsd.org (Postfix) with ESMTP id CD45F37B406
	for <freebsd-gnats-submit@FreeBSD.org>; Mon, 17 Sep 2001 10:30:56 -0700 (PDT)
Received: (from nobody@localhost)
	by freefall.freebsd.org (8.11.4/8.11.4) id f8HHUub00713;
	Mon, 17 Sep 2001 10:30:56 -0700 (PDT)
	(envelope-from nobody)
Message-Id: <200109171730.f8HHUub00713@freefall.freebsd.org>
Date: Mon, 17 Sep 2001 10:30:56 -0700 (PDT)
From: Brad Knotwell <knotwell@ix.netcom.com>
To: freebsd-gnats-submit@FreeBSD.org
Subject: kevent.data value incorrect for UDP sockets
X-Send-Pr-Version: www-1.0

>Number:         30634
>Category:       kern
>Synopsis:       kevent.data value incorrect for UDP sockets
>Confidential:   no
>Severity:       non-critical
>Priority:       medium
>Responsible:    kbyanc
>State:          closed
>Quarter:        
>Keywords:       
>Date-Required:  
>Class:          sw-bug
>Submitter-Id:   current-users
>Arrival-Date:   Mon Sep 17 10:40:03 PDT 2001
>Closed-Date:    Sat Jan 03 13:45:07 PST 2004
>Last-Modified:  Sat Jan 03 13:45:07 PST 2004
>Originator:     Brad Knotwell
>Release:        4.2
>Organization:
home
>Environment:
FreeBSD freebsd 4.2-RELEASE FreeBSD 4.2-RELEASE #3: Thu Jul 19
     root@freebsd:/usr/src/sys/compile/ALPHA-SVR  i386

>Description:
When kevent returns a read event for a UDP socket, the data field (AKA the number of bytes to be read) is 16 bytes too long.

NOTE:  the problem exists in 4.3 release as well.

>How-To-Repeat:
1)  Compile up the following discard server 
2)  Run nc -u 127.0.0.1 6060
3)  Send data to netcat and watch the server's output

It should be consistently off by sixteen bytes.  NOTE:  this may be a documentation bug and "the number of bytes in the socket buffer" needs to be explained further.   

#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/event.h>

/* loopback discardserver on port 6060 */
int main(void)
{
    int queue,sock;
    struct sockaddr_in saddr;
    struct kevent evt;

    memset(&saddr,0,sizeof(struct sockaddr_in));
    saddr.sin_family = AF_INET;
    saddr.sin_port = htons(6060);
    saddr.sin_addr.s_addr = htonl(INADDR_LOOPBACK);

    memset(&evt,0,sizeof(struct kevent));
    evt.filter = EVFILT_READ;
    evt.flags =  EV_ADD;

    if(((queue = kqueue()) == -1) ||
       ((evt.ident = (sock = socket(AF_INET,SOCK_DGRAM,0))) == -1) ||
       (bind(sock,(struct sockaddr *)&saddr,sizeof saddr)) ||
       (kevent(queue,&evt,1,NULL,0,NULL)))
        return(fprintf(stderr,"bad setup\n"),EXIT_FAILURE);

    while(1){
        char *buf;

        if((kevent(queue,NULL,0,&evt,1,NULL) == -1) ||
           (!evt.data))
            break;

        if((buf = malloc(evt.data)) == NULL) continue;

        recv(sock,buf,evt.data,0);
        printf("size to read(%d) : data : %s\n",evt.data,buf);

        free(buf);
    }

    return(EXIT_SUCCESS);
}

>Fix:

>Release-Note:
>Audit-Trail:
State-Changed-From-To: open->patched 
State-Changed-By: kbyanc 
State-Changed-When: Fri Nov 1 13:38:48 PST 2002 
State-Changed-Why:  
Fix committed to -current.  Thanks! 


Responsible-Changed-From-To: freebsd-bugs->kbyanc 
Responsible-Changed-By: kbyanc 
Responsible-Changed-When: Fri Nov 1 13:38:48 PST 2002 
Responsible-Changed-Why:  
I'll take this so I don't forget the MFC. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=30634 
State-Changed-From-To: patched->closed 
State-Changed-By: kbyanc 
State-Changed-When: Sat Jan 3 13:42:31 PST 2004 
State-Changed-Why:  
I feel it has gotten to late in the FreeBSD 4.x branch's life cycle to  
worry about MFC'ing this now.  It is in FreeBSD 5.0 and later. 

http://www.freebsd.org/cgi/query-pr.cgi?pr=30634 
>Unformatted:
