我必須手動創建我自己的網絡數據包使用任何Protocol.i應發送到客戶端並解析或分析數據包。可以any1幫助我與哪裏和hpw開始?我有關於套接字編程的想法..創建ip網絡數據包
0
A
回答
10
您可以使用<linux/ip.h>
它包含指向ip數據包的結構。你也可以使用套接字。
這裏後面的例子。這個例子發送一個ICMP ECHO數據包並等待響應
/* send icmp packet example */
#include <stdio.h>
#include <stdlib.h>
#include <netdb.h>
#include <linux/ip.h>
#include <linux/icmp.h>
int main(int argc, char* argv[])
{
struct iphdr *ip, *ip_reply;
struct icmphdr* icmp;
struct sockaddr_in connection;
char *dst_addr="192.168.1.33";
char *src_addr="192.168.1.34";
char *packet, *buffer;
int sockfd, optval, addrlen;
packet = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
buffer = malloc(sizeof(struct iphdr) + sizeof(struct icmphdr));
ip = (struct iphdr*) packet;
icmp = (struct icmphdr*) (packet + sizeof(struct iphdr));
ip->ihl = 5;
ip->version = 4;
ip->tot_len = sizeof(struct iphdr) + sizeof(struct icmphdr);
ip->protocol = IPPROTO_ICMP;
ip->saddr = inet_addr(src_addr);
ip->daddr = inet_addr(dst_addr);
ip->check = in_cksum((unsigned short *)ip, sizeof(struct iphdr));
icmp->type = ICMP_ECHO;
icmp->checksum = in_cksum((unsigned short *)icmp, sizeof(struct icmphdr));
/* open ICMP socket */
if ((sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_ICMP)) == -1) {
perror("socket");
exit(EXIT_FAILURE);
}
/* IP_HDRINCL must be set on the socket so that the kernel does not attempt
* to automatically add a default ip header to the packet*/
setsockopt(sockfd, IPPROTO_IP, IP_HDRINCL, &optval, sizeof(int));
connection.sin_family = AF_INET;
connection.sin_addr.s_addr = ip->daddr;
sendto(sockfd, packet, ip->tot_len, 0, (struct sockaddr *)&connection, sizeof(struct sockaddr));
printf("Sent %d byte packet to %s\n", ip->tot_len, dst_addr);
addrlen = sizeof(connection);
if (recvfrom(sockfd, buffer, sizeof(struct iphdr) + sizeof(struct icmphdr), 0, (struct sockaddr *)&connection, &addrlen) == -1)
{
perror("recv");
}
else
{
char *cp;
ip_reply = (struct iphdr*) buffer;
cp = (char *)&ip_reply->saddr;
printf("Received %d byte reply from %u.%u.%u.%u:\n", ntohs(ip_reply->tot_len), cp[0]&0xff,cp[1]&0xff,cp[2]&0xff,cp[3]&0xff);
printf("ID: %d\n", ntohs(ip_reply->id));
printf("TTL: %d\n", ip_reply->ttl);
}
}
unsigned short in_cksum(unsigned short *addr, int len)
{
register int sum = 0;
u_short answer = 0;
register u_short *w = addr;
register int nleft = len;
/*
* Our algorithm is simple, using a 32 bit accumulator (sum), we add
* sequential 16 bit words to it, and at the end, fold back all the
* carry bits from the top 16 bits into the lower 16 bits.
*/
while (nleft > 1)
{
sum += *w++;
nleft -= 2;
}
/* mop up an odd byte, if necessary */
if (nleft == 1)
{
*(u_char *) (&answer) = *(u_char *) w;
sum += answer;
}
/* add back carry outs from top 16 bits to low 16 bits */
sum = (sum >> 16) + (sum & 0xffff); /* add hi 16 to low 16 */
sum += (sum >> 16); /* add carry */
answer = ~sum; /* truncate to 16 bits */
return (answer);
}
+0
爲什麼你沒有在數據包中附加mac頭文件?內核是否被照顧? –
0
相關問題
- 1. 在Java中創建網絡數據包分層結構
- 2. 創建網絡
- 3. 將數據發送到IP網絡
- 4. 網絡數據包裝
- 5. 網絡數據包攔截
- 6. 創建ZigBee網絡
- 7. 創建網絡時指定網絡ID
- 8. 找出IP網絡
- 9. 網絡和TCP/IP
- 10. Java網絡:創建沒有靜態IP的服務器
- 11. 通過網絡跟蹤網絡數據包
- 12. 如何使用java創建tcp/ip數據包?
- 13. 爲自定義數據包創建IP標頭時出錯
- 14. 如何在node.js中創建較低級別的網絡數據包
- 15. 確定網絡數據包結構
- 16. 通過3G網絡發送數據包
- 17. 捕捉iphone網絡數據包
- 18. 魔法數據包和虛擬網絡
- 19. Android:如何統計網絡數據包?
- 20. Windows網絡數據包修改
- 21. 瞭解GIOP網絡數據包
- 22. 處理排序的網絡數據包
- 23. WP7的網絡數據包分析器
- 24. python中的網絡數據包長度
- 25. 網絡編程UDP數據包
- 26. Kubernetes出站數據包網絡
- 27. 代表網絡數據包的C++類
- 28. 在c中捕獲網絡數據包
- 29. 網絡數據包連接意圖
- 30. 在iOS中攔截網絡數據包
其實並沒有那麼糟糕。你瞭解OSI模型嗎?以及IP協議如何工作?應該有圖書館爲你做這個...谷歌爲他們 –