-
Notifications
You must be signed in to change notification settings - Fork 10
/
sockets.h
81 lines (64 loc) · 2.04 KB
/
sockets.h
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
#ifndef SOCKETS_H
#define SOCKETS_H
#include <stdio.h>
#include <stdint.h>
#include <stdbool.h>
#if (defined _WIN32)
#include <Windows.h>
#include <winsock.h>
#include <winsock2.h>
// # pragma comment(lib, "wsock32.lib")
#elif (defined _LINUX)
#include <sys/types.h>
#include <sys/socket.h>
#include <sys/select.h>
#include <unistd.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <errno.h>
#include <string.h>
#include <fcntl.h>
#endif
#if (defined _WIN32)
typedef struct sockaddr sockaddr_t;
typedef struct sockaddr_in sockaddr_in_t;
typedef struct in_addr in_addr_t;
typedef int socklen_t;
#elif (defined _LINUX)
typedef int SOCKET;
typedef struct sockaddr sockaddr_t;
typedef struct sockaddr_in sockaddr_in_t;
//typedef struct in_addr in_addr_t;
#define closesocket(S) close(S)
#define INVALID_SOCKET -1
#endif
#ifndef HOSTNAME_MAX
#define HOSTNAME_MAX 64
#endif
#ifndef IW_ESSID_MAX_SIZE
#define IW_ESSID_MAX_SIZE 64
#endif
#define MAC_LEN 6
#define IPADDR_LEN 4
// windows: wifi infos via
// C:> netsh wlan show interfaces
// linux: wifi infos via
// $ sudo ifconfig
bool parse_ipstr(const char* str, uint8_t* ip);
bool parse_macstr(const char* str, uint8_t* mac);
void print_socketerror(const char* msg);
bool sockets_init(void);
void sockets_deinit(void);
bool sockopt_broadcast(SOCKET sock);
bool sockopt_blocking(SOCKET sock, bool blocking);
SOCKET udpsock(void);
void setup_addr_in(sockaddr_in_t* addr, const char* addrstr, unsigned short port);
#define setup_addr_in_any(addr, port) setup_addr_in(addr, "0.0.0.0", port)
#define setup_addr_in_broadcast(addr, port) setup_addr_in(addr, "255.255.255.255", port)
#define STREQU(STR1, STR2) (strcmp(STR1, STR2) == 0)
#define eprintf(...) fprintf (stderr, __VA_ARGS__)
int dump_own_addrs(void);
int get_ifc_addr(const char* ifname, char* ipstr);
int wifi_get_ssid(const char* ifname, char* essid, uint8_t* bssid);
#endif // #ifndef SOCKETS_H