-
-
Notifications
You must be signed in to change notification settings - Fork 0
/
common.h
86 lines (79 loc) · 2.33 KB
/
common.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
82
83
84
85
86
#pragma once
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#ifdef _WIN32
#include <winsock2.h>
#include <ws2tcpip.h>
#else
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#endif
#define LOG_INFO(msg, ...) printf("[*] " __FILE__ ":%d (%s) :: " msg "\n", __LINE__, __func__, ##__VA_ARGS__);
#define LOG_ERROR(msg, ...) fprintf(stderr, "[!] " __FILE__ ":%d (%s) :: " msg "\n", __LINE__, __func__, ##__VA_ARGS__)
#define WFT_SOCKET_LISTEN_BACKLOG 69
int wft_socket_create(void) {
#ifdef _WIN32
WSADATA wsaData;
if (WSAStartup(MAKEWORD(2, 2), &wsaData)) {
LOG_ERROR("unable to init Winsock (%d)", WSAGetLastError());
exit(1);
}
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (INVALID_SOCKET == (long long unsigned int) fd) {
LOG_ERROR("unable to create socket (%d)", WSAGetLastError());
WSACleanup();
exit(1);
}
return fd;
#else
int fd = socket(AF_INET, SOCK_STREAM, 0);
if (-1 == fd) {
LOG_ERROR("unable to create socket (%s)", strerror(errno));
exit(1);
}
return fd;
#endif
}
void wft_socket_bind(int fd, const struct sockaddr_in * restrict addr) {
#ifdef _WIN32
if (SOCKET_ERROR != bind(fd, (const struct sockaddr *) addr, sizeof(*addr))) return;
LOG_ERROR("unable to bind socket (%d)", WSAGetLastError());
closesocket(fd);
WSACleanup();
#else
if (-1 != bind(fd, (const struct sockaddr *) addr, sizeof(*addr))) return;
LOG_ERROR("unable to bind socket (%s)", strerror(errno));
close(fd);
#endif
exit(1);
}
void wft_socket_listen(int fd) {
#ifdef _WIN32
if (SOCKET_ERROR != listen(fd, WFT_SOCKET_LISTEN_BACKLOG)) return;
LOG_ERROR("unable to listen through socket (%d)", WSAGetLastError());
closesocket(fd);
WSACleanup();
#else
if (-1 != listen(fd, WFT_SOCKET_LISTEN_BACKLOG)) return;
LOG_ERROR("unable to listen through socket (%s)", strerror(errno));
close(fd);
#endif
exit(1);
}
void wft_socket_connect(int fd, const struct sockaddr_in * restrict addr) {
#ifdef _WIN32
if (SOCKET_ERROR != connect(fd, (const struct sockaddr *) addr, sizeof(*addr))) return;
LOG_ERROR("could not connect to server (%d)", WSAGetLastError());
closesocket(fd);
WSACleanup();
#else
if (-1 != connect(fd, (const struct sockaddr *) addr, sizeof(*addr))) return;
LOG_ERROR("could not connect to server");
close(fd);
#endif
exit(1);
}