aboutsummaryrefslogtreecommitdiff
path: root/client/helloworld/net.c
blob: 83192e0f4ca151b5b9cc09a9a914d085ca051578 (plain)
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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
/*
 * Created by yuuta on 7/22/22.
 *
 * A simple cross-platform (Unix and Windows) implementation of a socket client.
 */

#include "net.h"

#include <stdint.h>
#include <errno.h>
#include <stdio.h>
#include <string.h>

#ifdef WIN32
#include <windows.h> /* For DWORD */
#include <winsock2.h>
#include <ws2tcpip.h>

#define errno_sock WSAGetLastError()

#else

#include <sys/socket.h>
#include <netdb.h>
#include <unistd.h>
#include <sys/time.h>

#define closesocket(socket) close(socket)
#define errno_sock errno

#endif

int net_connect(const char *host,
                const uint16_t port,
                SOCKET *sock) {
    int r;
    struct addrinfo *res;
    char service[6];

    snprintf(service, 6, "%u", port);
    if ((r = getaddrinfo(host, service, NULL, &res))) {
        return r;
    }

    SOCKET fd;
    if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) <= 0) {
        const int e = errno_sock;
        freeaddrinfo(res);
        return e;
    }
    if (connect(fd, res->ai_addr, res->ai_addrlen) < 0) {
        const int e = errno_sock;
        closesocket(fd);
        freeaddrinfo(res);
        return e;
    }
    freeaddrinfo(res);
    *sock = fd;
    return 0;
}

int net_read(const SOCKET *sock,
             void *buffer,
             const size_t length,
             size_t *read,
             const unsigned int timeout) {

#ifdef WIN32
    DWORD to = timeout * 1000;
    if (setsockopt(*sock, SOL_SOCKET, SO_RCVTIMEO, (const char*)&to, sizeof to)) {
#else
    struct timeval tv;
    tv.tv_sec = timeout;
    tv.tv_usec = 0;
    if (setsockopt(*sock, SOL_SOCKET, SO_RCVTIMEO, (const char *) &tv, sizeof tv)) {
#endif
        return errno_sock;
    }

    int bytes;
    if ((bytes = (int) recv(*sock, buffer, length, 0)) <= 0) {
        if (bytes < 0) {
            const int e = errno_sock;
#ifdef WIN32
            if (e == WSAETIMEDOUT) {
#else
            if (e == EAGAIN) {
#endif
                return NET_TIMEOUT;
            }
            return e;
        }
        return NET_CLOSED;
    }
    *read = bytes;
    return 0;
}

int net_send(const SOCKET *sock,
             const void *buffer,
             size_t len) {
    size_t pos;
    const uint8_t *ptr = buffer;
    int retval;

    for (pos = 0U; pos < len; pos += retval) {
        size_t n = len - pos;
        retval = (int) send(*sock, &ptr[pos], (int) n, 0);
        if (retval <= 0) {
            return errno_sock;
        }
    }
    return 0;
}

int net_init(void) {
#ifdef WIN32
    int r;
    WORD wVersionRequested;
    WSADATA wsaData;
    wVersionRequested = MAKEWORD(2, 2);
    if ((r = WSAStartup(wVersionRequested, &wsaData))) {
        fprintf(stderr, "WSAStartup failed with error: %d\n", r);
        return r;
    }
    if (LOBYTE(wsaData.wVersion) != 2 || HIBYTE(wsaData.wVersion) != 2) {
        fprintf(stderr, "Could not find a usable version of Winsock.dll\n");
        WSACleanup();
        return 1;
    }
#endif
    return 0;
}

void net_close(const SOCKET *sock) {
    closesocket(*sock);
}

#ifdef WIN32
static __declspec( thread ) LPTSTR err_buf = NULL;
#else
static _Thread_local char err_buf[1024];
#endif

void net_free(void) {
#ifdef WIN32
    WSACleanup();
    if (err_buf) {
        LocalFree(err_buf);
        err_buf = NULL;
    }
#endif
}

char *net_strerror(int errnum) {
#ifdef WIN32
    if (err_buf) {
        LocalFree(err_buf);
        err_buf = NULL;
    }
    FormatMessage(
            FORMAT_MESSAGE_FROM_SYSTEM |
            FORMAT_MESSAGE_ALLOCATE_BUFFER |
            FORMAT_MESSAGE_IGNORE_INSERTS,
            NULL,
            errnum,
            MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
            (LPTSTR) &err_buf,
            0,
            NULL);
    if (!err_buf) {
        return "Unknown error";
    }
#else
    int r;
    if ((r = strerror_r(errnum, err_buf, sizeof(err_buf) / sizeof(char)))) {
        snprintf(err_buf, 1024, "%d (%d)", errnum, r);
    }
#endif
    return err_buf;
}