将printf change sendTo从失败添加到成功
我知道这听起来很奇怪。printf不应更改任何内容,但如果没有它,sendTo将失败。 该程序是从c代码中复制出来的,由ubuntux86上的cpp编译器编译。 我有一个程序来发送arp请求。如果没有此打印,发送失败。 奇怪的是,我做了一个最小的程序,它根本不在printf变量中使用(只在printf中使用),有printf它工作,没有printf它不工作(在sendTo上获取无效参数错误) 附件是仅用于显示问题的最低版本: 此发送失败:将printf change sendTo从失败添加到成功,c,sockets,network-programming,C,Sockets,Network Programming,我知道这听起来很奇怪。printf不应更改任何内容,但如果没有它,sendTo将失败。 该程序是从c代码中复制出来的,由ubuntux86上的cpp编译器编译。 我有一个程序来发送arp请求。如果没有此打印,发送失败。 奇怪的是,我做了一个最小的程序,它根本不在printf变量中使用(只在printf中使用),有printf它工作,没有printf它不工作(在sendTo上获取无效参数错误) 附件是仅用于显示问题的最低版本: 此发送失败: int retVal = sendto(arp_fd,
int retVal = sendto(arp_fd, &pkt, sizeof(pkt), 0, (struct sockaddr *) &sa,sizeof(sa));
if (retVal < 0) {
perror("sendto");
close(arp_fd);
exit(1);
}
完整的程序:
#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;
}
#包括“sys/socket.h”
#包括“sys/types.h”
#包括“stdio.h”
#包括“unistd.h”
#包括“string.h”
#包括“净/if.h”
#包括“stdlib.h”
#包括“arpa/inet.h”
#包括“netinet/in.h”
#包括“sys/ioctl.h”
#包括“netpacket/packet.h”
#包括“net/ethernet.h”
#为ARP 0x0806定义以太类型
#定义乙醚0x0001的硬件类型
#为ARP请求0x0001定义操作码
#为0x06定义HW_LEN_
#为IP 0x04定义HW_LEN_
#为IP 0x0800定义原型类型
typedef无符号字符字节1;
typedef无符号短整型字节2;
typedef无符号长整型字节4;
//用于在结构中正确分配内存
#布拉格语包(1)
类型定义结构arp_数据包{
//ETH头
字节1 dest_mac[6];
字节1 src_mac[6];
字节2乙醚型;
//ARP报头
字节2 hw_类型;
字节2原型;
字节1硬件单元大小;
字节1原型大小;
字节2 arp_操作码;
字节1发送方_mac[6];
字节4发送方ip;
字节1 target_mac[6];
字节4目标ip;
//围场
字符填充[18];
}ARP_PKT;
int main(){
结构sockaddr_ll sa;
//以太网报头
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_表示ARP);
//ARP报头
pkt.hw_type=htons(hw_type_表示乙醚);
pkt.proto_type=htons(proto_type_表示IP);
pkt.hw_size=hw_LEN_表示乙醚;
pkt.proto_size=HW_LEN_表示IP;
pkt.arp_操作码=htons(操作码表示请求的arp_);
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”);
//填充物
memset(pkt.padding,0,18*sizeof(byte1));
sa.sll_family=AF_数据包;
sa.sll_i指数=3;
sa.sll_协议=HTON(ETH_P_ARP);
/*发送它*/
int arp_fd=套接字(PF_数据包,SOCK_原始,htons(ETH_P_arp));
结构ifreq-ifr;
printf(“MAC地址:是%02x:%02x:%02x:%02x:%02x:%02x:%02x\n”,
ifr.ifr\u hwaddr.sa\u数据[0]和0xFF,
ifr.ifr\u hwaddr.sa\u数据[1]和0xFF,
ifr.ifr\u hwaddr.sa\u数据[2]和0xFF,
ifr.ifr\u hwaddr.sa\u数据[3]&0xFF,
ifr.ifr\u hwaddr.sa\u数据[5]&0xFF,
ifr.ifr_hwaddr.sa_数据[4]&0xFF);
int retVal=sendto(arp_fd,&pkt,sizeof(pkt),0,(struct sockaddr*)&sa,sizeof(sa));
如果(返回值<0){
perror(“sendto”);
关闭(arp_fd);
出口(1);
}
printf(“\n=PACKET=PACKET=PACKET=PACKET=printf=\n”);
返回0;
}
这是因为在struct sockaddr\u ll sa
中有未初始化的值。更具体地说,对printf的调用会强制编译器在堆栈上保存三个寄存器,从而移动sa
位置及其值。您可以printf(“halen%p%d\n”,&sa.sll_-halen,sa.sll_-halen)在printf
之前添加code>,查看它的变化,这取决于是否添加printf
只需添加sa.sll_halen=0代码>查看差异…并为您的实际程序初始化整个结构。失败时发生了什么?您的代码是否打印出错误?错误是:sendto:perror(“sendto”)打印的无效参数;在perror
之后添加printf(“[%d]”,errno)
。在代码中,你看到了任何代码吗?<代码> siZeof(ByTE1)< /C> >在C和C++中返回不同的结果。参见@ SHIPLU.MOKADAD.IM:为什么C++中的<代码> siZoof(unChar char)< /C> >不同?我认为你混淆了代码> > sieOf(/a)<代码> > C中的代码> > sieOf(int)< /> > C++中的代码> siZeOf(char)< /Cord>。我不明白原因,但该死的,你是对的,不是所有的字节:)
#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;
}