Fixed a small bug associated with UDP length and return code form recv.
authorDavid Zhu <yuzhu@cs.berkeley.edu>
Thu, 24 Mar 2011 18:41:00 +0000 (11:41 -0700)
committerDavid Zhu <yuzhu@cs.berkeley.edu>
Mon, 2 Apr 2012 22:03:28 +0000 (15:03 -0700)
kern/src/net/udp.c
kern/src/socket.c
tests/udp_test.c

index ce78b1e..9c09477 100644 (file)
@@ -256,6 +256,7 @@ int udp_input(struct pbuf *p){
                pbuf_free(p);
                return -1;
        }
+                       printk("start of udp %p\n", p->payload);
        udphdr = (struct udp_hdr *)p->payload;
        /* convert the src port and dst port to host order */
        src = ntohs(udphdr->src_port);
index e378233..5bf35cc 100644 (file)
@@ -247,12 +247,15 @@ intreg_t sys_recvfrom(struct proc *p, int socket, void *restrict buffer, size_t
                                copied = buf->len - sizeof(struct udp_hdr);
                                if (copied > length)
                                        copied = length;
-                               
-                       pbuf_header(buf, -PBUF_TRANSPORT_HLEN);
+                       pbuf_header(buf, -UDP_HDR_SZ);
+                       printk("loc of payload %p\n", buf->payload);
                        // copy it to user space
                        returnval = memcpy_to_user_errno(p, buffer, buf->payload, copied);
                        }
                }
        }
-       return returnval;
+       if (returnval < 0) 
+               return -1;
+       else
+               return copied;
 }
index 9e1d745..ad2b767 100644 (file)
@@ -31,9 +31,7 @@ int main(int argc, char* argv[]) {
        bzero(&server, sizeof(server));
        server.sin_family = AF_INET;
        server.sin_port = htons(atoi(argv[2]));
-       server.sin_addr.s_addr = inet_addr("10.0.0.1"); //hardcoded server
-
-       //memcpy(&server.sin_addr.s_addr, host->h_addr, host->h_length);
+       server.sin_addr.s_addr = inet_addr("10.0.0.1"); //hardcoded server 
        
        char* printbuf = (char*)&server.sin_addr.s_addr;
        int size = sizeof(server.sin_addr.s_addr);      
@@ -55,12 +53,16 @@ int main(int argc, char* argv[]) {
        int sendsize = sendto(sockfd, buf, strlen(buf), 0, (struct sockaddr*) &server, socklen);
        printf("sendto returns %d, errno %d\n", sendsize, errno);
        //assume BUF_SIZE is larger than the packet.. so we will get to see what actually comes back..
-       if ((n = recvfrom(sockfd, recv_buf, BUF_SIZE, 0, (struct sockaddr*) &server, &socklen)< 0)){
-               printf("recv failed\n");
+       int j=0;
+       for (j=0; j<1; j++){
+               strcpy(recv_buf, "DEADBEEFDEADBEE");
+               if (((n = recvfrom(sockfd, recv_buf, BUF_SIZE, 0, (struct sockaddr*) &server, &socklen))< 0)){
+                       printf("recv failed\n");
+               }
+               recv_buf[n-2] = 0; //null terminate
+               printf("recv %d with length %d from result %s\n", j,n,  recv_buf);
        }
 
-       buf[n-2] = 0; //null terminate
 
-       printf("recv from result %s\n", buf);
        close(sockfd);
 }