/
minisocket.c
761 lines (627 loc) · 19.2 KB
/
minisocket.c
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
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
/*
* Implementation of minisockets.
*/
#include "minithread.h"
#include "miniroute.h"
#include "minisocket.h"
#include "interrupts.h"
#include "alarm.h"
#define TCP_PORT_TYPE_SERVER 0
#define TCP_PORT_TYPE_CLIENT 1
#define TCP_MINIMUM_SERVER 0
#define TCP_MAXIMUM_SERVER 32767
#define TCP_MINIMUM_CLIENT 32768
#define TCP_MAXIMUM_CLIENT 65535
minisocket_t* minisockets;
semaphore_t server_mutex;
semaphore_t client_mutex;
queue_t sockets_to_delete;
semaphore_t delete_semaphore;
semaphore_t destroy_semaphore;
int currentClientPort;
void delete_sockets(void *arg); // forward declaration
/* Initializes the minisocket layer. */
void minisocket_initialize()
{
int i = TCP_MINIMUM_SERVER;
currentClientPort = 0;
//int currentClientPort = 0;
minisockets = (minisocket_t*) malloc(sizeof(minisocket_t) * (TCP_MAXIMUM_CLIENT - TCP_MINIMUM_SERVER + 1));
if (minisockets == NULL)
return;
while (i <= TCP_MAXIMUM_CLIENT)
{
minisockets[i] = NULL;
i++;
}
//Mutex that controls access to the minithreads array for server ports
server_mutex = semaphore_create();
semaphore_initialize(server_mutex, 1);
//Mutex that controls access to the minithreads array for client ports
client_mutex = semaphore_create();
semaphore_initialize(client_mutex, 1);
//Queue of sockets that will be deleted
sockets_to_delete = queue_new();
//Semaphore that signals the thread to delete sockets performs
delete_semaphore = semaphore_create();
semaphore_initialize(delete_semaphore, 0);
//Synchronize access to semaphore_destroy()
destroy_semaphore = semaphore_create();
semaphore_initialize(destroy_semaphore, 1);
//Fork the thread that deletes sockets on command
minithread_fork((proc_t) &delete_sockets, (void*) NULL);
}
// Create a packed reliable header given the parameters
mini_header_reliable_t create_reliable_header(network_address_t src_addr_raw,
int src_port_raw, network_address_t dst_addr_raw, int dst_port_raw,
char message_type, unsigned int seq_num_raw, unsigned int ack_num_raw)
{
mini_header_reliable_t header = (mini_header_reliable_t) malloc(sizeof(struct mini_header_reliable));
//Fields
char src_port[2];
char dst_port[2];
char src_addr[8];
char dst_addr[8];
char seq_num[4];
char ack_num[4];
//Pack everything
pack_unsigned_short(src_port, (unsigned short) src_port_raw);
pack_unsigned_short(dst_port, (unsigned short) dst_port_raw);
pack_address(src_addr, src_addr_raw);
pack_address(dst_addr, dst_addr_raw);
pack_unsigned_int(seq_num, seq_num_raw);
pack_unsigned_int(ack_num, ack_num_raw);
//Set header fields
header->protocol = (char) PROTOCOL_MINISTREAM;
header->message_type = message_type;
memcpy(header->source_address, src_addr, 8);
memcpy(header->destination_address, dst_addr, 8);
memcpy(header->source_port, src_port, 2);
memcpy(header->destination_port, dst_port, 2);
memcpy(header->seq_number, seq_num, 4);
memcpy(header->ack_number, ack_num, 4);
return header;
}
/* Used to wakeup a thread when it's attempting retransmissions */
void wake_up_semaphore(void* arg)
{
minisocket_t socket = (minisocket_t) arg;
interrupt_level_t prev_level = set_interrupt_level(DISABLED);
if (socket != NULL && (socket->waiting == TCP_PORT_WAITING_ACK
|| socket->waiting == TCP_PORT_WAITING_SYNACK))
{
semaphore_V(socket->wait_for_ack_semaphore);
}
set_interrupt_level(prev_level);
}
//Transmit a packet and handle retransmission attempts
int transmit_packet(minisocket_t socket, network_address_t dst_addr, int dst_port,
short incr_seq, char message_type, int data_len, char* data,
minisocket_error* error)
{
mini_header_reliable_t newReliableHeader;
void *alarmId;
int sendSucessful;
network_address_t my_addr;
int success = 0;
int connected = 0;
if (message_type == MSG_ACK)
connected = 1;
network_get_my_address(my_addr);
if (socket == NULL)
{
*error = SOCKET_INVALIDPARAMS;
return -1;
}
if (socket->status == TCP_PORT_CLOSING)
{
*error = SOCKET_SENDERROR;
return -1;
}
newReliableHeader = create_reliable_header(my_addr, socket->port_number, dst_addr,
dst_port, message_type, socket->seq_number, socket->ack_number);
socket->timeout = 100;
while(socket->timeout <= 6400)
{
printf("sending packet to %d, seq=%d, ack=%d, type=%d\n", dst_port, socket->seq_number, socket->ack_number, message_type);
sendSucessful = network_send_pkt(dst_addr, sizeof(struct mini_header_reliable),
(char*) newReliableHeader, data_len, (char*) data);
if (sendSucessful == -1)
{
socket->timeout *= 2;
continue;
}
alarmId = register_alarm(socket->timeout, &wake_up_semaphore, socket);
if (message_type == MSG_SYN)
{
socket->waiting = TCP_PORT_WAITING_SYNACK;
semaphore_P(socket->wait_for_ack_semaphore);
}
else if (!connected)
{
socket->waiting = TCP_PORT_WAITING_ACK;
semaphore_P(socket->wait_for_ack_semaphore);
}
else {
socket->waiting = TCP_PORT_WAITING_NONE;
}
semaphore_P(socket->mutex);
if (socket->waiting == TCP_PORT_WAITING_NONE)
{
/* I think incr_seq belongs before the _P on wait_for_ack, but that breaks it for now :/
if (incr_seq)
{
semaphore_P(socket->mutex);
socket->seq_number++;
semaphore_V(socket->mutex);
}
*/ if (message_type == MSG_SYN)
{
//we got a synack back, so we need to make sure to send an ack to the server
newReliableHeader = create_reliable_header(my_addr, socket->port_number, dst_addr,
dst_port, MSG_ACK, socket->seq_number, socket->ack_number);
socket->timeout = 100;
message_type = MSG_ACK;
connected = 1;
semaphore_V(socket->mutex);
continue;
}
deregister_alarm(alarmId);
success = 1;
semaphore_V(socket->mutex);
break;
}
else
{
if (socket->status == TCP_PORT_UNABLE_TO_CONNECT)
{
deregister_alarm(alarmId);
success = 0;
semaphore_V(socket->mutex);
break;
}
socket->timeout *= 2;
semaphore_V(socket->mutex);
}
}
semaphore_P(socket->mutex);
socket->timeout = 100;
if (success == 0)
{
socket->waiting = TCP_PORT_WAITING_NONE;
*error = SOCKET_NOSERVER;
}
semaphore_V(socket->mutex);
*error = SOCKET_NOERROR;
free(newReliableHeader);
return 0;
}
void delete_socket(void* arg)
{
minisocket_t socket = (minisocket_t) arg;
minisocket_destroy(socket, 0);
}
//Supposed to be run in the background
void delete_sockets(void *arg) {
minisocket_t socket;
semaphore_P(delete_semaphore);
queue_dequeue(sockets_to_delete, (void**) &socket);
register_alarm(15000, &delete_socket, (void*) socket);
semaphore_V(delete_semaphore);
}
//Destroys minisockets
void minisocket_destroy(minisocket_t minisocket, int FIN) {
int portNumber;
int i, threads;
interrupt_level_t prev_level;
minisocket_error error;
if (minisocket == NULL)
return;
portNumber = minisocket->port_number;
semaphore_P(destroy_semaphore);
minisocket->waiting = TCP_PORT_WAITING_TO_CLOSE;
if (minisockets[portNumber] == NULL)
return;
semaphore_V(minisocket->packet_ready);
semaphore_P(minisocket->mutex);
if (minisockets[portNumber] == NULL)
return;
if (FIN == 1) {
transmit_packet(minisocket, minisocket->destination_addr, minisocket->destination_port,
1, MSG_FIN, 0, NULL, &error);
}
minisocket->status = TCP_PORT_CLOSING;
prev_level = set_interrupt_level(DISABLED);
threads = minisocket->num_waiting_on_mutex;
for (i = 0; i < threads; i++)
{
semaphore_V(minisocket->mutex);
i++;
}
set_interrupt_level(prev_level);
minisockets[portNumber] = NULL;
semaphore_destroy(minisocket->wait_for_ack_semaphore);
semaphore_destroy(minisocket->mutex);
semaphore_destroy(minisocket->packet_ready);
if (minisocket->data_length != 0)
free(minisocket->data_buffer);
queue_free(minisocket->waiting_packets);
free(minisocket);
semaphore_V(destroy_semaphore);
}
/*
* Creates a socket
* The argument "port" is the port number of the created socket
*/
minisocket_t minisocket_create_socket(int port)
{
minisocket_t newMinisocket;
newMinisocket = (minisocket_t) malloc(sizeof(struct minisocket));
if (newMinisocket == NULL)
return NULL;
//Initialize fields
newMinisocket->port_number = port;
newMinisocket->status = TCP_PORT_LISTENING;
newMinisocket->seq_number = 0;
newMinisocket->ack_number = 0;
newMinisocket->data_buffer = NULL;
newMinisocket->data_length = 0;
newMinisocket->num_waiting_on_mutex = 0;
newMinisocket->timeout = 100;
newMinisocket->wait_for_ack_semaphore = semaphore_create();
if (newMinisocket->wait_for_ack_semaphore == NULL)
{
free(newMinisocket);
return NULL;
}
semaphore_initialize(newMinisocket->wait_for_ack_semaphore, 0);
newMinisocket->mutex = semaphore_create();
if (newMinisocket->mutex == NULL)
{
free(newMinisocket->wait_for_ack_semaphore);
free(newMinisocket);
return NULL;
}
semaphore_initialize(newMinisocket->mutex, 1);
newMinisocket->packet_ready = semaphore_create();
if (newMinisocket->packet_ready == NULL)
{
free(newMinisocket->mutex);
free(newMinisocket->wait_for_ack_semaphore);
free(newMinisocket);
return NULL;
}
semaphore_initialize(newMinisocket->packet_ready, 0);
newMinisocket->waiting_packets = queue_new();
if (newMinisocket->waiting_packets == NULL)
{
free(newMinisocket->packet_ready);
free(newMinisocket->mutex);
free(newMinisocket->wait_for_ack_semaphore);
free(newMinisocket);
return NULL;
}
return newMinisocket;
}
/*
* Listen for a connection from somebody else. When communication link is
* created return a minisocket_t through which the communication can be made
* from now on.
*
* The argument "port" is the port number on the local machine to which the
* client will connect.
*
* Return value: the minisocket_t created, otherwise NULL with the errorcode
* stored in the "error" variable.
*/
minisocket_t minisocket_server_create(int port, minisocket_error *error)
{
minisocket_t newMinisocket;
int ack_check;
int connected = 1;
network_interrupt_arg_t *arg;
mini_header_reliable_t header;
if (error == NULL)
return NULL;
if (port < TCP_MINIMUM_SERVER || port > TCP_MAXIMUM_SERVER)
{
*error = SOCKET_INVALIDPARAMS;
return NULL;
}
semaphore_P(server_mutex);
//Checks if port already exists
if (minisockets[port] != NULL)
{
*error = SOCKET_PORTINUSE;
semaphore_V(server_mutex);
return NULL;
}
newMinisocket = minisocket_create_socket(port);
if (newMinisocket == NULL)
{
*error = SOCKET_OUTOFMEMORY;
semaphore_V(server_mutex);
return NULL;
}
newMinisocket->port_type = TCP_PORT_TYPE_SERVER;
minisockets[port] = newMinisocket;
semaphore_V(server_mutex);
while (connected == 1)
{
semaphore_P(newMinisocket->packet_ready);
queue_dequeue(newMinisocket->waiting_packets, (void **) &arg);
header = (mini_header_reliable_t) arg->buffer;
if (header->message_type != MSG_SYN)
continue;
newMinisocket->status = TCP_PORT_CONNECTING;
unpack_address(header->source_address, newMinisocket->destination_addr);
newMinisocket->destination_port = unpack_unsigned_short(header->source_port);
ack_check = transmit_packet(newMinisocket, newMinisocket->destination_addr,
newMinisocket->destination_port, 1, MSG_SYNACK, 0, NULL, error);
if (ack_check == -1)
{
newMinisocket->status = TCP_PORT_LISTENING;
network_address_blankify(newMinisocket->destination_addr);
newMinisocket->destination_port = 0;
}
else
{
newMinisocket->status = TCP_PORT_CONNECTED;
connected = 0;
}
}
*error = SOCKET_NOERROR;
return newMinisocket;
}
/*
* Get the minisocket associated with a given port number
*/
minisocket_t minisocket_get(int num)
{
return minisockets[num];
}
/*
* Initiate the communication with a remote site. When communication is
* established create a minisocket through which the communication can be made
* from now on.
*
* The first argument is the network address of the remote machine.
*
* The argument "port" is the port number on the remote machine to which the
* connection is made. The port number of the local machine is one of the free
* port numbers.
*
* Return value: the minisocket_t created, otherwise NULL with the errorcode
* stored in the "error" variable.
*/
minisocket_t minisocket_client_create(network_address_t addr, int port, minisocket_error *error)
{
minisocket_t newMinisocket;
int totalClientPorts = TCP_MAXIMUM_CLIENT - TCP_MINIMUM_CLIENT + 1;
int convertedPortNumber = (currentClientPort % totalClientPorts) + TCP_MINIMUM_CLIENT;
int i = 1;
int transmitCheck;
if (error == NULL)
return NULL;
semaphore_P(client_mutex);
while (i < totalClientPorts && (minisockets[convertedPortNumber] != NULL))
{
convertedPortNumber = ((currentClientPort + i) % totalClientPorts) + TCP_MINIMUM_CLIENT;
i++;
}
currentClientPort += (i-1);
if (minisockets[convertedPortNumber] != NULL)
{
*error = SOCKET_NOMOREPORTS;
semaphore_V(client_mutex);
return NULL;
}
newMinisocket = minisocket_create_socket(convertedPortNumber);
if (newMinisocket == NULL)
{
*error = SOCKET_OUTOFMEMORY;
semaphore_V(client_mutex);
return NULL;
}
newMinisocket->port_type = TCP_PORT_TYPE_CLIENT;
network_address_copy(addr, newMinisocket->destination_addr);
newMinisocket->destination_port = convertedPortNumber;
minisockets[convertedPortNumber] = newMinisocket;
newMinisocket->status = TCP_PORT_CONNECTING;
semaphore_V(client_mutex);
transmitCheck = transmit_packet(newMinisocket, addr, port, 1, MSG_SYN, 0, NULL, error);
if (transmitCheck == -1)
{
//*error set by transmit_packet()
minisockets[convertedPortNumber] = NULL;
free(newMinisocket);
return NULL;
}
newMinisocket->ack_number++;
//newMinisocket->waiting = TCP_PORT_WAITING_SYNACK;
//semaphore_P(newMinisocket->wait_for_ack_semaphore);
transmitCheck = transmit_packet(newMinisocket, addr, port, 1, MSG_ACK, 0, NULL, error);
if (transmitCheck == -1)
{
minisockets[convertedPortNumber] = NULL;
free(newMinisocket);
return NULL;
}
newMinisocket->status = TCP_PORT_CONNECTED;
*error = SOCKET_NOERROR;
return newMinisocket;
}
/*
* Send a message to the other end of the socket.
*
* The send call should block until the remote host has ACKnowledged receipt of
* the message. This does not necessarily imply that the application has called
* 'minisocket_receive', only that the packet is buffered pending a future
* receive.
*
* It is expected that the order of calls to 'minisocket_send' implies the order
* in which the concatenated messages will be received.
*
* 'minisocket_send' should block until the whole message is reliably
* transmitted or an error/timeout occurs
*
* Arguments: the socket on which the communication is made (socket), the
* message to be transmitted (msg) and its length (len).
* Return value: returns the number of successfully transmitted bytes. Sets the
* error code and returns -1 if an error is encountered.
*/
int minisocket_send(minisocket_t socket, minimsg_t msg, int len, minisocket_error *error)
{
int portNumber;
int sentLength;
int sentData = 0;
int maxDataSize;
int check;
//interrupt_level_t prev_level;
if (error == NULL)
return -1;
if (socket == NULL || msg == NULL || len <= 0)
{
*error = SOCKET_INVALIDPARAMS;
return -1;
}
portNumber = socket->port_number;
if (socket->status == TCP_PORT_CLOSING || socket->waiting == TCP_PORT_WAITING_TO_CLOSE
|| minisockets[portNumber] == NULL)
{
*error = SOCKET_SENDERROR;
return -1;
}
/*prev_level = set_interrupt_level(DISABLED);
socket->num_waiting_on_mutex++;
semaphore_P(socket->mutex);
socket->num_waiting_on_mutex--;
set_interrupt_level(prev_level);
*/
if (socket->status == TCP_PORT_CLOSING || socket->waiting == TCP_PORT_WAITING_TO_CLOSE
|| minisockets[portNumber] == NULL)
{
*error = SOCKET_SENDERROR;
// semaphore_V(socket->mutex);
return -1;
}
maxDataSize = MAX_NETWORK_PKT_SIZE - sizeof(struct mini_header_reliable);
while (len > 0)
{
sentLength = (maxDataSize > len ? len : maxDataSize);
check = transmit_packet(socket, socket->destination_addr, socket->destination_port, 1,
MSG_ACK, sentLength, (msg+sentData), error);
if (check == -1)
{
// semaphore_V(socket->mutex);
minisocket_destroy(socket, 0);
return (sentData == 0 ? -1 : sentData);
}
len -= maxDataSize;
sentData += sentLength;
}
// semaphore_V(socket->mutex);
*error = SOCKET_NOERROR;
return sentData;
}
/*
* Receive a message from the other end of the socket. Blocks until
* some data is received (which can be smaller than max_len bytes).
*
* Arguments: the socket on which the communication is made (socket), the memory
* location where the received message is returned (msg) and its
* maximum length (max_len).
* Return value: -1 in case of error and sets the error code, the number of
* bytes received otherwise
*/
int minisocket_receive(minisocket_t socket, minimsg_t msg, int max_len, minisocket_error *error)
{
int portNumber;
int packetSize;
char* packetContents;
network_interrupt_arg_t* arg;
int dataLength;
int returnLength;
char* oldDataBuffer;
char* newDataBuffer;
int newLength;
if (socket == NULL || msg == NULL || max_len <= 0)
{
*error = SOCKET_INVALIDPARAMS;
return -1;
}
portNumber = socket->port_number;
if (socket->status == TCP_PORT_CLOSING || socket -> waiting == TCP_PORT_WAITING_TO_CLOSE
|| minisockets[portNumber] == NULL)
{
*error = SOCKET_RECEIVEERROR;
return -1;
}
if (socket->status == TCP_PORT_CLOSING || socket -> waiting == TCP_PORT_WAITING_TO_CLOSE
|| minisockets[portNumber] == NULL)
{
*error = SOCKET_RECEIVEERROR;
semaphore_V(socket->mutex);
return -1;
}
if (socket->data_length == 0 && queue_length(socket->waiting_packets) == 0)
{
semaphore_P(socket->packet_ready);
if (socket->status == TCP_PORT_CLOSING || socket->waiting == TCP_PORT_WAITING_TO_CLOSE
|| minisockets[portNumber] == NULL)
{
*error = SOCKET_RECEIVEERROR;
semaphore_V(socket->mutex);
return -1;
}
}
semaphore_P(socket->mutex);
if (queue_length(socket->waiting_packets) > 0)
{
queue_dequeue(socket->waiting_packets, (void**) &arg);
packetSize = arg->size;
//check this line
packetContents = (char*) (arg->buffer + sizeof(struct mini_header_reliable));
dataLength = packetSize - sizeof(struct mini_header_reliable);
if (dataLength > 0)
{
newDataBuffer = (char*) malloc(socket->data_length + dataLength);
memcpy(newDataBuffer, socket->data_buffer, socket->data_length);
memcpy(newDataBuffer+socket->data_length, packetContents, dataLength);
free(socket->data_buffer);
socket->data_buffer = newDataBuffer;
socket->data_length += dataLength;
}
free(arg);
}
returnLength = socket->data_length;
if (returnLength > max_len)
returnLength = max_len;
memcpy(msg, socket->data_buffer, returnLength);
newLength = socket->data_length - returnLength;
if (newLength <= 0)
{
free(socket->data_buffer);
socket->data_buffer = NULL;
}
else
{
oldDataBuffer = socket->data_buffer;
socket->data_buffer = (char*) malloc(newLength);
memcpy(socket->data_buffer, oldDataBuffer+returnLength, newLength);
free(oldDataBuffer);
}
socket->data_length = newLength;
semaphore_V(socket->mutex);
*error = SOCKET_NOERROR;
return returnLength;
}
/* Close a connection. If minisocket_close is issued, any send or receive should
* fail. As soon as the other side knows about the close, it should fail any
* send or receive in progress. The minisocket is destroyed by minisocket_close
* function. The function should never fail.
*/
void minisocket_close(minisocket_t socket)
{
//minisocket_destroy(socket, 1);
}