]> git.proxmox.com Git - mirror_frr.git/blob - babeld/net.c
Merge pull request #624 "Babel"
[mirror_frr.git] / babeld / net.c
1 /*
2 Copyright (c) 2007, 2008 by Juliusz Chroboczek
3
4 Permission is hereby granted, free of charge, to any person obtaining a copy
5 of this software and associated documentation files (the "Software"), to deal
6 in the Software without restriction, including without limitation the rights
7 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
8 copies of the Software, and to permit persons to whom the Software is
9 furnished to do so, subject to the following conditions:
10
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
13
14 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
15 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
16 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
17 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
18 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
19 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
20 THE SOFTWARE.
21 */
22
23 #include <unistd.h>
24 #include <fcntl.h>
25 #include <string.h>
26 #include <sys/ioctl.h>
27 #include <sys/types.h>
28 #include <sys/uio.h>
29 #include <sys/socket.h>
30 #include <netinet/in.h>
31 #include <netinet/in_systm.h>
32 #include <netinet/ip.h>
33 #include <arpa/inet.h>
34 #include <errno.h>
35
36 #include "babeld.h"
37 #include "util.h"
38 #include "net.h"
39 #include "sockopt.h"
40
41 int
42 babel_socket(int port)
43 {
44 struct sockaddr_in6 sin6;
45 int s, rc;
46 int saved_errno;
47 int one = 1, zero = 0;
48
49 s = socket(PF_INET6, SOCK_DGRAM, 0);
50 if(s < 0)
51 return -1;
52
53 rc = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
54 if(rc < 0)
55 goto fail;
56
57 rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
58 if(rc < 0)
59 goto fail;
60
61 rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
62 &zero, sizeof(zero));
63 if(rc < 0)
64 goto fail;
65
66 rc = setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
67 &one, sizeof(one));
68 if(rc < 0)
69 goto fail;
70
71 rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
72 &one, sizeof(one));
73 if(rc < 0)
74 goto fail;
75
76 setsockopt_ipv6_tclass (s, IPTOS_PREC_INTERNETCONTROL);
77
78 rc = fcntl(s, F_GETFL, 0);
79 if(rc < 0)
80 goto fail;
81
82 rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
83 if(rc < 0)
84 goto fail;
85
86 rc = fcntl(s, F_GETFD, 0);
87 if(rc < 0)
88 goto fail;
89
90 rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
91 if(rc < 0)
92 goto fail;
93
94 memset(&sin6, 0, sizeof(sin6));
95 sin6.sin6_family = AF_INET6;
96 sin6.sin6_port = htons(port);
97 rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
98 if(rc < 0)
99 goto fail;
100
101 return s;
102
103 fail:
104 saved_errno = errno;
105 close(s);
106 errno = saved_errno;
107 return -1;
108 }
109
110 int
111 babel_recv(int s, void *buf, int buflen, struct sockaddr *sin, int slen)
112 {
113 struct iovec iovec;
114 struct msghdr msg;
115 int rc;
116
117 memset(&msg, 0, sizeof(msg));
118 iovec.iov_base = buf;
119 iovec.iov_len = buflen;
120 msg.msg_name = sin;
121 msg.msg_namelen = slen;
122 msg.msg_iov = &iovec;
123 msg.msg_iovlen = 1;
124
125 rc = recvmsg(s, &msg, 0);
126 return rc;
127 }
128
129 int
130 babel_send(int s,
131 void *buf1, int buflen1, void *buf2, int buflen2,
132 struct sockaddr *sin, int slen)
133 {
134 struct iovec iovec[2];
135 struct msghdr msg;
136 int rc;
137
138 iovec[0].iov_base = buf1;
139 iovec[0].iov_len = buflen1;
140 iovec[1].iov_base = buf2;
141 iovec[1].iov_len = buflen2;
142 memset(&msg, 0, sizeof(msg));
143 msg.msg_name = (struct sockaddr*)sin;
144 msg.msg_namelen = slen;
145 msg.msg_iov = iovec;
146 msg.msg_iovlen = 2;
147
148 again:
149 rc = sendmsg(s, &msg, 0);
150 if(rc < 0) {
151 if(errno == EINTR)
152 goto again;
153 else if(errno == EAGAIN) {
154 int rc2;
155 rc2 = wait_for_fd(1, s, 5);
156 if(rc2 > 0)
157 goto again;
158 errno = EAGAIN;
159 }
160 }
161 return rc;
162 }
163
164 int
165 tcp_server_socket(int port, int local)
166 {
167 struct sockaddr_in6 sin6;
168 int s, rc, saved_errno;
169 int one = 1;
170
171 s = socket(PF_INET6, SOCK_STREAM, 0);
172 if(s < 0)
173 return -1;
174
175 rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
176 if(rc < 0)
177 goto fail;
178
179 rc = fcntl(s, F_GETFL, 0);
180 if(rc < 0)
181 goto fail;
182
183 rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
184 if(rc < 0)
185 goto fail;
186
187 rc = fcntl(s, F_GETFD, 0);
188 if(rc < 0)
189 goto fail;
190
191 rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
192 if(rc < 0)
193 goto fail;
194
195 memset(&sin6, 0, sizeof(sin6));
196 sin6.sin6_family = AF_INET6;
197 sin6.sin6_port = htons(port);
198 if(local) {
199 rc = inet_pton(AF_INET6, "::1", &sin6.sin6_addr);
200 if(rc < 0)
201 goto fail;
202 }
203 rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
204 if(rc < 0)
205 goto fail;
206
207 rc = listen(s, 2);
208 if(rc < 0)
209 goto fail;
210
211 return s;
212
213 fail:
214 saved_errno = errno;
215 close(s);
216 errno = saved_errno;
217 return -1;
218 }