hiperfifo.c (12535B)
1 /*************************************************************************** 2 * _ _ ____ _ 3 * Project ___| | | | _ \| | 4 * / __| | | | |_) | | 5 * | (__| |_| | _ <| |___ 6 * \___|\___/|_| \_\_____| 7 * 8 * Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al. 9 * 10 * This software is licensed as described in the file COPYING, which 11 * you should have received as part of this distribution. The terms 12 * are also available at https://curl.se/docs/copyright.html. 13 * 14 * You may opt to use, copy, modify, merge, publish, distribute and/or sell 15 * copies of the Software, and permit persons to whom the Software is 16 * furnished to do so, under the terms of the COPYING file. 17 * 18 * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY 19 * KIND, either express or implied. 20 * 21 * SPDX-License-Identifier: curl 22 * 23 ***************************************************************************/ 24 /* <DESC> 25 * multi socket API usage with libevent 2 26 * </DESC> 27 */ 28 /* Example application source code using the multi socket interface to 29 download many files at once. 30 31 Written by Jeff Pohlmeyer 32 33 Requires libevent version 2 and a (POSIX?) system that has mkfifo(). 34 35 This is an adaptation of libcurl's "hipev.c" and libevent's "event-test.c" 36 sample programs. 37 38 When running, the program creates the named pipe "hiper.fifo" 39 40 Whenever there is input into the fifo, the program reads the input as a list 41 of URL's and creates some new easy handles to fetch each URL via the 42 curl_multi "hiper" API. 43 44 45 Thus, you can try a single URL: 46 % echo http://www.yahoo.com > hiper.fifo 47 48 Or a whole bunch of them: 49 % cat my-url-list > hiper.fifo 50 51 The fifo buffer is handled almost instantly, so you can even add more URL's 52 while the previous requests are still being downloaded. 53 54 Note: 55 For the sake of simplicity, URL length is limited to 1023 char's ! 56 57 This is purely a demo app, all retrieved data is simply discarded by the write 58 callback. 59 60 */ 61 62 #include <stdio.h> 63 #include <string.h> 64 #include <stdlib.h> 65 #include <sys/time.h> 66 #include <time.h> 67 #include <unistd.h> 68 #include <sys/poll.h> 69 #include <curl/curl.h> 70 #include <event2/event.h> 71 #include <event2/event_struct.h> 72 #include <fcntl.h> 73 #include <sys/stat.h> 74 #include <errno.h> 75 #include <sys/cdefs.h> 76 77 #define MSG_OUT stdout /* Send info to stdout, change to stderr if you want */ 78 79 80 /* Global information, common to all connections */ 81 struct GlobalInfo { 82 struct event_base *evbase; 83 struct event fifo_event; 84 struct event timer_event; 85 CURLM *multi; 86 int still_running; 87 FILE *input; 88 int stopped; 89 }; 90 91 92 /* Information associated with a specific easy handle */ 93 struct ConnInfo { 94 CURL *easy; 95 char *url; 96 struct GlobalInfo *global; 97 char error[CURL_ERROR_SIZE]; 98 }; 99 100 101 /* Information associated with a specific socket */ 102 struct SockInfo { 103 curl_socket_t sockfd; 104 CURL *easy; 105 int action; 106 long timeout; 107 struct event ev; 108 struct GlobalInfo *global; 109 }; 110 111 #define mycase(code) \ 112 case code: s = __STRING(code) 113 114 /* Die if we get a bad CURLMcode somewhere */ 115 static void mcode_or_die(const char *where, CURLMcode code) 116 { 117 if(CURLM_OK != code) { 118 const char *s; 119 switch(code) { 120 mycase(CURLM_BAD_HANDLE); break; 121 mycase(CURLM_BAD_EASY_HANDLE); break; 122 mycase(CURLM_OUT_OF_MEMORY); break; 123 mycase(CURLM_INTERNAL_ERROR); break; 124 mycase(CURLM_UNKNOWN_OPTION); break; 125 mycase(CURLM_LAST); break; 126 default: s = "CURLM_unknown"; break; 127 mycase(CURLM_BAD_SOCKET); 128 fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s); 129 /* ignore this error */ 130 return; 131 } 132 fprintf(MSG_OUT, "ERROR: %s returns %s\n", where, s); 133 exit(code); 134 } 135 } 136 137 138 /* Update the event timer after curl_multi library calls */ 139 static int multi_timer_cb(CURLM *multi, long timeout_ms, struct GlobalInfo *g) 140 { 141 struct timeval timeout; 142 (void)multi; 143 144 timeout.tv_sec = timeout_ms/1000; 145 timeout.tv_usec = (timeout_ms%1000)*1000; 146 fprintf(MSG_OUT, "multi_timer_cb: Setting timeout to %ld ms\n", timeout_ms); 147 148 /* 149 * if timeout_ms is -1, just delete the timer 150 * 151 * For all other values of timeout_ms, this should set or *update* the timer 152 * to the new value 153 */ 154 if(timeout_ms == -1) 155 evtimer_del(&g->timer_event); 156 else /* includes timeout zero */ 157 evtimer_add(&g->timer_event, &timeout); 158 return 0; 159 } 160 161 162 /* Check for completed transfers, and remove their easy handles */ 163 static void check_multi_info(struct GlobalInfo *g) 164 { 165 char *eff_url; 166 CURLMsg *msg; 167 int msgs_left; 168 struct ConnInfo *conn; 169 CURL *easy; 170 CURLcode res; 171 172 fprintf(MSG_OUT, "REMAINING: %d\n", g->still_running); 173 while((msg = curl_multi_info_read(g->multi, &msgs_left))) { 174 if(msg->msg == CURLMSG_DONE) { 175 easy = msg->easy_handle; 176 res = msg->data.result; 177 curl_easy_getinfo(easy, CURLINFO_PRIVATE, &conn); 178 curl_easy_getinfo(easy, CURLINFO_EFFECTIVE_URL, &eff_url); 179 fprintf(MSG_OUT, "DONE: %s => (%d) %s\n", eff_url, res, conn->error); 180 curl_multi_remove_handle(g->multi, easy); 181 free(conn->url); 182 curl_easy_cleanup(easy); 183 free(conn); 184 } 185 } 186 if(g->still_running == 0 && g->stopped) 187 event_base_loopbreak(g->evbase); 188 } 189 190 191 192 /* Called by libevent when we get action on a multi socket */ 193 static void event_cb(int fd, short kind, void *userp) 194 { 195 struct GlobalInfo *g = (struct GlobalInfo*) userp; 196 CURLMcode rc; 197 198 int action = 199 ((kind & EV_READ) ? CURL_CSELECT_IN : 0) | 200 ((kind & EV_WRITE) ? CURL_CSELECT_OUT : 0); 201 202 rc = curl_multi_socket_action(g->multi, fd, action, &g->still_running); 203 mcode_or_die("event_cb: curl_multi_socket_action", rc); 204 205 check_multi_info(g); 206 if(g->still_running <= 0) { 207 fprintf(MSG_OUT, "last transfer done, kill timeout\n"); 208 if(evtimer_pending(&g->timer_event, NULL)) { 209 evtimer_del(&g->timer_event); 210 } 211 } 212 } 213 214 215 216 /* Called by libevent when our timeout expires */ 217 static void timer_cb(int fd, short kind, void *userp) 218 { 219 struct GlobalInfo *g = (struct GlobalInfo *)userp; 220 CURLMcode rc; 221 (void)fd; 222 (void)kind; 223 224 rc = curl_multi_socket_action(g->multi, 225 CURL_SOCKET_TIMEOUT, 0, &g->still_running); 226 mcode_or_die("timer_cb: curl_multi_socket_action", rc); 227 check_multi_info(g); 228 } 229 230 231 232 /* Clean up the SockInfo structure */ 233 static void remsock(struct SockInfo *f) 234 { 235 if(f) { 236 if(event_initialized(&f->ev)) { 237 event_del(&f->ev); 238 } 239 free(f); 240 } 241 } 242 243 244 245 /* Assign information to a SockInfo structure */ 246 static void setsock(struct SockInfo *f, curl_socket_t s, CURL *e, int act, 247 struct GlobalInfo *g) 248 { 249 int kind = 250 ((act & CURL_POLL_IN) ? EV_READ : 0) | 251 ((act & CURL_POLL_OUT) ? EV_WRITE : 0) | EV_PERSIST; 252 253 f->sockfd = s; 254 f->action = act; 255 f->easy = e; 256 if(event_initialized(&f->ev)) { 257 event_del(&f->ev); 258 } 259 event_assign(&f->ev, g->evbase, f->sockfd, (short)kind, event_cb, g); 260 event_add(&f->ev, NULL); 261 } 262 263 264 265 /* Initialize a new SockInfo structure */ 266 static void addsock(curl_socket_t s, CURL *easy, int action, 267 struct GlobalInfo *g) 268 { 269 struct SockInfo *fdp = calloc(1, sizeof(struct SockInfo)); 270 271 fdp->global = g; 272 setsock(fdp, s, easy, action, g); 273 curl_multi_assign(g->multi, s, fdp); 274 } 275 276 /* CURLMOPT_SOCKETFUNCTION */ 277 static int sock_cb(CURL *e, curl_socket_t s, int what, void *cbp, void *sockp) 278 { 279 struct GlobalInfo *g = (struct GlobalInfo*) cbp; 280 struct SockInfo *fdp = (struct SockInfo*) sockp; 281 const char *whatstr[]={ "none", "IN", "OUT", "INOUT", "REMOVE" }; 282 283 fprintf(MSG_OUT, 284 "socket callback: s=%d e=%p what=%s ", s, e, whatstr[what]); 285 if(what == CURL_POLL_REMOVE) { 286 fprintf(MSG_OUT, "\n"); 287 remsock(fdp); 288 } 289 else { 290 if(!fdp) { 291 fprintf(MSG_OUT, "Adding data: %s\n", whatstr[what]); 292 addsock(s, e, what, g); 293 } 294 else { 295 fprintf(MSG_OUT, 296 "Changing action from %s to %s\n", 297 whatstr[fdp->action], whatstr[what]); 298 setsock(fdp, s, e, what, g); 299 } 300 } 301 return 0; 302 } 303 304 305 306 /* CURLOPT_WRITEFUNCTION */ 307 static size_t write_cb(void *ptr, size_t size, size_t nmemb, void *data) 308 { 309 (void)ptr; 310 (void)data; 311 return size * nmemb; 312 } 313 314 315 /* CURLOPT_PROGRESSFUNCTION */ 316 static int xferinfo_cb(void *p, curl_off_t dltotal, curl_off_t dlnow, 317 curl_off_t ult, curl_off_t uln) 318 { 319 struct ConnInfo *conn = (struct ConnInfo *)p; 320 (void)ult; 321 (void)uln; 322 323 fprintf(MSG_OUT, "Progress: %s (%" CURL_FORMAT_CURL_OFF_T 324 "/%" CURL_FORMAT_CURL_OFF_T ")\n", conn->url, dlnow, dltotal); 325 return 0; 326 } 327 328 329 /* Create a new easy handle, and add it to the global curl_multi */ 330 static void new_conn(const char *url, struct GlobalInfo *g) 331 { 332 struct ConnInfo *conn; 333 CURLMcode rc; 334 335 conn = calloc(1, sizeof(*conn)); 336 conn->error[0] = '\0'; 337 338 conn->easy = curl_easy_init(); 339 if(!conn->easy) { 340 fprintf(MSG_OUT, "curl_easy_init() failed, exiting!\n"); 341 exit(2); 342 } 343 conn->global = g; 344 conn->url = strdup(url); 345 curl_easy_setopt(conn->easy, CURLOPT_URL, conn->url); 346 curl_easy_setopt(conn->easy, CURLOPT_WRITEFUNCTION, write_cb); 347 curl_easy_setopt(conn->easy, CURLOPT_WRITEDATA, conn); 348 curl_easy_setopt(conn->easy, CURLOPT_VERBOSE, 1L); 349 curl_easy_setopt(conn->easy, CURLOPT_ERRORBUFFER, conn->error); 350 curl_easy_setopt(conn->easy, CURLOPT_PRIVATE, conn); 351 curl_easy_setopt(conn->easy, CURLOPT_NOPROGRESS, 0L); 352 curl_easy_setopt(conn->easy, CURLOPT_XFERINFOFUNCTION, xferinfo_cb); 353 curl_easy_setopt(conn->easy, CURLOPT_PROGRESSDATA, conn); 354 curl_easy_setopt(conn->easy, CURLOPT_FOLLOWLOCATION, 1L); 355 fprintf(MSG_OUT, 356 "Adding easy %p to multi %p (%s)\n", conn->easy, g->multi, url); 357 rc = curl_multi_add_handle(g->multi, conn->easy); 358 mcode_or_die("new_conn: curl_multi_add_handle", rc); 359 360 /* note that the add_handle() sets a time-out to trigger soon so that 361 the necessary socket_action() gets called */ 362 } 363 364 /* This gets called whenever data is received from the fifo */ 365 static void fifo_cb(int fd, short event, void *arg) 366 { 367 char s[1024]; 368 long int rv = 0; 369 int n = 0; 370 struct GlobalInfo *g = (struct GlobalInfo *)arg; 371 (void)fd; 372 (void)event; 373 374 do { 375 s[0]='\0'; 376 rv = fscanf(g->input, "%1023s%n", s, &n); 377 s[n]='\0'; 378 if(n && s[0]) { 379 if(!strcmp(s, "stop")) { 380 g->stopped = 1; 381 if(g->still_running == 0) 382 event_base_loopbreak(g->evbase); 383 } 384 else 385 new_conn(s, arg); /* if we read a URL, go get it! */ 386 } 387 else 388 break; 389 } while(rv != EOF); 390 } 391 392 /* Create a named pipe and tell libevent to monitor it */ 393 static const char *fifo = "hiper.fifo"; 394 static int init_fifo(struct GlobalInfo *g) 395 { 396 struct stat st; 397 curl_socket_t sockfd; 398 399 fprintf(MSG_OUT, "Creating named pipe \"%s\"\n", fifo); 400 if(lstat(fifo, &st) == 0) { 401 if((st.st_mode & S_IFMT) == S_IFREG) { 402 errno = EEXIST; 403 perror("lstat"); 404 return 1; 405 } 406 } 407 unlink(fifo); 408 if(mkfifo (fifo, 0600) == -1) { 409 perror("mkfifo"); 410 return 1; 411 } 412 sockfd = open(fifo, O_RDWR | O_NONBLOCK, 0); 413 if(sockfd == -1) { 414 perror("open"); 415 return 1; 416 } 417 g->input = fdopen(sockfd, "r"); 418 419 fprintf(MSG_OUT, "Now, pipe some URL's into > %s\n", fifo); 420 event_assign(&g->fifo_event, g->evbase, sockfd, EV_READ|EV_PERSIST, 421 fifo_cb, g); 422 event_add(&g->fifo_event, NULL); 423 return 0; 424 } 425 426 static void clean_fifo(struct GlobalInfo *g) 427 { 428 event_del(&g->fifo_event); 429 fclose(g->input); 430 unlink(fifo); 431 } 432 433 int main(int argc, char **argv) 434 { 435 struct GlobalInfo g; 436 (void)argc; 437 (void)argv; 438 439 memset(&g, 0, sizeof(g)); 440 g.evbase = event_base_new(); 441 if(init_fifo(&g)) 442 return 1; 443 g.multi = curl_multi_init(); 444 evtimer_assign(&g.timer_event, g.evbase, timer_cb, &g); 445 446 /* setup the generic multi interface options we want */ 447 curl_multi_setopt(g.multi, CURLMOPT_SOCKETFUNCTION, sock_cb); 448 curl_multi_setopt(g.multi, CURLMOPT_SOCKETDATA, &g); 449 curl_multi_setopt(g.multi, CURLMOPT_TIMERFUNCTION, multi_timer_cb); 450 curl_multi_setopt(g.multi, CURLMOPT_TIMERDATA, &g); 451 452 /* we do not call any curl_multi_socket*() function yet as we have no handles 453 added! */ 454 455 event_base_dispatch(g.evbase); 456 457 /* this, of course, does not get called since the only way to stop this 458 program is via ctrl-C, but it is here to show how cleanup /would/ be 459 done. */ 460 clean_fifo(&g); 461 event_del(&g.timer_event); 462 event_base_free(g.evbase); 463 curl_multi_cleanup(g.multi); 464 return 0; 465 }