HedongGao opened a new pull request, #17699:
URL: https://github.com/apache/nuttx/pull/17699

   
   
   ## Summary
   
   According to RFC768 page2 and referring to the Linux implementation, the 
message with udp length of 0 is supported.
   
   ## Impact
   
   Users are allowed to send udp messages with length 0.
   
   ## Testing
   
   Change the code of nutt-apps/examples/udp/udpclient.c as follow:
   
   `
   void udp_client(void)
   {
   #ifdef CONFIG_EXAMPLES_UDP_IPv6
     struct sockaddr_in6 server;
   #else
     struct sockaddr_in server;
   #endif
     unsigned char outbuf[SENDSIZE];
     socklen_t addrlen;
     int sockfd;
     int nbytes;
     int offset;
   #ifdef CONFIG_EXAMPLES_UDP_BROADCAST
     int optval;
     int ret;
   #endif
   
     /* Create a new UDP socket */
   
     sockfd = create_socket();
     if (sockfd < 0)
       {
         printf("client ERROR: create_socket failed\n");
         exit(1);
       }
   
   #ifdef CONFIG_EXAMPLES_UDP_BROADCAST
     optval = 1;
     ret = setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, &optval, sizeof(int));
     if (ret < 0)
       {
         printf("Failed to set SO_BROADCAST\n");
         exit(1);
       }
   #endif
   
     /* Set up the server address */
   
   #ifdef CONFIG_EXAMPLES_UDP_IPv6
     server.sin6_family     = AF_INET6;
     server.sin6_port       = HTONS(CONFIG_EXAMPLES_UDP_SERVER_PORTNO);
     memcpy(server.sin6_addr.s6_addr16,
             g_udpserver_ipv6, 8 * sizeof(uint16_t));
     addrlen                = sizeof(struct sockaddr_in6);
   #else
     server.sin_family      = AF_INET;
     server.sin_port        = HTONS(CONFIG_EXAMPLES_UDP_SERVER_PORTNO);
     server.sin_addr.s_addr = (in_addr_t)g_udpserver_ipv4;
     addrlen                = sizeof(struct sockaddr_in);
   #endif
   
     /* Then send and receive 256 messages */
   
     for (offset = 0; offset < 20; offset++)
       {
         /* Set up the output buffer */
   
         fill_buffer(outbuf, offset);
   
         /* Send the message */
   
         printf("client: %d. Sending %d bytes\n", offset, SENDSIZE);
         nbytes = sendto(sockfd, outbuf, SENDSIZE, 0,
                         (struct sockaddr *)&server, addrlen);
         printf("client: %d. Sent %d bytes\n", offset, nbytes);
   
         if (nbytes < 0)
           {
             printf("client: %d. sendto failed: %d\n", offset, errno);
             close(sockfd);
             exit(-1);
           }
         else if (nbytes != SENDSIZE)
           {
             printf("client: %d. Bad send length: %d Expected: %d\n",
                    offset, nbytes, SENDSIZE);
             close(sockfd);
             exit(-1);
           }
   
         /* Now, sleep a bit.  No packets should be dropped due to overrunning
          * the server.
          */
   
         sleep(1);
       }
   
     for (offset = 0; offset < 20; offset++)
       {
         /* Send the message */
   
         printf("client: %d. Sending %d bytes\n", offset, 0);
         nbytes = sendto(sockfd, outbuf, 0, 0,
                         (struct sockaddr *)&server, addrlen);
         printf("client: %d. Sent %d bytes\n", offset, 0);
   
         if (nbytes < 0)
           {
             printf("client: %d. sendto failed: %d\n", offset, errno);
             close(sockfd);
             exit(-1);
           }
         else if (nbytes != 0)
           {
             printf("client: %d. Bad send length: %d Expected: %d\n",
                    offset, nbytes, 0);
             close(sockfd);
             exit(-1);
           }
   
         /* Now, sleep a bit.  No packets should be dropped due to overrunning
          * the server.
          */
   
         sleep(1);
       }
   
     close(sockfd);
   }
   
   `
   
   Open the compilation configuration of CONFIG_EXAMPLES_UDP, compile 
udpclient, build the SIM environment, and allow udpclient in sim and udpserver 
on the host side. The server can receive UDP messages with length 0.
   
   
   `
   16:00:21.820640 IP (tos 0x0, ttl 64, id 110, offset 0, flags [DF], proto UDP 
(17), length 124)
       10.0.1.2.5472 > 10.0.0.1.5471: [udp sum ok] UDP, length 96
   16:00:22.830757 IP (tos 0x0, ttl 64, id 111, offset 0, flags [DF], proto UDP 
(17), length 28)
       10.0.1.2.5472 > 10.0.0.1.5471: [udp sum ok] UDP, length 0
   16:00:23.840546 IP (tos 0x0, ttl 64, id 112, offset 0, flags [DF], proto UDP 
(17), length 28)
       10.0.1.2.5472 > 10.0.0.1.5471: [udp sum ok] UDP, length 0
   `


-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to