一.大致流程:
建立一个client端,一个server端,自己构建IP头和UDP头,写入数据(hello,world!)后通过原始套接字(SOCK_RAW)将包发出去。
server端收到数据后,打印UDP数据并发送确认消息(yes),client收到yes后将其打印。
二.其中:
client端IP:192.168.11.104 端口:8600
server端IP:192.168.11.105 端口:8686
三.注意事项:
1.运行原始套接字socket需要有root权限。
2.注意主机字节序和网络字节序的转
四.涉及的数据结构
1.ip部分的结构图:
2.ip结构体定义:
struct iphdr /* 该结构体在<netinet/ip.h>中定义 */
{
#if __BYTE_ORDER == __LITTLE_ENDIAN /* 如果是小端字节序 */
unsigned int ihl:; /*首部长度*/
unsigned int version:; /* 版本 */
#elif __BYTE_ORDER == __BIG_ENDIAN
unsigned int version:;
unsigned int ihl:;
#else
# error "Please fix <bits/endian.h>"
#endif
u_int8_t tos; /* 区分服务 */
u_int16_t tot_len; /* 总长度 */
u_int16_t id; /* 标识 */
u_int16_t frag_off; /* 标志(3位)+片偏移(13位) */
u_int8_t ttl; /* 生存时间 */
u_int8_t protocol; /* 协议 */
u_int16_t check; /* 首部检验和 */
u_int32_t saddr; /* 源地址 */
u_int32_t daddr; /* 目的地址 */
/*The options start here. */
};
3.udp数据包的结构图:
4.udp结构体定义:
struct udphdr /* 该结构体在<netiniet/udp.h>中定义 */
{
u_int16_t source; /*源端口*/
u_int16_t dest; /*目的端口*/
u_int16_t len; /*长度*/
u_int16_t check; /*校验和*/
};
五.实现如下:
client端:
/*
============================================================================
Name : test_client.c
Author : huh
Version :
Copyright : ---notice---
Description : Hello World in C, Ansi-style
============================================================================
*/ #include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/ip_icmp.h>
#include <netinet/udp.h> #define MAXLINE 1024*10 struct udp_front //udp
{
uint32_t srcip;
uint32_t desip;
u_int8_t zero;
u_int8_t protocol;
u_int16_t len;
}; u_int16_t in_chksum(u_int16_t *addr, int len);
u_int16_t udp_check(char *sendbuf, int len, const struct udp_front front);
int make_message(char sendbuf[], int send_buf_len, uint32_t src_ip, u_int16_t src_port, uint32_t des_ip, u_int16_t des_port); int main()
{
int raw_sockfd;
int size = *;
char send_message[MAXLINE];
struct sockaddr_in server_address;
//创建原始套接字
raw_sockfd = socket(AF_INET, SOCK_RAW, IPPROTO_UDP);
//创建套接字地址
bzero(&server_address,sizeof(server_address));
server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr("192.168.11.105");
//设置套接字为随数据包含IP首部(设置这个选项后需要我们手动写入IP头)
setsockopt(raw_sockfd, IPPROTO_IP, IP_HDRINCL, &size, sizeof(size)); int len;
bzero(&send_message, sizeof(send_message));
//拼接完整的UDP数据包(IP头+UDP头+数据)
int mesg_len = make_message(send_message, MAXLINE, inet_addr("192.168.11.104"), , inet_addr("192.168.11.105"), );
//将IP数据包发送出去
sendto(raw_sockfd, send_message, mesg_len, , (struct sockaddr *)&server_address, sizeof(server_address));
close(raw_sockfd);
//
//下面我们开始接受服务器返回的包
int client_sockfd;
int server_len;
char recv_message[MAXLINE];
struct sockaddr_in server_addr;
client_sockfd = socket(AF_INET, SOCK_DGRAM, );
server_addr.sin_family = AF_INET;
server_addr.sin_addr.s_addr = inet_addr("192.168.11.104");
server_addr.sin_port = htons();
server_len = sizeof(server_address);
bind(client_sockfd, (struct sockaddr *)&server_addr, server_len); bzero(&recv_message, sizeof(recv_message));
len = recvfrom(client_sockfd, recv_message, MAXLINE, , NULL, NULL);
printf("收到的应答:%s\n",recv_message);
return ;
} //拼接IP数据报
int make_message(char sendbuf[], int send_buf_len, uint32_t src_ip, u_int16_t src_port, uint32_t des_ip, u_int16_t des_port)
{
char message[];
bzero(message, sizeof(message));
strcpy(message,"hello,world!");
printf("message len:%d\n",strlen(message));
struct iphdr *ip;
ip = (struct iphdr *)sendbuf;
ip->ihl = sizeof(struct iphdr) >> ; //首部长度
ip->version = ; //ip协议版本
ip->tos = ; //服务类型字段
ip->tot_len = ; //总长度
ip->id = ; //
ip->frag_off = ;
ip->ttl = ;
ip->protocol = IPPROTO_UDP;
ip->check = ; //内核会算相应的效验和
ip->saddr = src_ip;
ip->daddr = des_ip; struct udp_front front;
front.srcip = src_ip;
front.desip = des_ip;
front.len = htons(+strlen(message));
front.protocol = ;
front.zero = ; struct udphdr *udp;
udp = (struct udphdr *)(sendbuf + sizeof(struct iphdr));
udp->source = htons(src_port); //源端口
udp->dest = htons(des_port); //目的端口
udp->check = ; //效验和,效验整个udp数据报
strcpy((sendbuf++), message);
udp->len = htons(+strlen(message)); //udp数据报总长度 udp->check = udp_check((sendbuf+), +strlen(message), front); ip->tot_len = htons( + + strlen(message)); //总长度
printf("ip->tot_len:%d\n",ip->tot_len);
ip->check = in_chksum((unsigned short *)sendbuf, ); return ntohs(ip->tot_len);
} //计算udp效验和
unsigned short udp_check(char *sendbuf, int len, const struct udp_front front)
{
char str[MAXLINE];
bzero(&str, MAXLINE);
bcopy(&front, str, sizeof(front));
bcopy(sendbuf, str+sizeof(front), len);
struct udp_front *ptr;
ptr = (struct udp_front *)str;
char *s;
s = (str+);
return in_chksum((unsigned short *)str, sizeof(front)+len);
} //效验和算法
uint16_t in_chksum(uint16_t *addr, int len)
{
int nleft = len;
uint32_t sum = ;
uint16_t *w = addr;
uint16_t answer = ;
//把ICMP报头二进制数据以2字节为单位累加起来
while (nleft > )
{
sum += *w++;
nleft -= ;
}
if (nleft == )
{
*(unsigned char *)(&answer) = *(unsigned char *)w;
sum += answer;
}
sum = (sum>>) + (sum&0xffff);
sum += (sum>>);
answer = ~sum;
return answer;
}
server端:
server端是一个简单的收包服务器,监听8686端口,当有udp数据包到来时,打印信息并返回给client一个信息。
#include <sys/types.h>
#include <sys/socket.h>
#include <stdio.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h> #define HOST_IP "192.168.11.105"
#define HOST_PORT 8686 #define MAXLINE 1024*50 int main()
{
int server_sockfd;
int server_len, client_len;
struct sockaddr_in server_address;
struct sockaddr_in client_address; server_sockfd = socket(AF_INET, SOCK_DGRAM, ); server_address.sin_family = AF_INET;
server_address.sin_addr.s_addr = inet_addr(HOST_IP);
server_address.sin_port = htons(HOST_PORT); server_len = sizeof(server_address);
bind(server_sockfd, (struct sockaddr *)&server_address, server_len); for( ; ; )
{
int len;
char recv_mesg[MAXLINE];
char send_mesg[];
client_len = sizeof(struct sockaddr_in);
printf("server2 waiting!\n");
len = recvfrom(server_sockfd, recv_mesg, MAXLINE, , (struct sockaddr *) &client_address, (socklen_t *) &client_len);
printf("收到包的长度为:%d\n",len);
printf("%s\n",recv_mesg);
strcpy(send_mesg,"yes");
sendto(server_sockfd, send_mesg, strlen(send_mesg), , (struct sockaddr *) &client_address, client_len); //将包发出去
}
return ;
}
server.c