144 lines
3.2 KiB
C
144 lines
3.2 KiB
C
#ifndef INCLUDED_C_SOCKET_H
|
|
#define INCLUDED_C_SOCKET_H
|
|
|
|
#ifndef INCLUDED_C_TYPES_H
|
|
#include <c_Types.h>
|
|
#endif /*INCLUDED_C_TYPES_H*/
|
|
|
|
#ifndef INCLUDED_STDIO_H
|
|
#define INCLUDED_STDIO_H
|
|
#include <stdio.h>
|
|
#endif /*INCLUDED_STDIO_H*/
|
|
|
|
#ifndef INCLUDED_STDLIB_H
|
|
#define INCLUDED_STDLIB_H
|
|
#include <stdlib.h>
|
|
#endif /*INCLUDED_STDLIB_H*/
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
|
|
#ifndef _WIN32_WINNT
|
|
#define _WIN32_WINNT 0x0600
|
|
#endif
|
|
|
|
#ifndef INCLUDED_WINSOCK2_H
|
|
#define INCLUDED_WINSOCK2_H
|
|
#include <winsock2.h>
|
|
#endif /*INCLUDED_WINSOCK2_H*/
|
|
|
|
#ifndef INCLUDED_IPHLPAPI_H
|
|
#define INCLUDED_IPHLPAPI_H
|
|
#include <iphlpapi.h>
|
|
#endif /*INCLUDED_IPHLPAPI_H*/
|
|
|
|
#ifndef INCLUDED_WS2TCPIP_H
|
|
#define INCLUDED_WS2TCPIP_H
|
|
#include <ws2tcpip.h>
|
|
#endif /*INCLUDED_WS2TCPIP_H*/
|
|
|
|
|
|
typedef SOCKET c_socket_t;
|
|
|
|
#define C_INVALID_SOCKET INVALID_SOCKET
|
|
#define C_SOCKET_ERROR SOCKET_ERROR
|
|
|
|
#define c_Socket_Close(x) (closesocket(x), (x)=C_INVALID_SOCKET)
|
|
#define c_Socket_IsValid(s) ((s)!=INVALID_SOCKET)
|
|
#define c_Socket_GetErrno() (WSAGetLastError())
|
|
|
|
C_STATIC_FORCE_INLINE
|
|
void c_Socket_Init(void) {
|
|
WSADATA wsa;
|
|
int err = WSAStartup(MAKEWORD(2,2), &wsa);
|
|
assert(err==0);
|
|
}
|
|
|
|
C_STATIC_FORCE_INLINE
|
|
void c_Socket_Destroy(void) {
|
|
WSACleanup();
|
|
}
|
|
|
|
#else /* other platform */
|
|
|
|
#ifndef INCLUDED_SYS_TYPES_H
|
|
#define INCLUDED_SYS_TYPES_H
|
|
#include <sys/types.h>
|
|
#endif /*INCLUDED_SYS_TYPES_H*/
|
|
|
|
#ifndef INCLUDED_SYS_SOCKET_H
|
|
#define INCLUDED_SYS_SOCKET_H
|
|
#include <sys/socket.h>
|
|
#endif /*INCLUDED_SYS_SOCKET_H*/
|
|
|
|
#ifndef INCLUDED_NETDB_H
|
|
#define INCLUDED_NETDB_H
|
|
#include <netdb.h>
|
|
#endif /*INCLUDED_NETDB_H*/
|
|
|
|
#ifndef INCLUDED_NETINET_IN_H
|
|
#define INCLUDED_NETINET_IN_H
|
|
#include <netinet/in.h>
|
|
#endif /*INCLUDED_NETINET_IN_H*/
|
|
|
|
#ifndef INCLUDED_ARPA_INET_H
|
|
#define INCLUDED_ARPA_INET_H
|
|
#include <arpa/inet.h>
|
|
#endif /*INCLUDED_ARPA_INET_H*/
|
|
|
|
#ifndef INCLUDED_UNISTD_H
|
|
#define INCLUDED_UNISTD_H
|
|
#include <unistd.h>
|
|
#endif /*INCLUDED_UNISTD_H*/
|
|
|
|
#ifndef INCLUDED_ERRNO_H
|
|
#define INCLUDED_ERRNO_H
|
|
#include <errno.h>
|
|
#endif /*INCLUDED_ERRNO_H*/
|
|
|
|
|
|
|
|
|
|
typedef int c_socket_t;
|
|
|
|
#define C_INVALID_SOCKET (-1)
|
|
#define C_SOCKET_ERROR (-1)
|
|
|
|
#define c_Socket_IsValid(s) ((s)>=0)
|
|
#define c_Socket_Close(x) (close(x), (x)=C_INVALID_SOCKET)
|
|
#define c_Socket_GetErrno() (errno)
|
|
#define c_Socket_Init()
|
|
#define c_Socket_Destroy()
|
|
|
|
#endif
|
|
|
|
|
|
/* ------------------------------------------------------------------------------------------------------------------ */
|
|
/* */
|
|
|
|
C_STATIC_FORCE_INLINE
|
|
void c_Socket_SetReuseAddr(c_socket_t s, c_bool_t is_reuse) {
|
|
int reuse = is_reuse?1:0;
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, (const char*)&reuse, sizeof(reuse));
|
|
#else
|
|
setsockopt(s, SOL_SOCKET, SO_REUSEADDR, &reuse, sizeof(reuse));
|
|
#endif
|
|
}
|
|
|
|
C_STATIC_FORCE_INLINE
|
|
void c_Socket_SetIPV6Only(c_socket_t s, c_bool_t is_IPV6only) {
|
|
int ipv6only = is_IPV6only?1:0;
|
|
#if defined(_WIN32) || defined(_WIN64)
|
|
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, (const char*)&ipv6only, sizeof(ipv6only));
|
|
#else
|
|
setsockopt(s, IPPROTO_IPV6, IPV6_V6ONLY, &ipv6only, sizeof(ipv6only));
|
|
#endif
|
|
}
|
|
|
|
|
|
#endif /*INCLUDED_C_SOCKET_H*/
|