File: socket.c

package info (click to toggle)
vice 3.7.1%2Bdfsg1-2
  • links: PTS
  • area: contrib
  • in suites: bookworm
  • size: 49,876 kB
  • sloc: ansic: 431,530; cpp: 35,357; sh: 9,747; makefile: 7,010; perl: 1,822; yacc: 1,197; lex: 589; sed: 184; objc: 115; xml: 36
file content (1130 lines) | stat: -rw-r--r-- 33,062 bytes parent folder | download
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
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
/*! \file socket.c \n
 *  \author Spiro Trikaliotis\n
 *  \brief  Abstraction from network sockets.
 *
 * socket.c - Abstraction from network sockets.
 *
 * Written by
 *  Spiro Trikaliotis <spiro.trikaliotis@gmx.de>
 *  Marco van den Heuvel <blackystardust68@yahoo.com>
 *
 * based on code from network.c written by
 *  Andreas Matthies <andreas.matthies@gmx.net>
 *
 * This file is part of VICE, the Versatile Commodore Emulator.
 * See README for copyright notice.
 *
 *  This program is free software; you can redistribute it and/or modify
 *  it under the terms of the GNU General Public License as published by
 *  the Free Software Foundation; either version 2 of the License, or
 *  (at your option) any later version.
 *
 *  This program is distributed in the hope that it will be useful,
 *  but WITHOUT ANY WARRANTY; without even the implied warranty of
 *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 *  GNU General Public License for more details.
 *
 *  You should have received a copy of the GNU General Public License
 *  along with this program; if not, write to the Free Software
 *  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
 *  02111-1307  USA.
 *
 */

#include "vice.h"

#ifdef HAVE_NETWORK

#include <assert.h>
#include <errno.h>
#include <stdlib.h>
#include <string.h>

/* hopefully fixed in configure */
#if 0
/* get rid of the asserts when compiling in non debug mode. somehow
   configure fails to define NDEBUG instead */
#ifndef DEBUG
#undef assert
#define assert(x)
#endif
#endif

#ifdef HAVE_STRINGS_H
#include <strings.h>
#endif

#include "socketimpl.h"

/* Fix Windows' definition of 'INVALID_SOCKET (SOCKET)(~0)', which breaks the
 * code further down. Any 'normal' OS uses -1, but Microsft had to use an
 * unsigned int with INVALID_SOCKET being the largest value for that unsigned
 * int.
 *
 * Since Windows only works on two's complement systems, this will work.
 */
#ifdef WINDOWS_COMPILE
# undef INVALID_SOCKET
# define INVALID_SOCKET -1
#endif

#include "archdep.h"
#include "lib.h"
#include "log.h"
#include "vicesocket.h"
#include "signals.h"

#ifndef HAVE_SOCKLEN_T
typedef size_t socklen_t;
#endif

#ifndef SOL_TCP
#define SOL_TCP IPPROTO_TCP
#endif

/*! \brief determine the number of elements of an array

 \param _x
   The array of which the size is to be determined

 \remark
   The count of elements of the array

 \remark
   Make sure to really pass an array, not a pointer, as _x
*/
#define arraysize(_x) ( sizeof _x / sizeof _x[0] )

/*! \internal \brief Access to socket addresses
 *
 * This union is used to access socket addresses.
 * It replaces the casts needed otherwise.
 *
 * Furthermore, it ensures we have always enough
 * memory for any type needed. (sizeof struct sockaddr_in6
 * is bigger than struct sockaddr).
 *
 * Note, however, that there is no consensus if the C standard
 * guarantees that all union members start at the same
 * address. There are arguments for and against this.
 *
 * However, in practice, this approach works.
 */
union socket_addresses_u {
    struct sockaddr generic;     /*!< the generic type needed for calling the socket API */

#ifdef HAVE_UNIX_DOMAIN_SOCKETS
    struct sockaddr_un local;    /*!< an Unix Domain Socket (file system) socket address */
#endif

    struct sockaddr_in ipv4;     /*!< an IPv4 socket address */

#ifdef HAVE_IPV6
    struct sockaddr_in6 ipv6;    /*!< an IPv6 socket address */
#endif
};

/*! \internal \brief opaque structure describing an address for use with socket functions
 *
 */
struct vice_network_socket_address_s {
    unsigned int used;      /*!< 1 if this entry is being used, 0 else.
                             * This is used for debugging the buffer
                             * allocation strategy.
                             */
    int domain;             /*!< the address family (AF_INET, ...) of this address */
    int protocol;           /*!< the protocol of this address. This can be used to distinguish between different types of an address family. */
#ifdef HAVE_SOCKLEN_T
    socklen_t len;          /*!< the length of the socket address */
#else
    int len;
#endif
    union socket_addresses_u address; /* the socket address */
};

/*! \internal \brief opaque structure describing a socket */
struct vice_network_socket_s {
    SOCKET sockfd;           /*!< the socket handle */
    vice_network_socket_address_t address; /*!< place for an address. It is updated on accept(). */
    unsigned int used;      /*!< 1 if this entry is being used, 0 else.
                             * This is used for debugging the buffer
                             * allocation strategy.
                             */
};

#ifndef HAVE_HTONL
# ifndef htonl
/*! \internal \brief convert a long from host order into network order

 \param ip
   The value in host order which is to be converted into network order

 \return
   The value in network order

 \remark
   This implementation is only used if the architecture does
   not already provide it.

*/
static unsigned int htonl(unsigned int ip)
{
#  ifdef WORDS_BIGENDIAN
    return ip;
#  else /* !WORDS_BIGENDIAN */
    unsigned int ip2;

    ip2 = ((ip >> 24) & 0xff) + (((ip >> 16) & 0xff) << 8) + (((ip >> 8) & 0xff) << 16) + ((ip & 0xff) << 24);
    return ip2;
#  endif /* WORDS_BIGENDIAN */
}
# endif /* !htonl */
#endif /* !HAVE_HTONL */

#ifndef HAVE_HTONS
# ifndef htons
/*! \internal \brief convert a short from host order into network order

 \param ip
   The value in host order which is to be converted into network order

 \return
   The value in network order

 \remark
   This implementation is only used if the architecture does
   not already provide it.

*/
static unsigned short htons(unsigned short ip)
{
#  ifdef WORDS_BIGENDIAN
    return ip;
#  else /* !WORDS_BIGENDIAN */
    unsigned short ip2;

    ip2 = ((ip >> 8) & 0xff) + ((ip & 0xff) << 8);
    return ip2;
#  endif /* WORDS_BIGENDIAN */
}
# endif /* !htons */
#endif /* !HAVE_HTONS */

/*! \internal \brief a memory pool for network addresses */
static vice_network_socket_address_t address_pool[16];
/*! \internal \brief usage bit pattern for address_pool */
static unsigned int address_pool_usage = 0;

/*! \internal \brief a memory pool for sockets */
static vice_network_socket_t socket_pool[16];
/*! \internal \brief usage bit pattern for socket_pool */
static unsigned int socket_pool_usage = 0;

/*! \internal \brief Get the next free entry of a pool

  \param PoolUsage
     Pointer to a bit pattern that contains the allocations
     of the pool.

  \return
     The next free pool entry, or -1 if there is none left.

  \remark
     A 1 bit marks a used pool entry, a 0 bit a free pool entry.

  \remark
     In the current implementation, this function is restricted
     to 16 entries.
*/
static int get_new_pool_entry(unsigned int * PoolUsage)
{
    int next_free = -1;

    static const int nextentry[] = {
        0, 1, 0, 2, 0, 1, 0, 3,
        0, 1, 0, 2, 0, 1, 0, -1
    };

    next_free = nextentry[*PoolUsage & 0x0f];
    if (next_free < 0) {
        next_free = nextentry[(*PoolUsage >> 4) & 0x0f];
        if (next_free >= 0) {
            next_free += 4;
        } else {
            next_free = nextentry[(*PoolUsage >> 8) & 0x0f];
            if (next_free >= 0) {
                next_free += 8;
            } else {
                next_free = nextentry[(*PoolUsage >> 12) & 0x0f];
                if (next_free >= 0) {
                    next_free += 12;
                }
            }
        }
    }

    if (next_free >= 0) {
        *PoolUsage |= 1u << next_free;
    }

    return next_free;
}

/*! \internal \brief Get a free memory area for a socket

  \param sockfd
     socket handle as initialisation value.

  \return
     NULL on error;
     else, a pointer to an empty vice_network_socket_t structure

  \remark
     If not used anymore, the returned pointer must be freed
     with a call to vice_network_socket_close().
*/
static vice_network_socket_t * vice_network_alloc_new_socket(SOCKET sockfd)
{
    vice_network_socket_t * return_address = NULL;
    int i = get_new_pool_entry(&socket_pool_usage);

    if (i >= arraysize(socket_pool)) {
        i = -1;
    }

    assert(i >= 0);

    if (i >= 0) {
        assert(socket_pool[i].used == 0);

        return_address = &socket_pool[i];
        memset(return_address, 0, sizeof *return_address);
        return_address->used = 1;
        return_address->sockfd = sockfd;
    }

    return return_address;
}

/*! \internal \brief Initialise networking
 *
 * This function initialises networking. It ensures
 * that the initialisation is performed only once.
 *
 * \return
 *   0 on success, else -1.
 *
 * \remark
 *   If this function returns with an error, it is
 *   likely that all subsequent networking functions
 *   will fail.
 */
static int socket_init(void)
{
    static int init_done = 0;

    if (init_done) {
        return 0;
    }

    init_done = 1;

    return archdep_network_init();
}

/*! \internal \brief Initialise a socket address structure
 *
 * This function initialises a socket address structure.
 *
 * Regardless of the structure was used before or not, after
 * this call, the memory contents are as if the structure
 * was never used before.
 *
 * \param address
 *   Pointer to the memory structure to initialise
 *
 * \remark
 *   This function marks the memory structure as being used
 *   and initialises the length of the address field.
 */
static void initialize_socket_address(vice_network_socket_address_t * address)
{
    memset(address, 0, sizeof *address);
    address->used = 1;
    address->len = sizeof address->address;
}

/*! \internal \brief Get a free memory area for a socket address

  \return
     NULL on error;
     else, a pointer to an empty vice_network_socket_address_t structure

  \remark
     If not used anymore, the returned pointer must be freed
     with a call to vice_network_address_close().
*/
static vice_network_socket_address_t * vice_network_alloc_new_socket_address(void)
{
    vice_network_socket_address_t * return_address = NULL;
    int i = get_new_pool_entry(&address_pool_usage);

    if (i >= arraysize(address_pool)) {
        i = -1;
    }

    assert(i >= 0);

    if (i >= 0) {
        assert(address_pool[i].used == 0);

        return_address = &address_pool[i];
        initialize_socket_address(return_address);
    }

    return return_address;
}

/*! \brief Open a socket and initialise it for server operation

  \param server_address
     The address of the server to which to bind to.

  \return
     0 on error;
     else, a handle to the socket on success.

  \remark
     The server_address variable determines the type of
     socket to be used (IPv4, IPv6, Unix Domain Socket, ...)
     Thus, server_address must not be NULL.
*/
vice_network_socket_t *vice_network_server(
        const vice_network_socket_address_t * server_address)
{
#if defined(SO_REUSEADDR) || defined(TCP_NODELAY)
    const int so_setting = 1;
#endif
    int sockfd = INVALID_SOCKET;
    int error = 1;
    int err;

    assert(server_address != NULL);

    do {
        if (socket_init() < 0) {
            log_error(LOG_DEFAULT,
                "vice_network_server(): socket_init() failed");
            break;
        }

        sockfd = (int)socket(server_address->domain, SOCK_STREAM, server_address->protocol);
        if (sockfd == INVALID_SOCKET) {
            err = errno;
            log_error(LOG_DEFAULT,
                "vice_network_server(): socket() returned INVALID_SOCKET: %s",
                strerror(err));
            break;
        }

        /*
            Fix the "Address In Use" error upon reconnecting to tcp socket monitor port
            by setting SO_REUSEPORT/ADDR options on socket before bind()

            Ignore setsockopt() failures - just continue - the socket is still valid.
        */
#ifdef HAVE_IPV6
        if ((server_address->domain == PF_INET) || (server_address->domain == PF_INET6)) {
#else
        if ((server_address->domain == PF_INET)) {
#endif
#if defined(SO_REUSEADDR)
            setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, (const void*)&so_setting, sizeof(so_setting));
#endif
#if defined(TCP_NODELAY)
            setsockopt(sockfd, SOL_TCP, TCP_NODELAY, (const void*)&so_setting, sizeof(so_setting));
#endif
        }

        if (bind(sockfd, &server_address->address.generic, server_address->len) < 0) {
            err = errno;
            log_error(LOG_DEFAULT,
                "vice_network_server(): bind() failed: %s",
                strerror(err));
            break;
        }
        if (listen(sockfd, 2) < 0) {
            err = errno;
            log_error(LOG_DEFAULT,
                "vice_network_server(): listen() failed: %s",
                strerror(err));
            break;
        }
        error = 0;
    } while (0);

    if (error) {
        if (sockfd != INVALID_SOCKET) {
            closesocket(sockfd);
        }
        sockfd = INVALID_SOCKET;
    }

    return sockfd == INVALID_SOCKET ? NULL : vice_network_alloc_new_socket(sockfd);
}

/*! \brief Open a socket and initialise it for client operation

  \param server_address
     The address of the server to which to connect to.

  \return
     0 on error;
     else, a handle to the socket on success.

  \remark
     The server_address variable determines the type of
     socket to be used (IPv4, IPv6, Unix Domain Socket, ...)
*/
vice_network_socket_t * vice_network_client(const vice_network_socket_address_t * server_address)
{
    int sockfd = INVALID_SOCKET;
    int error = 1;

    assert(server_address != NULL);

    do {
        if (socket_init() < 0) {
            break;
        }

        sockfd = (int)socket(server_address->domain, SOCK_STREAM, server_address->protocol);

        if (sockfd == INVALID_SOCKET) {
            break;
        }

#if defined(TCP_NODELAY)
        setsockopt(sockfd, SOL_TCP, TCP_NODELAY, (const void*)&error, sizeof(error)); /* just an integer with 1, not really an error */
#endif

        if (connect(sockfd, &server_address->address.generic, server_address->len) < 0) {
            break;
        }
        error = 0;
    } while (0);

    if (error) {
        if (sockfd != INVALID_SOCKET) {
            closesocket(sockfd);
        }
        sockfd = INVALID_SOCKET;
    }

    return sockfd == INVALID_SOCKET ? NULL : vice_network_alloc_new_socket(sockfd);
}

/*! \internal \brief Generate an IPv4 socket address

  Initialises a socket address with an IPv4 address.

  \param socket_address
     Pointer to an empty socket address that will be initialized

  \param address_string
     A string describing the address to set.

  \param port
     A default port number. If address_string does not
     specify a port number, the number here will be used.
     If address_string specifies a port number, that one
     will be used instead.

  \return
     0 on success,
     else an error occurred.

  \remark
     address_string must be specified in the form
     &lt;host&gt;
     or
     &lt;host&gt;:&lt;port&gt;
     with &lt;host&gt; being the name or the IPv4 address of the host,
     and &lt;port&gt; being the port number. If the first form is used,
     the default port will be used.
*/
static int vice_network_address_generate_ipv4(
        vice_network_socket_address_t *socket_address,
        const char *address_string,
        unsigned short port)
{
    char * address_part = lib_strdup(address_string);
    int error = 1;

    do {
        /* preset the socket address with port and INADDR_ANY */

        memset(&socket_address->address, 0, sizeof socket_address->address);
        socket_address->domain = PF_INET;
        socket_address->protocol = IPPROTO_TCP;
        socket_address->len = sizeof socket_address->address.ipv4;
        socket_address->address.ipv4.sin_family = AF_INET;
        socket_address->address.ipv4.sin_port = htons(port);
        socket_address->address.ipv4.sin_addr.s_addr = INADDR_ANY;

        if (address_string) {
            /* an address string was specified, try to use it */
            struct hostent * host_entry;

            char * port_part = NULL;

            /* try to find out if a port has been specified */
            port_part = strchr(address_string, ':');

            if (port_part) {
                char * p;
                unsigned long new_port;

                /* yes, there is a port: Copy the part before, so we can modify it */
                p = lib_strdup(address_string);

                p[port_part - address_string] = 0;
                lib_free(address_part);
                address_part = p;

                ++port_part;

                new_port = strtoul(port_part, &p, 10);

                if (*p == 0) {
                    socket_address->address.ipv4.sin_port = htons((unsigned short) new_port);
                }
            }

            if (address_part[0] == 0) {
                /* there was no address give, do not try to process it. */
                error = 0;
                break;
            }

            host_entry = gethostbyname(address_part);

            if (host_entry != NULL && host_entry->h_addrtype == AF_INET) {
                if (host_entry->h_length != sizeof socket_address->address.ipv4.sin_addr.s_addr) {
                    /* something weird happened... SHOULD NOT HAPPEN! */
                    log_message(LOG_DEFAULT,
                                "gethostbyname() returned an IPv4 address, "
                                "but the length is wrong: %d",
                                host_entry->h_length );
                    break;
                }

                memcpy(&socket_address->address.ipv4.sin_addr.s_addr,
                        host_entry->h_addr_list[0],
                        host_entry->h_length);
            } else {
                /* Assume it is an IP address */

                if (address_part[0] != 0) {
#ifdef HAVE_INET_ATON
                    /*
                     * this implementation is preferable, as inet_addr() cannot
                     * return the broadcast address, as it has the same encoding
                     * as INADDR_NONE.
                     *
                     * Unfortunately, not all ports have inet_aton(), so, we must
                     * provide both implementations.
                     */
                    if (inet_aton(address_part,
                                &socket_address->address.ipv4.sin_addr.s_addr) == 0) {
                        /* no valid IP address */
                        break;
                    }
#else
                    in_addr_t ip = inet_addr(address_part);

                    if (ip == INADDR_NONE) {
                        /* no valid IP address */
                        break;
                    }
                    socket_address->address.ipv4.sin_addr.s_addr = ip;
#endif
                }
            }

            error = 0;
        }
    } while (0);

    lib_free(address_part);
    return error;
}

/*! \internal \brief Generate an IPv6 socket address

  Initialises a socket address with an IPv6 address.

  \param socket_address
     Pointer to an empty socket address that will be initialized

  \param address_string
     A string describing the address to set.

  \param port
     A default port number. If address_string does not
     specify a port number, the number here will be used.
     If address_string specifies a port number, that one
     will be used instead.

  \return
     0 on success,
     else an error occurred.

  \remark
     address_string must be specified in one of the forms
     &lt;host&gt;
     or
     [&lt;host&gt;]:&lt;port&gt;
     with &lt;hostname&gt; being the name of the host,
     &lt;hostipv6&gt; being the IP of the host, and
     &lt;host&gt; being the name of the host or its IPv6,
     and &lt;port&gt; being the port number.

     The extra braces [...] in case the port is specified are
     needed as IPv6 addresses themselves already contain colons,
     and it would be impossible to clearly distinguish between
     an IPv6 address, and an IPv6 address where a port has been
     specified. This format is a common one.

  \remark
     On platforms which do not support IPv6, this function
     returns -1 as error.

  \todo
     Specifying a port is not implemented yet.
*/
static int vice_network_address_generate_ipv6(vice_network_socket_address_t * socket_address, const char * address_string, unsigned short port)
{
#ifdef HAVE_IPV6
    int error = 1;

    do {
        struct hostent * host_entry = NULL;
#ifndef HAVE_GETHOSTBYNAME2
        int err6;
#endif

        /* preset the socket address */

        memset(&socket_address->address, 0, sizeof socket_address->address);
        socket_address->domain = PF_INET6;
        socket_address->protocol = IPPROTO_TCP;
        socket_address->len = sizeof socket_address->address.ipv6;
        socket_address->address.ipv6.sin6_family = AF_INET6;
        socket_address->address.ipv6.sin6_port = htons(port);
        socket_address->address.ipv6.sin6_addr = in6addr_any;

        if (!address_string || address_string[0] == 0) {
            /* there was no address give, do not try to process it. */
            error = 0;
            break;
        }
#ifdef HAVE_GETHOSTBYNAME2
        host_entry = gethostbyname2(address_string, AF_INET6);
#else
        host_entry = getipnodebyname(address_string, AF_INET6, AI_DEFAULT, &err6);
#endif
        if (host_entry == NULL) {
            break;
        }

        memcpy(&socket_address->address.ipv6.sin6_addr, host_entry->h_addr, host_entry->h_length);

#ifndef HAVE_GETHOSTBYNAME2
        freehostent(host_entry);
#endif
        error = 0;
    } while (0);

    return error;
#else /* #ifdef HAVE_IPV6 */
    log_message(LOG_DEFAULT, "IPv6 is not supported in this installation of VICE!\n");
    return -1;
#endif /* #ifdef HAVE_IPV6 */
}

/*! \internal \brief Generate a unix domain socket address

  Initialises a socket address with a unix domain socket address

  \param socket_address
     Pointer to an empty socket address that will be initialized

  \param address_string
     A string describing the address to set.

  \return
     0 on success,
     else an error occurred.

  \remark
     address_string must be the path of a special file
     which represents the Unix domain socket.

  \remark
     On platforms which do not support unix domain sockets, this function
     returns -1 as error.
*/
static int vice_network_address_generate_local(vice_network_socket_address_t * socket_address, const char * address_string)
{
#ifdef HAVE_UNIX_DOMAIN_SOCKETS
    int error = 1;

    do {
        if (address_string[0] == 0) {
            break;
        }

        /* initialise the socket address */

        memset(&socket_address->address, 0, sizeof socket_address->address);
        socket_address->domain = PF_UNIX;
        socket_address->protocol = 0;
        socket_address->len = sizeof socket_address->address.local;
        socket_address->address.local.sun_family = AF_UNIX;

        if (strlen(address_string) >= sizeof socket_address->address.local.sun_path) {
            log_message(LOG_DEFAULT,
                        "Unix domain socket name of '%s' is too long; only %"PRI_SIZE_T" chars are allowed.",
                        address_string, sizeof(socket_address->address.local.sun_path));
            break;
        }
        strcpy(socket_address->address.local.sun_path, address_string);

        error = 0;
    } while (0);

    return error;

#else /* #ifdef HAVE_UNIX_DOMAIN_SOCKETS */
    log_message(LOG_DEFAULT, "Unix domain sockets are not supported in this installation of VICE!\n");
    return -1;
#endif /* #ifdef HAVE_UNIX_DOMAIN_SOCKETS */
}

/*! \brief Generate a socket address

  Initialises a socket address with the value
  specified as input parameter.

  \param address_string
     A string describing the address to set.

  \param port
     A default port number. If address_string does not
     specify a port number, the number here will be used.
     If address_string specifies a port number, that one
     will be used instead.

  \return socket_address
     Pointer to a socket address that has been initialised
     to the specification in address_string (and port, if
     supported).
     NULL in case of an error.

  \remark
     If address_string starts with a pipe ('|'), then the
     address_string is treated as a unix domain socket.
     Otherwise, address_string can be prepended with ip6://
     or ip4://, in which case address_string is treated
     exactly as an IPv6 or IPv4 address, respectively.

     If there is no specification, this routine tests for
     an IPv6 address. If this fails, it tries an IPv4 address.

*/
vice_network_socket_address_t * vice_network_address_generate(const char * address_string, unsigned short port)
{
    vice_network_socket_address_t * socket_address = NULL;
    int error = 1;

    do {
        socket_address = vice_network_alloc_new_socket_address();
        if (socket_address == NULL) {
            break;
        }

        if (address_string && address_string[0] == '|') {
            if (vice_network_address_generate_local(socket_address, &address_string[1])) {
                break;
            }
        } else if (address_string && strncmp("ip6://", address_string, sizeof "ip6://" - 1) == 0) {
            if (vice_network_address_generate_ipv6(socket_address, &address_string[sizeof "ip6://" - 1], port)) {
                break;
            }
        } else if (address_string && strncmp("ip4://", address_string, sizeof "ip4://" - 1) == 0) {
            if (vice_network_address_generate_ipv4(socket_address, &address_string[sizeof "ip4://" - 1], port)) {
                break;
            }
        } else {
            /* the user did not specify the type of the address, try to guess it by trying IPv6, then IPv4 */
#ifdef HAVE_IPV6
            if (vice_network_address_generate_ipv6(socket_address, address_string, port))
#endif /* #ifdef HAVE_IPV6 */
            {
                if (vice_network_address_generate_ipv4(socket_address, address_string, port)) {
                    break;
                }
            }
        }

        error = 0;
    } while (0);

    if (error && socket_address) {
        vice_network_address_close(socket_address);
        socket_address = NULL;
    }

    return socket_address;
}


/*! \brief Return memory of a socket address

  This function returns a socket address to the memory pool
  of addresses.

  \param address
     A socket address that has been allocated by
     vice_network_alloc_new_socket_address()
     or by vice_network_address_generate().
*/
void vice_network_address_close(vice_network_socket_address_t * address)
{
    if (address) {
        assert(address->used == 1);
        assert(((address_pool_usage & (1u << (address - address_pool))) != 0));

        address->used = 0;
        address_pool_usage &= ~(1u << (address - address_pool));
    }
}

/*! \brief Accept a connection on a listening socket

  This function accepts a connection on a socket that
  has been opened with vice_network_server().

  \param sockfd
     The socket that has been oped with vice_network_server()

  \return
     A new socket that can be used for transmission on this this connection.
*/
vice_network_socket_t * vice_network_accept(vice_network_socket_t * sockfd)
{
    SOCKET newsocket = INVALID_SOCKET;

    initialize_socket_address( &sockfd->address );

    newsocket = accept(sockfd->sockfd, &sockfd->address.address.generic, &sockfd->address.len);

    return newsocket == INVALID_SOCKET ? NULL : vice_network_alloc_new_socket(newsocket);
}

/*! \brief Close a socket

  This function closes the socket that has been
  opened either by vice_network_server(), vice_network_client()
  or vice_network_accept().

  \param sockfd
     The socket to be closed

  \return
     0 on success, else an error occurred.
*/
int vice_network_socket_close(vice_network_socket_t * sockfd)
{
    SOCKET localsockfd = INVALID_SOCKET;
    int error = -1;

    if (sockfd) {
        localsockfd = sockfd->sockfd;

        assert(sockfd->used == 1);
        assert(((socket_pool_usage & (1u << (sockfd - socket_pool))) != 0));

        sockfd->used = 0;
        socket_pool_usage &= ~(1u << (sockfd - socket_pool));

        error = closesocket(localsockfd);
    }

    return error;
}

/*! \brief Send data on a connected socket

  This function sends outgoing data to a connected socket.
  opened either by vice_network_client()
  or vice_network_accept().

  \param sockfd
     The connected socket to send to

  \param buffer
     Pointer to the buffer which holds the data to send

  \param buffer_length
     The length of the buffer pointed to by buffer.

  \param flags
     Flags for the socket. These flags are architecture dependent.

  \return
     the number of bytes send. For non-blocking sockets,
     this can be less than len. For blocking sockets (default),
     any return value different than len must be treated as an error.

  \note Amazing there's docs on this, but send() returns size_t, not int, so
        properly checking the return type could fail.
*/
int vice_network_send(vice_network_socket_t * sockfd, const void * buffer,
                         size_t buffer_length, int flags)
{
    size_t ret;
    signals_pipe_set();
    ret = send(sockfd->sockfd, buffer, buffer_length, flags);
    if (ret > buffer_length) {
        log_error(LOG_DEFAULT, "vice_network_send: internal error");
        ret = -1; /* signal error */
    }
    signals_pipe_unset();
    return (int)ret;
}

/*! \brief Receive data from a connected socket

  This function receives incoming data from a connected socket.
  opened either by vice_network_client()
  or vice_network_accept().

  \param sockfd
     The connected socket to receive from

  \param buffer
     Pointer to the buffer which will hold the received data

  \param buffer_length
     The length of the buffer pointed to by buffer. This
     indicates the maximum number of bytes to receive.

  \param flags
     Flags for the socket. These flags are architecture dependent.

  \return
     the number of bytes received. This can be less than
     buffer_length.

     When the return value is 0, either the other site has
     gracefully closed the socket (and all data has been
     received), or no data was received in the case
     of a non-blocking socket.

     In case of an error, -1 is returned.
*/
int vice_network_receive(vice_network_socket_t * sockfd, void * buffer, size_t buffer_length, int flags)
{
    size_t ret;
    signals_pipe_set();
    ret = recv(sockfd->sockfd, buffer, buffer_length, flags);
    signals_pipe_unset();
    return (int)ret;
}

/*! \brief Check if a socket has incoming data to receive

  This function is called in order to determine if there is
  data to be received on a socket. For a blocking socket, this
  is the only way to receive data without actually blocking.

  \param readsockfd
     The connected socket to test for data

  \return
     1 if the specified socket has data; 0 if it does not contain
     any data, and -1 in case of an error.
*/
int vice_network_select_poll_one(vice_network_socket_t * readsockfd)
{
    TIMEVAL timeout = { 0, 0 };

    fd_set fdsockset;

    FD_ZERO(&fdsockset);
    FD_SET(readsockfd->sockfd, &fdsockset);

    return select( readsockfd->sockfd + 1, &fdsockset, NULL, NULL, &timeout);
}

/*! \brief Monitor multiple sockets

  This function blocks for many different connections and returns when any
  has data.

  \param readsockfd
     NULL terminated list of sockets to monitor

  \return
     1 if the specified socket has data; 0 if it does not contain
     any data, and -1 in case of an error.
*/
int vice_network_select_multiple(vice_network_socket_t ** readsockfd)
{
    fd_set fdsockset;
    SOCKET max_sockfd = INVALID_SOCKET;
    TIMEVAL time = {0, 250000};

    FD_ZERO(&fdsockset);
    while(*readsockfd != NULL) {
        FD_SET((*readsockfd)->sockfd, &fdsockset);
        if((*readsockfd)->sockfd > max_sockfd) {
            max_sockfd = (*readsockfd)->sockfd;
        }
        readsockfd++;
    }

    if(max_sockfd == INVALID_SOCKET) {
        return -1;
    }

    return select(max_sockfd + 1, &fdsockset, NULL, NULL, &time);
}

/*! \brief Get the error of the last socket operation

  This function determines the error code for the last
  socket operation.

  \return
     the error code

  \remark
      It does not distinguish between
      different sockets, thus, make sure to call it directly
      after an erroneous socket operation. Furthermore,
      reading the error status does not reset the error code.
      Thus, do not expect correct results if the previous
      operation did not produce an error.

   \todo
       Generate a mapping between WIN32 and Unix style
       error numbers
*/
int vice_network_get_errorcode(void)
{
    return ARCHDEP_SOCKET_ERROR;
}
#endif