diff --git a/include/ssocket.h b/include/ssocket.h index 8bd1654..8b69ce0 100644 --- a/include/ssocket.h +++ b/include/ssocket.h @@ -76,21 +76,27 @@ enum { ERR_SOCK_EAI_SOCKTYPE, }; -static inline int make_sock(SOCKET* sock); -static inline int sock_connect(SOCKET sock, const char* connect_ip, unsigned short port); -static inline int sock_bindlisten(SOCKET sock, const char* server_ip, unsigned short port); -static inline int sock_accpet(SOCKET sock,SOCKET* client, char** accept_ip, unsigned short* port); + static inline int make_server_sock(SOCKET* sock, const char* server_ip, unsigned short port); static inline int make_client_sock(SOCKET* sock, const char* connect_ip, unsigned short port); static inline void close_sock(SOCKET sock); static inline void out_sock_err(FILE* output, int errcode); static inline void get_sock_err(char* buff_128, size_t buff_len, int errcode); - static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsigned short port, struct addrinfo** resaddr); static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsigned short port, struct addrinfo** resaddr); +static inline int make_sock(SOCKET* sock); +static inline int sock_connect(SOCKET sock, const char* connect_ip, unsigned short port); +static inline int sock_bind(SOCKET sock, const char* server_ip, unsigned short port); +static inline int sock_bindlisten(SOCKET sock, const char* server_ip, unsigned short port); +static inline int sock_accpet(SOCKET sock, SOCKET* client, char** accept_ip, unsigned short* port); +static inline int make_sock_tcp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_STREAM); } +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 _getaddrinfo(struct addrinfo* inf, struct addrinfo** resaddr, const char* ip, const char* port); static inline int _socket(SOCKET* sock, struct addrinfo* resaddr); @@ -98,12 +104,8 @@ static inline int _connect(SOCKET sock, struct addrinfo* resaddr); static inline int _bind(SOCKET sock, struct addrinfo* resaddr); static inline int _bindlisten(SOCKET sock, struct addrinfo* resaddr); static inline int _accept(SOCKET sock, SOCKET* client, struct addrinfo* inf); - static inline int _sock(SOCKET* sock, int af, int type); -static inline int make_sock_tcp4(SOCKET* sock) { return _sock(sock, AF_INET, SOCK_STREAM); } -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); } + //not recommand and it will be remove static inline int make_sock(SOCKET* sock) { return make_sock_tcp4(sock); }; @@ -220,6 +222,21 @@ RES: return res; } +static inline int sock_bind(SOCKET sock, const char* server_ip, unsigned short port) { + int res = ERR_SOCK_SUCCESS; + char buf[8] = { 0 }; + sprintf(buf, "%u", port); + struct addrinfo inf = { 0 }; + struct addrinfo* resaddr = NULL; + res = _getaddrinfo(&inf, &resaddr, server_ip, buf); + if (res != ERR_SOCK_SUCCESS) goto RES; + res = _bind(sock, resaddr); + if (res != ERR_SOCK_SUCCESS) goto RES; +RES: + if (resaddr) freeaddrinfo(resaddr); + return res; +} + static inline int sock_bindlisten(SOCKET sock, const char* server_ip, unsigned short port) { int res = ERR_SOCK_SUCCESS; char buf[8] = { 0 }; @@ -307,6 +324,7 @@ static inline int make_server_sock_ex(SOCKET* sock, const char* server_ip, unsig res = _bindlisten(*sock, *resaddr); if (res != ERR_SOCK_SUCCESS) return res; + return res; } static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsigned short port, @@ -336,7 +354,9 @@ static inline int make_client_sock_ex(SOCKET* sock, const char* connect_ip, unsi res = _connect(*sock, *resaddr); if (res != ERR_SOCK_SUCCESS) return res; } + return res; } + static inline void close_sock(SOCKET sock) { if(sock == -1) return; #if WIN_PART