You cannot select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

561 lines
18 KiB
C

/*
* Copyright (c) 2016 The ZLToolKit project authors. All Rights Reserved.
*
* This file is part of ZLToolKit(https://github.com/ZLMediaKit/ZLToolKit).
*
* Use of this source code is governed by MIT license that can be found in the
* LICENSE file in the root of the source tree. All contributing project authors
* may be found in the AUTHORS file in the root of the source tree.
*/
#ifndef NETWORK_SOCKUTIL_H
#define NETWORK_SOCKUTIL_H
#if defined(_WIN32)
#include <winsock2.h>
#include <ws2tcpip.h>
#include <iphlpapi.h>
#pragma comment (lib, "Ws2_32.lib")
#pragma comment(lib,"Iphlpapi.lib")
#else
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <net/if.h>
#include <netinet/in.h>
#include <netinet/tcp.h>
#endif // defined(_WIN32)
#include <cstring>
#include <cstdint>
#include <map>
#include <vector>
#include <string>
namespace toolkit {
#if defined(_WIN32)
#ifndef socklen_t
#define socklen_t int
#endif //!socklen_t
int ioctl(int fd, long cmd, u_long *ptr);
int close(int fd);
#endif // defined(_WIN32)
#if !defined(SOCKET_DEFAULT_BUF_SIZE)
#define SOCKET_DEFAULT_BUF_SIZE (256 * 1024)
#else
#if SOCKET_DEFAULT_BUF_SIZE == 0 && !defined(__linux__)
// just for linux, because in some high-throughput environments,
// kernel control is more efficient and reasonable than program
// settings. For example, refer to cloudflare's blog
#undef SOCKET_DEFAULT_BUF_SIZE
#define SOCKET_DEFAULT_BUF_SIZE (256 * 1024)
#endif
#endif
#define TCP_KEEPALIVE_INTERVAL 30
#define TCP_KEEPALIVE_PROBE_TIMES 9
#define TCP_KEEPALIVE_TIME 120
//套接字工具类封装了socket、网络的一些基本操作 [AUTO-TRANSLATED:33a88b27]
//Socket tool class, encapsulating some basic socket and network operations
class SockUtil {
public:
/**
* tcp
* @param host ip
* @param port
* @param async
* @param local_ip ip
* @param local_port
* @return -1socket fd
* Create a TCP client socket and connect to the server
* @param host Server IP or domain name
* @param port Server port number
* @param async Whether to connect asynchronously
* @param local_ip Local network card IP to bind
* @param local_port Local port number to bind
* @return -1 represents failure, others are socket fd numbers
* [AUTO-TRANSLATED:3f0a872c]
*/
static int connect(const char *host, uint16_t port, bool async = true, const char *local_ip = "::", uint16_t local_port = 0);
/**
* tcp
* @param port
* @param local_ip ip
* @param back_log accept
* @return -1socket fd
* Create a TCP listening socket
* @param port Local port to listen on
* @param local_ip Local network card IP to bind
* @param back_log Accept queue length
* @return -1 represents failure, others are socket fd numbers
* [AUTO-TRANSLATED:d56ad901]
*/
static int listen(const uint16_t port, const char *local_ip = "::", int back_log = 1024);
/**
* udp
* @param port
* @param local_ip ip
* @param enable_reuse bind
* @return -1socket fd
* Create a UDP socket
* @param port Local port to listen on
* @param local_ip Local network card IP to bind
* @param enable_reuse Whether to allow repeated bind port
* @return -1 represents failure, others are socket fd numbers
* [AUTO-TRANSLATED:a3762f0f]
*/
static int bindUdpSock(const uint16_t port, const char *local_ip = "::", bool enable_reuse = true);
/**
* @brief sock
* @param sock, socket fd
* @return 0 , -1
* @brief Release the binding relationship related to sock
* @param sock, socket fd number
* @return 0 Success, -1 Failure
* [AUTO-TRANSLATED:50b002e8]
*/
static int dissolveUdpSock(int sock);
/**
* TCP_NODELAYTCP
* @param fd socket fd
* @param on
* @return 0-1
* Enable TCP_NODELAY to reduce TCP interaction delay
* @param fd socket fd number
* @param on Whether to enable
* @return 0 represents success, -1 represents failure
* [AUTO-TRANSLATED:11b57392]
*/
static int setNoDelay(int fd, bool on = true);
/**
* socketSIG_PIPE(mac)
* @param fd socket fd
* @return 0-1
* Write socket does not trigger SIG_PIPE signal (seems to be effective only on Mac)
* @param fd socket fd number
* @return 0 represents success, -1 represents failure
* [AUTO-TRANSLATED:bdb49ca5]
*/
static int setNoSigpipe(int fd);
/**
* socket
* @param fd socket fd
* @param noblock
* @return 0-1
* Set whether the read and write socket is blocked
* @param fd socket fd number
* @param noblock Whether to block
* @return 0 represents success, -1 represents failure
* [AUTO-TRANSLATED:2f9717df]
*/
static int setNoBlocked(int fd, bool noblock = true);
/**
* socket8K
*
* @param fd socket fd
* @param size
* @return 0-1
* Set the socket receive buffer, default is around 8K, generally has an upper limit
* Can be adjusted through kernel configuration file
* @param fd socket fd number
* @param size Receive buffer size
* @return 0 represents success, -1 represents failure
* [AUTO-TRANSLATED:4dcaa8b8]
*/
static int setRecvBuf(int fd, int size = SOCKET_DEFAULT_BUF_SIZE);
/**
* socket8K
*
* @param fd socket fd
* @param size
* @return 0-1
* Set the socket receive buffer, default is around 8K, generally has an upper limit
* Can be adjusted through kernel configuration file
* @param fd socket fd number
* @param size Receive buffer size
* @return 0 represents success, -1 represents failure
* [AUTO-TRANSLATED:4dcaa8b8]
*/
static int setSendBuf(int fd, int size = SOCKET_DEFAULT_BUF_SIZE);
/**
* (TIME_WAITE)
* @param fd socket fd
* @param on
* @return 0-1
* Set subsequent bindable reuse port (in TIME_WAIT state)
* @param fd socket fd number
* @param on whether to enable this feature
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:4dcb4dff]
*/
static int setReuseable(int fd, bool on = true, bool reuse_port = true);
/**
* udp广
* @param fd socket fd
* @param on
* @return 0-1
* Run sending or receiving UDP broadcast messages
* @param fd socket fd number
* @param on whether to enable this feature
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:d5ce73e0]
*/
static int setBroadcast(int fd, bool on = true);
/**
* TCP KeepAlive
* @param fd socket fd
* @param on
* @param idle keepalive
* @param interval keepalive
* @param times keepalive
* @return 0-1
* Enable TCP KeepAlive feature
* @param fd socket fd number
* @param on whether to enable this feature
* @param idle keepalive idle time
* @param interval keepalive probe time interval
* @param times keepalive probe times
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:9b44a8ec]
*/
static int setKeepAlive(int fd, bool on = true, int interval = TCP_KEEPALIVE_INTERVAL, int idle = TCP_KEEPALIVE_TIME, int times = TCP_KEEPALIVE_PROBE_TIMES);
/**
* FD_CLOEXEC()
* @param fd fdsocket
* @param on
* @return 0-1
* Enable FD_CLOEXEC feature (related to multiple processes)
* @param fd fd number, not necessarily a socket
* @param on whether to enable this feature
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:964368da]
*/
static int setCloExec(int fd, bool on = true);
/**
* SO_LINGER
* @param sock socket fd
* @param second socket
* @return 0-1
* Enable SO_LINGER feature
* @param sock socket fd number
* @param second kernel waiting time for closing socket timeout, in seconds
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:92230daf]
*/
static int setCloseWait(int sock, int second = 0);
/**
* dns
* @param host ip
* @param port
* @param addr sockaddr
* @return
* DNS resolution
* @param host domain name or IP
* @param port port number
* @param addr sockaddr structure
* @return whether successful
* [AUTO-TRANSLATED:3b79cf5d]
*/
static bool getDomainIP(const char *host, uint16_t port, struct sockaddr_storage &addr, int ai_family = AF_INET,
int ai_socktype = SOCK_STREAM, int ai_protocol = IPPROTO_TCP, int expire_sec = 60);
/**
* ttl
* @param sock socket fd
* @param ttl ttl
* @return 0-1
* Set multicast TTL
* @param sock socket fd number
* @param ttl TTL value
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:1828beb5]
*/
static int setMultiTTL(int sock, uint8_t ttl = 64);
/**
*
* @param sock socket fd
* @param local_ip ip
* @return 0-1
* Set multicast sending network card
* @param sock socket fd number
* @param local_ip local network card IP
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:25e8e9d7]
*/
static int setMultiIF(int sock, const char *local_ip);
/**
*
* @param fd socket fd
* @param acc
* @return 0-1
* Set whether to receive multicast packets sent by the local machine
* @param fd socket fd number
* @param acc whether to receive
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:83cec1e8]
*/
static int setMultiLOOP(int fd, bool acc = false);
/**
*
* @param fd socket fd
* @param addr
* @param local_ip ip
* @return 0-1
* Join multicast
* @param fd socket fd number
* @param addr multicast address
* @param local_ip local network card IP
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:45523b25]
*/
static int joinMultiAddr(int fd, const char *addr, const char *local_ip = "0.0.0.0");
/**
* 退
* @param fd socket fd
* @param addr
* @param local_ip ip
* @return 0-1
* Exit multicast
* @param fd socket fd number
* @param addr multicast address
* @param local_ip local network card ip
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:081785d3]
*/
static int leaveMultiAddr(int fd, const char *addr, const char *local_ip = "0.0.0.0");
/**
*
* @param sock socket fd
* @param addr
* @param src_ip
* @param local_ip ip
* @return 0-1
* Join multicast and only receive multicast data from the specified source
* @param sock socket fd number
* @param addr multicast address
* @param src_ip source address
* @param local_ip local network card ip
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:061989eb]
*/
static int joinMultiAddrFilter(int sock, const char *addr, const char *src_ip, const char *local_ip = "0.0.0.0");
/**
* 退
* @param fd socket fd
* @param addr
* @param src_ip
* @param local_ip ip
* @return 0-1
* Exit multicast
* @param fd socket fd number
* @param addr multicast address
* @param src_ip source address
* @param local_ip local network card ip
* @return 0 represents success, -1 for failure
* [AUTO-TRANSLATED:9cd166c7]
*/
static int leaveMultiAddrFilter(int fd, const char *addr, const char *src_ip, const char *local_ip = "0.0.0.0");
/**
* socket
* @param fd socket fd
* @return
* Get the current error of the socket
* @param fd socket fd number
* @return error code
* [AUTO-TRANSLATED:e4500a0f]
*/
static int getSockError(int fd);
/**
*
* @return vector<map<ip:name> >
* Get the list of network cards
* @return vector<map<ip:name> >
* [AUTO-TRANSLATED:94687465]
*/
static std::vector<std::map<std::string, std::string>> getInterfaceList();
/**
* ip
* Get the default local ip of the host
* [AUTO-TRANSLATED:9eb5d031]
*/
static std::string get_local_ip();
/**
* socketip
* @param sock socket fd
* Get the local ip bound to the socket
* @param sock socket fd number
* [AUTO-TRANSLATED:4e7b6040]
*/
static std::string get_local_ip(int sock);
/**
* socket
* @param sock socket fd
* Get the local port bound to the socket
* @param sock socket fd number
* [AUTO-TRANSLATED:7b212118]
*/
static uint16_t get_local_port(int sock);
/**
* socketip
* @param sock socket fd
* Get the remote ip bound to the socket
* @param sock socket fd number
* [AUTO-TRANSLATED:952ddef8]
*/
static std::string get_peer_ip(int sock);
/**
* socket
* @param sock socket fd
* Get the remote port bound to the socket
* @param sock socket fd number
* [AUTO-TRANSLATED:3b9bcf2e]
*/
static uint16_t get_peer_port(int sock);
static bool support_ipv6();
/**
* 线in_addrip
* Thread-safe conversion of in_addr to IP string
* [AUTO-TRANSLATED:e0ff8b4b]
*/
static std::string inet_ntoa(const struct in_addr &addr);
static std::string inet_ntoa(const struct in6_addr &addr);
static std::string inet_ntoa(const struct sockaddr *addr);
static uint16_t inet_port(const struct sockaddr *addr);
static struct sockaddr_storage make_sockaddr(const char *ip, uint16_t port);
static socklen_t get_sock_len(const struct sockaddr *addr);
static bool get_sock_local_addr(int fd, struct sockaddr_storage &addr);
static bool get_sock_peer_addr(int fd, struct sockaddr_storage &addr);
/**
* ip
* @param if_name
* Get the IP of the network card
* @param if_name Network card name
* [AUTO-TRANSLATED:e88f1554]
*/
static std::string get_ifr_ip(const char *if_name);
/**
*
* @param local_op ip
* Get the network card name
* @param local_op Network card IP
* [AUTO-TRANSLATED:cdaad7f0]
*/
static std::string get_ifr_name(const char *local_op);
/**
*
* @param if_name
* Get the subnet mask based on the network card name
* @param if_name Network card name
* [AUTO-TRANSLATED:a6714ee2]
*/
static std::string get_ifr_mask(const char *if_name);
/**
* 广
* @param if_name
* Get the broadcast address based on the network card name
* @param if_name Network card name
* [AUTO-TRANSLATED:20348c92]
*/
static std::string get_ifr_brdaddr(const char *if_name);
/**
* ip
* @param src_ip ip
* @param dts_ip ip
* Determine if two IPs are in the same network segment
* @param src_ip My IP
* @param dts_ip Peer IP
* [AUTO-TRANSLATED:95acb68f]
*/
static bool in_same_lan(const char *src_ip, const char *dts_ip);
/**
* ipv4
* Determine if it is an IPv4 address
* [AUTO-TRANSLATED:b5af4ea0]
*/
static bool is_ipv4(const char *str);
/**
* ipv6
* Determine if it is an IPv6 address
* [AUTO-TRANSLATED:70526900]
*/
static bool is_ipv6(const char *str);
};
} // namespace toolkit
#endif // !NETWORK_SOCKUTIL_H