LinuxQuestions.org
Latest LQ Deal: Linux Power User Bundle
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
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.

Notices


Reply
  Search this Thread
Old 03-10-2009, 05:04 AM   #1
kornerr
Member
 
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
Code:
    sockaddr_in srvAddr,
                cliAddr;
    //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
Code:
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.
Thanks.
 
Old 03-11-2009, 03:31 AM   #2
kornerr
Member
 
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...
 
  


Reply


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 07:10 AM
ssh console broadcast messages time112852 Linux - Software 2 11-23-2005 03:45 PM
Telnet Connections and broadcast messages CatSC Linux - Security 4 12-21-2004 12:53 AM
strange broadcast logging in messages file PennyroyalFrog Linux - Newbie 1 10-12-2004 10:37 PM
Receiving winpopup broadcast messages vrsic Linux - Software 8 10-30-2003 03:31 AM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 11:11 AM.

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