* Copyright (C) 2002-2003, Herman Bloggs <hermanator12002@yahoo.com> * 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., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA #include <glib/gi18n-lib.h> #include "libc_internal.h" # define S_ISDIR(m) (((m)&S_IFDIR)==S_IFDIR) static char errbuf[1024]; static int wpurple_is_socket( int fd ) { int optlen = sizeof(int); if( (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&optval, &optlen)) == SOCKET_ERROR ) { int error = WSAGetLastError(); if( error == WSAENOTSOCK ) purple_debug(PURPLE_DEBUG_WARNING, "wpurple", "wpurple_is_socket: getsockopt returned error: %d\n", error); int wpurple_socket (int namespace, int style, int protocol) { ret = socket( namespace, style, protocol ); if (ret == (int)INVALID_SOCKET) { errno = WSAGetLastError(); int wpurple_connect(int socket, struct sockaddr *addr, u_long length) { ret = connect( socket, addr, length ); if( ret == SOCKET_ERROR ) { errno = WSAGetLastError(); if( errno == WSAEWOULDBLOCK ) int wpurple_getsockopt(int socket, int level, int optname, void *optval, socklen_t *optlenptr) { if(getsockopt(socket, level, optname, optval, optlenptr) == SOCKET_ERROR ) { errno = WSAGetLastError(); int wpurple_setsockopt(int socket, int level, int optname, const void *optval, socklen_t optlen) { if(setsockopt(socket, level, optname, optval, optlen) == SOCKET_ERROR ) { errno = WSAGetLastError(); int wpurple_getsockname(int socket, struct sockaddr *addr, socklen_t *lenptr) { if(getsockname(socket, addr, lenptr) == SOCKET_ERROR) { errno = WSAGetLastError(); int wpurple_bind(int socket, struct sockaddr *addr, socklen_t length) { if(bind(socket, addr, length) == SOCKET_ERROR) { errno = WSAGetLastError(); int wpurple_listen(int socket, unsigned int n) { if(listen(socket, n) == SOCKET_ERROR) { errno = WSAGetLastError(); int wpurple_sendto(int socket, const void *buf, size_t len, int flags, const struct sockaddr *to, socklen_t tolen) { if ((ret = sendto(socket, buf, len, flags, to, tolen) errno = WSAGetLastError(); if(errno == WSAEWOULDBLOCK || errno == WSAEINPROGRESS) /* This is not a full implementation of fcntl. Update as needed.. */ int wpurple_fcntl(int socket, int command, ...) { ret = ioctlsocket(socket, FIONBIO, &imode); ret = ioctlsocket(socket, FIONBIO, &imode); if( ret == SOCKET_ERROR ) { errno = WSAGetLastError(); purple_debug(PURPLE_DEBUG_WARNING, "wpurple", "wpurple_fcntl: Unsupported command\n"); int wpurple_ioctl(int fd, int command, void* val) { if (ioctlsocket(fd, FIONBIO, (unsigned long *)val) == SOCKET_ERROR) { errno = WSAGetLastError(); INTERFACE_INFO InterfaceList[20]; unsigned long nBytesReturned; if (WSAIoctl(fd, SIO_GET_INTERFACE_LIST, sizeof(InterfaceList), &nBytesReturned, errno = WSAGetLastError(); struct ifconf *ifc = val; char *tmp = ifc->ifc_buf; nBytesReturned / sizeof(INTERFACE_INFO); for (i = 0; i < nNumInterfaces; i++) { INTERFACE_INFO ii = InterfaceList[i]; struct ifreq *ifr = (struct ifreq *) tmp; struct sockaddr_in *sa = (struct sockaddr_in *) &ifr->ifr_addr; sa->sin_family = ii.iiAddress.AddressIn.sin_family; sa->sin_port = ii.iiAddress.AddressIn.sin_port; sa->sin_addr.s_addr = ii.iiAddress.AddressIn.sin_addr.s_addr; tmp += sizeof(struct ifreq); /* Make sure that we can fit in the original buffer */ if (tmp >= (ifc->ifc_buf + ifc->ifc_len + sizeof(struct ifreq))) { /* Replace the length with the actually used length */ ifc->ifc_len = ifc->ifc_len - (ifc->ifc_buf - tmp); int wpurple_inet_aton(const char *name, struct in_addr *addr) { if((addr->s_addr = inet_addr(name)) == INADDR_NONE) /* Thanks to GNU wget for this inet_ntop() implementation */ wpurple_inet_ntop (int af, const void *src, char *dst, socklen_t cnt) /* struct sockaddr can't accomodate struct sockaddr_in6. */ struct sockaddr_in6 sin6; ZeroMemory(&sa, sizeof(sa)); sa.sin.sin_family = AF_INET; sa.sin.sin_addr = *(struct in_addr *) src; srcsize = sizeof (sa.sin); sa.sin6.sin6_family = AF_INET6; sa.sin6.sin6_addr = *(struct in6_addr *) src; srcsize = sizeof (sa.sin6); if (WSAAddressToString ((struct sockaddr *) &sa, srcsize, NULL, dst, &dstlen) != 0) errno = WSAGetLastError(); return (const char *) dst; wpurple_inet_pton(int af, const char *src, void *dst) /* struct sockaddr can't accomodate struct sockaddr_in6. */ struct sockaddr_in6 sin6; sa.sin.sin_family = AF_INET; srcsize = sizeof (sa.sin); sa.sin6.sin6_family = AF_INET6; srcsize = sizeof (sa.sin6); if (WSAStringToAddress((LPTSTR)src, af, NULL, (struct sockaddr *) &sa, &srcsize) != 0) errno = WSAGetLastError(); memcpy(dst, &sa.sin.sin_addr, sizeof(sa.sin.sin_addr)); memcpy(dst, &sa.sin6.sin6_addr, sizeof(sa.sin6.sin6_addr)); struct hostent* wpurple_gethostbyname(const char *name) { if((hp = gethostbyname(name)) == NULL) { errno = WSAGetLastError(); char* wpurple_strerror(int errornum) { if (errornum > WSABASEERR) { case WSAECONNABORTED: /* 10053 */ g_snprintf(errbuf, sizeof(errbuf), "%s", _("Connection interrupted by other software on your computer.")); case WSAECONNRESET: /* 10054 */ g_snprintf(errbuf, sizeof(errbuf), "%s", _("Remote host closed connection.")); case WSAETIMEDOUT: /* 10060 */ g_snprintf(errbuf, sizeof(errbuf), "%s", _("Connection timed out.")); case WSAECONNREFUSED: /* 10061 */ g_snprintf(errbuf, sizeof(errbuf), "%s", _("Connection refused.")); case WSAEADDRINUSE: /* 10048 */ g_snprintf(errbuf, sizeof(errbuf), "%s", _("Address already in use.")); g_snprintf(errbuf, sizeof(errbuf), "Windows socket error #%d", errornum); const char *tmp = g_strerror(errornum); g_snprintf(errbuf, sizeof(errbuf), "%s", tmp); * We need to figure out whether fd is a file or socket handle. int wpurple_read(int fd, void *buf, unsigned int size) { g_return_val_if_reached(-1); if(wpurple_is_socket(fd)) { if((ret = recv(fd, buf, size, 0)) == SOCKET_ERROR) { errno = WSAGetLastError(); if(errno == WSAEWOULDBLOCK || errno == WSAEINPROGRESS) /* success reading socket */ /* fd is not a socket handle.. pass it off to read */ return _read(fd, buf, size); int wpurple_send(int fd, const void *buf, unsigned int size, int flags) { ret = send(fd, buf, size, flags); if (ret == SOCKET_ERROR) { errno = WSAGetLastError(); if(errno == WSAEWOULDBLOCK || errno == WSAEINPROGRESS) int wpurple_write(int fd, const void *buf, unsigned int size) { g_return_val_if_reached(-1); if(wpurple_is_socket(fd)) return wpurple_send(fd, buf, size, 0); return _write(fd, buf, size); int wpurple_recv(int fd, void *buf, size_t len, int flags) { if((ret = recv(fd, buf, len, flags)) == SOCKET_ERROR) { errno = WSAGetLastError(); if(errno == WSAEWOULDBLOCK || errno == WSAEINPROGRESS) int wpurple_close(int fd) { g_return_val_if_reached(-1); if( wpurple_is_socket(fd) ) { if( (ret = closesocket(fd)) == SOCKET_ERROR ) { errno = WSAGetLastError(); int wpurple_gethostname(char *name, size_t size) { if(gethostname(name, size) == SOCKET_ERROR) { errno = WSAGetLastError(); int wpurple_gettimeofday(struct timeval *p, struct timezone *z) { struct _timeb timebuffer; z->tz_minuteswest = _timezone/60; z->tz_dsttime = _daylight; p->tv_sec = timebuffer.time; /* seconds since 1-1-1970 */ p->tv_usec = timebuffer.millitm*1000; /* microseconds */