1 /*
2  * winstub.h - MS-Windows compatiblity hacks
3  * $Id: winstub.h 172 2005-06-16 20:08:53Z rdenisc $
4  */
5 
6 /***********************************************************************
7  *  Copyright (C) 2002-2004 Remi Denis-Courmont.                       *
8  *  This program is free software; you can redistribute and/or modify  *
9  *  it under the terms of the GNU General Public License as published  *
10  *  by the Free Software Foundation; version 2 of the license.         *
11  *                                                                     *
12  *  This program is distributed in the hope that it will be useful,    *
13  *  but WITHOUT ANY WARRANTY; without even the implied warranty of     *
14  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.               *
15  *  See the GNU General Public License for more details.               *
16  *                                                                     *
17  *  You should have received a copy of the GNU General Public License  *
18  *  along with this program; if not, you can get it from:              *
19  *  http://www.gnu.org/copyleft/gpl.html                               *
20  ***********************************************************************/
21 
22 #ifndef __RDC_WINSTUB_H
23 # define __RDC_WINSTUB_H
24 
25 /* gai functions do not link properly with my version of Mingw */
26 
27 # include <winsock2.h> /* struct sockaddr & all the predefined constants */
28 # include <ws2tcpip.h> /* struct sockaddr_in and the like */
29 # include <io.h> /* prevents close() name mangling */
30 # include <stdint.h>
31 
32 #ifndef HAVE_GAI_STRERROR
33 # undef gai_strerror // broken on my Mingw32
34 #endif
35 
36 /* <ws2tcpip.h> breaks RFC2553 by not defining EAI_SYSTEM.
37  * Give it a bogus value that should never happen in getaddrinfo(). */
38 # ifndef EAI_SYSTEM
39 #  define EAI_SYSTEM WSAEINPROGRESS
40 # endif
41 
42 /* Winsock does not define these constants */
43 # ifndef SHUT_RD
44 #  define SHUT_RD	SD_RECEIVE
45 #  define SHUT_WR	SD_SEND
46 #  define SHUT_RDWR	SD_BOTH
47 # endif
48 
49 # ifndef EINPROGRESS
50 #  define EINPROGRESS	WSAEINPROGRESS
51 #endif
52 
53 # define main winstub_main
54 
55 # ifdef __cplusplus
56 extern "C"
57 {
58 # endif
59 	void winsock_perror (const char *str);
60 # define perror winsock_perror
61 
62 	const char *winsock_strerror (int errnum);
63 # define strerror winsock_strerror
64 
65 	int winsock_close (int fd);
66 # define close winsock_close
67 
68 	size_t winsock_recvfrom (int fd, void *buf, size_t len, int flags,
69 				 struct sockaddr *addr, socklen_t *alen);
70 # define recvfrom winsock_recvfrom
71 
72 	size_t winsock_sendto (int fd, const void *buf, size_t len, int flags,
73 				const struct sockaddr *addr, socklen_t alen);
74 # define sendto winsock_sendto
75 
76 	size_t winsock_read (int fd, void *buf, size_t len);
77 # define read winsock_read
78 
79 	size_t winsock_write (int fd, const void *buf, size_t len);
80 # define write winsock_write
81 
82 	size_t winsock_recv (int fd, void *buf, size_t len, int flags);
83 # define recv winsock_recv
84 
85 	size_t winsock_send (int fd, const void *buf, size_t len, int flags);
86 # define send winsock_send
87 
88 	int winsock_socket (int pf, int type, int proto);
89 # define socket winsock_socket
90 
91 	int winsock_bind (int fd, struct sockaddr *addr, socklen_t addrlen);
92 # define bind winsock_bind
93 
94 	int winsock_listen (int fd, int max);
95 # define listen winsock_listen
96 
97 	int winsock_accept (int fd, struct sockaddr *addr, socklen_t *len);
98 # define accept winsock_accept
99 
100 	int winsock_connect (int fd, struct sockaddr *addr, socklen_t len);
101 # define connect winsock_connect
102 
103 	int winsock_shutdown (int fd, int how);
104 # define shutdown winsock_shutdown
105 
106 	int winsock_setsockopt (int fd, int lvl, int opt, const void *data,
107 				socklen_t len);
108 # define setsockopt winsock_setsockopt
109 
110 	int winsock_getsockopt (int fd, int lvl, int opt, void *data,
111 				socklen_t *len);
112 # define getsockopt winsock_getsockopt
113 
114 	struct hostent *winsock_gethostbyaddr (const char *addr, int len,
115 						int type);
116 # define gethostbyaddr winsock_gethostbyaddr
117 
118 	struct hostent *winsock_gethostbyname (const char *name);
119 # define gethostbyname winsock_gethostbyname
120 
121 	int winsock_getpeername (int fd, struct sockaddr *addr,
122 					socklen_t *len);
123 # define getpeername winsock_getpeername
124 
125 	int winsock_getsockname (int fd, struct sockaddr *addr,
126 					socklen_t *len);
127 # define getsockname winsock_getsockname
128 
129 	char *winsock_inet_ntoa (struct in_addr in);
130 # define inet_ntoa winsock_inet_ntoa
131 
132 	unsigned long winsock_inet_addr (const char *dotip);
133 # define inet_addr winsock_inet_addr
134 
135 	struct servent *winsock_getservbyport (int port, const char *proto);
136 # define getservbyport winsock_getservbyport
137 
138 	struct servent *winsock_getservbyname (const char *name,
139 						const char *proto);
140 # define getservbyname winsock_getservbyname
141 
142 	uint16_t winsock_ntohs (uint16_t s);
143 # define ntohs winsock_ntohs
144 
145 	uint16_t winsock_htons (uint16_t s);
146 # define htons winsock_htons
147 
148 	uint32_t winsock_ntohl (uint32_t l);
149 # define ntohl winsock_ntohl
150 
151 	uint32_t winsock_htonl (uint32_t l);
152 # define htonl winsock_htonl
153 
154 	int winsock_getaddrinfo (const char *node, const char *service,
155 	                         const struct addrinfo *hints,
156 	                         struct addrinfo **res);
157 # define getaddrinfo winsock_getaddrinfo
158 
159 	void winsock_freeaddrinfo (struct addrinfo *infos);
160 # define freeaddrinfo winsock_freeaddrinfo
161 
162 	int winsock_getnameinfo (const struct sockaddr *sa, int salen, char *host,
163 	                         int hostlen, char *serv, int servlen, int flags);
164 # define getnameinfo winsock_getnameinfo
165 
166 /*
167  * POSIX functions which do not exists on Windows
168  */
169 	unsigned int sleep (unsigned int s);
170 	int setuid (uid_t uid);
171 	int seteuid (uid_t uid);
172 	uid_t getuid (void);
173 	uid_t geteuid (void);
174 
175 	struct passwd
176 	{
177 		uid_t pw_uid;
178 	};
179 	struct passwd *getpwnam (const char *username);
180 	struct passwd *getpwuid (uid_t uid);
181 
182 	void openlog (const char *ident, int option, int facility);
183 	void syslog (int priority, const char *fmt, ...);
184 	void closelog (void);
185 
186 # define LOG_CONS	0x02
187 # define LOG_NDELAY	0x08
188 # define LOG_NOWAIT	0x10
189 # define LOG_ODELAY	0x04
190 # define LOG_PERROR	0x20
191 # define LOG_PID	0x01
192 
193 # define LOG_AUTH	0040
194 # define LOG_AUTHPRIV	0120
195 # define LOG_CRON	0110
196 # define LOG_DAEMON	0030
197 # define LOG_FTP	0130
198 # define LOG_LOCAL0	0200
199 # define LOG_LOCAL1	0210
200 # define LOG_LOCAL2	0220
201 # define LOG_LOCAL3	0230
202 # define LOG_LOCAL4	0240
203 # define LOG_LOCAL5	0250
204 # define LOG_LOCAL6	0260
205 # define LOG_LOCAL7	0270
206 # define LOG_LPR	0060
207 # define LOG_MAIL	0020
208 # define LOG_NEWS	0070
209 # define LOG_SYSLOG	0050
210 # define LOG_USER	0010
211 # define LOG_UUCP	0100
212 
213 # define LOG_EMERG	0
214 # define LOG_ALERT	1
215 # define LOG_CRIT	2
216 # define LOG_ERR	3
217 # define LOG_WARNING	4
218 # define LOG_NOTICE	5
219 # define LOG_INFO	6
220 # define LOG_DEBUG	7
221 
222 # ifdef __cplusplus
223 }
224 # endif
225 #endif /* not __RDC_WINSTUB_H */
226