-
Notifications
You must be signed in to change notification settings - Fork 4
/
send.c
130 lines (107 loc) · 3.05 KB
/
send.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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
/*
* Authors:
* Alexander Aring <alex.aring@gmail.com>
*
* Original Authors:
* Lars Fenneberg <lf@elemental.net>
*
* This software is Copyright 1996,1997,2019 by the above mentioned author(s),
* All Rights Reserved.
*
* The license which is distributed with this software in the file COPYRIGHT
* applies to this software. If your distribution is missing this file, you
* may request it from <alex.aring@gmail.com>.
*/
#include <linux/ipv6.h>
#include <netinet/icmp6.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "helpers.h"
#include "buffer.h"
#include "config.h"
#include "config.h"
#include "send.h"
#include "log.h"
#include "rpl.h"
static int really_send(int sock, const struct iface *iface,
const struct in6_addr *dest,
struct safe_buffer *sb)
{
struct sockaddr_in6 addr;
int rc;
memset((void *)&addr, 0, sizeof(addr));
addr.sin6_family = AF_INET6;
addr.sin6_port = htons(IPPROTO_ICMPV6);
memcpy(&addr.sin6_addr, dest, sizeof(struct in6_addr));
struct iovec iov;
iov.iov_len = sb->used;
iov.iov_base = (caddr_t)sb->buffer;
char __attribute__((aligned(8))) chdr[CMSG_SPACE(sizeof(struct in6_pktinfo))];
memset(chdr, 0, sizeof(chdr));
struct cmsghdr *cmsg = (struct cmsghdr *)chdr;
cmsg->cmsg_len = CMSG_LEN(sizeof(struct in6_pktinfo));
cmsg->cmsg_level = IPPROTO_IPV6;
cmsg->cmsg_type = IPV6_PKTINFO;
struct in6_pktinfo *pkt_info = (struct in6_pktinfo *)CMSG_DATA(cmsg);
pkt_info->ipi6_ifindex = iface->ifindex;
memcpy(&pkt_info->ipi6_addr, iface->ifaddr_src, sizeof(struct in6_addr));
#ifdef HAVE_SIN6_SCOPE_ID
if (IN6_IS_ADDR_LINKLOCAL(&addr.sin6_addr) || IN6_IS_ADDR_MC_LINKLOCAL(&addr.sin6_addr))
addr.sin6_scope_id = iface->ifindex;
#endif
struct msghdr mhdr;
memset(&mhdr, 0, sizeof(mhdr));
mhdr.msg_name = (caddr_t)&addr;
mhdr.msg_namelen = sizeof(struct sockaddr_in6);
mhdr.msg_iov = &iov;
mhdr.msg_iovlen = 1;
mhdr.msg_control = (void *)cmsg;
mhdr.msg_controllen = sizeof(chdr);
rc = sendmsg(sock, &mhdr, 0);
safe_buffer_free(sb);
return rc;
}
void send_dio(int sock, struct dag *dag)
{
struct safe_buffer *sb;
int rc;
sb = safe_buffer_new();
if (!sb)
return;
dag_build_dio(dag, sb);
rc = really_send(sock, dag->iface, &all_rpl_addr, sb);
flog(LOG_INFO, "foo! %s %d %s", dag->iface->ifname, rc ,strerror(errno));
}
void send_dao(int sock, const struct in6_addr *to, struct dag *dag)
{
struct safe_buffer *sb;
int rc;
sb = safe_buffer_new();
if (!sb)
return;
dag_build_dao(dag, sb);
rc = really_send(sock, dag->iface, to, sb);
flog(LOG_INFO, "send_dao! %d", rc);
}
void send_dao_ack(int sock, const struct in6_addr *to, struct dag *dag)
{
struct safe_buffer *sb;
int rc;
sb = safe_buffer_new();
if (!sb)
return;
dag_build_dao_ack(dag, sb);
rc = really_send(sock, dag->iface, to, sb);
flog(LOG_INFO, "send_dao_ack! %d", rc);
}
void send_dis(int sock, struct iface *iface)
{
struct safe_buffer *sb;
int rc;
sb = safe_buffer_new();
if (!sb)
return;
dag_build_dis(sb);
rc = really_send(sock, iface, &all_rpl_addr, sb);
flog(LOG_INFO, "send_dis! %d", rc);
}