1 /***********************************************************************
2  Freeciv - Copyright (C) 1996 - A Kjeldberg, L Gregersen, P Unold
3    This program is free software; you can redistribute it and/or modify
4    it under the terms of the GNU General Public License as published by
5    the Free Software Foundation; either version 2, or (at your option)
6    any later version.
7 
8    This program is distributed in the hope that it will be useful,
9    but WITHOUT ANY WARRANTY; without even the implied warranty of
10    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
11    GNU General Public License for more details.
12 ***********************************************************************/
13 
14 #ifndef FC__NETINTF_H
15 #define FC__NETINTF_H
16 
17 #ifdef __cplusplus
18 extern "C" {
19 #endif /* __cplusplus */
20 
21 #include <freeciv_config.h>
22 
23 /***********************************************************************
24   Common network interface.
25 ***********************************************************************/
26 
27 #ifdef FREECIV_HAVE_NETINET_IN_H
28 #include <netinet/in.h>
29 #endif
30 #ifdef FREECIV_HAVE_SYS_SELECT_H
31 #include <sys/select.h>
32 #endif
33 #ifdef FREECIV_HAVE_SYS_SOCKET_H
34 #include <sys/socket.h>
35 #endif
36 #ifdef FREECIV_HAVE_SYS_TIME_H
37 #include <sys/time.h>
38 #endif
39 #ifdef FREECIV_HAVE_SYS_TYPES_H
40 #include <sys/types.h>
41 #endif
42 #ifdef FREECIV_HAVE_UNISTD_H
43 #include <unistd.h>
44 #endif
45 #ifdef FREECIV_HAVE_WS2TCPIP_H
46 #include <ws2tcpip.h>
47 #endif
48 
49 /* utility */
50 #include "ioz.h"
51 #include "net_types.h"
52 #include "support.h"   /* bool type */
53 
54 #ifdef FD_ZERO
55 #define FC_FD_ZERO FD_ZERO
56 #else
57 #define FC_FD_ZERO(p) memset((void *)(p), 0, sizeof(*(p)))
58 #endif
59 
60 #ifdef IPV6_ADD_MEMBERSHIP
61 #define FC_IPV6_ADD_MEMBERSHIP IPV6_ADD_MEMBERSHIP
62 #else
63 #define FC_IPV6_ADD_MEMBERSHIP IPV6_JOIN_GROUP
64 #endif
65 
66 #ifndef FREECIV_HAVE_SOCKLEN_T
67 typedef int socklen_t;
68 #endif  /* FREECIV_HAVE_SOCKLEN_T */
69 
70 union fc_sockaddr {
71   struct sockaddr saddr;
72   struct sockaddr_in saddr_in4;
73 #ifdef FREECIV_IPV6_SUPPORT
74   struct sockaddr_in6 saddr_in6;
75 #endif /* FREECIV_IPV6_SUPPORT */
76 };
77 
78 /* get 'struct sockaddr_list' and related functions: */
79 #define SPECLIST_TAG fc_sockaddr
80 #define SPECLIST_TYPE union fc_sockaddr
81 #include "speclist.h"
82 
83 #define fc_sockaddr_list_iterate(sockaddrlist, paddr) \
84     TYPED_LIST_ITERATE(union fc_sockaddr, sockaddrlist, paddr)
85 #define fc_sockaddr_list_iterate_end  LIST_ITERATE_END
86 
87 #ifdef FREECIV_MSWINDOWS
88 typedef TIMEVAL fc_timeval;
89 #else  /* FREECIV_MSWINDOWS */
90 typedef struct timeval fc_timeval;
91 #endif /* FREECIV_MSWINDOWS */
92 
93 int fc_connect(int sockfd, const struct sockaddr *serv_addr, socklen_t addrlen);
94 int fc_select(int n, fd_set *readfds, fd_set *writefds, fd_set *exceptfds,
95               fc_timeval *timeout);
96 int fc_readsocket(int sock, void *buf, size_t size);
97 int fc_writesocket(int sock, const void *buf, size_t size);
98 void fc_closesocket(int sock);
99 
100 void fc_nonblock(int sockfd);
101 struct fc_sockaddr_list *net_lookup_service(const char *name, int port,
102 					    enum fc_addr_family family);
103 fz_FILE *fc_querysocket(int sock, void *buf, size_t size);
104 int find_next_free_port(int starting_port, int highest_port,
105                         enum fc_addr_family family,
106                         char *net_interface, bool not_avail_ok);
107 
108 void sockaddr_debug(union fc_sockaddr *addr, enum log_level lvl);
109 int sockaddr_size(union fc_sockaddr *addr);
110 bool sockaddr_ipv6(union fc_sockaddr *addr);
111 
112 #ifdef __cplusplus
113 }
114 #endif /* __cplusplus */
115 
116 #endif  /* FC__NETINTF_H */
117