-
Notifications
You must be signed in to change notification settings - Fork 1
/
shl_socket.h
247 lines (183 loc) · 5.72 KB
/
shl_socket.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
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
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
/**
* BSD 3-Clause License
*
* Copyright (c) 2021, undersquire
* All rights reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions are met:
*
* 1. Redistributions of source code must retain the above copyright notice, this
* list of conditions and the following disclaimer.
*
* 2. Redistributions in binary form must reproduce the above copyright notice,
* this list of conditions and the following disclaimer in the documentation
* and/or other materials provided with the distribution.
*
* 3. Neither the name of the copyright holder nor the names of its
* contributors may be used to endorse or promote products derived from
* this software without specific prior written permission.
*
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
* AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE
* DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE
* FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
* DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
* SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER
* CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#ifndef SHL_SOCKET_H
#define SHL_SOCKET_H
#ifdef __cplusplus
extern "C" {
#endif
enum shl_socket_type {
SHL_SOCKET_TCP
};
typedef int shl_socket_t;
struct shl_socket_info {
char address[64];
unsigned short port;
};
/* Open a socket using specified backend */
shl_socket_t shl_socket_open(int type);
/* Connect a socket to the specified remote host */
int shl_socket_connect(shl_socket_t socket, char *address, unsigned short port);
/* Bind a socket to the specified port and start listening */
int shl_socket_listen(shl_socket_t socket, unsigned short port);
/* Accept a socket and store info into specified client_info structure */
shl_socket_t shl_socket_accept(shl_socket_t socket, struct shl_socket_info *info);
/* Send data through connected socket */
int shl_socket_send(shl_socket_t socket, char *buf, int size);
/* Receive data from connected socket */
int shl_socket_recv(shl_socket_t socket, char *buf, int size);
/* Close a socket */
void shl_socket_close(shl_socket_t socket);
#ifdef SHL_SOCKET_IMPL
#ifdef _WIN32
#include <windows.h>
shl_socket_t shl_socket_open(int type)
{
shl_socket_t s = -1;
WSADATA data;
if (WSAStartup(MAKEWORD(2, 2), &data) != 0)
return s;
switch (type) {
case SHL_SOCKET_TCP:
s = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
break;
}
return s;
}
int shl_socket_connect(shl_socket_t socket, char *address, unsigned short port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = port;
inet_pton(AF_INET, address, &addr.sin_addr);
return connect(socket, (struct sockaddr *)&addr, sizeof(addr));
}
int shl_socket_listen(shl_socket_t socket, unsigned short port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.S_un.S_addr = INADDR_ANY;
if (bind(socket, (struct sockaddr *)&addr, sizeof(addr)) != 0)
return -1;
return listen(socket, SOMAXCONN);
}
shl_socket_t shl_socket_accept(shl_socket_t socket, struct shl_socket_info *info)
{
shl_socket_t client = -1;
struct sockaddr_in addr;
int addr_size = sizeof(addr);
client = accept(socket, (struct sockaddr *)&addr, &addr_size);
if (client == -1)
return client;
if (info != NULL) {
info->port = addr.sin_port;
inet_ntop(AF_INET, &addr.sin_addr, info->address, INET_ADDRSTRLEN);
}
return client;
}
int shl_socket_send(shl_socket_t socket, char *buf, int size)
{
return send(socket, buf, size, 0);
}
int shl_socket_recv(shl_socket_t socket, char *buf, int size)
{
return recv(socket, buf, size, 0);
}
void shl_socket_close(shl_socket_t socket)
{
closesocket(socket);
}
#else
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/socket.h>
shl_socket_t shl_socket_open(int type)
{
shl_socket_t socket = -1;
switch (type) {
case SHL_SOCKET_TCP:
socket = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP);
break;
}
return socket;
}
int shl_socket_connect(shl_socket_t socket, char *address, unsigned short port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = port;
inet_pton(AF_INET, address, &(addr.sin_addr));
return connect(socket, (struct sockaddr *)&addr, sizeof(addr));
}
int shl_socket_listen(shl_socket_t socket, unsigned short port)
{
struct sockaddr_in addr;
addr.sin_family = AF_INET;
addr.sin_port = port;
addr.sin_addr.s_addr = INADDR_ANY;
if (bind(socket, (struct sockaddr *)&addr, sizeof(addr)) != 0)
return -1;
return listen(socket, SOMAXCONN);
}
shl_socket_t shl_socket_accept(shl_socket_t socket, struct shl_socket_info *info)
{
shl_socket_t client = -1;
struct sockaddr_in addr;
int addr_size = sizeof(addr);
client = accept(socket, (struct sockaddr *)&addr, &addr_size);
if (client == -1)
return -1;
if (info != NULL) {
info->port = addr.sin_port;
inet_ntop(AF_INET, &(addr.sin_addr), info->address, INET_ADDRSTRLEN);
}
return client;
}
int shl_socket_send(shl_socket_t socket, char *buf, int size)
{
return send(socket, buf, size, 0);
}
int shl_socket_recv(shl_socket_t socket, char *buf, int size)
{
return recv(socket, buf, size, 0);
}
void shl_socket_close(shl_socket_t socket)
{
close(socket);
}
#endif /* UNIX */
#endif /* IMPL */
#ifdef __cplusplus
}
#endif
#endif /* SHL_SOCKET_H */