Review your favorite Linux distribution.
Go Back > Forums > Non-*NIX Forums > Programming
User Name
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.


  Search this Thread
Old 03-10-2009, 04:04 AM   #1
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Rep: Reputation: 35
How to receive DHCP broadcast messages?

I try to receive any DHCP broadcast with
    sockaddr_in srvAddr,
    //int mSrvSocket = socket(AF_INET, SOCK_RAW, IPPROTO_DIVERT);
    int mSrvSocket = socket(AF_INET, SOCK_DGRAM, 0);
    if (mSrvSocket < 0)
        throw std::runtime_error("Could not create server socket");
    int broadcast = 1;
    if (setsockopt(mSrvSocket, SOL_SOCKET, SO_BROADCAST, &broadcast, sizeof(broadcast)) < 0)
        throw std::runtime_error("Could not set socket be broadcast aware");
    bzero(&srvAddr, sizeof(srvAddr));
    bzero(&cliAddr, sizeof(cliAddr));
    srvAddr.sin_family = AF_INET;
    srvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
    srvAddr.sin_port = htons(67);
    if (bind(mSrvSocket, (sockaddr*)&srvAddr, sizeof(srvAddr)) < 0)
        throw std::runtime_error("Could not bind the socket");
    ssize_t rcvbufsize = BUF_LEN;
    if (setsockopt(mSrvSocket, SOL_SOCKET, SO_RCVBUF, &rcvbufsize, sizeof(rcvbufsize)) < 0)
        throw std::runtime_error("Could not set new receive buffer size");
    while (1) {
        socklen_t len = sizeof(cliAddr);
        ssize_t n = recvfrom(mSrvSocket, buf, BUF_LEN, 0, (sockaddr*)&cliAddr, &len);
        buf[n] = 0;
        std::cout << "Buf: " << buf;
It doesn't work. When a PC with DHCP issues a request only Wireshark can see DHCP broadcast, but not me.
I tried to use no setsockopt with SO_BROADCAST. I tried to use
srvAddr.sin_addr.s_addr = htonl(INADDR_ANY);
with INADDR_BROADCAST instead of INADDR_ANY, with and without htonl. Nothing helps.
Also, I do not have iptables installed.
Old 03-11-2009, 02:31 AM   #2
Registered: Dec 2004
Location: Russia, Siberia, Kemerovo
Distribution: Slackware
Posts: 893

Original Poster
Rep: Reputation: 35
It all came down to the fact I had to enable 'promisc' mode on Ethernet interface...


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off

Similar Threads
Thread Thread Starter Forum Replies Last Post
Cannot get LinPopup to receive messages jamespetts Linux - Networking 10 02-01-2006 06:10 AM
ssh console broadcast messages time112852 Linux - Software 2 11-23-2005 02:45 PM
Telnet Connections and broadcast messages CatSC Linux - Security 4 12-20-2004 11:53 PM
strange broadcast logging in messages file PennyroyalFrog Linux - Newbie 1 10-12-2004 09:37 PM
Receiving winpopup broadcast messages vrsic Linux - Software 8 10-30-2003 02:31 AM > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 06:45 PM.

Main Menu
Write for LQ is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Facebook: linuxquestions Google+: linuxquestions
Open Source Consulting | Domain Registration