-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathsocket.c
More file actions
345 lines (251 loc) · 6.3 KB
/
Copy pathsocket.c
File metadata and controls
345 lines (251 loc) · 6.3 KB
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
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
#include "config.h"
#include "socket.h"
#include <string.h>
#include <stdlib.h>
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <iconv.h>
#include <endian.h>
#include <stdarg.h>
static iconv_t conv_ucs2_to_utf8=NULL;
static iconv_t conv_utf8_to_ucs2=NULL;
private __attribute__((noreturn)) bool socketPanic(Socket* socket, const char* format, ...)
{
va_list args;
va_start(args,format);
vfprintf(stderr,format,args);
fprintf(stderr,"\n");
if(socket->onError!=NULL)
socket->onError();
else
panicv(format,args);
va_end(args);
abort();
}
public uint8_t readByte(Socket* socket)
{
uint8_t n;
socketRead(socket,&n,sizeof(n));
return n;
}
public uint16_t readShort(Socket* socket)
{
uint16_t n;
socketRead(socket,&n,sizeof(n));
return be16toh(n);
}
public uint32_t readInt(Socket* socket)
{
uint32_t n;
socketRead(socket,&n,sizeof(n));
return be32toh(n);
}
public uint64_t readLong(Socket* socket)
{
uint64_t n;
socketRead(socket,&n,sizeof(n));
return be64toh(n);
}
public bool readBool(Socket* socket)
{
switch(readByte(socket))
{
case 0:
return false;
case 1:
return true;
default:
panic("Invalid bool value");
}
}
public float readFloat(Socket* socket)
{
union{float f;uint32_t i;}u;
u.i=readInt(socket);
return u.f;
}
public double readDouble(Socket* socket)
{
union{double f;uint64_t i;}u;
u.i=readLong(socket);
return u.f;
}
// Write
public void writeByte(Socket* socket, uint8_t n)
{
socketWrite(socket, &n, sizeof(n));
}
public void writeShort(Socket* socket, uint16_t h)
{
uint16_t n=htobe16(h);
socketWrite(socket, &n, sizeof(n));
}
public void writeInt(Socket* socket, uint32_t h)
{
uint32_t n=htobe32(h);
socketWrite(socket, &n, sizeof(n));
}
public void writeLong(Socket* socket, uint64_t h)
{
uint64_t n=htobe64(h);
socketWrite(socket, &n, sizeof(n));
}
public void writeFloat(Socket* socket, float h)
{
uint32_t n;
float* bytes=(float*)&n;
*bytes=h;
writeInt(socket,n);
}
public void writeDouble(Socket* socket, double h)
{
uint64_t n;
double* bytes=(double*)&n;
*bytes=h;
writeLong(socket,n);
}
public void writeBool(Socket* socket, bool n)
{
writeByte(socket,n!=false);
}
public char* readString16(Socket* socket)
{
int length=readShort(socket);
uint16_t ucs_string[length];
socketRead(socket,ucs_string,sizeof(ucs_string));
char utf8_string[length*2+1];
memset(utf8_string,0,sizeof(utf8_string));
size_t in_left=length*2;
size_t out_left=length*2;
char *in=(char*)ucs_string;
char *out=utf8_string;
while(in_left>0)
{
iconv(conv_ucs2_to_utf8,&in,&in_left,&out,&out_left);
}
//printf("string: '%s'\n",utf8_string);
return strdup(utf8_string);
}
public void readStream(Socket* socket)
{
while(true)
{
byte type=readByte(socket);
if(type==0x7f)
return;
//printf("\tmeta: %02x %i %i\n",type, type&0x1f, type>>5);
switch(type>>5)
{
case 0:
readByte(socket);
break;
case 1:
readShort(socket);
break;
case 2:
readInt(socket);
break;
case 3:
readFloat(socket);
break;
case 4:
readString16(socket);
break;
case 5:
readShort(socket);
readByte(socket);
readShort(socket);
break;
case 6:
readInt(socket);
readInt(socket);
readInt(socket);
break;
default:
socketPanic(socket,"unknown metadata 0x%02x (0x%02x)",type,type>>5);
}
}
}
public void writeString16(Socket* socket, const char* string)
{
char* utf8_string=strdupa(string);
int length=strlen(utf8_string);
uint16_t ucs_string[length];
size_t in_left=length;
size_t out_left=sizeof(ucs_string);
char *in=utf8_string;
char *out=(char*)ucs_string;
while(in_left>0)
{
iconv(conv_utf8_to_ucs2,&in,&in_left,&out,&out_left);
}
int ucs_length=length-out_left/2;
writeShort(socket,ucs_length);
socketWrite(socket, ucs_string, ucs_length*2);
}
public void socketFlush(Socket* socket)
{
if(socket->buffer_length==0)
return;
int ret=write(socket->fd, socket->buffer, socket->buffer_length);
if(ret!=socket->buffer_length)
{
socketPanic(socket,"write error len:%i ret:%i fd:%i",socket->buffer_length,ret,socket->fd);
}
socket->buffer_length=0;
}
public bool socketOpen(Socket* sock, const char* host, short port)
{
assert(conv_ucs2_to_utf8!=NULL && conv_utf8_to_ucs2!=NULL);
assert(sock!=NULL);
sock->fd = socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
if(sock->fd<0)
return false;
struct sockaddr_in address={0};
address.sin_family = AF_INET;
address.sin_port = htons(port);
int ret = inet_pton(AF_INET, host, &address.sin_addr);
assert(ret>0);
ret = connect(sock->fd, (struct sockaddr *)&address, sizeof(address) );
if(ret!=0)
{
close(sock->fd);
return false;
}
return true;
}
public void socketClose(Socket* socket)
{
}
public void socketWrite(Socket* socket, void* data, size_t length)
{
assert(socket->fd>0);
if(socket->buffer_length+length>=sizeof(socket->buffer))
socketFlush(socket);
assert(socket->buffer_length+length<sizeof(socket->buffer));
memcpy(socket->buffer+socket->buffer_length,data,length);
socket->buffer_length+=length;
}
public void socketRead(Socket* socket, void* data, size_t length)
{
byte* ptr=data;
while(length>0)
{
int ret=read(socket->fd, ptr, length);
if(ret<=0)
socketPanic(socket,"read error");
length-=ret;
ptr+=ret;
}
}
public void socketInit()
{
if(conv_ucs2_to_utf8==NULL)
conv_ucs2_to_utf8=iconv_open("UTF-8","UCS-2BE");
if(conv_utf8_to_ucs2==NULL)
conv_utf8_to_ucs2=iconv_open("UCS-2BE","UTF-8");
}