-
Notifications
You must be signed in to change notification settings - Fork 28
/
Copy pathUDPServerUseRecvmsg.c
106 lines (89 loc) · 2.82 KB
/
UDPServerUseRecvmsg.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/uio.h>
#include <sys/param.h>
#include "../../tool/AddrTool.h"
int main(int argc, char const *argv[])
{
if (argc != 2) {
printf("usage : %s <port>\n", argv[0]);
exit(1);
}
int fd = -1;
if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("socket fd create fail!");
exit(1);
}
int on = 1;
if (setsockopt(fd, IPPROTO_IP, IP_RECVORIGDSTADDR, &on, sizeof(on)) < 0) {
perror("setsockopt error!");
exit(1);
}
struct sockaddr_in svraddr, cliaddr;
bzero(&svraddr, sizeof(svraddr));
svraddr.sin_family = AF_INET;
svraddr.sin_port = htons(atoi(argv[1]));
svraddr.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(fd ,(struct sockaddr *)&svraddr, sizeof(svraddr)) < 0) {
perror("bind error!");
exit(1);
}
char *buf1 = malloc(5);
char *buf2 = malloc(5);
char *buf3 = malloc(10);
char *buf4 = malloc(10);
bzero(buf1, 5);
bzero(buf2, 5);
bzero(buf3, 10);
bzero(buf4, 10);
struct iovec iov[4];
iov[0].iov_base = buf1;
iov[0].iov_len = sizeof(buf1);
iov[1].iov_base = buf2;
iov[1].iov_len = sizeof(buf2);
iov[2].iov_base = buf3;
iov[2].iov_len = sizeof(buf3);
iov[3].iov_base = buf4;
iov[3].iov_len = sizeof(buf4);
struct cmsghdr *cmsgptr;
char control[64];
struct msghdr msg;
bzero(&msg, sizeof(msg));
msg.msg_iov = iov;
msg.msg_iovlen = sizeof(iov) / sizeof(iov[0]);
msg.msg_name = &cliaddr;
msg.msg_namelen = sizeof(cliaddr);
msg.msg_control = control;
msg.msg_controllen = sizeof(control);
if (recvmsg(fd, &msg, 0) < 0) {
perror("recvmsg error!");
exit(1);
}
char *srcInfo = getAddrInfo((struct sockaddr_in *)msg.msg_name);
printf("udp req from %s\n", srcInfo);
free(srcInfo);
/* 用这种方法无法得到 udp 套接字的目的 IP */
char *dstInfo = getSockInfo(fd);
printf("[uncorrect method] udp dst ip/port %s\n", dstInfo);
free(dstInfo);
/* 获得 udp 套接字目的 IP 的正确姿势 */
for (cmsgptr = CMSG_FIRSTHDR(&msg); cmsgptr != NULL; cmsgptr = CMSG_NXTHDR(&msg, cmsgptr)) {
if (cmsgptr->cmsg_level == IPPROTO_IP && cmsgptr->cmsg_type == IP_RECVORIGDSTADDR) {
char *ptr = CMSG_DATA(cmsgptr);
struct sockaddr_in dstaddr;
memcpy(&dstaddr, ptr, sizeof(struct sockaddr_in));
char *addrInfo = getAddrInfo(&dstaddr);
printf("[correct method] udp dst ip/port %s\n", addrInfo);
free(addrInfo);
}
}
printf("buf1 = %s\n", buf1);
printf("buf2 = %s\n", buf2);
printf("buf3 = %s\n", buf3);
printf("buf4 = %s\n", buf4);
return 0;
}