验证UDP组播ipid增长情况(linux C):结果,suse上ipid不一定连续增长

用如下程序验证linux下udp 单播、组播报文 ipid的增长情况

1、源文件:send-udpmulticast-packet.c

/*
* send udp multicast packet
* send-udpmulticast-packet.c
*/
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>

#define MAXLINE 1024
//#define MULTI_DST_ADDR "239.255.250.250"
#define UNI_DST_ADDR "172.16.0.1"
#define PORT 9131

// Driver code
int main() {
int sockfd;
char buffer[MAXLINE];
char *hello = "Hello from client";
struct sockaddr_in servaddr;

// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 ) {
perror("socket creation failed");
exit(EXIT_FAILURE);
}

memset(&servaddr, 0, sizeof(servaddr));

// Filling server information
servaddr.sin_family = AF_INET;
servaddr.sin_port = htons(PORT);

int n, len;
while (1) {
printf("Select dst address from 239.255.250.x or unicast. input 1 for 239.x.x.x and 2 for unicast and others for quit\n");
char sel = getchar(); //最好用int,因为有特殊字符,这里用char为了调试显示方便

//clear stdin. there should be a charter ‘\n‘
//fflush(stdin); //不建议使用,C和C++的标准里从来没有定义过 fflush(stdin)
char c;
while( (c = getchar()) != EOF && c != ‘\n‘); //使用getchar不断获取缓冲区内内容直内空为止,建议使用,C primer 上也介绍了这种方法

if(sel == ‘1‘){
printf("selected 239.x\n");
/*
*#include<arpa/inet.h>
*int inet_pton(int family,const char *strptr,void *addrptr);//成功返回1,格式不对返回0,出错返回-1
*作用:p代表表达式 n代表数值 以后所写的所有代码中都有可能会需要这个函数,所以这个函数很重要
*将char所指向的字符串,通过addrptr指针存放
*他的反函数: inet_ntop()作用相反。
*需要注意的是:当他发生错误的时候,errno的值会被置为EAFNOSUPPORT 关于errno值我们一会儿介绍。
*/
char dstInetAddr[100] = "239.255.250.";
char lastbyte[10];
/*(void)sprintf(lastbyte,"%d", turn);
turn++;
strcat(dstInetAddr,lastbyte);*/
printf("Select dst x(1-254) for 239.255.250.x\n");
int dstx = getchar() - ‘0‘;
//clear stdin. there should be a charter ‘\n‘
char c;
while( (c = getchar()) != EOF && c != ‘\n‘);
(void)sprintf(lastbyte,"%d", dstx);
strcat(dstInetAddr,lastbyte);

printf("send to destination: %s\n", dstInetAddr);
inet_pton(AF_INET,dstInetAddr,&servaddr.sin_addr);
//servaddr.sin_addr.s_addr = INADDR_ANY;
}
else if(sel == ‘2‘) {
printf("selected unicast dest address\n");
inet_pton(AF_INET,UNI_DST_ADDR,&servaddr.sin_addr);
}
else {
break;
}

for(int i = 0; i < 10; i++)
{
sendto(sockfd, (const char *)hello, strlen(hello),
MSG_CONFIRM, (const struct sockaddr *) &servaddr,
sizeof(servaddr));
printf("Hello message sent %d.\n", i);
sleep(1);
}
}

n = recvfrom(sockfd, (char *)buffer, MAXLINE,
MSG_WAITALL, (struct sockaddr *) &servaddr,
&len);
buffer[n] = ‘\0‘;
printf("Server : %s\n", buffer);

close(sockfd);
return 0;
}

 

2、gcc -o sudpmulti  send-udpmulticast-packet.c

3、本机启动tcpdump

sudo tcpdump -i eth0 -nnvXS udp port 9131

说明:-i 指定网络接口, -nn不解析域名主机名,- X显示以太头, -S 显示绝对序列号

4、./sudpmulti 执行

suse上验证结果:

连续发送:id连续
Line 3: 10:22:33.818167 IP (tos 0x0, ttl 1, id 11129, offset 0, flags [DF], proto UDP (17), length 45)
Line 8: 10:22:33.818208 IP (tos 0x0, ttl 1, id 11130, offset 0, flags [DF], proto UDP (17), length 45)
Line 13: 10:22:33.818223 IP (tos 0x0, ttl 1, id 11131, offset 0, flags [DF], proto UDP (17), length 45)
Line 18: 10:22:33.818256 IP (tos 0x0, ttl 1, id 11132, offset 0, flags [DF], proto UDP (17), length 45)
Line 23: 10:22:33.818291 IP (tos 0x0, ttl 1, id 11133, offset 0, flags [DF], proto UDP (17), length 45)
Line 28: 10:22:33.818334 IP (tos 0x0, ttl 1, id 11134, offset 0, flags [DF], proto UDP (17), length 45)

第二次发送,起始id和上次不连续
Line 39: 10:22:49.166762 IP (tos 0x0, ttl 1, id 11944, offset 0, flags [DF], proto UDP (17), length 45)
Line 44: 10:22:49.166803 IP (tos 0x0, ttl 1, id 11945, offset 0, flags [DF], proto UDP (17), length 45)
Line 49: 10:22:49.166815 IP (tos 0x0, ttl 1, id 11946, offset 0, flags [DF], proto UDP (17), length 45)
Line 54: 10:22:49.166825 IP (tos 0x0, ttl 1, id 11947, offset 0, flags [DF], proto UDP (17), length 45)
Line 59: 10:22:49.166837 IP (tos 0x0, ttl 1, id 11948, offset 0, flags [DF], proto UDP (17), length 45)
Line 64: 10:22:49.166848 IP (tos 0x0, ttl 1, id 11949, offset 0, flags [DF], proto UDP (17), length 45)

增加sleep(1)后,不连续
Line 72: 10:24:59.302272 IP (tos 0x0, ttl 1, id 38853, offset 0, flags [DF], proto UDP (17), length 45)
Line 73: 10:25:00.302518 IP (tos 0x0, ttl 1, id 38938, offset 0, flags [DF], proto UDP (17), length 45)
Line 74: 10:25:01.302784 IP (tos 0x0, ttl 1, id 39038, offset 0, flags [DF], proto UDP (17), length 45)
Line 76: 10:25:02.303051 IP (tos 0x0, ttl 1, id 39258, offset 0, flags [DF], proto UDP (17), length 45)
Line 81: 10:25:03.303385 IP (tos 0x0, ttl 1, id 39416, offset 0, flags [DF], proto UDP (17), length 45)
Line 86: 10:25:04.303635 IP (tos 0x0, ttl 1, id 39523, offset 0, flags [DF], proto UDP (17), length 45)

 

验证UDP组播ipid增长情况(linux C):结果,suse上ipid不一定连续增长

上一篇:win7、vs2013编译webkit总结


下一篇:『学了就忘』Linux基础 — 1、UNIX系统介绍