mark PurpleImageClass as private
[pidgin-git.git] / libpurple / win32 / libc_interface.c
blob25d7e9bfe232a5d1a28a5c6aeedcb8cd68def2d4
1 /*
2 * purple
4 * Copyright (C) 2002-2003, Herman Bloggs <hermanator12002@yahoo.com>
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
22 #include <config.h>
24 #include <glib/gi18n-lib.h>
25 #include <winsock2.h>
26 #include <ws2tcpip.h>
27 #include <io.h>
28 #include <stdlib.h>
29 #include <stdio.h>
30 #include <errno.h>
31 #include <sys/timeb.h>
32 #include <sys/stat.h>
33 #include <time.h>
34 #include <glib.h>
35 #include "debug.h"
36 #include "libc_internal.h"
37 #include "util.h"
38 #include <glib/gstdio.h>
39 #include "util.h"
41 #ifndef S_ISDIR
42 # define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR)
43 #endif
45 static char errbuf[1024];
47 /* helpers */
48 static int wpurple_is_socket( int fd ) {
49 int optval;
50 int optlen = sizeof(int);
52 if( (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&optval, &optlen)) == SOCKET_ERROR ) {
53 int error = WSAGetLastError();
54 if( error == WSAENOTSOCK )
55 return FALSE;
56 else {
57 purple_debug(PURPLE_DEBUG_WARNING, "wpurple", "wpurple_is_socket: getsockopt returned error: %d\n", error);
58 return FALSE;
61 return TRUE;
64 /* socket.h */
65 int wpurple_socket (int namespace, int style, int protocol) {
66 int ret;
68 ret = socket( namespace, style, protocol );
70 if (ret == (int)INVALID_SOCKET) {
71 errno = WSAGetLastError();
72 return -1;
74 return ret;
77 int wpurple_connect(int socket, struct sockaddr *addr, u_long length) {
78 int ret;
80 ret = connect( socket, addr, length );
82 if( ret == SOCKET_ERROR ) {
83 errno = WSAGetLastError();
84 if( errno == WSAEWOULDBLOCK )
85 errno = WSAEINPROGRESS;
86 return -1;
88 return 0;
91 int wpurple_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlenptr) {
92 if(getsockopt(socket, level, optname, optval, optlenptr) == SOCKET_ERROR ) {
93 errno = WSAGetLastError();
94 return -1;
96 return 0;
99 int wpurple_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen) {
100 if(setsockopt(socket, level, optname, optval, optlen) == SOCKET_ERROR ) {
101 errno = WSAGetLastError();
102 return -1;
104 return 0;
107 int wpurple_getsockname(int socket, struct sockaddr *addr, socklen_t *lenptr) {
108 if(getsockname(socket, addr, lenptr) == SOCKET_ERROR) {
109 errno = WSAGetLastError();
110 return -1;
112 return 0;
115 int wpurple_bind(int socket, struct sockaddr *addr, socklen_t length) {
116 if(bind(socket, addr, length) == SOCKET_ERROR) {
117 errno = WSAGetLastError();
118 return -1;
120 return 0;
123 int wpurple_listen(int socket, unsigned int n) {
124 if(listen(socket, n) == SOCKET_ERROR) {
125 errno = WSAGetLastError();
126 return -1;
128 return 0;
131 int wpurple_sendto(int socket, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) {
132 int ret;
133 if ((ret = sendto(socket, buf, len, flags, to, tolen)
134 ) == SOCKET_ERROR) {
135 errno = WSAGetLastError();
136 if(errno == WSAEWOULDBLOCK || errno == WSAEINPROGRESS)
137 errno = EAGAIN;
138 return -1;
140 return ret;
143 /* fcntl.h */
144 /* This is not a full implementation of fcntl. Update as needed.. */
145 int wpurple_fcntl(int socket, int command, ...) {
147 switch( command ) {
148 case F_GETFL:
149 return 0;
151 case F_SETFL:
153 va_list args;
154 int val;
155 int ret=0;
157 va_start(args, command);
158 val = va_arg(args, int);
159 va_end(args);
161 switch( val ) {
162 case O_NONBLOCK:
164 u_long imode=1;
165 ret = ioctlsocket(socket, FIONBIO, &imode);
166 break;
168 case 0:
170 u_long imode=0;
171 ret = ioctlsocket(socket, FIONBIO, &imode);
172 break;
174 default:
175 errno = EINVAL;
176 return -1;
177 }/*end switch*/
178 if( ret == SOCKET_ERROR ) {
179 errno = WSAGetLastError();
180 return -1;
182 return 0;
184 default:
185 purple_debug(PURPLE_DEBUG_WARNING, "wpurple", "wpurple_fcntl: Unsupported command\n");
186 return -1;
187 }/*end switch*/
190 /* sys/ioctl.h */
191 int wpurple_ioctl(int fd, int command, void* val) {
192 switch( command ) {
193 case FIONBIO:
195 if (ioctlsocket(fd, FIONBIO, (unsigned long *)val) == SOCKET_ERROR) {
196 errno = WSAGetLastError();
197 return -1;
199 return 0;
201 case SIOCGIFCONF:
203 INTERFACE_INFO InterfaceList[20];
204 unsigned long nBytesReturned;
205 if (WSAIoctl(fd, SIO_GET_INTERFACE_LIST,
206 0, 0, &InterfaceList,
207 sizeof(InterfaceList), &nBytesReturned,
208 0, 0) == SOCKET_ERROR) {
209 errno = WSAGetLastError();
210 return -1;
211 } else {
212 int i;
213 struct ifconf *ifc = val;
214 char *tmp = ifc->ifc_buf;
215 int nNumInterfaces =
216 nBytesReturned / sizeof(INTERFACE_INFO);
217 for (i = 0; i < nNumInterfaces; i++) {
218 INTERFACE_INFO ii = InterfaceList[i];
219 struct ifreq *ifr = (struct ifreq *) tmp;
220 struct sockaddr_in *sa = (struct sockaddr_in *) &ifr->ifr_addr;
222 sa->sin_family = ii.iiAddress.AddressIn.sin_family;
223 sa->sin_port = ii.iiAddress.AddressIn.sin_port;
224 sa->sin_addr.s_addr = ii.iiAddress.AddressIn.sin_addr.s_addr;
225 tmp += sizeof(struct ifreq);
227 /* Make sure that we can fit in the original buffer */
228 if (tmp >= (ifc->ifc_buf + ifc->ifc_len + sizeof(struct ifreq))) {
229 break;
232 /* Replace the length with the actually used length */
233 ifc->ifc_len = ifc->ifc_len - (ifc->ifc_buf - tmp);
234 return 0;
237 default:
238 errno = EINVAL;
239 return -1;
240 }/*end switch*/
243 /* arpa/inet.h */
244 int wpurple_inet_aton(const char *name, struct in_addr *addr) {
245 if((addr->s_addr = inet_addr(name)) == INADDR_NONE)
246 return 0;
247 else
248 return 1;
251 /* Thanks to GNU wget for this inet_ntop() implementation */
252 const char *
253 wpurple_inet_ntop (int af, const void *src, char *dst, socklen_t cnt)
255 /* struct sockaddr can't accomodate struct sockaddr_in6. */
256 union {
257 struct sockaddr_in6 sin6;
258 struct sockaddr_in sin;
259 } sa;
260 DWORD dstlen = cnt;
261 size_t srcsize;
263 ZeroMemory(&sa, sizeof(sa));
264 switch (af)
266 case AF_INET:
267 sa.sin.sin_family = AF_INET;
268 sa.sin.sin_addr = *(struct in_addr *) src;
269 srcsize = sizeof (sa.sin);
270 break;
271 case AF_INET6:
272 sa.sin6.sin6_family = AF_INET6;
273 sa.sin6.sin6_addr = *(struct in6_addr *) src;
274 srcsize = sizeof (sa.sin6);
275 break;
276 default:
277 abort ();
280 if (WSAAddressToString ((struct sockaddr *) &sa, srcsize, NULL, dst, &dstlen) != 0)
282 errno = WSAGetLastError();
283 return NULL;
285 return (const char *) dst;
289 wpurple_inet_pton(int af, const char *src, void *dst)
291 /* struct sockaddr can't accomodate struct sockaddr_in6. */
292 union {
293 struct sockaddr_in6 sin6;
294 struct sockaddr_in sin;
295 } sa;
296 int srcsize;
298 switch(af)
300 case AF_INET:
301 sa.sin.sin_family = AF_INET;
302 srcsize = sizeof (sa.sin);
303 break;
304 case AF_INET6:
305 sa.sin6.sin6_family = AF_INET6;
306 srcsize = sizeof (sa.sin6);
307 break;
308 default:
309 errno = WSAEPFNOSUPPORT;
310 return -1;
313 if (WSAStringToAddress((LPTSTR)src, af, NULL, (struct sockaddr *) &sa, &srcsize) != 0)
315 errno = WSAGetLastError();
316 return -1;
319 switch(af)
321 case AF_INET:
322 memcpy(dst, &sa.sin.sin_addr, sizeof(sa.sin.sin_addr));
323 break;
324 case AF_INET6:
325 memcpy(dst, &sa.sin6.sin6_addr, sizeof(sa.sin6.sin6_addr));
326 break;
329 return 1;
333 /* netdb.h */
334 struct hostent* wpurple_gethostbyname(const char *name) {
335 struct hostent *hp;
337 if((hp = gethostbyname(name)) == NULL) {
338 errno = WSAGetLastError();
339 return NULL;
341 return hp;
344 /* string.h */
345 char* wpurple_strerror(int errornum) {
346 if (errornum > WSABASEERR) {
347 switch(errornum) {
348 case WSAECONNABORTED: /* 10053 */
349 g_snprintf(errbuf, sizeof(errbuf), "%s", _("Connection interrupted by other software on your computer."));
350 break;
351 case WSAECONNRESET: /* 10054 */
352 g_snprintf(errbuf, sizeof(errbuf), "%s", _("Remote host closed connection."));
353 break;
354 case WSAETIMEDOUT: /* 10060 */
355 g_snprintf(errbuf, sizeof(errbuf), "%s", _("Connection timed out."));
356 break;
357 case WSAECONNREFUSED: /* 10061 */
358 g_snprintf(errbuf, sizeof(errbuf), "%s", _("Connection refused."));
359 break;
360 case WSAEADDRINUSE: /* 10048 */
361 g_snprintf(errbuf, sizeof(errbuf), "%s", _("Address already in use."));
362 break;
363 default:
364 g_snprintf(errbuf, sizeof(errbuf), "Windows socket error #%d", errornum);
366 } else {
367 const char *tmp = g_strerror(errornum);
368 g_snprintf(errbuf, sizeof(errbuf), "%s", tmp);
370 return errbuf;
373 /* unistd.h */
376 * We need to figure out whether fd is a file or socket handle.
378 int wpurple_read(int fd, void *buf, unsigned int size) {
379 int ret;
381 if (fd < 0) {
382 errno = EBADF;
383 g_return_val_if_reached(-1);
386 if(wpurple_is_socket(fd)) {
387 if((ret = recv(fd, buf, size, 0)) == SOCKET_ERROR) {
388 errno = WSAGetLastError();
389 if(errno == WSAEWOULDBLOCK || errno == WSAEINPROGRESS)
390 errno = EAGAIN;
391 return -1;
393 else {
394 /* success reading socket */
395 return ret;
397 } else {
398 /* fd is not a socket handle.. pass it off to read */
399 return _read(fd, buf, size);
403 int wpurple_send(int fd, const void *buf, unsigned int size, int flags) {
404 int ret;
406 ret = send(fd, buf, size, flags);
408 if (ret == SOCKET_ERROR) {
409 errno = WSAGetLastError();
410 if(errno == WSAEWOULDBLOCK || errno == WSAEINPROGRESS)
411 errno = EAGAIN;
412 return -1;
414 return ret;
417 int wpurple_write(int fd, const void *buf, unsigned int size) {
419 if (fd < 0) {
420 errno = EBADF;
421 g_return_val_if_reached(-1);
424 if(wpurple_is_socket(fd))
425 return wpurple_send(fd, buf, size, 0);
426 else
427 return _write(fd, buf, size);
430 int wpurple_recv(int fd, void *buf, size_t len, int flags) {
431 int ret;
433 if((ret = recv(fd, buf, len, flags)) == SOCKET_ERROR) {
434 errno = WSAGetLastError();
435 if(errno == WSAEWOULDBLOCK || errno == WSAEINPROGRESS)
436 errno = EAGAIN;
437 return -1;
438 } else {
439 return ret;
443 int wpurple_close(int fd) {
444 int ret;
446 if (fd < 0) {
447 errno = EBADF;
448 g_return_val_if_reached(-1);
451 if( wpurple_is_socket(fd) ) {
452 if( (ret = closesocket(fd)) == SOCKET_ERROR ) {
453 errno = WSAGetLastError();
454 return -1;
456 else
457 return 0;
459 else
460 return _close(fd);
463 int wpurple_gethostname(char *name, size_t size) {
464 if(gethostname(name, size) == SOCKET_ERROR) {
465 errno = WSAGetLastError();
466 return -1;
468 return 0;
471 /* sys/time.h */
473 int wpurple_gettimeofday(struct timeval *p, struct timezone *z) {
474 int res = 0;
475 struct _timeb timebuffer;
477 if (z != 0) {
478 _tzset();
479 z->tz_minuteswest = _timezone/60;
480 z->tz_dsttime = _daylight;
483 if (p != 0) {
484 _ftime(&timebuffer);
485 p->tv_sec = timebuffer.time; /* seconds since 1-1-1970 */
486 p->tv_usec = timebuffer.millitm*1000; /* microseconds */
489 return res;
492 /* time.h */
494 struct tm * wpurple_localtime_r (const time_t *time, struct tm *resultp) {
495 struct tm* tmptm;
497 if(!time)
498 return NULL;
499 tmptm = localtime(time);
500 if(resultp && tmptm)
501 return memcpy(resultp, tmptm, sizeof(struct tm));
502 else
503 return NULL;