ip.cpp 2.91 KB
Newer Older
Martin Sustrik's avatar
Martin Sustrik committed
1
/*
2
    Copyright (c) 2007-2013 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 <netinet/in.h>
31
#include <netinet/tcp.h>
32 33 34 35 36 37
#endif

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

38 39 40 41
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.
42
#if defined ZMQ_HAVE_SOCK_CLOEXEC
43 44 45 46
    type_ |= SOCK_CLOEXEC;
#endif

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

    //  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).
58
#if !defined ZMQ_HAVE_SOCK_CLOEXEC && defined FD_CLOEXEC
59 60 61 62
    int rc = fcntl (s, F_SETFD, FD_CLOEXEC);
    errno_assert (rc != -1);
#endif

63 64 65 66 67 68
    //  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

69 70 71
    return s;
}

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

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

95 96 97 98 99 100 101 102 103 104 105 106 107 108 109
#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
110