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
26 #include <sys/ioctl.h>
27 #include <sys/types.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>
42 babel_socket(int port
)
44 struct sockaddr_in6 sin6
;
47 int one
= 1, zero
= 0;
49 s
= socket(PF_INET6
, SOCK_DGRAM
, 0);
53 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_V6ONLY
, &one
, sizeof(one
));
57 rc
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
61 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_LOOP
,
66 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_UNICAST_HOPS
,
71 rc
= setsockopt(s
, IPPROTO_IPV6
, IPV6_MULTICAST_HOPS
,
76 setsockopt_ipv6_tclass (s
, IPTOS_PREC_INTERNETCONTROL
);
78 rc
= fcntl(s
, F_GETFL
, 0);
82 rc
= fcntl(s
, F_SETFL
, (rc
| O_NONBLOCK
));
86 rc
= fcntl(s
, F_GETFD
, 0);
90 rc
= fcntl(s
, F_SETFD
, rc
| FD_CLOEXEC
);
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
));
111 babel_recv(int s
, void *buf
, int buflen
, struct sockaddr
*sin
, int slen
)
117 memset(&msg
, 0, sizeof(msg
));
118 iovec
.iov_base
= buf
;
119 iovec
.iov_len
= buflen
;
121 msg
.msg_namelen
= slen
;
122 msg
.msg_iov
= &iovec
;
125 rc
= recvmsg(s
, &msg
, 0);
131 void *buf1
, int buflen1
, void *buf2
, int buflen2
,
132 struct sockaddr
*sin
, int slen
)
134 struct iovec iovec
[2];
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
;
149 rc
= sendmsg(s
, &msg
, 0);
153 else if(errno
== EAGAIN
) {
155 rc2
= wait_for_fd(1, s
, 5);
165 tcp_server_socket(int port
, int local
)
167 struct sockaddr_in6 sin6
;
168 int s
, rc
, saved_errno
;
171 s
= socket(PF_INET6
, SOCK_STREAM
, 0);
175 rc
= setsockopt(s
, SOL_SOCKET
, SO_REUSEADDR
, &one
, sizeof(one
));
179 rc
= fcntl(s
, F_GETFL
, 0);
183 rc
= fcntl(s
, F_SETFL
, (rc
| O_NONBLOCK
));
187 rc
= fcntl(s
, F_GETFD
, 0);
191 rc
= fcntl(s
, F_SETFD
, rc
| FD_CLOEXEC
);
195 memset(&sin6
, 0, sizeof(sin6
));
196 sin6
.sin6_family
= AF_INET6
;
197 sin6
.sin6_port
= htons(port
);
199 rc
= inet_pton(AF_INET6
, "::1", &sin6
.sin6_addr
);
203 rc
= bind(s
, (struct sockaddr
*)&sin6
, sizeof(sin6
));