23 lines
453 B
C
23 lines
453 B
C
/*创建链路层的原始套接字*/
|
|
#include <netinet/ether.h>
|
|
#include <netinet/in.h>
|
|
#include <sys/socket.h>
|
|
#include <stdio.h>
|
|
#include <unistd.h>
|
|
|
|
int main(int argc, char const *argv[])
|
|
{
|
|
// 创建原始套接字
|
|
int sock_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));
|
|
if (sock_fd < 0)
|
|
{
|
|
perror("raw socket");
|
|
return -1;
|
|
}
|
|
printf("原始套接字创建成功\n");
|
|
|
|
close(sock_fd);
|
|
|
|
return 0;
|
|
}
|