net.c 7.24 KB
Newer Older
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
1
/*
2
Copyright (c) 2007, 2008 by Juliusz Chroboczek
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in
all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
THE SOFTWARE.
*/

#include <unistd.h>
24
#include <stdio.h>
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
25 26 27
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
28
#include <sys/types.h>
29
#include <sys/uio.h>
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
30
#include <sys/socket.h>
31
#include <sys/un.h>
32
#define __USE_GNU
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
33
#include <netinet/in.h>
34
#include <netinet/in_systm.h>
35
#include <netinet/ip.h>
36
#include <arpa/inet.h>
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
37 38
#include <errno.h>

39
#include "babeld.h"
40
#include "util.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
41 42 43
#include "net.h"

int
44
babel_socket(int port)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
45 46 47 48
{
    struct sockaddr_in6 sin6;
    int s, rc;
    int saved_errno;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
49
    int one = 1, zero = 0;
50
    const int ds = 0xc0;        /* CS6 - Network Control */
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
51 52 53 54 55

    s = socket(PF_INET6, SOCK_DGRAM, 0);
    if(s < 0)
        return -1;

56
    rc = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
57 58 59
    if(rc < 0)
        goto fail;

60
    rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
61 62 63 64 65 66 67 68
    if(rc < 0)
        goto fail;

    rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_LOOP,
                    &zero, sizeof(zero));
    if(rc < 0)
        goto fail;

69
    rc = setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
70
                    &one, sizeof(one));
71 72 73 74
    if(rc < 0)
        goto fail;

    rc = setsockopt(s, IPPROTO_IPV6, IPV6_MULTICAST_HOPS,
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
75
                    &one, sizeof(one));
76 77 78
    if(rc < 0)
        goto fail;

79 80 81 82 83 84 85 86 87
#ifdef IPV6_TCLASS
    rc = setsockopt(s, IPPROTO_IPV6, IPV6_TCLASS, &ds, sizeof(ds));
#else
    rc = -1;
    errno = ENOSYS;
#endif
    if(rc < 0)
        perror("Couldn't set traffic class");

88 89 90 91
    rc = setsockopt(s, IPPROTO_IPV6, IPV6_RECVPKTINFO, &one, sizeof(one));
    if(rc < 0)
	goto fail;

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
92 93 94 95 96 97 98 99
    rc = fcntl(s, F_GETFL, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
    if(rc < 0)
        goto fail;

100 101 102 103 104 105 106 107
    rc = fcntl(s, F_GETFD, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
    if(rc < 0)
        goto fail;

108 109 110 111 112 113 114
    memset(&sin6, 0, sizeof(sin6));
    sin6.sin6_family = AF_INET6;
    sin6.sin6_port = htons(port);
    rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
    if(rc < 0)
        goto fail;

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
115 116 117 118 119 120 121 122 123 124
    return s;

 fail:
    saved_errno = errno;
    close(s);
    errno = saved_errno;
    return -1;
}

int
125 126
babel_recv(int s, void *buf, int buflen, struct sockaddr *sin, int slen,
	   unsigned char *src_return)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
127 128 129
{
    struct iovec iovec;
    struct msghdr msg;
130 131 132 133
    unsigned char cmsgbuf[128];
    struct cmsghdr *cmsg;
    int rc, found;
    unsigned char src[16] = {0};
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
134 135 136 137 138 139 140 141

    memset(&msg, 0, sizeof(msg));
    iovec.iov_base = buf;
    iovec.iov_len = buflen;
    msg.msg_name = sin;
    msg.msg_namelen = slen;
    msg.msg_iov = &iovec;
    msg.msg_iovlen = 1;
142 143
    msg.msg_control = cmsgbuf;
    msg.msg_controllen = sizeof(cmsgbuf);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
144 145

    rc = recvmsg(s, &msg, 0);
146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168
    if(rc < 0)
	return rc;

    found = 0;
    memset(src, 0, 16);
    cmsg = CMSG_FIRSTHDR(&msg);
    while(cmsg != NULL) {
	if(cmsg->cmsg_level == IPPROTO_IPV6 &&
	   cmsg->cmsg_type == IPV6_PKTINFO) {
	    struct in6_pktinfo *info =(struct in6_pktinfo*)CMSG_DATA(cmsg);
	    memcpy(src, info->ipi6_addr.s6_addr, 16);
	    found = 1;
	    break;
	}
	cmsg = CMSG_NXTHDR(&msg, cmsg);
    }

    if(!found) {
	errno = EDESTADDRREQ;
	return -1;
    }
    if(src_return != NULL)
	memcpy(src_return, src, 16);
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
169 170 171 172
    return rc;
}

int
173 174 175
babel_send(int s,
           const void *buf1, int buflen1, const void *buf2, int buflen2,
           const struct sockaddr *sin, int slen)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
176 177 178
{
    struct iovec iovec[2];
    struct msghdr msg;
179
    int rc, count = 0;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
180 181 182 183 184 185 186 187 188 189

    iovec[0].iov_base = (void*)buf1;
    iovec[0].iov_len = buflen1;
    iovec[1].iov_base = (void*)buf2;
    iovec[1].iov_len = buflen2;
    memset(&msg, 0, sizeof(msg));
    msg.msg_name = (struct sockaddr*)sin;
    msg.msg_namelen = slen;
    msg.msg_iov = iovec;
    msg.msg_iovlen = 2;
190

191 192
    /* The Linux kernel can apparently keep returning EAGAIN indefinitely. */

193
 again:
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
194
    rc = sendmsg(s, &msg, 0);
195
    if(rc < 0) {
196 197 198 199 200
        if(errno == EINTR) {
            count++;
            if(count < 100)
                goto again;
        } else if(errno == EAGAIN) {
201 202
            int rc2;
            rc2 = wait_for_fd(1, s, 5);
203 204 205 206 207
            if(rc2 > 0) {
                count++;
                if(count < 100)
                    goto again;
            }
208 209 210
            errno = EAGAIN;
        }
    }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
211 212
    return rc;
}
213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236

int
tcp_server_socket(int port, int local)
{
    struct sockaddr_in6 sin6;
    int s, rc, saved_errno;
    int one = 1;

    s = socket(PF_INET6, SOCK_STREAM, 0);
    if(s < 0)
        return -1;

    rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_GETFL, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFL, (rc | O_NONBLOCK));
    if(rc < 0)
        goto fail;

237 238 239 240 241 242 243 244
    rc = fcntl(s, F_GETFD, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
    if(rc < 0)
        goto fail;

245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268
    memset(&sin6, 0, sizeof(sin6));
    sin6.sin6_family = AF_INET6;
    sin6.sin6_port = htons(port);
    if(local) {
        rc = inet_pton(AF_INET6, "::1", &sin6.sin6_addr);
        if(rc < 0)
            goto fail;
    }
    rc = bind(s, (struct sockaddr*)&sin6, sizeof(sin6));
    if(rc < 0)
        goto fail;

    rc = listen(s, 2);
    if(rc < 0)
        goto fail;

    return s;

 fail:
    saved_errno = errno;
    close(s);
    errno = saved_errno;
    return -1;
}
269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321

int
unix_server_socket(const char *path)
{
    struct sockaddr_un sun;
    int s, rc, saved_errno;

    if(strlen(path) >= sizeof(sun.sun_path))
        return -1;

    s = socket(PF_UNIX, SOCK_STREAM, 0);
    if(s < 0)
        return -1;

    rc = fcntl(s, F_GETFL, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFL, rc | O_NONBLOCK);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_GETFD, 0);
    if(rc < 0)
        goto fail;

    rc = fcntl(s, F_SETFD, rc | FD_CLOEXEC);
    if(rc < 0)
        goto fail;

    memset(&sun, 0, sizeof(sun));
    sun.sun_family = AF_UNIX;
    strncpy(sun.sun_path, path, sizeof(sun.sun_path));
    rc = bind(s, (struct sockaddr *)&sun, sizeof(sun));
    if(rc < 0)
        goto fail;

    rc = listen(s, 2);
    if(rc < 0)
        goto fail_unlink;

    return s;

fail_unlink:
    saved_errno = errno;
    unlink(path);
    errno = saved_errno;
fail:
    saved_errno = errno;
    close(s);
    errno = saved_errno;
    return -1;
}