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