Project Home
Project Home
Wiki
Wiki
Discussion Forums
Discussions
Project Information
Project Info
Forum Topic - Sending UDP Broadcast problem: (3 Items)
   
Sending UDP Broadcast problem  
Working on a BOOTP/DHCP client I am having problems sending broadcast packets.  In my send function if I don’t set the 
MSG_DONTROUTE flag in the sendTo() call then it goes to the local broadcast address, which when our address is 0.0.0.0 
turns out to be 0.255.255.255, which doesn’t work.  If I do set it then in the IP section of the packet my "Time To 
Live" is then set to 1 and when my Ip address is not 0, a broadcast still goes to the network broadcast address (i.e. 
172.30.255.255) instead of the general broadcast address (255.255.255.255) which is the address that I am requesting in 
sendTo().

Do you have any ideas what can be done so that packets addressed to 255.255.255.255 always go to 255.255.255.255 and the
 time to live count stays at 64?
Your dhcp.client does it OK, so it must be possible to do.

Send code looks like the following:
      socklen_t tolen = sizeof(toAddress);
      int flags = 0;

      if (INADDR_BROADCAST == toAddress.sin_addr.s_addr) {
         flags = MSG_DONTROUTE;
      }

      ssize_t writeReturn = sendto(mySocketHandle, dataBuffer, dataBufferSize, flags, (struct sockaddr*)&toAddress,  
tolen);

      if (writeReturn != -1) {
         numWritten = (size_t)writeReturn;
      } else {
         zError("Sendto Error to 0x%0X of %d", toAddress.sin_addr.s_addr, errno);
      }


Socket setup code looks like the following:
         address = myRemoteAddress.c_str();

         //zError("UDP Client connection to %s : %d", address, myRemotePort);

         memset(&hints, 0x00, sizeof(hints));
         hints.ai_flags    = AI_NUMERICHOST;
         hints.ai_family   = AF_UNSPEC;
         hints.ai_socktype = SOCK_DGRAM;
         /********************************************************************/
         /* Check if we were provided the address of the server using        */
         /* inet_pton() to convert the text form of the address to binary    */
         /* form. If it is numeric then we want to prevent getaddrinfo()     */
         /* from doing any name resolution.                                  */
         /********************************************************************/
         rc = inet_pton(AF_INET, address, &serveraddr);
         /* valid IPv4 text address? */
         if (rc == 1) {
            hints.ai_family = AF_INET;
            hints.ai_flags |= AI_NUMERICHOST;
         } else {
            rc = inet_pton(AF_INET6, address, &serveraddr);
            /* valid IPv6 text address? */
            if (rc == 1) {
               hints.ai_family = AF_INET6;
               hints.ai_flags |= AI_NUMERICHOST;
            }
         }
         /********************************************************************/
         /* Get the address information for the server using getaddrinfo().  */
         /********************************************************************/
         char servport[32];
         snprintf(servport, 32, "%d", myRemotePort);
         rc = getaddrinfo(address, servport, &hints, &res);
         if (rc != 0) {
            zError("Host not found --> %s\n", gai_strerror(rc));
            if (rc == EAI_SYSTEM)
               perror("getaddrinfo() failed");
            break;
         }

         /********************************************************************/
         /* The socket() function returns a socket descriptor, which represents   */
         /* an endpoint.  The statement also identifies the address family,  */
         /* socket type, and protocol using the information returned from    */
         /* getaddrinfo().                                                   */
         /********************************************************************/
         //zError("UDP Client family=0x%0x, type=0x%0x protocol=0x%0x", res->ai_family, res->ai_socktype, 0/*res->
ai_protocol*/);
         mySocketHandle = socket(res->ai_family, res->ai_socktype, 0/* TODO Fix: CURRENTLY HARDCODED...
View Full Message
Re: Sending UDP Broadcast problem  
Just wondering, why are you developing a DHCP client? There is already 
one as part of QNX, does it not meet your needs?
http://www.qnx.com/developers/docs/6.4.1/neutrino/utilities/d/dhcp.client.html
/P
Re: Sending UDP Broadcast problem  
No it doesn't meet our needs.  We need to simultaneously do BOOTP and DHCP at the same time.  Some servers will only 
respond to BOOTP requests, so doing DHCP would not get us an address.  If we run your DHCP client and a BOOTP app then 
packets coming in would go to one app or the other, which would be a problem for both.