我知道这听起来很奇怪。printf 不应该改变任何东西,但没有它 sendTo 失败。 该程序是从 c 代码中复制出来的,并由 cpp 编译器在 ubuntux86 上编译。 我有一个发送 arp 请求的程序。没有这个 printf,sendTo 就失败了。奇怪的是,我做了一个根本不在 printf 变量中使用的最小程序(仅在 printf 中)并且使用 printf 它可以工作,而没有 printf 它不起作用(在 sendTo 上出现无效参数错误)。
附件是仅用于显示问题的最小版本:
此发送失败:
int retVal = sendto(arp_fd, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa,sizeof(sa));
if (retVal < 0) {
perror("sendto");
close(arp_fd);
exit(1);
}
添加时它起作用:
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);
完整的程序:
#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;
}