1. 程式人生 > >Linux網路程式設計——原始套接字程式設計

Linux網路程式設計——原始套接字程式設計

原始套接字的建立

int socket ( int family, int type, int protocol );

引數:

family:協議族 這裡寫 PF_PACKET

type:  套接字類,這裡寫 SOCK_RAW

protocol:協議類別,指定可以接收或傳送的資料包型別,不能寫 “0”,取值如下,注意,傳參時需要用 htons() 進行位元組序轉換。

ETH_P_IP:IPV4資料包

ETH_P_ARP:ARP資料包

ETH_P_ALL:任何協議型別的資料包

返回值:

成功( >0 ):套接字,這裡為鏈路層的套接字

失敗( <0 ):出錯

例項如下:

  1. // 所需標頭檔案
  2. #include <sys/socket.h>
  3. #include <netinet/ether.h>
  4. #include <stdio.h>  // perror
  5. int main(int argc,char *argv[])  
  6. {  
  7.     int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL) );  
  8.     if(sock_raw_fd < 0){  
  9.         perror("socket");  
  10.         return -1;  
  11.     }  
  12.     return 0;  
  13. }  

獲取鏈路層的資料包

ssize_t recvfrom(  int sockfd, 

void *buf, 

size_t nbytes,

int flags,

struct sockaddr *from, 

socklen_t *addrlen );

引數:

sockfd:原始套接字

buf:接收資料緩衝區

nbytes:接收資料緩衝區的大小

flags:套接字標誌(常為0)

from:這裡沒有用,寫 NULL

addrlen:這裡沒有用,寫 NULL

返回值:

成功:接收到的字元數

失敗:-1

例項如下:

  1. #include <stdio.h>
  2. #include <netinet/in.h>
  3. #include <sys/socket.h>
  4. #include <netinet/ether.h>
  5. int main(int argc,char *argv[])  
  6. {  
  7.     unsigned char buf[1024] = {0};  
  8.     int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));  
  9.     //獲取鏈路層的資料包
  10.     int len = recvfrom(sock_raw_fd, buf, sizeof(buf), 0, NULL, NULL);  
  11.     printf("len = %d\n", len);  
  12.     return 0;  
  13. }  

混雜模式

預設的情況下,我們接收資料,目的地址是本地地址,才會接收。有時候我們想接收所有經過網絡卡的所有資料流,而不論其目的地址是否是它,這時候我們需要設定網絡卡為混雜模式

網絡卡的混雜模式一般在網路管理員分析網路資料作為網路故障診斷手段時用到,同時這個模式也被網路黑客利用來作為網路資料竊聽的入口。在 Linux 作業系統中設定網絡卡混雜模式時需要管理員許可權。在 Windows 作業系統和 Linux 作業系統中都有使用混雜模式的抓包工具,比如著名的開源軟體 Wireshark。

通過命令給 Linux 網絡卡設定混雜模式(需要管理員許可權)

設定混雜模式:ifconfig eth0 promisc

取消混雜模式:ifconfig eth0 -promisc

通過程式碼給 Linux 網絡卡設定混雜模式

程式碼如下:

  1. struct ifreq req;   //網路介面地址
  2. strncpy(req.ifr_name, "eth0", IFNAMSIZ);            //指定網絡卡名稱
  3. if(-1 == ioctl(sock_raw_fd, SIOCGIFINDEX, &req))    //獲取網路介面
  4. {  
  5.     perror("ioctl");  
  6.     close(sock_raw_fd);  
  7.     exit(-1);  
  8. }  
  9. req.ifr_flags |= IFF_PROMISC;  
  10. if(-1 == ioctl(sock_raw_fd, SIOCSIFINDEX, &req))    //網絡卡設定混雜模式
  11. {  
  12.     perror("ioctl");  
  13.     close(sock_raw_fd);  
  14.     exit(-1);  
  15. }  

傳送自定義的資料包:

ssize_t sendto(   int sockfd,

const void *buf,

size_t nbytes,int flags,

const struct sockaddr *to,        

socklen_t addrlen );

引數:

sockfd:原始套接字

buf:傳送資料緩衝區

nbytes:傳送資料緩衝區的大小

flags:一般為 0

to本機網路介面,指傳送的資料應該從本機的哪個網絡卡出去,而不是以前的目的地址

addrlen:to 所指向內容的長度

返回值:

成功:傳送資料的字元數

失敗: -1

本機網路介面的定義

傳送完整程式碼如下:

  1. struct sockaddr_ll sll;                 //原始套接字地址結構
  2. struct ifreq req;                   //網路介面地址
  3. strncpy(req.ifr_name, "eth0", IFNAMSIZ);            //指定網絡卡名稱
  4. if(-1 == ioctl(sock_raw_fd, SIOCGIFINDEX, &req))    //獲取網路介面
  5. {  
  6.     perror("ioctl");  
  7.     close(sock_raw_fd);  
  8.     exit(-1);  
  9. }  
  10. /*將網路介面賦值給原始套接字地址結構*/
  11. bzero(&sll, sizeof(sll));  
  12. sll.sll_ifindex = req.ifr_ifindex;  
  13. // 傳送資料
  14. // send_msg, msg_len 這裡還沒有定義,模擬一下
  15. int len = sendto(sock_raw_fd, send_msg, msg_len, 0 , (struct sockaddr *)&sll, sizeof(sll));  
  16. if(len == -1)  
  17. {  
  18.     perror("sendto");  
  19. }  

這裡標頭檔案情況如下:

  1. #include <net/if.h>// struct ifreq
  2. #include <sys/ioctl.h> // ioctl、SIOCGIFADDR
  3. #include <sys/socket.h> // socket
  4. #include <netinet/ether.h> // ETH_P_ALL
  5. #include <netpacket/packet.h> // struct sockaddr_ll