ObjFW
socket_helpers.h
1 /*
2  * Copyright (c) 2008, 2009, 2010, 2011, 2012, 2013, 2014, 2015, 2016, 2017
3  * Jonathan Schleifer <js@heap.zone>
4  *
5  * All rights reserved.
6  *
7  * This file is part of ObjFW. It may be distributed under the terms of the
8  * Q Public License 1.0, which can be found in the file LICENSE.QPL included in
9  * the packaging of this file.
10  *
11  * Alternatively, it may be distributed under the terms of the GNU General
12  * Public License, either version 2 or 3, which can be found in the file
13  * LICENSE.GPLv2 or LICENSE.GPLv3 respectively included in the packaging of this
14  * file.
15  */
16 
17 #include "config.h"
18 
19 #include "unistd_wrapper.h"
20 
21 #ifdef HAVE_ARPA_INET_H
22 # include <arpa/inet.h>
23 #endif
24 #ifdef HAVE_NETDB_H
25 # include <netdb.h>
26 #endif
27 
28 #include "socket.h"
29 
30 #ifndef INVALID_SOCKET
31 # define INVALID_SOCKET -1
32 #endif
33 
34 #ifdef HAVE_GETADDRINFO
35 # ifndef AI_NUMERICSERV
36 # define AI_NUMERICSERV 0
37 # endif
38 # ifndef AI_NUMERICHOST
39 # define AI_NUMERICHOST 0
40 # endif
41 #endif
42 
43 #ifndef INADDR_NONE
44 # define INADDR_NONE ((in_addr_t)-1)
45 #endif
46 
47 #ifndef SOMAXCONN
48 /*
49  * Use 16 as everything > 17 fails on Nintendo 3DS and 16 is a less arbitrary
50  * number than 17.
51  */
52 # define SOMAXCONN 16
53 #endif
54 
55 #ifndef SOCK_CLOEXEC
56 # define SOCK_CLOEXEC 0
57 #endif
58 
59 #if !defined(OF_WINDOWS) && !defined(OF_WII)
60 # define closesocket(sock) close(sock)
61 #endif
62 
63 #ifdef OF_MORPHOS
64 # define hstrerror(err) "unknown (no hstrerror)"
65 typedef uint32_t in_addr_t;
66 #endif
67 
68 #ifdef OF_MORPHOS_IXEMUL
69 typedef uint32_t in_addr_t;
70 #endif
71 
72 #ifdef OF_WII
73 # define accept(sock, addr, addrlen) net_accept(sock, addr, addrlen)
74 # define bind(sock, addr, addrlen) net_bind(sock, addr, addrlen)
75 # define closesocket(sock) net_close(sock)
76 # define connect(sock, addr, addrlen) net_connect(sock, addr, addrlen)
77 # define gethostbyname(name) net_gethostbyname(name)
78 # define h_errno 0
79 # define hstrerror(err) "unknown (no hstrerror)"
80 # define listen(sock, backlog) net_listen(sock, backlog)
81 # define poll(fds, nfds, timeout) net_poll(fds, nfds, timeout)
82 # define recv(sock, buf, len, flags) net_recv(sock, buf, len, flags)
83 # define recvfrom(sock, buf, len, flags, addr, addrlen) \
84  net_recvfrom(sock, buf, len, flags, addr, addrlen)
85 # define select(nfds, readfds, writefds, errorfds, timeout) \
86  net_select(nfds, readfds, writefds, errorfds, timeout)
87 # define send(sock, buf, len, flags) net_send(sock, buf, len, flags)
88 # define sendto(sock, buf, len, flags, addr, addrlen) \
89  net_sendto(sock, buf, len, flags, addr, addrlen)
90 # define setsockopt(sock, level, name, value, len) \
91  net_setsockopt(sock, level, name, value, len)
92 # define socket(domain, type, proto) net_socket(domain, type, proto)
93 typedef u32 in_addr_t;
94 typedef u32 nfds_t;
95 #endif