]>
git.proxmox.com Git - mirror_frr.git/blob - babeld/net.c
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.
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.
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/>.
15 * This file incorporates work covered by the following copyright and
18 Copyright (c) 2007, 2008 by Juliusz Chroboczek
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:
27 The above copyright notice and this permission notice shall be included in
28 all copies or substantial portions of the Software.
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
42 #include <sys/ioctl.h>
43 #include <sys/types.h>
45 #include <sys/socket.h>
46 #include <netinet/in.h>
47 #include <arpa/inet.h>
55 babel_socket(int port
)
57 struct sockaddr_in6 sin6
;
60 int one
= 1, zero
= 0;
62 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
66 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, &one
, sizeof(one
));
70 rc
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
74 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
79 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
84 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
89 rc
= fcntl(s
, F_GETFL
, 0);
93 rc
= fcntl(s
, F_SETFL
, (rc
| O_NONBLOCK
));
97 rc
= fcntl(s
, F_GETFD
, 0);
101 rc
= fcntl(s
, F_SETFD
, rc
| FD_CLOEXEC
);
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
));
122 babel_recv(int s
, void *buf
, int buflen
, struct sockaddr
*sin
, int slen
)
128 memset(&msg
, 0, sizeof(msg
));
129 iovec
.iov_base
= buf
;
130 iovec
.iov_len
= buflen
;
132 msg
.msg_namelen
= slen
;
133 msg
.msg_iov
= &iovec
;
136 rc
= recvmsg(s
, &msg
, 0);
142 void *buf1
, int buflen1
, void *buf2
, int buflen2
,
143 struct sockaddr
*sin
, int slen
)
145 struct iovec iovec
[2];
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
;
160 rc
= sendmsg(s
, &msg
, 0);
164 else if(errno
== EAGAIN
) {
166 rc2
= wait_for_fd(1, s
, 5);
176 tcp_server_socket(int port
, int local
)
178 struct sockaddr_in6 sin6
;
179 int s
, rc
, saved_errno
;
182 s
= socket(PF_INET6
, SOCK_STREAM
, 0);
186 rc
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
190 rc
= fcntl(s
, F_GETFL
, 0);
194 rc
= fcntl(s
, F_SETFL
, (rc
| O_NONBLOCK
));
198 rc
= fcntl(s
, F_GETFD
, 0);
202 rc
= fcntl(s
, F_SETFD
, rc
| FD_CLOEXEC
);
206 memset(&sin6
, 0, sizeof(sin6
));
207 sin6
.sin6_family
= AF_INET6
;
208 sin6
.sin6_port
= htons(port
);
210 rc
= inet_pton(AF_INET6
, "::1", &sin6
.sin6_addr
);
214 rc
= bind(s
, (struct sockaddr
*)&sin6
, sizeof(sin6
));