knetfile.c 18 KB

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