发送给原始套接字的数据----疑问:
#include<stdio.h>
#include<netinet/in.h>
#include<sys/socket.h>
#include<netinet/ether.h>
#include<net/if.h>
#include<sys/ioctl.h>
#include<netpacket/packet.h>
/*
ETH_P_ALL : 任何协议类型的数据包
*/
int main(int argc,char* argv[])
{
charp[]="hello world\n";
structsockaddr_ll sll;
structifreq ethreq;
int sock_raw_fd;
//strcpy(ethreq.ifr_name,"eth0",IFNAMSIZ); //
//createa socket
sock_raw_fd=socket(PF_INET,SOCK_RAW,htons(ETH_P_ALL));
/*if(-1==ioctl(sock_raw_fd,SIOCGIFINDEX,Qreq)) //
{
perror("ioctl");
close(sock_raw_fd);
exit(-1);
}
*/
/*
如何看待 ioctl()函数??
*/
/*
将网络接口赋值给原始套接字地址结构
*/
bzero(&sll,sizeof(sll));
sll.sll_ifindex=ethreq.ifr_ifindex;
//发送数据
//send_msg, msg_len 这里还没有定义,模拟一下
int len=sendto(sock_raw_fd,p,sizeof(p),0,(structsockaddr*)&sll,sizeof(sll));
if(len==-1)
{
perror("sendto");
}else
printf("len=%d\n",len);
return0;
}
/*
我现在有个疑问:
虽然,我们创建了一个原始套接字,但这个原始套接字并没有进行绑定操作,即:这个原始套接字没有IP地址和端口号,
那么,当我们把一个数据发送给这个原始套接字时,这个原始套接字如何处理这个数据呢???
这个原始套接字怎么知道该把这个数据包发送到哪个位置呢???
我们来看一下,数据包的发送过程是怎样的::????
这个数据包会不会经由网卡发送出去???
为什么???
*/