CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 9 of 9
  1. #1
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Question Unable to read UDP broadcasts

    I have a DSP connected to a Windows 7 laptop by a dedicated Ethernet connection (no routers or anything else in the way).

    The laptop has all firewalls disabled (I’ve even tried stopping the Windows firewall service and DHCP).

    The DSP is sending well-formed BOOTP broadcast packets every three seconds to port 67. Wireshark running on the laptop sees
    these BOOTP packets coming in and shows they are correct.

    I have a C++ program running on the laptop with a socket successfully bound to port 67. I can see this using CurrPorts.
    Nothing else is shown as accessing port 67.

    The program never sees any packets coming in.

    If I run a program in the DSP that sends ordinary (non-broadcast) UDP packets to port 67, Wireshark sees them coming in and
    reports that they are corrupt BOOTP packets, but now, the same program on the laptop gets them correctly.

    Someone suggested that SO_BROADCAST needs to be set as a socket option in the laptop program (even though the Microsoft
    documentation says this is for transmission only). I have tried this and, as expected, it has no effect; I still cannot read any
    broadcast packets.

    Any idea what’s going on here and why I cannot read any broadcast packets?

  2. #2
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Unable to read UDP broadcasts

    What type of protocol is used: IPv4 or IPv6?
    Note that in case of IPv6 there are some changes of "broadcasting". See http://msdn.microsoft.com/en-us/libr...(v=vs.85).aspx
    Victor Nijegorodov

  3. #3
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Re: Unable to read UDP broadcasts

    IPv4

  4. #4
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Unable to read UDP broadcasts

    Could you show your code?
    Victor Nijegorodov

  5. #5
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Re: Unable to read UDP broadcasts

    For some reason, this board has removed all the indentation...

    #include "stdafx.h"
    #include <stdio.h>
    #include <winsock2.h>
    #pragma comment(lib, "ws2_32.lib")

    int inner_bootp(SOCKET boot_sock) {
    sockaddr_in boot;
    sockaddr_in s;
    unsigned char B[400];
    memset(&boot, 0, sizeof(boot));
    boot.sin_family = AF_INET;
    boot.sin_addr.s_addr = INADDR_ANY;
    boot.sin_port = htons(67);
    if (bind(boot_sock, (sockaddr *)&boot, sizeof(boot))==SOCKET_ERROR) return 1;
    DWORD br = TRUE;
    if (setsockopt(boot_sock, SOL_SOCKET, SO_BROADCAST, (char *)&br, sizeof(br))==SOCKET_ERROR) return 2; // fatuous
    printf("Waiting for BOOTP...\n");
    for (; {
    for (; {
    fd_set readFDS;
    timeval tout;
    tout.tv_sec = 10;
    tout.tv_usec = 0;
    FD_ZERO(&readFDS);
    FD_SET(boot_sock, &readFDS);
    int t = select(-1, &readFDS, 0, 0, &tout);
    if (t==SOCKET_ERROR) return 3;
    if (t) break; // not time-out
    if (MessageBox(0, L"Still waiting", L"No BOOTP detected", MB_RETRYCANCEL | MB_TOPMOST) != IDRETRY) return 0;
    }
    int size = sizeof(s);
    if (recvfrom(boot_sock, (char *)B, sizeof(B), 0, (SOCKADDR *)&s, &size)==SOCKET_ERROR) return 4;
    if (B[0] != 1) { // BOOTP
    printf("non-BOOTP received\n");
    } else {
    int Id = (B[4]<<24) | (B[5]<<16) | (B[6]<<8) | B[7];
    if (Id == 0x12345678) break;
    printf("unexpected transaction ID %08X\n", Id);
    }
    }
    char info[20];
    sprintf(info, "%02x-%02x-%02x-%02x-%02x-%02x", B[28],B[29],B[30],B[31],B[32],B[33]);
    printf("Info: Bootp request from:\t%s\n", info);
    return 0;
    }

    void WaitForBOOTP(void) {
    SOCKET boot_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
    if (boot_sock==INVALID_SOCKET) {
    printf("failed to create bootp socket (%d)\n", WSAGetLastError());
    } else {
    int n = inner_bootp(boot_sock);
    if (n) printf("wait for bootp fails %d (%d)\n", n, WSAGetLastError());
    closesocket(boot_sock);
    }
    }

    void main() {
    WSADATA Data;
    if (WSAStartup(MAKEWORD(1, 1), &Data)) {
    printf("Unable to initialize WinSock for host info\n");
    } else {
    WaitForBOOTP();
    WSACleanup();
    printf("Done\n");
    }
    }

  6. #6
    Join Date
    Feb 2012
    Location
    Fremont,CA
    Posts
    37

    Re: Unable to read UDP broadcasts

    This code now works. Problem resolved:


    static void Main(string[] args)
    {
    try
    {
    var multicastAddress = IPAddress.Parse("239.255.255.250");

    string localName = Dns.GetHostName();
    IPHostEntry hostEntry = new IPHostEntry();
    hostEntry = Dns.GetHostEntry(localName);
    IPAddress localIPAddr = null;
    foreach (IPAddress address in hostEntry.AddressList)
    {
    // UPnP only supports IPv4
    if (address.AddressFamily == AddressFamily.InterNetwork)
    {
    localIPAddr = address;
    break;
    }
    }
    if (localIPAddr != null)
    {
    using (var socket = new Socket(AddressFamily.InterNetwork, SocketType.Dgram, ProtocolType.Udp))
    {
    Console.WriteLine("calling socket.Bind()");
    socket.SetSocketOption(SocketOptionLevel.Socket, SocketOptionName.ReuseAddress, 1);
    // Bind to local IP address
    EndPoint localEP = (EndPoint)new IPEndPoint(localIPAddr, 1900);
    socket.Bind(localEP);
    // This is required to see the multicast messages
    socket.SetSocketOption(SocketOptionLevel.IP, SocketOptionName.AddMembership, new MulticastOption(multicastAddress, localIPAddr));
    // Receive broadcasts from any remote endpoint
    EndPoint remoteEP = (EndPoint)new IPEndPoint(IPAddress.Any, 0);
    var response = new byte[0x8000];
    while (true)
    {
    socket.ReceiveFrom(response, SocketFlags.None, ref remoteEP);
    var stringResponse = Encoding.UTF8.GetString(response).Trim().TrimEnd('\0');
    Console.WriteLine(string.Format("Received: {0}", stringResponse));
    }
    }
    }
    else
    {
    Console.WriteLine("Error: Unable to get an IpV4 address.");
    }
    }
    catch (Exception ex)
    {
    Console.WriteLine(string.Format("Exception: {0}", ex.Message));
    }
    }
    }

  7. #7
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Re: Unable to read UDP broadcasts

    I do not see the relevance of this code which relates to multicast issues. My problem is clearly stated as being unable to receive broadcast packets.

    My problem has not been resolved.

  8. #8
    VictorN's Avatar
    VictorN is offline Super Moderator Power Poster
    Join Date
    Jan 2003
    Location
    Hanover Germany
    Posts
    20,395

    Re: Unable to read UDP broadcasts

    Quote Originally Posted by Peter Robertson View Post
    For some reason, this board has removed all the indentation...

    Code:
    #include "stdafx.h"
    #include <stdio.h>
    #include <winsock2.h>
    #pragma comment(lib, "ws2_32.lib")
    
    int inner_bootp(SOCKET boot_sock) {
       sockaddr_in   boot;
       sockaddr_in   s;
       unsigned char B[400];
       memset(&boot, 0, sizeof(boot));
       boot.sin_family      = AF_INET;
       boot.sin_addr.s_addr = INADDR_ANY;
       boot.sin_port        = htons(67);
       if (bind(boot_sock, (sockaddr *)&boot, sizeof(boot))==SOCKET_ERROR) return 1;
       DWORD br = TRUE;
       if (setsockopt(boot_sock, SOL_SOCKET, SO_BROADCAST, (char *)&br, sizeof(br))==SOCKET_ERROR) return 2; // fatuous
       printf("Waiting for BOOTP...\n");
       for (;;) {
          for (;;) {
             fd_set  readFDS;
             timeval tout;
             tout.tv_sec  = 10;
             tout.tv_usec = 0;
             FD_ZERO(&readFDS);
             FD_SET(boot_sock, &readFDS);
             int t = select(-1, &readFDS, 0, 0, &tout);
             if (t==SOCKET_ERROR) return 3;
             if (t) break;   // not time-out
             if (MessageBox(0, L"Still waiting", L"No BOOTP detected", MB_RETRYCANCEL | MB_TOPMOST) != IDRETRY) return 0;
          }
          int size = sizeof(s);
          if (recvfrom(boot_sock, (char *)B, sizeof(B), 0, (SOCKADDR *)&s, &size)==SOCKET_ERROR) return 4;
          if (B[0] != 1) {    // BOOTP
             printf("non-BOOTP received\n");
          } else {
             int Id = (B[4]<<24) | (B[5]<<16) | (B[6]<<8) | B[7];
             if (Id == 0x12345678) break;
             printf("unexpected transaction ID %08X\n",  Id);
          }
       }
       char info[20];
       sprintf(info, "%02x-%02x-%02x-%02x-%02x-%02x", B[28],B[29],B[30],B[31],B[32],B[33]);
       printf("Info: Bootp request from:\t%s\n", info);
       return 0;
    }
    
    void WaitForBOOTP(void) {
       SOCKET boot_sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
       if (boot_sock==INVALID_SOCKET) {
          printf("failed to create bootp socket (%d)\n", WSAGetLastError());
       } else {
          int n = inner_bootp(boot_sock);
          if (n) printf("wait for bootp fails %d (%d)\n", n, WSAGetLastError());
          closesocket(boot_sock);
       }
    }
    
    void main() {
       WSADATA Data;
       if (WSAStartup(MAKEWORD(1, 1), &Data)) {
          printf("Unable to initialize WinSock for host info\n");
       } else {
          WaitForBOOTP();
          WSACleanup();
          printf("Done\n");
       }
    }
    You have to use Code tags!
    Victor Nijegorodov

  9. #9
    Join Date
    Nov 2012
    Location
    Edinburgh
    Posts
    20

    Re: Unable to read UDP broadcasts

    Thanks for that.

    I am reminded of the advice of a very wise teacher many years ago: the number one rule of programming is "never fiddle with a user's data".

    Life would be so much easier if everyone followed that advice.

Tags for this Thread

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured