ip.cpp 4.32 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2014 Contributors as noted in the AUTHORS file
Martin Sustrik's avatar
Martin Sustrik committed
3 4 5 6

    This file is part of 0MQ.

    0MQ is free software; you can redistribute it and/or modify it under
7
    the terms of the GNU Lesser General Public License as published by
Martin Sustrik's avatar
Martin Sustrik committed
8 9 10 11 12 13
    the Free Software Foundation; either version 3 of the License, or
    (at your option) any later version.

    0MQ is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14
    GNU Lesser General Public License for more details.
Martin Sustrik's avatar
Martin Sustrik committed
15

16
    You should have received a copy of the GNU Lesser General Public License
Martin Sustrik's avatar
Martin Sustrik committed
17 18 19 20 21
    along with this program.  If not, see <http://www.gnu.org/licenses/>.
*/

#include "ip.hpp"
#include "err.hpp"
22
#include "platform.hpp"
Martin Sustrik's avatar
Martin Sustrik committed
23

24 25 26
#if defined ZMQ_HAVE_WINDOWS
#include "windows.hpp"
#else
27
#include <fcntl.h>
28 29
#include <sys/types.h>
#include <sys/socket.h>
30
#include <netdb.h>
31
#include <netinet/in.h>
32
#include <netinet/tcp.h>
33 34 35 36 37 38
#endif

#if defined ZMQ_HAVE_OPENVMS
#include <ioctl.h>
#endif

39 40 41 42
zmq::fd_t zmq::open_socket (int domain_, int type_, int protocol_)
{
    //  Setting this option result in sane behaviour when exec() functions
    //  are used. Old sockets are closed and don't block TCP ports etc.
43
#if defined ZMQ_HAVE_SOCK_CLOEXEC
44 45 46 47
    type_ |= SOCK_CLOEXEC;
#endif

    fd_t s = socket (domain_, type_, protocol_);
48 49
#ifdef ZMQ_HAVE_WINDOWS
    if (s == INVALID_SOCKET)
jdc8's avatar
jdc8 committed
50
        return INVALID_SOCKET;
51 52 53 54
#else
    if (s == -1)
        return -1;
#endif
55 56 57 58

    //  If there's no SOCK_CLOEXEC, let's try the second best option. Note that
    //  race condition can cause socket not to be closed (if fork happens
    //  between socket creation and this point).
59
#if !defined ZMQ_HAVE_SOCK_CLOEXEC && defined FD_CLOEXEC
60 61 62 63
    int rc = fcntl (s, F_SETFD, FD_CLOEXEC);
    errno_assert (rc != -1);
#endif

64 65 66 67 68 69
    //  On Windows, preventing sockets to be inherited by child processes.
#if defined ZMQ_HAVE_WINDOWS && defined HANDLE_FLAG_INHERIT
    BOOL brc = SetHandleInformation ((HANDLE) s, HANDLE_FLAG_INHERIT, 0);
    win_assert (brc);
#endif

70 71 72
    return s;
}

73 74
void zmq::unblock_socket (fd_t s_)
{
75
#if defined ZMQ_HAVE_WINDOWS
76 77 78
    u_long nonblock = 1;
    int rc = ioctlsocket (s_, FIONBIO, &nonblock);
    wsa_assert (rc != SOCKET_ERROR);
79
#elif defined ZMQ_HAVE_OPENVMS
80 81
    int nonblock = 1;
    int rc = ioctl (s_, FIONBIO, &nonblock);
82 83
    errno_assert (rc != -1);
#else
84 85
    int flags = fcntl (s_, F_GETFL, 0);
    if (flags == -1)
86
        flags = 0;
87
    int rc = fcntl (s_, F_SETFL, flags | O_NONBLOCK);
88 89 90 91
    errno_assert (rc != -1);
#endif
}

92 93
void zmq::enable_ipv4_mapping (fd_t s_)
{
Matt Arsenault's avatar
Matt Arsenault committed
94 95
  (void) s_;

96 97 98 99 100 101 102 103 104 105 106 107 108 109 110
#ifdef IPV6_V6ONLY
#ifdef ZMQ_HAVE_WINDOWS
    DWORD flag = 0;
#else
    int flag = 0;
#endif
    int rc = setsockopt (s_, IPPROTO_IPV6, IPV6_V6ONLY, (const char*) &flag,
        sizeof (flag));
#ifdef ZMQ_HAVE_WINDOWS
    wsa_assert (rc != SOCKET_ERROR);
#else
    errno_assert (rc == 0);
#endif
#endif
}
Matt Arsenault's avatar
Matt Arsenault committed
111

112
int zmq::get_peer_ip_address (fd_t sockfd_, std::string &ip_addr_)
113 114 115 116 117 118 119 120 121 122 123 124 125 126 127
{
    int rc;
    struct sockaddr_storage ss;

#if defined ZMQ_HAVE_HPUX || defined ZMQ_HAVE_WINDOWS
    int addrlen = static_cast <int> (sizeof ss);
#else
    socklen_t addrlen = sizeof ss;
#endif
    rc = getpeername (sockfd_, (struct sockaddr*) &ss, &addrlen);
#ifdef ZMQ_HAVE_WINDOWS
    if (rc == SOCKET_ERROR) {
        wsa_assert (WSAGetLastError () != WSANOTINITIALISED &&
                    WSAGetLastError () != WSAEFAULT &&
                    WSAGetLastError () != WSAEINPROGRESS &&
128
                    WSAGetLastError () != WSAENOTSOCK);
129
        return 0;
130 131 132 133 134 135
    }
#else
    if (rc == -1) {
        errno_assert (errno != EBADF &&
                      errno != EFAULT &&
                      errno != ENOTSOCK);
136
        return 0;
137 138 139 140 141 142 143
    }
#endif

    char host [NI_MAXHOST];
    rc = getnameinfo ((struct sockaddr*) &ss, addrlen, host, sizeof host,
        NULL, 0, NI_NUMERICHOST);
    if (rc != 0)
144
        return 0;
145 146

    ip_addr_ = host;
147 148 149 150 151 152 153 154

    union {
        struct sockaddr sa;
        struct sockaddr_storage sa_stor;
    } u;

    u.sa_stor = ss;
    return (int) u.sa.sa_family;
155
}
156 157 158

void zmq::set_ip_type_of_service (fd_t s_, int iptos)
{
159
    int rc = setsockopt(s_, IPPROTO_IP, IP_TOS, reinterpret_cast<const char*>(&iptos), sizeof(iptos));
160 161 162 163 164 165 166

#ifdef ZMQ_HAVE_WINDOWS
    wsa_assert (rc != SOCKET_ERROR);
#else
    errno_assert (rc == 0);
#endif
}