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