-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathclient.c
90 lines (77 loc) · 1.96 KB
/
client.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
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdint.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <string.h>
#include <limits.h>
#ifndef __u8
#define __u8 uint8_t
#endif
struct ipv6_sr_hdr {
__u8 nexthdr;
__u8 hdrlen;
__u8 type;
__u8 segments_left;
__u8 first_segment;
__u8 flag_1;
__u8 flag_2;
__u8 reserved;
struct in6_addr segments[0];
};
int test_sr(const char *dst, short port, const char *segment)
{
int fd, err, srh_len;
struct ipv6_sr_hdr *srh;
struct sockaddr_in6 sin6, sin6_bind;
srh_len = sizeof(*srh) + 2 * sizeof(struct in6_addr);
srh = malloc(srh_len);
if (!srh)
return -1;
srh->nexthdr = 0;
srh->hdrlen = 4;
srh->type = 4;
srh->segments_left = 1;
srh->first_segment = 1;
srh->flag_1 = 0;
srh->flag_2 = 0;
srh->reserved = 0;
memset(&srh->segments[0], 0, sizeof(struct in6_addr));
inet_pton(AF_INET6, segment, &srh->segments[1]);
fd = socket(AF_INET6, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
return -1;
}
err = setsockopt(fd, IPPROTO_IPV6, IPV6_RTHDR, srh, srh_len);
if (err < 0) {
perror("setsockopt");
close(fd);
return -1;
}
memset(&sin6, 0, sizeof(sin6));
sin6.sin6_family = AF_INET6;
sin6.sin6_port = htons(port);
inet_pton(AF_INET6, dst, &sin6.sin6_addr);
static char buffer[] = "Hello, I'm here\n";
int buffer_size;
buffer_size = strlen(buffer);
int n;
n = sendto(fd, buffer, buffer_size, 0, (struct sockaddr *) &sin6, sizeof(sin6));
if (n < 0) {
perror("Error sending UDP message");
return -1;
}
return 0;
}
int main(int ac, char **av)
{
if (ac < 4) {
fprintf(stderr, "Usage: %s dst port segment\n", av[0]);
return -1;
}
return test_sr(av[1], atoi(av[2]), av[3]);
}