Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8729903
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T08:54:46+00:00 2026-06-13T08:54:46+00:00

In linux, how can I transmit an UDP packet using 0.0.0.0 as source address.

  • 0

In linux, how can I transmit an UDP packet using 0.0.0.0 as source address.

Here is what I have tried so far.

#include <stdio.h>
#include <stdint.h>
#include <stdlib.h>
#include <net/if.h>
#include <string.h>
#include <unistd.h>
#include <assert.h>
#include <arpa/inet.h>
#include <sys/types.h>
#include <sys/socket.h>

int main(int argc, const char *argv[])
{
    struct sockaddr_in dst, src;
    struct ifreq ifr;
    int sock, tmp;
    char payload[128];

    memset(payload, 0, 128);

    memset(&dst, 0, sizeof(dst));
    dst.sin_family = AF_INET;
    dst.sin_addr.s_addr = inet_addr("255.255.255.255");
    dst.sin_port = htons(67);

    memset(&src,0,sizeof(src));
    src.sin_family = AF_INET;
    src.sin_addr.s_addr = inet_addr("0.0.0.0");
    src.sin_port = htons(68);

    sock = socket(AF_INET, SOCK_DGRAM, 0);
    if (sock < 0)
        perror("Failed to create socket");

    tmp = 1;
    if (setsockopt(sock, SOL_SOCKET, SO_BROADCAST, &tmp, sizeof(tmp)) < 0)
        perror("SO_BROADCAST failed");

    if (setsockopt(sock, SOL_SOCKET, SO_REUSEADDR, &tmp, sizeof(tmp)) < 0)
        perror("SO_REUSEADDR failed");

    if (setsockopt(sock, IPPROTO_IP, IP_FREEBIND, &tmp, sizeof(tmp)) < 0)
        perror("IP_FREEBIND failed");

    memset(&ifr, 0, sizeof(ifr));
    strncpy(ifr.ifr_name, "eth1", sizeof(ifr.ifr_name));

    if (setsockopt(sock, SOL_SOCKET, SO_BINDTODEVICE, (void *)&ifr, sizeof(ifr)) < 0)
        perror("SO_BINDTODEVICE failed");

    if (bind(sock, (const struct sockaddr*)&src, sizeof(struct sockaddr_in)) < 0)
        perror("bind failed");

    if (connect(sock, (const struct sockaddr*)&dst, sizeof(struct sockaddr_in)) < 0)
        perror("bind failed");

    if (write(sock, payload, 128) < 0 )
        perror("Write failed");

    close(sock);
    return 0;
}

The problem is the the source address is only set to 0.0.0.0 if no interfaces has an IPv4 address. If just one interface has an IPv4 address, this address is used as source address.

I have looked at the source code of some of the existing DHCP clients, and found that they are using RAW sockets and building the IP and UDP header manually. This is possibility, but I would like to avoid doing this manually.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-13T08:54:47+00:00Added an answer on June 13, 2026 at 8:54 am

    You should use RAW SOCKET and construct packet with your own efforts.

    As an example you can take this: https://github.com/fycth/DHCP-server-scanner/blob/master/src/dhcpd-detector.c

    This is my educational project. It is small and you can look how exactly this task is solved there.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Here's the output dumped from od -cx (on linux you can reproduce with echo
I have a question about assert() in Linux: can I use it in the
On linux you can set up a virtual interface using ifconfig eth0:0 .... There's
Can linux context switch after unlock in the below code if so we have
Under Linux I can open a directory using opendir and then use readdir to
On linux we can use time command. Or from C++: #include <sys/time.h> #include <sys/resource.h>
In Linux I can view or change the ephemeral port range using the /proc/sys/net/ipv4/ip_local_port_range
Possible Duplicate: Get IP address of an interface on linux How can I get
In Linux where can I find the source code for all system calls given
Under Linux I can issue a gcc -Wl,--defsym,main=main_x .. However in Mac OSX 10

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.