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