-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreceiver4.c
More file actions
67 lines (62 loc) · 1.52 KB
/
receiver4.c
File metadata and controls
67 lines (62 loc) · 1.52 KB
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
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <stdio.h>
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
int
main(void) {
char c;
struct sockaddr_in addr = {
.sin_family = AF_INET,
#if defined(__APPLE__) || defined(__FreeBSD__)
.sin_len = sizeof(struct sockaddr_in),
#endif
.sin_addr.s_addr = htonl(INADDR_ANY),
.sin_port = htons(1234)
};
char cmsgcmsg[CMSG_SPACE(sizeof(unsigned char))];
struct iovec iov = {
.iov_base = &c,
.iov_len = sizeof(char)
};
struct msghdr msg = {
.msg_iov = &iov,
.msg_iovlen = 1,
.msg_control = cmsgcmsg,
.msg_controllen = CMSG_SPACE(sizeof(unsigned char))
};
struct cmsghdr *cmsg;
int fd;
const int on = 1;
unsigned char tos;
if ((fd = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
perror("socket");
if (bind(fd, (struct sockaddr *) &addr, sizeof(struct sockaddr_in)) < 0)
perror("bind");
if (setsockopt(fd, IPPROTO_IP, IP_RECVTOS, &on, sizeof(int)) < 0)
perror("setsockopt");
for (;;) {
if (recvmsg(fd, &msg, 0) < 0) {
perror("recvmsg");
continue;
}
for (cmsg = CMSG_FIRSTHDR(&msg); cmsg != NULL; cmsg = CMSG_NXTHDR(&msg,cmsg)) {
if ((cmsg->cmsg_level == IPPROTO_IP) &&
#if defined(__linux__)
(cmsg->cmsg_type == IP_TOS) &&
#else
(cmsg->cmsg_type == IP_RECVTOS) &&
#endif
(cmsg->cmsg_len == CMSG_LEN(sizeof(unsigned char)))) {
tos = *(unsigned char *)CMSG_DATA(cmsg);
printf("tos = 0x%02x\n", tos);
}
}
}
if (close(fd) < 0)
perror("close");
return (0);
}