annotate PsiCLASS-1.0.2/samtools-0.1.19/knetfile.c @ 0:903fc43d6227 draft default tip

Uploaded
author lsong10
date Fri, 26 Mar 2021 16:52:45 +0000
parents
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
1 /* The MIT License
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
2
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
3 Copyright (c) 2008 by Genome Research Ltd (GRL).
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
4 2010 by Attractive Chaos <attractor@live.co.uk>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
5
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
6 Permission is hereby granted, free of charge, to any person obtaining
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
7 a copy of this software and associated documentation files (the
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
8 "Software"), to deal in the Software without restriction, including
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
9 without limitation the rights to use, copy, modify, merge, publish,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
10 distribute, sublicense, and/or sell copies of the Software, and to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
11 permit persons to whom the Software is furnished to do so, subject to
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
12 the following conditions:
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
13
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
14 The above copyright notice and this permission notice shall be
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
15 included in all copies or substantial portions of the Software.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
16
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
17 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
18 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
19 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
20 NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
21 BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
22 ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
23 CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
24 SOFTWARE.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
25 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
26
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
27 /* Probably I will not do socket programming in the next few years and
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
28 therefore I decide to heavily annotate this file, for Linux and
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
29 Windows as well. -ac */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
30
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
31 #include <time.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
32 #include <stdio.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
33 #include <ctype.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
34 #include <stdlib.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
35 #include <string.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
36 #include <errno.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
37 #include <unistd.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
38 #include <sys/types.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
39
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
40 #ifndef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
41 #include <netdb.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
42 #include <arpa/inet.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
43 #include <sys/socket.h>
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
44 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
45
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
46 #include "knetfile.h"
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
47
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
48 /* In winsock.h, the type of a socket is SOCKET, which is: "typedef
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
49 * u_int SOCKET". An invalid SOCKET is: "(SOCKET)(~0)", or signed
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
50 * integer -1. In knetfile.c, I use "int" for socket type
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
51 * throughout. This should be improved to avoid confusion.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
52 *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
53 * In Linux/Mac, recv() and read() do almost the same thing. You can see
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
54 * in the header file that netread() is simply an alias of read(). In
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
55 * Windows, however, they are different and using recv() is mandatory.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
56 */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
57
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
58 /* This function tests if the file handler is ready for reading (or
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
59 * writing if is_read==0). */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
60 static int socket_wait(int fd, int is_read)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
61 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
62 fd_set fds, *fdr = 0, *fdw = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
63 struct timeval tv;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
64 int ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
65 tv.tv_sec = 5; tv.tv_usec = 0; // 5 seconds time out
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
66 FD_ZERO(&fds);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
67 FD_SET(fd, &fds);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
68 if (is_read) fdr = &fds;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
69 else fdw = &fds;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
70 ret = select(fd+1, fdr, fdw, 0, &tv);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
71 #ifndef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
72 if (ret == -1) perror("select");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
73 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
74 if (ret == 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
75 fprintf(stderr, "select time-out\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
76 else if (ret == SOCKET_ERROR)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
77 fprintf(stderr, "select: %d\n", WSAGetLastError());
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
78 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
79 return ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
80 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
81
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
82 #ifndef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
83 /* This function does not work with Windows due to the lack of
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
84 * getaddrinfo() in winsock. It is addapted from an example in "Beej's
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
85 * Guide to Network Programming" (http://beej.us/guide/bgnet/). */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
86 static int socket_connect(const char *host, const char *port)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
87 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
88 #define __err_connect(func) do { perror(func); freeaddrinfo(res); return -1; } while (0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
89
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
90 int on = 1, fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
91 struct linger lng = { 0, 0 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
92 struct addrinfo hints, *res = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
93 memset(&hints, 0, sizeof(struct addrinfo));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
94 hints.ai_family = AF_UNSPEC;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
95 hints.ai_socktype = SOCK_STREAM;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
96 /* In Unix/Mac, getaddrinfo() is the most convenient way to get
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
97 * server information. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
98 if (getaddrinfo(host, port, &hints, &res) != 0) __err_connect("getaddrinfo");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
99 if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) __err_connect("socket");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
100 /* The following two setsockopt() are used by ftplib
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
101 * (http://nbpfaus.net/~pfau/ftplib/). I am not sure if they
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
102 * necessary. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
103 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) __err_connect("setsockopt");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
104 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &lng, sizeof(lng)) == -1) __err_connect("setsockopt");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
105 if (connect(fd, res->ai_addr, res->ai_addrlen) != 0) __err_connect("connect");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
106 freeaddrinfo(res);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
107 return fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
108 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
109 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
110 /* MinGW's printf has problem with "%lld" */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
111 char *int64tostr(char *buf, int64_t x)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
112 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
113 int cnt;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
114 int i = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
115 do {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
116 buf[i++] = '0' + x % 10;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
117 x /= 10;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
118 } while (x);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
119 buf[i] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
120 for (cnt = i, i = 0; i < cnt/2; ++i) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
121 int c = buf[i]; buf[i] = buf[cnt-i-1]; buf[cnt-i-1] = c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
122 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
123 return buf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
124 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
125
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
126 int64_t strtoint64(const char *buf)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
127 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
128 int64_t x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
129 for (x = 0; *buf != '\0'; ++buf)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
130 x = x * 10 + ((int64_t) *buf - 48);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
131 return x;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
132 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
133 /* In windows, the first thing is to establish the TCP connection. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
134 int knet_win32_init()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
135 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
136 WSADATA wsaData;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
137 return WSAStartup(MAKEWORD(2, 2), &wsaData);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
138 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
139 void knet_win32_destroy()
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
140 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
141 WSACleanup();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
142 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
143 /* A slightly modfied version of the following function also works on
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
144 * Mac (and presummably Linux). However, this function is not stable on
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
145 * my Mac. It sometimes works fine but sometimes does not. Therefore for
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
146 * non-Windows OS, I do not use this one. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
147 static SOCKET socket_connect(const char *host, const char *port)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
148 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
149 #define __err_connect(func) \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
150 do { \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
151 fprintf(stderr, "%s: %d\n", func, WSAGetLastError()); \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
152 return -1; \
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
153 } while (0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
154
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
155 int on = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
156 SOCKET fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
157 struct linger lng = { 0, 0 };
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
158 struct sockaddr_in server;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
159 struct hostent *hp = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
160 // open socket
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
161 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) __err_connect("socket");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
162 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (char*)&on, sizeof(on)) == -1) __err_connect("setsockopt");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
163 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (char*)&lng, sizeof(lng)) == -1) __err_connect("setsockopt");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
164 // get host info
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
165 if (isalpha(host[0])) hp = gethostbyname(host);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
166 else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
167 struct in_addr addr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
168 addr.s_addr = inet_addr(host);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
169 hp = gethostbyaddr((char*)&addr, 4, AF_INET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
170 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
171 if (hp == 0) __err_connect("gethost");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
172 // connect
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
173 server.sin_addr.s_addr = *((unsigned long*)hp->h_addr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
174 server.sin_family= AF_INET;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
175 server.sin_port = htons(atoi(port));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
176 if (connect(fd, (struct sockaddr*)&server, sizeof(server)) != 0) __err_connect("connect");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
177 // freehostent(hp); // strangely in MSDN, hp is NOT freed (memory leak?!)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
178 return fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
179 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
180 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
181
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
182 static off_t my_netread(int fd, void *buf, off_t len)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
183 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
184 off_t rest = len, curr, l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
185 /* recv() and read() may not read the required length of data with
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
186 * one call. They have to be called repeatedly. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
187 while (rest) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
188 if (socket_wait(fd, 1) <= 0) break; // socket is not ready for reading
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
189 curr = netread(fd, buf + l, rest);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
190 /* According to the glibc manual, section 13.2, a zero returned
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
191 * value indicates end-of-file (EOF), which should mean that
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
192 * read() will not return zero if EOF has not been met but data
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
193 * are not immediately available. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
194 if (curr == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
195 l += curr; rest -= curr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
196 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
197 return l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
198 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
199
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
200 /*************************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
201 * FTP specific routines *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
202 *************************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
203
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
204 static int kftp_get_response(knetFile *ftp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
205 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
206 #ifndef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
207 unsigned char c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
208 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
209 char c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
210 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
211 int n = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
212 char *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
213 if (socket_wait(ftp->ctrl_fd, 1) <= 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
214 while (netread(ftp->ctrl_fd, &c, 1)) { // FIXME: this is *VERY BAD* for unbuffered I/O
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
215 //fputc(c, stderr);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
216 if (n >= ftp->max_response) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
217 ftp->max_response = ftp->max_response? ftp->max_response<<1 : 256;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
218 ftp->response = realloc(ftp->response, ftp->max_response);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
219 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
220 ftp->response[n++] = c;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
221 if (c == '\n') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
222 if (n >= 4 && isdigit(ftp->response[0]) && isdigit(ftp->response[1]) && isdigit(ftp->response[2])
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
223 && ftp->response[3] != '-') break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
224 n = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
225 continue;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
226 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
227 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
228 if (n < 2) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
229 ftp->response[n-2] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
230 return strtol(ftp->response, &p, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
231 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
232
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
233 static int kftp_send_cmd(knetFile *ftp, const char *cmd, int is_get)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
234 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
235 if (socket_wait(ftp->ctrl_fd, 0) <= 0) return -1; // socket is not ready for writing
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
236 netwrite(ftp->ctrl_fd, cmd, strlen(cmd));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
237 return is_get? kftp_get_response(ftp) : 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
238 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
239
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
240 static int kftp_pasv_prep(knetFile *ftp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
241 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
242 char *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
243 int v[6];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
244 kftp_send_cmd(ftp, "PASV\r\n", 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
245 for (p = ftp->response; *p && *p != '('; ++p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
246 if (*p != '(') return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
247 ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
248 sscanf(p, "%d,%d,%d,%d,%d,%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
249 memcpy(ftp->pasv_ip, v, 4 * sizeof(int));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
250 ftp->pasv_port = (v[4]<<8&0xff00) + v[5];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
251 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
252 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
253
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
254
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
255 static int kftp_pasv_connect(knetFile *ftp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
256 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
257 char host[80], port[10];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
258 if (ftp->pasv_port == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
259 fprintf(stderr, "[kftp_pasv_connect] kftp_pasv_prep() is not called before hand.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
260 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
261 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
262 sprintf(host, "%d.%d.%d.%d", ftp->pasv_ip[0], ftp->pasv_ip[1], ftp->pasv_ip[2], ftp->pasv_ip[3]);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
263 sprintf(port, "%d", ftp->pasv_port);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
264 ftp->fd = socket_connect(host, port);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
265 if (ftp->fd == -1) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
266 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
267 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
268
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
269 int kftp_connect(knetFile *ftp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
270 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
271 ftp->ctrl_fd = socket_connect(ftp->host, ftp->port);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
272 if (ftp->ctrl_fd == -1) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
273 kftp_get_response(ftp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
274 kftp_send_cmd(ftp, "USER anonymous\r\n", 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
275 kftp_send_cmd(ftp, "PASS kftp@\r\n", 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
276 kftp_send_cmd(ftp, "TYPE I\r\n", 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
277 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
278 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
279
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
280 int kftp_reconnect(knetFile *ftp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
281 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
282 if (ftp->ctrl_fd != -1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
283 netclose(ftp->ctrl_fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
284 ftp->ctrl_fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
285 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
286 netclose(ftp->fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
287 ftp->fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
288 return kftp_connect(ftp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
289 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
290
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
291 // initialize ->type, ->host, ->retr and ->size
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
292 knetFile *kftp_parse_url(const char *fn, const char *mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
293 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
294 knetFile *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
295 char *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
296 int l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
297 if (strstr(fn, "ftp://") != fn) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
298 for (p = (char*)fn + 6; *p && *p != '/'; ++p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
299 if (*p != '/') return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
300 l = p - fn - 6;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
301 fp = calloc(1, sizeof(knetFile));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
302 fp->type = KNF_TYPE_FTP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
303 fp->fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
304 /* the Linux/Mac version of socket_connect() also recognizes a port
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
305 * like "ftp", but the Windows version does not. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
306 fp->port = strdup("21");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
307 fp->host = calloc(l + 1, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
308 if (strchr(mode, 'c')) fp->no_reconnect = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
309 strncpy(fp->host, fn + 6, l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
310 fp->retr = calloc(strlen(p) + 8, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
311 sprintf(fp->retr, "RETR %s\r\n", p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
312 fp->size_cmd = calloc(strlen(p) + 8, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
313 sprintf(fp->size_cmd, "SIZE %s\r\n", p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
314 fp->seek_offset = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
315 return fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
316 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
317 // place ->fd at offset off
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
318 int kftp_connect_file(knetFile *fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
319 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
320 int ret;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
321 long long file_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
322 if (fp->fd != -1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
323 netclose(fp->fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
324 if (fp->no_reconnect) kftp_get_response(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
325 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
326 kftp_pasv_prep(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
327 kftp_send_cmd(fp, fp->size_cmd, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
328 #ifndef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
329 if ( sscanf(fp->response,"%*d %lld", &file_size) != 1 )
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
330 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
331 fprintf(stderr,"[kftp_connect_file] %s\n", fp->response);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
332 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
333 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
334 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
335 const char *p = fp->response;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
336 while (*p != ' ') ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
337 while (*p < '0' || *p > '9') ++p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
338 file_size = strtoint64(p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
339 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
340 fp->file_size = file_size;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
341 if (fp->offset>=0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
342 char tmp[32];
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
343 #ifndef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
344 sprintf(tmp, "REST %lld\r\n", (long long)fp->offset);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
345 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
346 strcpy(tmp, "REST ");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
347 int64tostr(tmp + 5, fp->offset);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
348 strcat(tmp, "\r\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
349 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
350 kftp_send_cmd(fp, tmp, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
351 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
352 kftp_send_cmd(fp, fp->retr, 0);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
353 kftp_pasv_connect(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
354 ret = kftp_get_response(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
355 if (ret != 150) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
356 fprintf(stderr, "[kftp_connect_file] %s\n", fp->response);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
357 netclose(fp->fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
358 fp->fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
359 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
360 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
361 fp->is_ready = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
362 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
363 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
364
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
365
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
366 /**************************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
367 * HTTP specific routines *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
368 **************************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
369
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
370 knetFile *khttp_parse_url(const char *fn, const char *mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
371 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
372 knetFile *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
373 char *p, *proxy, *q;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
374 int l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
375 if (strstr(fn, "http://") != fn) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
376 // set ->http_host
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
377 for (p = (char*)fn + 7; *p && *p != '/'; ++p);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
378 l = p - fn - 7;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
379 fp = calloc(1, sizeof(knetFile));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
380 fp->http_host = calloc(l + 1, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
381 strncpy(fp->http_host, fn + 7, l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
382 fp->http_host[l] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
383 for (q = fp->http_host; *q && *q != ':'; ++q);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
384 if (*q == ':') *q++ = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
385 // get http_proxy
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
386 proxy = getenv("http_proxy");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
387 // set ->host, ->port and ->path
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
388 if (proxy == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
389 fp->host = strdup(fp->http_host); // when there is no proxy, server name is identical to http_host name.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
390 fp->port = strdup(*q? q : "80");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
391 fp->path = strdup(*p? p : "/");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
392 } else {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
393 fp->host = (strstr(proxy, "http://") == proxy)? strdup(proxy + 7) : strdup(proxy);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
394 for (q = fp->host; *q && *q != ':'; ++q);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
395 if (*q == ':') *q++ = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
396 fp->port = strdup(*q? q : "80");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
397 fp->path = strdup(fn);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
398 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
399 fp->type = KNF_TYPE_HTTP;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
400 fp->ctrl_fd = fp->fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
401 fp->seek_offset = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
402 return fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
403 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
404
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
405 int khttp_connect_file(knetFile *fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
406 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
407 int ret, l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
408 char *buf, *p;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
409 if (fp->fd != -1) netclose(fp->fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
410 fp->fd = socket_connect(fp->host, fp->port);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
411 buf = calloc(0x10000, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough.
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
412 l += sprintf(buf + l, "GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
413 l += sprintf(buf + l, "Range: bytes=%lld-\r\n", (long long)fp->offset);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
414 l += sprintf(buf + l, "\r\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
415 netwrite(fp->fd, buf, l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
416 l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
417 while (netread(fp->fd, buf + l, 1)) { // read HTTP header; FIXME: bad efficiency
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
418 if (buf[l] == '\n' && l >= 3)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
419 if (strncmp(buf + l - 3, "\r\n\r\n", 4) == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
420 ++l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
421 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
422 buf[l] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
423 if (l < 14) { // prematured header
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
424 netclose(fp->fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
425 fp->fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
426 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
427 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
428 ret = strtol(buf + 8, &p, 0); // HTTP return code
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
429 if (ret == 200 && fp->offset>0) { // 200 (complete result); then skip beginning of the file
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
430 off_t rest = fp->offset;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
431 while (rest) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
432 off_t l = rest < 0x10000? rest : 0x10000;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
433 rest -= my_netread(fp->fd, buf, l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
434 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
435 } else if (ret != 206 && ret != 200) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
436 free(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
437 fprintf(stderr, "[khttp_connect_file] fail to open file (HTTP code: %d).\n", ret);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
438 netclose(fp->fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
439 fp->fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
440 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
441 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
442 free(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
443 fp->is_ready = 1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
444 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
445 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
446
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
447 /********************
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
448 * Generic routines *
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
449 ********************/
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
450
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
451 knetFile *knet_open(const char *fn, const char *mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
452 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
453 knetFile *fp = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
454 if (mode[0] != 'r') {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
455 fprintf(stderr, "[kftp_open] only mode \"r\" is supported.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
456 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
457 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
458 if (strstr(fn, "ftp://") == fn) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
459 fp = kftp_parse_url(fn, mode);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
460 if (fp == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
461 if (kftp_connect(fp) == -1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
462 knet_close(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
463 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
464 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
465 kftp_connect_file(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
466 } else if (strstr(fn, "http://") == fn) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
467 fp = khttp_parse_url(fn, mode);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
468 if (fp == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
469 khttp_connect_file(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
470 } else { // local file
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
471 #ifdef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
472 /* In windows, O_BINARY is necessary. In Linux/Mac, O_BINARY may
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
473 * be undefined on some systems, although it is defined on my
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
474 * Mac and the Linux I have tested on. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
475 int fd = open(fn, O_RDONLY | O_BINARY);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
476 #else
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
477 int fd = open(fn, O_RDONLY);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
478 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
479 if (fd == -1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
480 perror("open");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
481 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
482 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
483 fp = (knetFile*)calloc(1, sizeof(knetFile));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
484 fp->type = KNF_TYPE_LOCAL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
485 fp->fd = fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
486 fp->ctrl_fd = -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
487 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
488 if (fp && fp->fd == -1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
489 knet_close(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
490 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
491 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
492 return fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
493 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
494
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
495 knetFile *knet_dopen(int fd, const char *mode)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
496 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
497 knetFile *fp = (knetFile*)calloc(1, sizeof(knetFile));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
498 fp->type = KNF_TYPE_LOCAL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
499 fp->fd = fd;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
500 return fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
501 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
502
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
503 off_t knet_read(knetFile *fp, void *buf, off_t len)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
504 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
505 off_t l = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
506 if (fp->fd == -1) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
507 if (fp->type == KNF_TYPE_FTP) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
508 if (fp->is_ready == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
509 if (!fp->no_reconnect) kftp_reconnect(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
510 kftp_connect_file(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
511 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
512 } else if (fp->type == KNF_TYPE_HTTP) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
513 if (fp->is_ready == 0)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
514 khttp_connect_file(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
515 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
516 if (fp->type == KNF_TYPE_LOCAL) { // on Windows, the following block is necessary; not on UNIX
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
517 off_t rest = len, curr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
518 while (rest) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
519 do {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
520 curr = read(fp->fd, buf + l, rest);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
521 } while (curr < 0 && EINTR == errno);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
522 if (curr < 0) return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
523 if (curr == 0) break;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
524 l += curr; rest -= curr;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
525 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
526 } else l = my_netread(fp->fd, buf, len);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
527 fp->offset += l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
528 return l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
529 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
530
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
531 off_t knet_seek(knetFile *fp, int64_t off, int whence)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
532 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
533 if (whence == SEEK_SET && off == fp->offset) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
534 if (fp->type == KNF_TYPE_LOCAL) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
535 /* Be aware that lseek() returns the offset after seeking,
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
536 * while fseek() returns zero on success. */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
537 off_t offset = lseek(fp->fd, off, whence);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
538 if (offset == -1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
539 // Be silent, it is OK for knet_seek to fail when the file is streamed
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
540 // fprintf(stderr,"[knet_seek] %s\n", strerror(errno));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
541 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
542 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
543 fp->offset = offset;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
544 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
545 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
546 else if (fp->type == KNF_TYPE_FTP)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
547 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
548 if (whence==SEEK_CUR)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
549 fp->offset += off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
550 else if (whence==SEEK_SET)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
551 fp->offset = off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
552 else if ( whence==SEEK_END)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
553 fp->offset = fp->file_size+off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
554 fp->is_ready = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
555 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
556 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
557 else if (fp->type == KNF_TYPE_HTTP)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
558 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
559 if (whence == SEEK_END) { // FIXME: can we allow SEEK_END in future?
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
560 fprintf(stderr, "[knet_seek] SEEK_END is not supported for HTTP. Offset is unchanged.\n");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
561 errno = ESPIPE;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
562 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
563 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
564 if (whence==SEEK_CUR)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
565 fp->offset += off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
566 else if (whence==SEEK_SET)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
567 fp->offset = off;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
568 fp->is_ready = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
569 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
570 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
571 errno = EINVAL;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
572 fprintf(stderr,"[knet_seek] %s\n", strerror(errno));
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
573 return -1;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
574 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
575
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
576 int knet_close(knetFile *fp)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
577 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
578 if (fp == 0) return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
579 if (fp->ctrl_fd != -1) netclose(fp->ctrl_fd); // FTP specific
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
580 if (fp->fd != -1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
581 /* On Linux/Mac, netclose() is an alias of close(), but on
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
582 * Windows, it is an alias of closesocket(). */
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
583 if (fp->type == KNF_TYPE_LOCAL) close(fp->fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
584 else netclose(fp->fd);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
585 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
586 free(fp->host); free(fp->port);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
587 free(fp->response); free(fp->retr); // FTP specific
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
588 free(fp->path); free(fp->http_host); // HTTP specific
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
589 free(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
590 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
591 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
592
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
593 #ifdef KNETFILE_MAIN
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
594 int main(void)
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
595 {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
596 char *buf;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
597 knetFile *fp;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
598 int type = 4, l;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
599 #ifdef _WIN32
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
600 knet_win32_init();
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
601 #endif
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
602 buf = calloc(0x100000, 1);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
603 if (type == 0) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
604 fp = knet_open("knetfile.c", "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
605 knet_seek(fp, 1000, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
606 } else if (type == 1) { // NCBI FTP, large file
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
607 fp = knet_open("ftp://ftp.ncbi.nih.gov/1000genomes/ftp/data/NA12878/alignment/NA12878.chrom6.SLX.SRP000032.2009_06.bam", "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
608 knet_seek(fp, 2500000000ll, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
609 l = knet_read(fp, buf, 255);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
610 } else if (type == 2) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
611 fp = knet_open("ftp://ftp.sanger.ac.uk/pub4/treefam/tmp/index.shtml", "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
612 knet_seek(fp, 1000, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
613 } else if (type == 3) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
614 fp = knet_open("http://www.sanger.ac.uk/Users/lh3/index.shtml", "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
615 knet_seek(fp, 1000, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
616 } else if (type == 4) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
617 fp = knet_open("http://www.sanger.ac.uk/Users/lh3/ex1.bam", "r");
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
618 knet_read(fp, buf, 10000);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
619 knet_seek(fp, 20000, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
620 knet_seek(fp, 10000, SEEK_SET);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
621 l = knet_read(fp, buf+10000, 10000000) + 10000;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
622 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
623 if (type != 4 && type != 1) {
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
624 knet_read(fp, buf, 255);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
625 buf[255] = 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
626 printf("%s\n", buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
627 } else write(fileno(stdout), buf, l);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
628 knet_close(fp);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
629 free(buf);
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
630 return 0;
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
631 }
903fc43d6227 Uploaded
lsong10
parents:
diff changeset
632 #endif