net.c 4.88 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 24 25 26

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>
#include <fcntl.h>
#include <string.h>
#include <sys/ioctl.h>
27
#include <sys/types.h>
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
28
#include <sys/socket.h>
29
#include <arpa/inet.h>
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
30 31 32
#include <netinet/in.h>
#include <errno.h>

33 34
#include "babel.h"
#include "util.h"
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
35 36 37
#include "net.h"

int
38
babel_socket(int port)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
39 40 41 42
{
    struct sockaddr_in6 sin6;
    int s, rc;
    int saved_errno;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
43
    int one = 1, zero = 0;
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
44 45 46 47 48

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

49
    rc = setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &one, sizeof(one));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
50 51 52
    if(rc < 0)
        goto fail;

53
    rc = setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one));
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
54 55 56 57 58 59 60 61
    if(rc < 0)
        goto fail;

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

62
    rc = setsockopt(s, IPPROTO_IPV6, IPV6_UNICAST_HOPS,
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
63
                    &one, sizeof(one));
64 65 66 67
    if(rc < 0)
        goto fail;

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

Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
72 73 74 75 76 77 78 79
    rc = fcntl(s, F_GETFL, 0);
    if(rc < 0)
        goto fail;

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

80 81 82 83 84 85 86 87
    rc = fcntl(s, F_GETFD, 0);
    if(rc < 0)
        goto fail;

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

88 89 90 91 92 93 94
    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
95 96 97 98 99 100 101 102 103 104
    return s;

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

int
105
babel_recv(int s, void *buf, int buflen, struct sockaddr *sin, int slen)
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123
{
    struct iovec iovec;
    struct msghdr msg;
    int rc;

    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;

    rc = recvmsg(s, &msg, 0);
    return rc;
}

int
124 125 126
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
127 128 129 130 131 132 133 134 135 136 137 138 139 140
{
    struct iovec iovec[2];
    struct msghdr msg;
    int rc;

    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;
141 142

 again:
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
143
    rc = sendmsg(s, &msg, 0);
144 145 146 147 148 149 150 151 152 153 154
    if(rc < 0) {
        if(errno == EINTR)
            goto again;
        else if(errno == EAGAIN) {
            int rc2;
            rc2 = wait_for_fd(1, s, 5);
            if(rc2 > 0)
                goto again;
            errno = EAGAIN;
        }
    }
Juliusz Chroboczek's avatar
Juliusz Chroboczek committed
155 156
    return rc;
}
157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180

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;

181 182 183 184 185 186 187 188
    rc = fcntl(s, F_GETFD, 0);
    if(rc < 0)
        goto fail;

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

189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212
    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;
}