Linux 網路編程——原始通訊端執行個體:發送 UDP 資料包

來源:互聯網
上載者:User

標籤:原始通訊端   網路編程   udp   

乙太網路報文格式:


詳細的說明,請看《MAC 頭部報文分析》。


IP 報文格式:



詳細的說明,請看《IP 資料報格式詳解》。


UDP 報文格式:



詳細的說明,請看《UDP 資料報格式詳解》。


校正和函數:

/*******************************************************功能:校正和函數參數:buf: 需要校正資料的首地址nword: 需要校正資料長度的一半傳回值:校正和*******************************************************/unsigned short checksum(unsigned short *buf, int nword){unsigned long sum;for(sum = 0; nword > 0; nword--){sum += htons(*buf);buf++;}sum = (sum>>16) + (sum&0xffff);sum += (sum>>16);return ~sum;}



這裡是在 ubuntu 下通過原始通訊端組一個 udp 資料包,給 PC 機的網路調試助手發送資訊:

#include <stdio.h>#include <stdlib.h>#include <string.h>#include <net/if.h>//struct ifreq#include <sys/ioctl.h>//ioctl、SIOCGIFADDR#include <sys/socket.h>#include <netinet/ether.h>//ETH_P_ALL#include <netpacket/packet.h>//struct sockaddr_llunsigned short checksum(unsigned short *buf, int nword);//校正和函數int main(int argc, char *argv[]){//1.建立通訊用的原始通訊端int sock_raw_fd = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL));//2.根據各種協議首部格式構建發送資料報unsigned char send_msg[1024] = {//--------------組MAC--------14------0x74, 0x27, 0xea, 0xb5, 0xef, 0xd8, //dst_mac: 74-27-EA-B5-FF-D80xc8, 0x9c, 0xdc, 0xb7, 0x0f, 0x19, //src_mac: c8:9c:dc:b7:0f:190x08, 0x00,                         //類型:0x0800 IP協議//--------------組IP---------20------0x45, 0x00, 0x00, 0x00,             //版本號碼:4, 首部長度:20位元組, TOS:0, --總長度--:0x00, 0x00, 0x00, 0x00,//16位標識、3位標誌、13位片位移都設定00x80, 17,   0x00, 0x00,//TTL:128、協議:UDP(17)、16位首部校正和10,  221,   20,  11,//src_ip: 10.221.20.1110,  221,   20,  10,//dst_ip: 10.221.20.10//--------------組UDP--------8+78=86------0x1f, 0x90, 0x1f, 0x90,             //src_port:0x1f90(8080), dst_port:0x1f90(8080)0x00, 0x00, 0x00, 0x00,               //#--16位UDP長度--30個位元組、#16位校正和};int len = sprintf(send_msg+42, "%s", "this is for the udp test");if(len % 2 == 1)//判斷len是否為奇數{len++;//如果是奇數,len就應該加1(因為UDP的資料部分如果不為偶數需要用0填補)}*((unsigned short *)&send_msg[16]) = htons(20+8+len);//IP總長度 = 20 + 8 + len*((unsigned short *)&send_msg[14+20+4]) = htons(8+len);//udp總長度 = 8 + len//3.UDP偽頭部unsigned char pseudo_head[1024] = {//------------UDP偽頭部--------12--10,  221,   20,  11,//src_ip: 10.221.20.1110,  221,   20,  10,//dst_ip: 10.221.20.100x00, 17,   0x00, 0x00,             //0,17,#--16位UDP長度--20個位元組};*((unsigned short *)&pseudo_head[10]) = htons(8 + len);//為頭部中的udp長度(和真實udp長度是同一個值)//4.構建udp校正和需要的資料報 = udp偽頭部 + udp資料報memcpy(pseudo_head+12, send_msg+34, 8+len);//--計算udp校正和時需要加上偽頭部--//5.對IP首部進行校正*((unsigned short *)&send_msg[24]) = htons(checksum((unsigned short *)(send_msg+14),20/2));//6.--對UDP資料進行校正--*((unsigned short *)&send_msg[40]) = htons(checksum((unsigned short *)pseudo_head,(12+8+len)/2));//6.發送資料struct sockaddr_ll sll;//原始通訊端地址結構struct ifreq ethreq;//網路介面地址strncpy(ethreq.ifr_name, "eth0", IFNAMSIZ);//指定網卡名稱if(-1 == ioctl(sock_raw_fd, SIOCGIFINDEX, ðreq))//擷取網路介面{perror("ioctl");close(sock_raw_fd);exit(-1);}/*將網路介面賦值給原始通訊端地址結構*/bzero(&sll, sizeof(sll));sll.sll_ifindex = ethreq.ifr_ifindex;len = sendto(sock_raw_fd, send_msg, 14+20+8+len, 0 , (struct sockaddr *)&sll, sizeof(sll));if(len == -1){perror("sendto");}return 0;}unsigned short checksum(unsigned short *buf, int nword){unsigned long sum;for(sum = 0; nword > 0; nword--){sum += htons(*buf);buf++;}sum = (sum>>16) + (sum&0xffff);sum += (sum>>16);return ~sum;}

運行結果如下:



原始碼下載請點此處。


Linux 網路編程——原始通訊端執行個體:發送 UDP 資料包

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.