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

  • Home
  • SEARCH
  • 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 8915471
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 15, 20262026-06-15T04:58:57+00:00 2026-06-15T04:58:57+00:00

I know that it sound strange. printf shouldnt change anything,but without it sendTo failed.

  • 0

I know that it sound strange. printf shouldnt change anything,but without it sendTo failed.
The program was copied from c code and compiled by cpp compiler on ubuntux86.
I have a program to send arp request. without this printf the sendTo failed.
The strange thing is that i did a minimal program that dont use in the printf variable at all (only in the printf) and with the printf it work and without the printf it doesn’t work (get invalid argument error on the sendTo).

The attach is a minimal version only for showing the problem:

This sendTo failed:

int retVal = sendto(arp_fd, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa,sizeof(sa));
if (retVal < 0) {
    perror("sendto");
    close(arp_fd);
    exit(1);
}

when adding this it work:

struct ifreq ifr;//There is no use except in the printf
printf("MAC address: is %02x:%02x:%02x:%02x:%02x:%02x \n",
        ifr.ifr_hwaddr.sa_data[0]&0xFF,
        ifr.ifr_hwaddr.sa_data[1]&0xFF,
        ifr.ifr_hwaddr.sa_data[2]&0xFF,
        ifr.ifr_hwaddr.sa_data[3]&0xFF,
        ifr.ifr_hwaddr.sa_data[5]&0xFF,
        ifr.ifr_hwaddr.sa_data[4]&0xFF);

The complete program:

#include "sys/socket.h"
#include "sys/types.h"
#include "stdio.h"
#include "unistd.h"
#include "string.h"
#include "net/if.h"
#include "stdlib.h"
#include "arpa/inet.h"
#include "netinet/in.h"
#include "sys/ioctl.h"
#include "netpacket/packet.h"
#include "net/ethernet.h"

#define ETHER_TYPE_FOR_ARP 0x0806
#define HW_TYPE_FOR_ETHER 0x0001
#define OP_CODE_FOR_ARP_REQ 0x0001
#define HW_LEN_FOR_ETHER 0x06
#define HW_LEN_FOR_IP 0x04
#define PROTO_TYPE_FOR_IP 0x0800

typedef unsigned char byte1;
typedef unsigned short int byte2;
typedef unsigned long int byte4;

// For Proper memory allocation in the structure
#pragma pack(1)
typedef struct arp_packet {
// ETH Header
    byte1 dest_mac[6];
    byte1 src_mac[6];
    byte2 ether_type;
// ARP Header
    byte2 hw_type;
    byte2 proto_type;
    byte1 hw_size;
    byte1 proto_size;
    byte2 arp_opcode;
    byte1 sender_mac[6];
    byte4 sender_ip;
    byte1 target_mac[6];
    byte4 target_ip;
// Paddign
    char padding[18];
} ARP_PKT;

int main() {
    struct sockaddr_ll sa;

// Ethernet Header
    ARP_PKT pkt;
    memset(pkt.dest_mac, 0xFF, (6 * sizeof(byte1)));
    memset(pkt.src_mac, 0x1, (6 * sizeof(byte1)));
    pkt.ether_type = htons(ETHER_TYPE_FOR_ARP);
// ARP Header
    pkt.hw_type = htons(HW_TYPE_FOR_ETHER);
    pkt.proto_type = htons(PROTO_TYPE_FOR_IP);
    pkt.hw_size = HW_LEN_FOR_ETHER;
    pkt.proto_size = HW_LEN_FOR_IP;
    pkt.arp_opcode = htons(OP_CODE_FOR_ARP_REQ);
    memcpy(pkt.sender_mac, pkt.src_mac, (6 * sizeof(byte1)));
    pkt.sender_ip = 2449647808;
    memset(pkt.target_mac, 0, (6 * sizeof(byte1)));
    pkt.target_ip = inet_addr("10.0.0.10");
// Padding
    memset(pkt.padding, 0, 18 * sizeof(byte1));

    sa.sll_family = AF_PACKET;
    sa.sll_ifindex = 3;
    sa.sll_protocol = htons(ETH_P_ARP);

    /* Send it! */
    int arp_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ARP));
    struct ifreq ifr;
    printf("MAC address: is %02x:%02x:%02x:%02x:%02x:%02x \n",
            ifr.ifr_hwaddr.sa_data[0]&0xFF,
            ifr.ifr_hwaddr.sa_data[1]&0xFF,
            ifr.ifr_hwaddr.sa_data[2]&0xFF,
            ifr.ifr_hwaddr.sa_data[3]&0xFF,
            ifr.ifr_hwaddr.sa_data[5]&0xFF,
            ifr.ifr_hwaddr.sa_data[4]&0xFF);
    int retVal = sendto(arp_fd, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa,sizeof(sa));
    if (retVal < 0) {
        perror("sendto");
        close(arp_fd);
        exit(1);
    }

    printf("\n=========PACKET=========\n");
    return 0;
}
  • 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-15T04:58:58+00:00Added an answer on June 15, 2026 at 4:58 am

    This is because you have uninitialized values in struct sockaddr_ll sa. More specifically, your call to printf forces the compiler to save three registers on the stack therefore moving sa location and its values. You can printf("halen %p %d\n", &sa.sll_halen, sa.sll_halen); before your printf to see it change depending on wether you add your printf or not.

    Just add sa.sll_halen = 0; to see the difference… And for your real program initialize the whole structure.

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

Sidebar

Related Questions

I know that may sound silly, but I'm trying to submit a form on
I know that the heading of this thread sound silly, but I can not
I know this may sound strange, but I want to retrieve the system default
I know it sounds strange but I see that there's a application controller, helper
I know this might sound like a strange question but I actually need to
I have a question, that may sound strange. I am interested to know if
I know it may sound very strange, but I need to know if there
I know that the title may sound absurd, but I don't know ho to
I know that the reminder application should display a badge or sound or kind
I know, I know, its sounds silly, but it seems that there are no

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.