2 Copyright (c) 2007, 2008 by Juliusz Chroboczek
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:
11 The above copyright notice and this permission notice shall be included in
12 all copies or substantial portions of the Software.
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
30 #include <sys/ioctl.h>
31 #include <sys/types.h>
33 #include <sys/socket.h>
34 #include <netinet/in.h>
35 #include <netinet/in_systm.h>
36 #include <netinet/ip.h>
37 #include <arpa/inet.h>
46 babel_socket(int port
)
48 struct sockaddr_in6 sin6
;
51 int one
= 1, zero
= 0;
53 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
57 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, &one
, sizeof(one
));
61 rc
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
65 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
70 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
75 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
80 setsockopt_ipv6_tclass (s
, IPTOS_PREC_INTERNETCONTROL
);
82 rc
= fcntl(s
, F_GETFL
, 0);
86 rc
= fcntl(s
, F_SETFL
, (rc
| O_NONBLOCK
));
90 rc
= fcntl(s
, F_GETFD
, 0);
94 rc
= fcntl(s
, F_SETFD
, rc
| FD_CLOEXEC
);
98 memset(&sin6
, 0, sizeof(sin6
));
99 sin6
.sin6_family
= AF_INET6
;
100 sin6
.sin6_port
= htons(port
);
101 rc
= bind(s
, (struct sockaddr
*)&sin6
, sizeof(sin6
));
115 babel_recv(int s
, void *buf
, int buflen
, struct sockaddr
*sin
, int slen
)
121 memset(&msg
, 0, sizeof(msg
));
122 iovec
.iov_base
= buf
;
123 iovec
.iov_len
= buflen
;
125 msg
.msg_namelen
= slen
;
126 msg
.msg_iov
= &iovec
;
129 rc
= recvmsg(s
, &msg
, 0);
135 void *buf1
, int buflen1
, void *buf2
, int buflen2
,
136 struct sockaddr
*sin
, int slen
)
138 struct iovec iovec
[2];
142 iovec
[0].iov_base
= buf1
;
143 iovec
[0].iov_len
= buflen1
;
144 iovec
[1].iov_base
= buf2
;
145 iovec
[1].iov_len
= buflen2
;
146 memset(&msg
, 0, sizeof(msg
));
148 msg
.msg_namelen
= slen
;
153 rc
= sendmsg(s
, &msg
, 0);
157 else if(errno
== EAGAIN
) {
159 rc2
= wait_for_fd(1, s
, 5);
169 tcp_server_socket(int port
, int local
)
171 struct sockaddr_in6 sin6
;
172 int s
, rc
, saved_errno
;
175 s
= socket(PF_INET6
, SOCK_STREAM
, 0);
179 rc
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
183 rc
= fcntl(s
, F_GETFL
, 0);
187 rc
= fcntl(s
, F_SETFL
, (rc
| O_NONBLOCK
));
191 rc
= fcntl(s
, F_GETFD
, 0);
195 rc
= fcntl(s
, F_SETFD
, rc
| FD_CLOEXEC
);
199 memset(&sin6
, 0, sizeof(sin6
));
200 sin6
.sin6_family
= AF_INET6
;
201 sin6
.sin6_port
= htons(port
);
203 rc
= inet_pton(AF_INET6
, "::1", &sin6
.sin6_addr
);
207 rc
= bind(s
, (struct sockaddr
*)&sin6
, sizeof(sin6
));