Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Adding printf change sendTo result from failure to success

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;
}
like image 890
Avihai Marchiano Avatar asked Nov 03 '22 10:11

Avihai Marchiano


1 Answers

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.

like image 141
kmkaplan Avatar answered Nov 11 '22 08:11

kmkaplan