- 论坛徽章:
- 0
|
I run the following program:
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <string.h>
#include <netinet/in.h>
#include <unistd.h>
#include <stdlib.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <time.h>
#include <linux/if_ether.h>
#include <linux/if_packet.h>
struct timeval time1,time2;
int timediff;
int open_socket()
{
//Define the socket
int arp_sock;
struct sockaddr_ll haddr;
int retval;
gettimeofday(&time1,NULL);
arp_sock = socket( PF_PACKET, SOCK_RAW, htons(ETH_P_ARP) );
if( arp_sock < 0)
{
printf(" failed to create arp_socket, arp_sock=%d with error(%d/%s)\n", arp_sock, errno, strerror(errno));
return -1;
}
gettimeofday(&time2,NULL);
timediff = (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec-time1.tv_usec;
printf(" the duration of open SOCK_RAW socket of PF_PACKET:%d us\n", timediff);
memset(&haddr, 0, sizeof(haddr));
haddr.sll_family = PF_PACKET;
haddr.sll_protocol = htons( ETH_P_ARP );
haddr.sll_ifindex = 2;
gettimeofday(&time1,NULL);
retval = bind( arp_sock,(struct sockaddr *)&haddr,sizeof( struct sockaddr_ll ) );
if( retval < 0 )
{
printf(" failed to bind, retval=%d with error(%d/%s)\n", retval, errno, strerror(errno));
return -1;
}
gettimeofday(&time2,NULL);
timediff = (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec-time1.tv_usec;
printf(" the duration of bind SOCK_RAW socket of PF_PACKET:%d us\n", timediff);
return arp_sock;
}
int main(int argc, char *argv[])
{
int retval = -1;
int i;
for( i=1; i<=10; i++)
{
printf("\ti=%d \n", i);
gettimeofday(&time1,NULL);
retval = open_socket();
if ( retval < 0 )
{
return -1;
}
gettimeofday(&time2,NULL);
timediff = (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec-time1.tv_usec;
printf(" the duration of open and bind SOCK_RAW socket of PF_PACKET:%d us \n", timediff);
if ( retval > 0)
{
gettimeofday(&time1,NULL);
close(retval);
gettimeofday(&time2,NULL);
timediff = (time2.tv_sec-time1.tv_sec)*1000000 + time2.tv_usec-time1.tv_usec;
printf(" the duration of close SOCK_RAW socket of PF_PACKET:%d us\n", timediff);
}
printf("\n");
}
return 0;
}
|
|