使用getaddrinfo重写部分内容
This commit is contained in:
parent
40fb8940cb
commit
1b85fae8c9
166
ssocket.h
166
ssocket.h
@ -4,6 +4,10 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <string.h>
|
#include <string.h>
|
||||||
|
|
||||||
|
#define SSOCKET_VERSION_MAJOR 1
|
||||||
|
#define SSOCKET_VERSION_MINOR 0
|
||||||
|
#define SSOCKET_VERSION_PATCH 2
|
||||||
|
|
||||||
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__) || defined(__NT__)
|
||||||
#define _OS_WIN 1
|
#define _OS_WIN 1
|
||||||
//define something for Windows (32-bit and 64-bit, this part is common)
|
//define something for Windows (32-bit and 64-bit, this part is common)
|
||||||
@ -55,6 +59,7 @@
|
|||||||
|
|
||||||
#if WIN_PART
|
#if WIN_PART
|
||||||
#include <winsock2.h>
|
#include <winsock2.h>
|
||||||
|
#include <WS2tcpip.h>
|
||||||
#include <windows.h>
|
#include <windows.h>
|
||||||
#define strcasecmp _stricmp
|
#define strcasecmp _stricmp
|
||||||
#define strncasecmp _strnicmp
|
#define strncasecmp _strnicmp
|
||||||
@ -64,8 +69,8 @@
|
|||||||
#include <arpa/inet.h>
|
#include <arpa/inet.h>
|
||||||
#include <pthread.h>
|
#include <pthread.h>
|
||||||
#include <strings.h>
|
#include <strings.h>
|
||||||
|
|
||||||
#define SOCKET long long int
|
#define SOCKET long long int
|
||||||
|
//#define INVALID_SOCKET (SOCKET)(~0)
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#ifndef ZZY_SLEEP
|
#ifndef ZZY_SLEEP
|
||||||
@ -87,14 +92,26 @@ enum {
|
|||||||
ERR_SOCK_SUCCESS,
|
ERR_SOCK_SUCCESS,
|
||||||
ERR_SOCK_MALLOC,
|
ERR_SOCK_MALLOC,
|
||||||
ERR_SOCK_NULLPTR,
|
ERR_SOCK_NULLPTR,
|
||||||
|
|
||||||
ERR_SOCK_WSAStartup,
|
ERR_SOCK_WSAStartup,
|
||||||
ERR_SOCK_DETERMINE_AGREEMENT,
|
ERR_SOCK_DETERMINE_AGREEMENT,
|
||||||
ERR_SOCK_CREATE_SOCKET,
|
ERR_SOCK_CREATE_SOCKET,
|
||||||
ERR_SOCK_BIND,
|
ERR_SOCK_BIND,
|
||||||
ERR_SOCK_LISTEN,
|
ERR_SOCK_LISTEN,
|
||||||
ERR_SOCK_ACCEPT,
|
ERR_SOCK_ACCEPT,
|
||||||
|
|
||||||
ERR_SOCK_CONNECT,
|
ERR_SOCK_CONNECT,
|
||||||
|
|
||||||
|
ERR_SOCK_ADDRINFO,
|
||||||
|
//getaddrinfo
|
||||||
|
ERR_SOCK_EAI_MAPPING_NOT_EXIST,
|
||||||
|
ERR_SOCK_EAI_AGAIN,
|
||||||
|
ERR_SOCK_EAI_BADFLAGS,
|
||||||
|
ERR_SOCK_EAI_FAIL,
|
||||||
|
ERR_SOCK_EAI_FAMILY,
|
||||||
|
ERR_SOCK_EAI_MEMORY,
|
||||||
|
ERR_SOCK_EAI_NONAME,
|
||||||
|
ERR_SOCK_EAI_SERVICE,
|
||||||
|
ERR_SOCK_EAI_SOCKTYPE,
|
||||||
};
|
};
|
||||||
|
|
||||||
static inline int make_sock(SOCKET* sock);
|
static inline int make_sock(SOCKET* sock);
|
||||||
@ -110,13 +127,19 @@ static inline void sock_thread(void(*thread_func)(void*), void* data);
|
|||||||
|
|
||||||
#define _SOCKET_TEST_IP "127.0.0.1"
|
#define _SOCKET_TEST_IP "127.0.0.1"
|
||||||
#define _SOCKET_TEST_PORT 6789
|
#define _SOCKET_TEST_PORT 6789
|
||||||
|
static inline int _socket(SOCKET* sock, const char* ip, const char* port);
|
||||||
|
static inline int _connect(SOCKET sock, const char* ip, const char* port);
|
||||||
|
static inline int _bind(SOCKET sock, const char* ip, const char* port);
|
||||||
|
static inline int _bindlisten(SOCKET sock, const char* ip, const char* port);
|
||||||
|
|
||||||
//SOCK_STREAM
|
static inline int _sock(SOCKET* sock, int af, int type);
|
||||||
//SOCK_DGRAM
|
static inline int make_sock(SOCKET* sock) { return make_sock_tcp4(sock); };
|
||||||
//static inline int make_sock_tcp(SOCKET* sock) { return _make_sock(sock, AF_INET, SOCK_STREAM); }
|
static inline int make_sock_tcp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_STREAM); }
|
||||||
//static inline int make_sock_udp(SOCKET* sock) { return _make_sock(sock, AF_INET, SOCK_DGRAM); }
|
static inline int make_sock_tcp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_STREAM); }
|
||||||
|
static inline int make_sock_udp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_DGRAM); }
|
||||||
|
static inline int make_sock_udp6(SOCKET* sock) { return _sock(sock, AF_INET6, SOCK_DGRAM); }
|
||||||
|
|
||||||
static inline int make_sock(SOCKET* sock) {
|
static inline int _sock(SOCKET* sock, int af, int type) {
|
||||||
#ifdef WIN_PART
|
#ifdef WIN_PART
|
||||||
WSADATA wsaData;
|
WSADATA wsaData;
|
||||||
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
if (WSAStartup(MAKEWORD(2, 2), &wsaData) != 0) {
|
||||||
@ -128,55 +151,106 @@ static inline int make_sock(SOCKET* sock) {
|
|||||||
return ERR_SOCK_DETERMINE_AGREEMENT;
|
return ERR_SOCK_DETERMINE_AGREEMENT;
|
||||||
}
|
}
|
||||||
#endif
|
#endif
|
||||||
*sock = socket(AF_INET, SOCK_STREAM, 0);
|
*sock = socket(af, type, 0);
|
||||||
if (*sock == -1) {
|
if (*sock == INVALID_SOCKET) {
|
||||||
return ERR_SOCK_CREATE_SOCKET;
|
return ERR_SOCK_CREATE_SOCKET;
|
||||||
}
|
}
|
||||||
return ERR_SOCK_SUCCESS;
|
return ERR_SOCK_SUCCESS;
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int sock_connect(SOCKET sock, const char* connect_ip, unsigned short port) {
|
static inline int _sock_eai_res_map(int res) {
|
||||||
struct sockaddr_in saddr;
|
switch (res) {
|
||||||
saddr.sin_family = AF_INET;
|
case EAI_AGAIN: return ERR_SOCK_EAI_AGAIN;
|
||||||
saddr.sin_port = htons(port);
|
case EAI_BADFLAGS: return ERR_SOCK_EAI_BADFLAGS;
|
||||||
if(connect_ip == NULL) {
|
case EAI_FAIL: return ERR_SOCK_EAI_FAIL;
|
||||||
saddr.sin_addr.s_addr = INADDR_ANY;
|
case EAI_FAMILY: return ERR_SOCK_EAI_FAMILY;
|
||||||
}
|
case EAI_MEMORY: return ERR_SOCK_EAI_MEMORY;
|
||||||
else {
|
case EAI_NONAME: return ERR_SOCK_EAI_NONAME;
|
||||||
saddr.sin_addr.s_addr = inet_addr(connect_ip);
|
case EAI_SERVICE: return ERR_SOCK_EAI_SERVICE;
|
||||||
|
case EAI_SOCKTYPE: return ERR_SOCK_EAI_SOCKTYPE;
|
||||||
|
default: return ERR_SOCK_EAI_MAPPING_NOT_EXIST;
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if(connect(sock, (struct sockaddr*)&saddr, sizeof(saddr))) {
|
static inline int _socket(SOCKET* sock, const char* ip, const char* port) {
|
||||||
return ERR_SOCK_CONNECT;
|
struct addrinfo inf = { 0 };
|
||||||
|
struct addrinfo* res_addr = NULL;
|
||||||
|
int res = getaddrinfo(ip, port, &inf, &res_addr);
|
||||||
|
if (res != 0) {
|
||||||
|
return _sock_eai_res_map(res);
|
||||||
}
|
}
|
||||||
|
for (struct addrinfo* p = res_addr; p != NULL; p = p->ai_next) {
|
||||||
|
if (*sock = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) {
|
||||||
|
if (*sock == INVALID_SOCKET) {
|
||||||
|
return ERR_SOCK_CREATE_SOCKET;
|
||||||
|
}
|
||||||
|
freeaddrinfo(res_addr);
|
||||||
return ERR_SOCK_SUCCESS;
|
return ERR_SOCK_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
freeaddrinfo(res_addr);
|
||||||
|
return ERR_SOCK_CONNECT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int _connect(SOCKET sock, const char* ip, const char* port) {
|
||||||
|
struct addrinfo inf = { 0 };
|
||||||
|
struct addrinfo* res_addr = NULL;
|
||||||
|
int res = getaddrinfo(ip, port, &inf, &res_addr);
|
||||||
|
if (res != 0) {
|
||||||
|
return _sock_eai_res_map(res);
|
||||||
|
}
|
||||||
|
for (struct addrinfo* p = res_addr; p != NULL; p = p->ai_next) {
|
||||||
|
if (connect(sock, p->ai_addr, p->ai_addrlen) == 0) {
|
||||||
|
freeaddrinfo(res_addr);
|
||||||
|
return ERR_SOCK_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
freeaddrinfo(res_addr);
|
||||||
|
return ERR_SOCK_CONNECT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int _bind(SOCKET sock, const char* ip, const char* port) {
|
||||||
|
struct addrinfo inf = { 0 };
|
||||||
|
struct addrinfo* res_addr = NULL;
|
||||||
|
int res = getaddrinfo(ip, port, &inf, &res_addr);
|
||||||
|
if (res != 0) {
|
||||||
|
return ERR_SOCK_ADDRINFO;
|
||||||
|
}
|
||||||
|
for (struct addrinfo* p = res_addr; p != NULL; p = p->ai_next) {
|
||||||
|
if (bind(sock, p->ai_addr, p->ai_addrlen) == 0) {
|
||||||
|
freeaddrinfo(res_addr);
|
||||||
|
return ERR_SOCK_SUCCESS;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
freeaddrinfo(res_addr);
|
||||||
|
return ERR_SOCK_CONNECT;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int _bindlisten(SOCKET sock, const char* ip, const char* port) {
|
||||||
|
int res = _bind(sock, ip, port);
|
||||||
|
if(res == ERR_SOCK_SUCCESS){
|
||||||
|
if (listen(sock, SOMAXCONN) == 0) {
|
||||||
|
return ERR_SOCK_SUCCESS;
|
||||||
|
}
|
||||||
|
return ERR_SOCK_LISTEN;
|
||||||
|
}
|
||||||
|
return res;
|
||||||
|
}
|
||||||
|
|
||||||
|
static inline int sock_connect(SOCKET sock, const char* connect_ip, unsigned short port) {
|
||||||
|
char buf[8] = { 0 };
|
||||||
|
sprintf(buf, "%u", port);
|
||||||
|
return _connect(sock, connect_ip, buf);
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int sock_bindlisten(SOCKET sock, const char* server_ip, unsigned short port) {
|
static inline int sock_bindlisten(SOCKET sock, const char* server_ip, unsigned short port) {
|
||||||
struct sockaddr_in saddr;
|
char buf[8] = { 0 };
|
||||||
saddr.sin_family = AF_INET;
|
sprintf(buf, "%u", port);
|
||||||
saddr.sin_port = htons(port);
|
return _bindlisten(sock, server_ip, buf);
|
||||||
if(server_ip == NULL) {
|
|
||||||
saddr.sin_addr.s_addr = INADDR_ANY;
|
|
||||||
}
|
|
||||||
else {
|
|
||||||
saddr.sin_addr.s_addr = inet_addr(server_ip);
|
|
||||||
}
|
|
||||||
|
|
||||||
int res = bind(sock, (struct sockaddr*)&saddr, sizeof(saddr));
|
|
||||||
if (res == -1) {
|
|
||||||
return ERR_SOCK_BIND;
|
|
||||||
}
|
|
||||||
|
|
||||||
res = listen(sock, SOMAXCONN);
|
|
||||||
if (res == -1) {
|
|
||||||
return ERR_SOCK_LISTEN;
|
|
||||||
}
|
|
||||||
return ERR_SOCK_SUCCESS;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
static inline int sock_accpet(SOCKET sock,SOCKET* client, char** accept_ip, unsigned short* port) {
|
static inline int sock_accpet(SOCKET sock,SOCKET* client, char** accept_ip, unsigned short* port) {
|
||||||
struct sockaddr_in caddr;
|
struct sockaddr_in caddr = { 0 };
|
||||||
socklen_t addrLen = sizeof(struct sockaddr_in);
|
socklen_t addrLen = sizeof(struct sockaddr_in);
|
||||||
|
|
||||||
if(client == NULL) {
|
if(client == NULL) {
|
||||||
@ -267,6 +341,16 @@ static inline void get_sock_err(char* buff_128, size_t buff_len, int errcode) {
|
|||||||
|
|
||||||
// ERR_SOCK_CONNECT,
|
// ERR_SOCK_CONNECT,
|
||||||
// };
|
// };
|
||||||
|
//https://learn.microsoft.com/zh-cn/windows/win32/api/ws2tcpip/nf-ws2tcpip-getaddrinfo?redirectedfrom=MSDN
|
||||||
|
//错误值 WSA 等效项 说明
|
||||||
|
//EAI_AGAIN WSATRY_AGAIN 名称解析暂时失败。
|
||||||
|
//EAI_BADFLAGS WSAEINVAL 为 pHints 参数的 ai_flags 成员提供了无效值。
|
||||||
|
//EAI_FAIL WSANO_RECOVERY 名称解析中发生不可恢复的失败。
|
||||||
|
//EAI_FAMILY WSAEAFNOSUPPORT 不支持 pHints 参数的 ai_family 成员。
|
||||||
|
//EAI_MEMORY WSA_NOT_ENOUGH_MEMORY 内存分配失败。
|
||||||
|
//EAI_NONAME WSAHOST_NOT_FOUND 所提供的参数的名称未解析,或者未提供 pNodeName 和 pServiceName 参数。
|
||||||
|
//EAI_SERVICE WSATYPE_NOT_FOUND pHints 参数的指定ai_socktype成员不支持 pServiceName 参数。
|
||||||
|
//EAI_SOCKTYPE WSAESOCKTNOSUPPORT 不支持 pHints 参数的 ai_socktype 成员。
|
||||||
|
|
||||||
sprintf(buff_128, "%3d", errcode);
|
sprintf(buff_128, "%3d", errcode);
|
||||||
}
|
}
|
||||||
|
@ -1,6 +1,7 @@
|
|||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
#include "../../ssocket.h"
|
#include "../../ssocket.h"
|
||||||
|
#include <limits.h>
|
||||||
|
|
||||||
#define CLIENT_SIZE 32
|
#define CLIENT_SIZE 32
|
||||||
SOCKET sfd;
|
SOCKET sfd;
|
||||||
@ -81,6 +82,7 @@ void acceptfunc(void* param) {
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
|
|
||||||
char Buf[128];
|
char Buf[128];
|
||||||
int res = make_server_sock(&sfd, _SOCKET_TEST_IP, _SOCKET_TEST_PORT);
|
int res = make_server_sock(&sfd, _SOCKET_TEST_IP, _SOCKET_TEST_PORT);
|
||||||
if(res != 0) {
|
if(res != 0) {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user