我编写了一个简单的原始套接字,该套接字应该可以接收使用libnet编写和组装的单个ipv4数据包。这是libnet数据包的代码,它工作正常(我认为,无法检查已发送的实际有效负载):
int main() {
uint32_t src, dst, seq, ack;
char payload1[1024], destip[16];
uint16_t sp = 2001, dp = 2000;
libnet_ptag_t tcp;
libnet_ptag_t ip4;
char errbuf[LIBNET_ERRBUF_SIZE];
int i, bytes;
int len;
libnet_t * l;
l = libnet_init(LIBNET_RAW4, NULL, errbuf);
//here follows the usual declaration of addresses and the payload using scanf
//and the command terminal (scanf("%s", payload);)
tcp = libnet_build_tcp(sp, dp, seq, ack, TH_SYN, 0, 0, 0, LIBNET_TCP_H +
strlen(payload), NULL, 0, l, 0);
ip4 = libnet_build_ipv4(LIBNET_TCP_H + LIBNET_IPV4_H + strlen(payload), 0, 1,
0, 64, IPPROTO_TCP, 0, src, dst, payload, strlen(payload), l, 0);
bytes = libnet_write(l);
return 0;
}
此程序有效,因为使用tcpdump时数据包会出现在网络中。 现在是原始套接字的程序:
int main() {
struct sockaddr_in server_addr;
char message[2000];
int sockfd, bytes;
//server_addr is filled out for the listen() call...
sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_TCP);
if(bind(sockfd, (struct sockaddr *) &server_addr, sizeof(server_addr)) == -1)
{
printf("error binding\n");
perror("bind()");
exit(0);
}
while(1) {
bytes = recv(sockfd, message, 2000, 0);
if(bytes == -1)
continue;
printf("captured %d bytes\n%s\n", bytes, message);
printf("as hex: %x\n", message); //I tried that to find
//out, if there was any
//kind of received message
//because at first, the
//socket didn't receive
//anything, except that it
//assured me the receiving
//of ... bytes (, so I
//wanted to print out the
//raw bytes.)
memset(message, 0, 2000);
}
}
现在,套接字设法接收字节,因为打印出的字节值显示了一个数字,也是从第一个程序作为数据包大小给出的确切数字(原始ipv4标头+有效负载= 40字节+有效负载大小)。只有收到的消息本身似乎会出现某种错误,因为无论我在数据包中发送什么,消息的%s输出始终为“ E”。十六进制输出随数据包的不同而不同,但是它并不符合我给定有效负载的十六进制版本(当我以十六进制的形式从数据包程序中打印出有效负载时),并且肯定没有任何相关值。现在我还以为,也许还会收到ipv4标头作为消息,所以我写了一个只读取消息缓冲区40多个字节的版本,但结果(“ E”和随机十六进制)是相同的。 / p>
我在做什么错?可能是错误的recv函数吗?还是我需要稍微键入消息缓冲区?还是在我的数据包发送程序中出现错误?