Skip to content
Snippets Groups Projects
Select Git revision
  • cb364743b80f4d7b863a604b24ce7c09efffbdb2
  • master default protected
  • release/202005
  • release/202001
  • release/201912
  • release/201911
  • release/201910
  • release/201908
  • release/201906
  • release/201905
  • release/201904
  • release/201903
  • release/201902
  • release/201901
  • release/201812
  • release/201811
  • release/201808
  • releases/beta1
  • packaging
  • native
  • release-0.2.x
  • 1.0.0
  • 0.2.0
  • 0.1.1
  • 0.1.0
25 results

pixbufmanipulator.cpp

Blame
  • Code owners
    Assign users and groups as approvers for specific file changes. Learn more.
    network_utils.cpp 9.41 KiB
    /*
     *  Copyright (C) 2019 Savoir-faire Linux Inc.
     *  Author(s) : Adrien Béraud <adrien.beraud@savoirfairelinux.com>
     *
     *  This program is free software; you can redistribute it and/or modify
     *  it under the terms of the GNU General Public License as published by
     *  the Free Software Foundation; either version 3 of the License, or
     *  (at your option) any later version.
     *
     *  This program 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
     *  GNU General Public License for more details.
     *
     *  You should have received a copy of the GNU General Public License
     *  along with this program. If not, see <https://www.gnu.org/licenses/>.
     */
    
    #include "network_utils.h"
    
    #ifdef _WIN32
    #include "utils.h"
    #include <io.h>
    #include <string>
    #include <cstring>
    #define close(x) closesocket(x)
    #define write(s, b, f) send(s, b, (int)strlen(b), 0)
    #else
    #include <fcntl.h>
    #endif
    
    #include <iostream>
    
    namespace dht {
    namespace net {
    
    int
    bindSocket(const SockAddr& addr, SockAddr& bound)
    {
        bool is_ipv6 = addr.getFamily() == AF_INET6;
        int sock = socket(is_ipv6 ? PF_INET6 : PF_INET, SOCK_DGRAM, 0);
        if (sock < 0)
            throw DhtException(std::string("Can't open socket: ") + strerror(sock));
        int set = 1;
    #ifdef SO_NOSIGPIPE
        setsockopt(sock, SOL_SOCKET, SO_NOSIGPIPE, (const char*)&set, sizeof(set));
    #endif
        if (is_ipv6)
            setsockopt(sock, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&set, sizeof(set));
        net::setNonblocking(sock);
        int rc = bind(sock, addr.get(), addr.getLength());
        if (rc < 0) {
            rc = errno;
            close(sock);
            throw DhtException("Can't bind socket on " + addr.toString() + " " + strerror(rc));
        }
        sockaddr_storage ss;
        socklen_t ss_len = sizeof(ss);
        getsockname(sock, (sockaddr*)&ss, &ss_len);
        bound = {ss, ss_len};
        return sock;
    }
    
    bool
    setNonblocking(int fd, bool nonblocking)
    {
    #ifdef _WIN32
        unsigned long mode = !!nonblocking;
        int rc = ioctlsocket(fd, FIONBIO, &mode);
        return rc == 0;