annotate src/win32/libc_interface.c @ 3771:765769211688

[gaim-migrate @ 3910] More FT cleanups by Wil Mahan. Thanks, Wil. committer: Tailor Script <tailor@pidgin.im>
author Rob Flynn <gaim@robflynn.com>
date Sun, 20 Oct 2002 23:46:56 +0000
parents ac6ca3890c53
children 10ffafd1c91f
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
3712
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
1 /*
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
2 * libc_interface.c
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
3 *
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
4 * Author: Herman Bloggs <hermanator12002@yahoo.com>
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
5 * Date: October 14, 2002
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
6 * Description: Commonly used libc routines.
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
7 */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
8 #include <winsock.h>
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
9 #include <stdlib.h>
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
10 #include <errno.h>
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
11
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
12 static char errbuf[1024];
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
13
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
14 /* helpers */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
15 static int wgaim_is_socket( int fd ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
16 int optval;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
17 unsigned int optlen = sizeof(int);
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
18
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
19 if( (getsockopt(fd, SOL_SOCKET, SO_TYPE, (void*)&optval, &optlen)) == SOCKET_ERROR ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
20 int error = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
21 if( error == WSAENOTSOCK )
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
22 return FALSE;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
23 else {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
24 debug_printf("wgaim_read: getsockopt returned error: %d\n", error);
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
25 return FALSE;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
26 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
27 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
28 return TRUE;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
29 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
30
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
31 /* socket.h */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
32 int wgaim_socket (int namespace, int style, int protocol) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
33 int ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
34
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
35 ret = socket( namespace, style, protocol );
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
36
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
37 if( ret == INVALID_SOCKET ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
38 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
39 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
40 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
41 return ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
42 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
43
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
44 int wgaim_connect(int socket, struct sockaddr *addr, u_long length) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
45 int ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
46
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
47 ret = connect( socket, addr, length );
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
48
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
49 if( ret == SOCKET_ERROR ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
50 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
51 if( errno == WSAEWOULDBLOCK )
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
52 errno = WSAEINPROGRESS;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
53 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
54 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
55 return 0;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
56 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
57
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
58 int wgaim_getsockopt(int socket, int level, int optname, void *optval, unsigned int *optlenptr) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
59 int ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
60
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
61 ret = getsockopt( socket, level, optname, optval, optlenptr );
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
62 if( ret == SOCKET_ERROR ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
63 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
64 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
65 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
66
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
67 return 0;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
68 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
69
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
70 /* fcntl.h */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
71 /* This is not a full implementation of fcntl. Update as needed.. */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
72 #define O_NONBLOCK 1
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
73 #define F_SETFL 1
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
74 int wgaim_fcntl(int socket, int command, int val) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
75 switch( command ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
76 case F_SETFL:
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
77 {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
78 int ret=0;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
79
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
80 switch( val ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
81 case O_NONBLOCK:
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
82 {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
83 u_long imode=1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
84 ret = ioctlsocket(socket, FIONBIO, &imode);
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
85 break;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
86 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
87 case 0:
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
88 {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
89 u_long imode=0;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
90 ret = ioctlsocket(socket, FIONBIO, &imode);
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
91 break;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
92 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
93 default:
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
94 errno = EINVAL;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
95 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
96 }/*end switch*/
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
97 if( ret == SOCKET_ERROR ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
98 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
99 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
100 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
101 return 0;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
102 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
103 default:
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
104 debug_printf("wgaim_fcntl: Unsupported command\n");
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
105 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
106 }/*end switch*/
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
107 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
108
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
109 /* sys/ioctl.h */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
110 int wgaim_ioctl(int fd, int command, void* val) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
111 switch( command ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
112 case FIONBIO:
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
113 {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
114 if (ioctlsocket(fd, FIONBIO, (unsigned long *)val) == SOCKET_ERROR) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
115 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
116 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
117 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
118 return 0;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
119 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
120 default:
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
121 errno = EINVAL;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
122 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
123 }/*end switch*/
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
124 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
125
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
126 /* arpa/inet.h */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
127 int wgaim_inet_aton(const char *name, struct in_addr *addr) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
128 if((addr->s_addr = inet_addr(name)) == INADDR_NONE)
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
129 return 0;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
130 else
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
131 return 1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
132 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
133
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
134 /* netdb.h */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
135 struct hostent* wgaim_gethostbyname(const char *name) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
136 struct hostent *hp;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
137
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
138 if((hp = gethostbyname(name)) == NULL) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
139 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
140 return NULL;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
141 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
142 return hp;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
143 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
144
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
145 /* string.h */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
146 char* wgaim_strerror( int errornum ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
147 if( errornum > WSABASEERR ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
148 sprintf( errbuf, "Windows socket error #%d", errornum );
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
149 return errbuf;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
150 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
151 else
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
152 return strerror( errornum );
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
153 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
154
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
155 /* unistd.h */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
156
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
157 /*
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
158 * We need to figure out whether fd is a file or socket handle.
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
159 */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
160 int wgaim_read(int fd, void *buf, unsigned int size) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
161 int ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
162
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
163 if( wgaim_is_socket(fd) ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
164 if( (ret = recv(fd, buf, size, 0)) == SOCKET_ERROR ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
165 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
166 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
167 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
168 else if( ret == 0 ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
169 /* connection has been gracefully closed */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
170 errno = WSAENOTCONN;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
171 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
172 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
173 else {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
174 /* success reading socket */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
175 return ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
176 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
177 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
178 else {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
179 /* fd is not a socket handle.. pass it off to read */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
180 return read(fd, buf, size);
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
181 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
182 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
183
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
184 int wgaim_write(int fd, const void *buf, unsigned int size) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
185 int ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
186
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
187 if( wgaim_is_socket(fd) ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
188 if( (ret = send(fd, buf, size, 0)) == SOCKET_ERROR ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
189 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
190 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
191 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
192 else {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
193 /* success */
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
194 return ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
195 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
196
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
197 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
198 else
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
199 return write(fd, buf, size);
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
200 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
201
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
202 int wgaim_close(int fd) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
203 int ret;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
204
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
205 if( wgaim_is_socket(fd) ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
206 if( (ret = closesocket(fd)) == SOCKET_ERROR ) {
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
207 errno = WSAGetLastError();
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
208 return -1;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
209 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
210 else
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
211 return 0;
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
212 }
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
213 else
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
214 return close(fd);
ac6ca3890c53 [gaim-migrate @ 3845]
Herman Bloggs <hermanator12002@yahoo.com>
parents:
diff changeset
215 }