-
Notifications
You must be signed in to change notification settings - Fork 0
/
net.c
246 lines (205 loc) · 4.73 KB
/
net.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
/*
Copyright (C) 2000 Masanao Izumo <mo@goice.co.jp>
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., 675 Mass Ave, Cambridge, MA 02139, USA.
*/
#include "config.h"
#include <stdio.h>
#include <stdlib.h>
#ifndef __W32__
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netdb.h>
#include <arpa/inet.h> /* for inet_ntoa */
#ifndef NO_STRING_H
#include <string.h>
#else
#include <strings.h>
#endif
#endif
#include "net.h"
#ifndef INADDR_NONE
#define INADDR_NONE 0xffffffff
#endif /* INADDR_NONE */
#ifndef __W32__
#ifndef INVALID_SOCKET
#define INVALID_SOCKET -1
#endif /* INVALID_SOCKET */
#ifndef SOCKET_ERROR
#define SOCKET_ERROR -1
#endif /* SOCKET_ERROR */
#endif /* __W32__ */
SOCKET open_socket(char *host, unsigned short port)
{
SOCKET fd;
struct sockaddr_in in;
memset(&in, 0, sizeof(in));
if((in.sin_addr.s_addr = inet_addr(host)) == INADDR_NONE)
{
struct hostent *hp;
if((hp = gethostbyname(host)) == NULL)
return (SOCKET)-1;
memcpy(&in.sin_addr, hp->h_addr, hp->h_length);
}
in.sin_port = htons(port);
in.sin_family = AF_INET;
if((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET)
return (SOCKET)-1;
if(connect(fd, (struct sockaddr *)&in, sizeof(in)) == SOCKET_ERROR)
{
closesocket(fd);
return (SOCKET)-1;
}
return fd;
}
#ifndef __W32__
long socket_write(SOCKET fd, char *buff, long bufsiz)
{
return write(fd, buff, bufsiz);
}
int socket_shutdown(SOCKET fd, int how)
{
return shutdown(fd, how);
}
FILE *socket_fdopen(SOCKET fd, char *mode)
{
return fdopen(fd, mode);
}
char *socket_fgets(char *buff, int n, FILE *fp)
{
return fgets(buff, n, fp);
}
int socket_fgetc(FILE *fp)
{
return getc(fp);
}
long socket_fread(void *buff, long bufsiz, FILE *fp)
{
return (long)fread(buff, 1, bufsiz, fp);
}
long socket_fwrite(void *buff, long bufsiz, FILE *fp)
{
return (long)fwrite(buff, 1, bufsiz, fp);
}
int socket_fclose(FILE *fp)
{
return fclose(fp);
}
int socket_fflush(FILE *fp)
{
return fflush(fp);
}
#else /* __W32__ */
#include "url.h"
/* Fake FILE * */
typedef struct _win32_fp_socket_t
{
SOCKET fd; /* Now, It has only fd */
} win32_fp_socket;
#define W32_FP2SOCKET(fp) (((win32_fp_socket *)(fp))->fd)
static long socket_safe_recv(SOCKET fd, char *buff, long bufsiz)
{
fd_set fds;
int maxfd, n;
FD_ZERO(&fds);
FD_SET(fd, &fds);
maxfd = fd + 1;
n = select(maxfd, &fds, NULL, NULL, NULL);
if(n <= 0)
return n;
return recv(fd, buff, bufsiz, 0);
}
long socket_write(SOCKET fd, char *buff, long bufsiz)
{
return send(fd, buff, bufsiz, 0);
}
int socket_shutdown(SOCKET fd, int how)
{
return shutdown(fd, how);
}
#pragma argsused
FILE *socket_fdopen(SOCKET fd, char *mode)
{
FILE *fp;
/* win32_fp_socket fake FILE.
* `mode' argument is ignored.
*/
if((fp = (FILE *)malloc(sizeof(win32_fp_socket))) == NULL)
return NULL;
memset(fp, 0, sizeof(win32_fp_socket));
W32_FP2SOCKET(fp) = fd;
return fp;
}
static int socket_getc(SOCKET fd)
{
int n;
unsigned char c;
n = socket_safe_recv(fd, (char *)&c, 1);
if(n <= 0)
return EOF;
return (int)c;
}
char *socket_fgets(char *buff, int n, FILE *fp)
{
SOCKET fd = W32_FP2SOCKET(fp);
int len;
n--; /* for '\0' */
if(n == 0)
*buff = '\0';
if(n <= 0)
return buff;
len = 0;
while(len < n)
{
int c;
c = socket_getc(fd);
if(c == -1)
break;
buff[len++] = c;
if(c == url_newline_code)
break;
}
if(len == 0)
return NULL;
buff[len] = '\0';
return buff;
}
int socket_fgetc(FILE *fp)
{
SOCKET fd = W32_FP2SOCKET(fp);
return socket_getc(SOCKET fd);
}
long socket_fread(void *buff, long bufsiz, FILE *fp)
{
SOCKET fd = W32_FP2SOCKET(fp);
return socket_safe_recv(fd, buff, bufsiz);
}
long socket_fwrite(void *buff, long bufsiz, FILE *fp)
{
SOCKET fd = W32_FP2SOCKET(fp);
return socket_write(fd, buff, bufsiz);
}
int socket_fclose(FILE *fp)
{
SOCKET fd = W32_FP2SOCKET(fp);
int ret;
ret = closesocket(fd);
free(fp);
return ret;
}
int socket_fflush(FILE *fp)
{
return 0;
}
#endif