| 1 |
#include <stdio.h> |
| 2 |
#include <fcntl.h> |
| 3 |
#include <errno.h> |
| 4 |
#include <ctype.h> |
| 5 |
#include <unistd.h> |
| 6 |
#include <string.h> |
| 7 |
#include <stdlib.h> |
| 8 |
#include <signal.h> |
| 9 |
#include <sys/wait.h> |
| 10 |
#include <sys/types.h> |
| 11 |
#ifndef _WIN32 |
| 12 |
#include <netdb.h> |
| 13 |
#include <arpa/inet.h> |
| 14 |
#include <sys/socket.h> |
| 15 |
#endif |
| 16 |
|
| 17 |
#ifdef USE_MALLOC_WRAPPERS |
| 18 |
# include "malloc_wrap.h" |
| 19 |
#endif |
| 20 |
|
| 21 |
#ifdef _WIN32 |
| 22 |
#define _KO_NO_NET |
| 23 |
#endif |
| 24 |
|
| 25 |
#ifndef _KO_NO_NET |
| 26 |
static int socket_wait(int fd, int is_read) |
| 27 |
{ |
| 28 |
fd_set fds, *fdr = 0, *fdw = 0; |
| 29 |
struct timeval tv; |
| 30 |
int ret; |
| 31 |
tv.tv_sec = 5; tv.tv_usec = 0; // 5 seconds time out |
| 32 |
FD_ZERO(&fds); |
| 33 |
FD_SET(fd, &fds); |
| 34 |
if (is_read) fdr = &fds; |
| 35 |
else fdw = &fds; |
| 36 |
ret = select(fd+1, fdr, fdw, 0, &tv); |
| 37 |
if (ret == -1) perror("select"); |
| 38 |
return ret; |
| 39 |
} |
| 40 |
|
| 41 |
static int socket_connect(const char *host, const char *port) |
| 42 |
{ |
| 43 |
#define __err_connect(func) do { perror(func); freeaddrinfo(res); return -1; } while (0) |
| 44 |
|
| 45 |
int on = 1, fd; |
| 46 |
struct linger lng = { 0, 0 }; |
| 47 |
struct addrinfo hints, *res = 0; |
| 48 |
memset(&hints, 0, sizeof(struct addrinfo)); |
| 49 |
hints.ai_family = AF_UNSPEC; |
| 50 |
hints.ai_socktype = SOCK_STREAM; |
| 51 |
if (getaddrinfo(host, port, &hints, &res) != 0) __err_connect("getaddrinfo"); |
| 52 |
if ((fd = socket(res->ai_family, res->ai_socktype, res->ai_protocol)) == -1) __err_connect("socket"); |
| 53 |
if (setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on)) == -1) __err_connect("setsockopt"); |
| 54 |
if (setsockopt(fd, SOL_SOCKET, SO_LINGER, &lng, sizeof(lng)) == -1) __err_connect("setsockopt"); |
| 55 |
if (connect(fd, res->ai_addr, res->ai_addrlen) != 0) __err_connect("connect"); |
| 56 |
freeaddrinfo(res); |
| 57 |
return fd; |
| 58 |
#undef __err_connect |
| 59 |
} |
| 60 |
|
| 61 |
static int write_bytes(int fd, const char *buf, size_t len) |
| 62 |
{ |
| 63 |
ssize_t bytes; |
| 64 |
do { |
| 65 |
bytes = write(fd, buf, len); |
| 66 |
if (bytes >= 0) { |
| 67 |
len -= bytes; |
| 68 |
} else if (errno != EAGAIN && errno != EWOULDBLOCK && errno != EINTR) { |
| 69 |
return -1; |
| 70 |
} |
| 71 |
} while (len > 0); |
| 72 |
|
| 73 |
return 0; |
| 74 |
} |
| 75 |
|
| 76 |
static int http_open(const char *fn) |
| 77 |
{ |
| 78 |
char *p, *proxy, *q, *http_host, *host, *port, *path, *buf; |
| 79 |
int fd, ret, l; |
| 80 |
ssize_t bytes = 0, bufsz = 0x10000; |
| 81 |
|
| 82 |
/* parse URL; adapted from khttp_parse_url() in knetfile.c */ |
| 83 |
if (strstr(fn, "http://") != fn) return 0; |
| 84 |
// set ->http_host |
| 85 |
for (p = (char*)fn + 7; *p && *p != '/'; ++p); |
| 86 |
l = p - fn - 7; |
| 87 |
http_host = calloc(l + 1, 1); |
| 88 |
strncpy(http_host, fn + 7, l); |
| 89 |
http_host[l] = 0; |
| 90 |
for (q = http_host; *q && *q != ':'; ++q); |
| 91 |
if (*q == ':') *q++ = 0; |
| 92 |
// get http_proxy |
| 93 |
proxy = getenv("http_proxy"); |
| 94 |
// set host, port and path |
| 95 |
if (proxy == 0) { |
| 96 |
host = strdup(http_host); // when there is no proxy, server name is identical to http_host name. |
| 97 |
port = strdup(*q? q : "80"); |
| 98 |
path = strdup(*p? p : "/"); |
| 99 |
} else { |
| 100 |
host = (strstr(proxy, "http://") == proxy)? strdup(proxy + 7) : strdup(proxy); |
| 101 |
for (q = host; *q && *q != ':'; ++q); |
| 102 |
if (*q == ':') *q++ = 0; |
| 103 |
port = strdup(*q? q : "80"); |
| 104 |
path = strdup(fn); |
| 105 |
} |
| 106 |
|
| 107 |
/* connect; adapted from khttp_connect() in knetfile.c */ |
| 108 |
l = 0; |
| 109 |
fd = socket_connect(host, port); |
| 110 |
buf = calloc(bufsz, 1); // FIXME: I am lazy... But in principle, 64KB should be large enough. |
| 111 |
l += snprintf(buf + l, bufsz, "GET %s HTTP/1.0\r\nHost: %s\r\n\r\n", |
| 112 |
path, http_host); |
| 113 |
if (write_bytes(fd, buf, l) != 0) { |
| 114 |
close(fd); |
| 115 |
fd = -1; |
| 116 |
goto out; |
| 117 |
} |
| 118 |
l = 0; |
| 119 |
retry: |
| 120 |
while (l < bufsz && (bytes = read(fd, buf + l, 1)) > 0) { // read HTTP header; FIXME: bad efficiency |
| 121 |
if (buf[l] == '\n' && l >= 3) |
| 122 |
if (strncmp(buf + l - 3, "\r\n\r\n", 4) == 0) break; |
| 123 |
++l; |
| 124 |
} |
| 125 |
if (bytes < 0 && (errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR)) goto retry; |
| 126 |
|
| 127 |
buf[l] = 0; |
| 128 |
if (bytes < 0 || l < 14) { // prematured header |
| 129 |
close(fd); |
| 130 |
fd = -1; |
| 131 |
goto out; |
| 132 |
} |
| 133 |
ret = strtol(buf + 8, &p, 0); // HTTP return code |
| 134 |
if (ret != 200) { |
| 135 |
close(fd); |
| 136 |
fd = -1; |
| 137 |
} |
| 138 |
out: |
| 139 |
free(buf); free(http_host); free(host); free(port); free(path); |
| 140 |
return fd; |
| 141 |
} |
| 142 |
|
| 143 |
typedef struct { |
| 144 |
int max_response, ctrl_fd; |
| 145 |
char *response; |
| 146 |
} ftpaux_t; |
| 147 |
|
| 148 |
static int kftp_get_response(ftpaux_t *aux) |
| 149 |
{ |
| 150 |
unsigned char c; |
| 151 |
int n = 0; |
| 152 |
char *p; |
| 153 |
if (socket_wait(aux->ctrl_fd, 1) <= 0) return 0; |
| 154 |
while (read(aux->ctrl_fd, &c, 1)) { // FIXME: this is *VERY BAD* for unbuffered I/O |
| 155 |
if (n >= aux->max_response) { |
| 156 |
aux->max_response = aux->max_response? aux->max_response<<1 : 256; |
| 157 |
aux->response = realloc(aux->response, aux->max_response); |
| 158 |
} |
| 159 |
aux->response[n++] = c; |
| 160 |
if (c == '\n') { |
| 161 |
if (n >= 4 && isdigit(aux->response[0]) && isdigit(aux->response[1]) && isdigit(aux->response[2]) |
| 162 |
&& aux->response[3] != '-') break; |
| 163 |
n = 0; |
| 164 |
continue; |
| 165 |
} |
| 166 |
} |
| 167 |
if (n < 2) return -1; |
| 168 |
aux->response[n-2] = 0; |
| 169 |
return strtol(aux->response, &p, 0); |
| 170 |
} |
| 171 |
|
| 172 |
static int kftp_send_cmd(ftpaux_t *aux, const char *cmd, int is_get) |
| 173 |
{ |
| 174 |
if (socket_wait(aux->ctrl_fd, 0) <= 0) return -1; // socket is not ready for writing |
| 175 |
if (write_bytes(aux->ctrl_fd, cmd, strlen(cmd)) != 0) return -1; |
| 176 |
return is_get? kftp_get_response(aux) : 0; |
| 177 |
} |
| 178 |
|
| 179 |
static int ftp_open(const char *fn) |
| 180 |
{ |
| 181 |
char *p, *host = 0, *port = 0, *retr = 0; |
| 182 |
char host2[80], port2[10]; |
| 183 |
int v[6], l, fd = -1, ret, pasv_port, pasv_ip[4]; |
| 184 |
ftpaux_t aux; |
| 185 |
|
| 186 |
/* parse URL */ |
| 187 |
if (strstr(fn, "ftp://") != fn) return 0; |
| 188 |
for (p = (char*)fn + 6; *p && *p != '/'; ++p); |
| 189 |
if (*p != '/') return 0; |
| 190 |
l = p - fn - 6; |
| 191 |
port = strdup("21"); |
| 192 |
host = calloc(l + 1, 1); |
| 193 |
strncpy(host, fn + 6, l); |
| 194 |
retr = calloc(strlen(p) + 8, 1); |
| 195 |
sprintf(retr, "RETR %s\r\n", p); |
| 196 |
|
| 197 |
/* connect to ctrl */ |
| 198 |
memset(&aux, 0, sizeof(ftpaux_t)); |
| 199 |
aux.ctrl_fd = socket_connect(host, port); |
| 200 |
if (aux.ctrl_fd == -1) goto ftp_open_end; /* fail to connect ctrl */ |
| 201 |
|
| 202 |
/* connect to the data stream */ |
| 203 |
kftp_get_response(&aux); |
| 204 |
kftp_send_cmd(&aux, "USER anonymous\r\n", 1); |
| 205 |
kftp_send_cmd(&aux, "PASS kopen@\r\n", 1); |
| 206 |
kftp_send_cmd(&aux, "TYPE I\r\n", 1); |
| 207 |
kftp_send_cmd(&aux, "PASV\r\n", 1); |
| 208 |
for (p = aux.response; *p && *p != '('; ++p); |
| 209 |
if (*p != '(') goto ftp_open_end; |
| 210 |
++p; |
| 211 |
sscanf(p, "%d,%d,%d,%d,%d,%d", &v[0], &v[1], &v[2], &v[3], &v[4], &v[5]); |
| 212 |
memcpy(pasv_ip, v, 4 * sizeof(int)); |
| 213 |
pasv_port = (v[4]<<8&0xff00) + v[5]; |
| 214 |
kftp_send_cmd(&aux, retr, 0); |
| 215 |
sprintf(host2, "%d.%d.%d.%d", pasv_ip[0], pasv_ip[1], pasv_ip[2], pasv_ip[3]); |
| 216 |
sprintf(port2, "%d", pasv_port); |
| 217 |
fd = socket_connect(host2, port2); |
| 218 |
if (fd == -1) goto ftp_open_end; |
| 219 |
ret = kftp_get_response(&aux); |
| 220 |
if (ret != 150) { |
| 221 |
close(fd); |
| 222 |
fd = -1; |
| 223 |
} |
| 224 |
close(aux.ctrl_fd); |
| 225 |
|
| 226 |
ftp_open_end: |
| 227 |
free(host); free(port); free(retr); free(aux.response); |
| 228 |
return fd; |
| 229 |
} |
| 230 |
#endif /* !defined(_KO_NO_NET) */ |
| 231 |
|
| 232 |
static char **cmd2argv(const char *cmd) |
| 233 |
{ |
| 234 |
int i, beg, end, argc; |
| 235 |
char **argv, *str; |
| 236 |
end = strlen(cmd); |
| 237 |
for (i = end - 1; i >= 0; --i) |
| 238 |
if (!isspace(cmd[i])) break; |
| 239 |
end = i + 1; |
| 240 |
for (beg = 0; beg < end; ++beg) |
| 241 |
if (!isspace(cmd[beg])) break; |
| 242 |
if (beg == end) return 0; |
| 243 |
for (i = beg + 1, argc = 0; i < end; ++i) |
| 244 |
if (isspace(cmd[i]) && !isspace(cmd[i-1])) |
| 245 |
++argc; |
| 246 |
argv = (char**)calloc(argc + 2, sizeof(void*)); |
| 247 |
argv[0] = str = (char*)calloc(end - beg + 1, 1); |
| 248 |
strncpy(argv[0], cmd + beg, end - beg); |
| 249 |
for (i = argc = 1; i < end - beg; ++i) |
| 250 |
if (isspace(str[i])) str[i] = 0; |
| 251 |
else if (str[i] && str[i-1] == 0) argv[argc++] = &str[i]; |
| 252 |
return argv; |
| 253 |
} |
| 254 |
|
| 255 |
#define KO_STDIN 1 |
| 256 |
#define KO_FILE 2 |
| 257 |
#define KO_PIPE 3 |
| 258 |
#define KO_HTTP 4 |
| 259 |
#define KO_FTP 5 |
| 260 |
|
| 261 |
typedef struct { |
| 262 |
int type, fd; |
| 263 |
pid_t pid; |
| 264 |
} koaux_t; |
| 265 |
|
| 266 |
void *kopen(const char *fn, int *_fd) |
| 267 |
{ |
| 268 |
koaux_t *aux = 0; |
| 269 |
*_fd = -1; |
| 270 |
if (strstr(fn, "http://") == fn) { |
| 271 |
aux = calloc(1, sizeof(koaux_t)); |
| 272 |
aux->type = KO_HTTP; |
| 273 |
aux->fd = http_open(fn); |
| 274 |
} else if (strstr(fn, "ftp://") == fn) { |
| 275 |
aux = calloc(1, sizeof(koaux_t)); |
| 276 |
aux->type = KO_FTP; |
| 277 |
aux->fd = ftp_open(fn); |
| 278 |
} else if (strcmp(fn, "-") == 0) { |
| 279 |
aux = calloc(1, sizeof(koaux_t)); |
| 280 |
aux->type = KO_STDIN; |
| 281 |
aux->fd = STDIN_FILENO; |
| 282 |
} else { |
| 283 |
const char *p, *q; |
| 284 |
for (p = fn; *p; ++p) |
| 285 |
if (!isspace(*p)) break; |
| 286 |
if (*p == '<') { // pipe open |
| 287 |
int need_shell, pfd[2]; |
| 288 |
pid_t pid; |
| 289 |
// a simple check to see if we need to invoke a shell; not always working |
| 290 |
for (q = p + 1; *q; ++q) |
| 291 |
if (ispunct(*q) && *q != '.' && *q != '_' && *q != '-' && *q != ':') |
| 292 |
break; |
| 293 |
need_shell = (*q != 0); |
| 294 |
if (pipe(pfd) != 0) return 0; |
| 295 |
pid = vfork(); |
| 296 |
if (pid == -1) { /* vfork() error */ |
| 297 |
close(pfd[0]); close(pfd[1]); |
| 298 |
return 0; |
| 299 |
} |
| 300 |
if (pid == 0) { /* the child process */ |
| 301 |
char **argv; /* FIXME: I do not know if this will lead to a memory leak */ |
| 302 |
close(pfd[0]); |
| 303 |
dup2(pfd[1], STDOUT_FILENO); |
| 304 |
close(pfd[1]); |
| 305 |
if (!need_shell) { |
| 306 |
argv = cmd2argv(p + 1); |
| 307 |
execvp(argv[0], argv); |
| 308 |
free(argv[0]); free(argv); |
| 309 |
} else execl("/bin/sh", "sh", "-c", p + 1, NULL); |
| 310 |
exit(1); |
| 311 |
} else { /* parent process */ |
| 312 |
close(pfd[1]); |
| 313 |
aux = calloc(1, sizeof(koaux_t)); |
| 314 |
aux->type = KO_PIPE; |
| 315 |
aux->fd = pfd[0]; |
| 316 |
aux->pid = pid; |
| 317 |
} |
| 318 |
} else { |
| 319 |
#ifdef _WIN32 |
| 320 |
*_fd = open(fn, O_RDONLY | O_BINARY); |
| 321 |
#else |
| 322 |
*_fd = open(fn, O_RDONLY); |
| 323 |
#endif |
| 324 |
if (*_fd >= 0) { |
| 325 |
aux = calloc(1, sizeof(koaux_t)); |
| 326 |
aux->type = KO_FILE; |
| 327 |
aux->fd = *_fd; |
| 328 |
} |
| 329 |
} |
| 330 |
} |
| 331 |
if (aux) *_fd = aux->fd; |
| 332 |
return aux; |
| 333 |
} |
| 334 |
|
| 335 |
int kclose(void *a) |
| 336 |
{ |
| 337 |
koaux_t *aux = (koaux_t*)a; |
| 338 |
if (aux->type == KO_PIPE) { |
| 339 |
int status; |
| 340 |
pid_t pid; |
| 341 |
pid = waitpid(aux->pid, &status, WNOHANG); |
| 342 |
if (pid != aux->pid) kill(aux->pid, 15); |
| 343 |
} |
| 344 |
free(aux); |
| 345 |
return 0; |
| 346 |
} |
| 347 |
|
| 348 |
#ifdef _KO_MAIN |
| 349 |
#define BUF_SIZE 0x10000 |
| 350 |
int main(int argc, char *argv[]) |
| 351 |
{ |
| 352 |
void *x; |
| 353 |
int l, fd; |
| 354 |
unsigned char buf[BUF_SIZE]; |
| 355 |
FILE *fp; |
| 356 |
if (argc == 1) { |
| 357 |
fprintf(stderr, "Usage: kopen <file>\n"); |
| 358 |
return 1; |
| 359 |
} |
| 360 |
x = kopen(argv[1], &fd); |
| 361 |
fp = fdopen(fd, "r"); |
| 362 |
if (fp == 0) { |
| 363 |
fprintf(stderr, "ERROR: fail to open the input\n"); |
| 364 |
return 1; |
| 365 |
} |
| 366 |
do { |
| 367 |
if ((l = fread(buf, 1, BUF_SIZE, fp)) != 0) |
| 368 |
fwrite(buf, 1, l, stdout); |
| 369 |
} while (l == BUF_SIZE); |
| 370 |
fclose(fp); |
| 371 |
kclose(x); |
| 372 |
return 0; |
| 373 |
} |
| 374 |
#endif |