44 #include <sys/types.h>
49 #include <arpa/inet.h>
50 #include <sys/socket.h>
61 void knet_silent(
int silent)
79 static int socket_wait(
int fd,
int is_read)
81 fd_set fds, *fdr = 0, *fdw = 0;
84 tv.tv_sec = 5; tv.tv_usec = 0;
87 if (is_read) fdr = &fds;
89 ret = select(fd+1, fdr, fdw, 0, &tv);
91 if (ret == -1) perror(
"select");
97 fprintf(stderr,
"select time-out\n");
100 else if (ret == SOCKET_ERROR)
104 fprintf(stderr,
"select: %d\n", WSAGetLastError());
115 static int socket_connect(
const char *host,
const char *port)
117 #define __err_connect(func) do { if(!knetsilent){perror(func);} freeaddrinfo(res); return -1; } while (0)
120 struct linger lng = { 0, 0 };
121 struct addrinfo hints, *res = 0;
122 memset(&hints, 0,
sizeof(
struct addrinfo));
123 hints.ai_family = AF_UNSPEC;
124 hints.ai_socktype = SOCK_STREAM;
127 if (getaddrinfo(host, port, &hints, &res) != 0) __err_connect(
"getaddrinfo");
128 if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) __err_connect(
"socket");
132 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on,
sizeof(on)) == -1) __err_connect(
"setsockopt");
133 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &lng,
sizeof(lng)) == -1) __err_connect(
"setsockopt");
134 if (connect(fd, res->ai_addr, res->ai_addrlen) != 0) __err_connect(
"connect");
140 char *int64tostr(
char *buf, int64_t x)
145 buf[i++] =
'0' + x % 10;
149 for (cnt = i, i = 0; i < cnt/2; ++i) {
150 int c = buf[i]; buf[i] = buf[cnt-i-1]; buf[cnt-i-1] = c;
155 int64_t strtoint64(
const char *buf)
158 for (x = 0; *buf !=
'\0'; ++buf)
159 x = x * 10 + ((int64_t) *buf - 48);
163 int knet_win32_init()
166 return WSAStartup(MAKEWORD(2, 2), &wsaData);
168 void knet_win32_destroy()
176 static SOCKET socket_connect(
const char *host,
const char *port)
178 #define __err_connect(func) \
180 if(!knetsilent) {fprintf(stderr, "%s: %d\n", func, WSAGetLastError());} \
186 struct linger lng = { 0, 0 };
187 struct sockaddr_in server;
188 struct hostent *hp = 0;
190 if ((fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) == INVALID_SOCKET) __err_connect(
"socket");
191 if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, (
char*)&on,
sizeof(on)) == -1) __err_connect(
"setsockopt");
192 if (setsockopt(fd, SOL_SOCKET, SO_LINGER, (
char*)&lng,
sizeof(lng)) == -1) __err_connect(
"setsockopt");
194 if (isalpha(host[0])) hp = gethostbyname(host);
197 addr.s_addr = inet_addr(host);
198 hp = gethostbyaddr((
char*)&addr, 4, AF_INET);
200 if (hp == 0) __err_connect(
"gethost");
202 server.sin_addr.s_addr = *((
unsigned long*)hp->h_addr);
203 server.sin_family= AF_INET;
204 server.sin_port = htons(atoi(port));
205 if (connect(fd, (
struct sockaddr*)&server,
sizeof(server)) != 0) __err_connect(
"connect");
211 static off_t my_netread(
int fd,
void *buf, off_t len)
213 off_t rest = len, curr, l = 0;
217 if (socket_wait(fd, 1) <= 0)
break;
218 curr = netread(fd, (
void*)((
char*)buf + l), rest);
223 if (curr == 0)
break;
224 l += curr; rest -= curr;
233 static int kftp_get_response(
knetFile *ftp)
242 if (socket_wait(ftp->ctrl_fd, 1) <= 0)
return 0;
243 while (netread(ftp->ctrl_fd, &c, 1)) {
245 if (n >= ftp->max_response) {
246 ftp->max_response = ftp->max_response? ftp->max_response<<1 : 256;
247 ftp->response = (
char*)realloc(ftp->response, ftp->max_response);
249 ftp->response[n++] = c;
251 if (n >= 4 && isdigit(ftp->response[0]) && isdigit(ftp->response[1]) && isdigit(ftp->response[2])
252 && ftp->response[3] !=
'-')
break;
257 if (n < 2)
return -1;
258 ftp->response[n-2] = 0;
259 return strtol(ftp->response, &p, 0);
262 static int kftp_send_cmd(
knetFile *ftp,
const char *cmd,
int is_get)
264 if (socket_wait(ftp->ctrl_fd, 0) <= 0)
return -1;
265 if(netwrite(ftp->ctrl_fd, cmd, strlen(cmd)) != strlen(cmd))
269 return is_get? kftp_get_response(ftp) : 0;
272 static int kftp_pasv_prep(
knetFile *ftp)
276 kftp_send_cmd(ftp,
"PASV\r\n", 1);
277 for (p = ftp->response; *p && *p !=
'('; ++p);
278 if (*p !=
'(')
return -1;
280 sscanf(p,
"%d,%d,%d,%d,%d,%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]);
281 memcpy(ftp->pasv_ip, v, 4 *
sizeof(
int));
282 ftp->pasv_port = (v[4]<<8&0xff00) + v[5];
287 static int kftp_pasv_connect(
knetFile *ftp)
289 char host[80], port[10];
290 if (ftp->pasv_port == 0) {
293 fprintf(stderr,
"[kftp_pasv_connect] kftp_pasv_prep() is not called before hand.\n");
297 sprintf(host,
"%d.%d.%d.%d", ftp->pasv_ip[0], ftp->pasv_ip[1], ftp->pasv_ip[2], ftp->pasv_ip[3]);
298 sprintf(port,
"%d", ftp->pasv_port);
299 ftp->fd = socket_connect(host, port);
300 if (ftp->fd == -1)
return -1;
306 ftp->ctrl_fd = socket_connect(ftp->host, ftp->port);
307 if (ftp->ctrl_fd == -1)
return -1;
308 kftp_get_response(ftp);
309 kftp_send_cmd(ftp,
"USER anonymous\r\n", 1);
310 kftp_send_cmd(ftp,
"PASS kftp@\r\n", 1);
311 kftp_send_cmd(ftp,
"TYPE I\r\n", 1);
317 if (ftp->ctrl_fd != -1) {
318 netclose(ftp->ctrl_fd);
323 return kftp_connect(ftp);
327 knetFile *kftp_parse_url(
const char *fn,
const char *mode)
332 if (strstr(fn,
"ftp://") != fn)
return 0;
333 for (p = (
char*)fn + 6; *p && *p !=
'/'; ++p);
334 if (*p !=
'/')
return 0;
337 fp->type = KNF_TYPE_FTP;
341 fp->port = strdup(
"21");
342 fp->host = (
char*)calloc(l + 1, 1);
343 if (strchr(mode,
'c')) fp->no_reconnect = 1;
344 strncpy(fp->host, fn + 6, l);
345 fp->retr = (
char*)calloc(strlen(p) + 8, 1);
346 sprintf(fp->retr,
"RETR %s\r\n", p);
347 fp->size_cmd = (
char*)calloc(strlen(p) + 8, 1);
348 sprintf(fp->size_cmd,
"SIZE %s\r\n", p);
359 if (fp->no_reconnect) kftp_get_response(fp);
362 kftp_send_cmd(fp, fp->size_cmd, 1);
363 if ( sscanf(fp->response,
"%*d %lld", &file_size) != 1 )
367 fprintf(stderr,
"[kftp_connect_file] %s\n", fp->response);
371 fp->file_size = file_size;
375 sprintf(tmp,
"REST %lld\r\n", (
long long)fp->offset);
377 strcpy(tmp,
"REST ");
378 int64tostr(tmp + 5, fp->offset);
381 kftp_send_cmd(fp, tmp, 1);
383 kftp_send_cmd(fp, fp->retr, 0);
384 kftp_pasv_connect(fp);
385 ret = kftp_get_response(fp);
389 fprintf(stderr,
"[kftp_connect_file] %s\n", fp->response);
404 knetFile *khttp_parse_url(
const char *fn,
const char *mode)
409 if (strstr(fn,
"http://") != fn)
return 0;
411 for (p = (
char*)fn + 7; *p && *p !=
'/'; ++p);
414 fp->http_host = (
char*)calloc(l + 1, 1);
415 strncpy(fp->http_host, fn + 7, l);
416 fp->http_host[l] = 0;
417 for (q = fp->http_host; *q && *q !=
':'; ++q);
418 if (*q ==
':') *q++ = 0;
420 proxy = getenv(
"http_proxy");
423 fp->host = strdup(fp->http_host);
424 fp->port = strdup(*q? q :
"80");
425 fp->path = strdup(*p? p :
"/");
427 fp->host = (strstr(proxy,
"http://") == proxy)? strdup(proxy + 7) : strdup(proxy);
428 for (q = fp->host; *q && *q !=
':'; ++q);
429 if (*q ==
':') *q++ = 0;
430 fp->port = strdup(*q? q :
"80");
431 fp->path = strdup(fn);
433 fp->type = KNF_TYPE_HTTP;
434 fp->ctrl_fd = fp->fd = -1;
439 int khttp_connect_file(
knetFile *fp)
443 if (fp->fd != -1) netclose(fp->fd);
444 fp->fd = socket_connect(fp->host, fp->port);
445 buf = (
char*)calloc(0x10000, 1);
446 l += sprintf(buf + l,
"GET %s HTTP/1.0\r\nHost: %s\r\n", fp->path, fp->http_host);
447 l += sprintf(buf + l,
"Range: bytes=%lld-\r\n", (
long long)fp->offset);
448 l += sprintf(buf + l,
"\r\n");
449 if(netwrite(fp->fd, buf, l) != l)
453 while (netread(fp->fd, buf + l, 1)) {
454 if (buf[l] ==
'\n' && l >= 3)
455 if (strncmp(buf + l - 3,
"\r\n\r\n", 4) == 0)
break;
464 ret = strtol(buf + 8, &p, 0);
465 if (ret == 200 && fp->offset>0) {
466 off_t rest = fp->offset;
468 off_t l = rest < 0x10000? rest : 0x10000;
469 rest -= my_netread(fp->fd, buf, l);
471 }
else if (ret != 206 && ret != 200) {
475 fprintf(stderr,
"[khttp_connect_file] fail to open file (HTTP code: %d).\n", ret);
490 knetFile *knet_open(
const char *fn,
const char *mode)
493 if (mode[0] !=
'r') {
496 fprintf(stderr,
"[kftp_open] only mode \"r\" is supported.\n");
500 if (strstr(fn,
"ftp://") == fn) {
501 fp = kftp_parse_url(fn, mode);
502 if (fp == 0)
return 0;
503 if (kftp_connect(fp) == -1) {
507 kftp_connect_file(fp);
508 }
else if (strstr(fn,
"http://") == fn) {
509 fp = khttp_parse_url(fn, mode);
510 if (fp == 0)
return 0;
511 khttp_connect_file(fp);
517 int fd = open(fn, O_RDONLY | O_BINARY);
519 int fd = open(fn, O_RDONLY);
526 fp->type = KNF_TYPE_LOCAL;
530 if (fp && fp->fd == -1) {
537 knetFile *knet_dopen(
int fd,
const char *mode)
540 fp->type = KNF_TYPE_LOCAL;
545 ssize_t knet_read(
knetFile *fp,
void *buf,
size_t len)
548 if (fp->fd == -1)
return 0;
549 if (fp->type == KNF_TYPE_FTP) {
550 if (fp->is_ready == 0) {
551 if (!fp->no_reconnect) kftp_reconnect(fp);
552 kftp_connect_file(fp);
554 }
else if (fp->type == KNF_TYPE_HTTP) {
555 if (fp->is_ready == 0)
556 khttp_connect_file(fp);
558 if (fp->type == KNF_TYPE_LOCAL) {
563 curr = read(fp->fd, (
void*)((
char*)buf + l), rest);
564 }
while (curr < 0 && EINTR == errno);
565 if (curr < 0)
return -1;
566 if (curr == 0)
break;
567 l += curr; rest -= curr;
569 }
else l = my_netread(fp->fd, buf, len);
574 off_t knet_seek(
knetFile *fp, off_t off,
int whence)
576 if (whence == SEEK_SET && off == fp->offset)
return 0;
577 if (fp->type == KNF_TYPE_LOCAL) {
579 off_t offset = lseek(fp->fd, off, whence);
580 if (offset == -1)
return -1;
583 }
else if (fp->type == KNF_TYPE_FTP) {
584 if (whence == SEEK_CUR) fp->offset += off;
585 else if (whence == SEEK_SET) fp->offset = off;
586 else if (whence == SEEK_END) fp->offset = fp->file_size + off;
590 }
else if (fp->type == KNF_TYPE_HTTP) {
591 if (whence == SEEK_END) {
594 fprintf(stderr,
"[knet_seek] SEEK_END is not supported for HTTP. Offset is unchanged.\n");
599 if (whence == SEEK_CUR) fp->offset += off;
600 else if (whence == SEEK_SET) fp->offset = off;
608 fprintf(stderr,
"[knet_seek] %s\n", strerror(errno));
615 if (fp == 0)
return 0;
616 if (fp->ctrl_fd != -1) netclose(fp->ctrl_fd);
620 if (fp->type == KNF_TYPE_LOCAL) close(fp->fd);
621 else netclose(fp->fd);
623 free(fp->host); free(fp->port);
624 free(fp->response); free(fp->retr);
625 free(fp->path); free(fp->http_host);
639 buf = calloc(0x100000, 1);
641 fp = knet_open(
"knetfile.c",
"r");
642 knet_seek(fp, 1000, SEEK_SET);
643 }
else if (type == 1) {
644 fp = knet_open(
"ftp://ftp.ncbi.nih.gov/1000genomes/ftp/data/NA12878/alignment/NA12878.chrom6.SLX.SRP000032.2009_06.bam",
"r");
645 knet_seek(fp, 2500000000ll, SEEK_SET);
646 l = knet_read(fp, buf, 255);
647 }
else if (type == 2) {
648 fp = knet_open(
"ftp://ftp.sanger.ac.uk/pub4/treefam/tmp/index.shtml",
"r");
649 knet_seek(fp, 1000, SEEK_SET);
650 }
else if (type == 3) {
651 fp = knet_open(
"http://www.sanger.ac.uk/Users/lh3/index.shtml",
"r");
652 knet_seek(fp, 1000, SEEK_SET);
653 }
else if (type == 4) {
654 fp = knet_open(
"http://www.sanger.ac.uk/Users/lh3/ex1.bam",
"r");
655 knet_read(fp, buf, 10000);
656 knet_seek(fp, 20000, SEEK_SET);
657 knet_seek(fp, 10000, SEEK_SET);
658 l = knet_read(fp, buf+10000, 10000000) + 10000;
660 if (type != 4 && type != 1) {
661 knet_read(fp, buf, 255);
664 }
else write(fileno(stdout), buf, l);