hat-util  0.6.16
Utility library
socket.h
Go to the documentation of this file.
1 #ifndef HAT_SOCKET_H
2 #define HAT_SOCKET_H
3 
4 #include <stdbool.h>
5 
6 #ifdef _WIN32
7 #include <winsock2.h>
8 #include <ws2tcpip.h>
9 #include <iphlpapi.h>
10 #include <windows.h>
11 #else
12 #include <sys/socket.h>
13 #include <arpa/inet.h>
14 #include <netdb.h>
15 #include <unistd.h>
16 #endif
17 
18 #include "buff.h"
19 
20 #define HAT_SOCKET_SUCCESS 0
21 #define HAT_SOCKET_ERROR (-1)
22 
23 #define HAT_SOCKET_TYPE_IP4_TCP 0
24 #define HAT_SOCKET_TYPE_IP4_UDP 1
25 
26 #ifdef __cplusplus
27 extern "C" {
28 #endif
29 
30 typedef uint32_t hat_socket_ip4_addr_t;
31 typedef uint16_t hat_socket_port_t;
32 
33 typedef struct {
34  int type;
39 
40  // internal
41 #ifdef _WIN32
42  SOCKET socket;
43 #else
44  int socket;
45 #endif
46 } hat_socket_t;
47 
48 
51 
54 
55 int hat_socket_open(hat_socket_t *s, int type);
57 int hat_socket_set_blocking(hat_socket_t *s, bool blocking);
58 
60  hat_socket_port_t port);
62  hat_socket_port_t port);
63 int hat_socket_listen(hat_socket_t *s, int backlog);
65 
72 
73 #ifdef __cplusplus
74 }
75 #endif
76 
77 #endif
Data buffer.
int hat_socket_listen(hat_socket_t *s, int backlog)
uint16_t hat_socket_port_t
Definition: socket.h:31
int hat_socket_send_to(hat_socket_t *s, hat_buff_t *data, hat_socket_ip4_addr_t host, hat_socket_port_t port)
int hat_socket_setup()
int hat_socket_bind(hat_socket_t *s, hat_socket_ip4_addr_t host, hat_socket_port_t port)
int hat_socket_receive(hat_socket_t *s, hat_buff_t *data)
int hat_socket_open(hat_socket_t *s, int type)
int hat_socket_addr_convert(char *addr, hat_socket_ip4_addr_t *host)
uint32_t hat_socket_ip4_addr_t
Definition: socket.h:30
int hat_socket_connect(hat_socket_t *s, hat_socket_ip4_addr_t host, hat_socket_port_t port)
int hat_socket_send(hat_socket_t *s, hat_buff_t *data)
int hat_socket_addr_resolve(char *name, hat_socket_ip4_addr_t *host)
int hat_socket_accept(hat_socket_t *s, hat_socket_t *client)
int hat_socket_receive_from(hat_socket_t *s, hat_buff_t *data, hat_socket_ip4_addr_t host, hat_socket_port_t port)
void hat_socket_cleanup()
int hat_socket_close(hat_socket_t *s)
int hat_socket_set_blocking(hat_socket_t *s, bool blocking)
Data buffer.
Definition: buff.h:28
Definition: socket.h:33
hat_socket_port_t remote_port
Definition: socket.h:38
hat_socket_ip4_addr_t remote_host
Definition: socket.h:37
int socket
Definition: socket.h:44
hat_socket_ip4_addr_t local_host
Definition: socket.h:35
hat_socket_port_t local_port
Definition: socket.h:36
int type
Definition: socket.h:34