Rizin
unix-like reverse engineering framework and cli tools
main.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <uv.h>
5 #include <curl/curl.h>
6 
8 CURLM *curl_handle;
10 
11 typedef struct curl_context_s {
13  curl_socket_t sockfd;
15 
18 
19  context = (curl_context_t*) malloc(sizeof *context);
20 
21  context->sockfd = sockfd;
22 
23  int r = uv_poll_init_socket(loop, &context->poll_handle, sockfd);
24  assert(r == 0);
25  context->poll_handle.data = context;
26 
27  return context;
28 }
29 
32  free(context);
33 }
34 
36  uv_close((uv_handle_t*) &context->poll_handle, curl_close_cb);
37 }
38 
39 
40 void add_download(const char *url, int num) {
41  char filename[50];
42  sprintf(filename, "%d.download", num);
43  FILE *file;
44 
45  file = fopen(filename, "w");
46  if (file == NULL) {
47  fprintf(stderr, "Error opening %s\n", filename);
48  return;
49  }
50 
51  CURL *handle = curl_easy_init();
52  curl_easy_setopt(handle, CURLOPT_WRITEDATA, file);
53  curl_easy_setopt(handle, CURLOPT_URL, url);
54  curl_multi_add_handle(curl_handle, handle);
55  fprintf(stderr, "Added download %s -> %s\n", url, filename);
56 }
57 
58 void check_multi_info(void) {
59  char *done_url;
60  CURLMsg *message;
61  int pending;
62 
63  while ((message = curl_multi_info_read(curl_handle, &pending))) {
64  switch (message->msg) {
65  case CURLMSG_DONE:
66  curl_easy_getinfo(message->easy_handle, CURLINFO_EFFECTIVE_URL,
67  &done_url);
68  printf("%s DONE\n", done_url);
69 
70  curl_multi_remove_handle(curl_handle, message->easy_handle);
71  curl_easy_cleanup(message->easy_handle);
72  break;
73 
74  default:
75  fprintf(stderr, "CURLMSG default\n");
76  abort();
77  }
78  }
79 }
80 
81 void curl_perform(uv_poll_t *req, int status, int events) {
83  int running_handles;
84  int flags = 0;
85  if (status < 0) flags = CURL_CSELECT_ERR;
86  if (!status && events & UV_READABLE) flags |= CURL_CSELECT_IN;
87  if (!status && events & UV_WRITABLE) flags |= CURL_CSELECT_OUT;
88 
90 
92 
93  curl_multi_socket_action(curl_handle, context->sockfd, flags, &running_handles);
95 }
96 
98  int running_handles;
99  curl_multi_socket_action(curl_handle, CURL_SOCKET_TIMEOUT, 0, &running_handles);
101 }
102 
103 void start_timeout(CURLM *multi, long timeout_ms, void *userp) {
104  if (timeout_ms <= 0)
105  timeout_ms = 1; /* 0 means directly call socket_action, but we'll do it in a bit */
106  uv_timer_start(&timeout, on_timeout, timeout_ms, 0);
107 }
108 
109 int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp, void *socketp) {
110  curl_context_t *curl_context;
111  if (action == CURL_POLL_IN || action == CURL_POLL_OUT) {
112  if (socketp) {
113  curl_context = (curl_context_t*) socketp;
114  }
115  else {
116  curl_context = create_curl_context(s);
117  curl_multi_assign(curl_handle, s, (void *) curl_context);
118  }
119  }
120 
121  switch (action) {
122  case CURL_POLL_IN:
124  break;
125  case CURL_POLL_OUT:
127  break;
128  case CURL_POLL_REMOVE:
129  if (socketp) {
130  uv_poll_stop(&((curl_context_t*)socketp)->poll_handle);
132  curl_multi_assign(curl_handle, s, NULL);
133  }
134  break;
135  default:
136  abort();
137  }
138 
139  return 0;
140 }
141 
142 int main(int argc, char **argv) {
143  loop = uv_default_loop();
144 
145  if (argc <= 1)
146  return 0;
147 
148  if (curl_global_init(CURL_GLOBAL_ALL)) {
149  fprintf(stderr, "Could not init cURL\n");
150  return 1;
151  }
152 
154 
155  curl_handle = curl_multi_init();
156  curl_multi_setopt(curl_handle, CURLMOPT_SOCKETFUNCTION, handle_socket);
157  curl_multi_setopt(curl_handle, CURLMOPT_TIMERFUNCTION, start_timeout);
158 
159  while (argc-- > 1) {
160  add_download(argv[argc], argc);
161  }
162 
164  curl_multi_cleanup(curl_handle);
165  return 0;
166 }
static mcore_handle handle
Definition: asm_mcore.c:8
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
static static sync static getppid static getegid const char static filename char static len const char char static bufsiz static mask static vfork const void static prot static getpgrp const char static swapflags static arg static fd static protocol static who struct sockaddr static addrlen static backlog struct timeval struct timezone static tz const struct iovec static count static mode const void const struct sockaddr static tolen const char static pathname void static offset struct stat static buf void long static basep static whence static length const void static len static semflg const void static shmflg const struct timespec req
Definition: sflib.h:128
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
const char * filename
Definition: ioapi.h:137
sprintf
Definition: kernel.h:365
int main(int argc, const char **argv)
Definition: main.c:340
static Sdb * s
Definition: main.c:15
void * malloc(size_t size)
Definition: malloc.c:123
static static fork const void static count static fd const char const char static newpath char char char static envp time_t static t const char static mode static whence const char static dir time_t static t unsigned static seconds const char struct utimbuf static buf static inc static sig const char static mode static oldfd struct tms static buf static getgid static geteuid const char static filename static arg static mask struct ustat static ubuf static getppid static setsid static egid sigset_t static set struct timeval struct timezone static tz fd_set fd_set fd_set struct timeval static timeout const char char static bufsiz const char static swapflags void static offset const char static length static mode static who const char struct statfs static buf unsigned unsigned num
Definition: sflib.h:126
static static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
assert(limit<=UINT32_MAX/2)
string FILE
Definition: benchmark.py:21
url
Definition: setup.py:262
static sockfd
Definition: sfsocketcall.h:114
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
curl_socket_t sockfd
Definition: main.c:13
uv_poll_t poll_handle
Definition: main.c:12
Definition: gzappend.c:170
Definition: uv.h:1780
Definition: uv.h:793
Definition: uv.h:860
uv_loop_t * loop
Definition: main.c:7
char * message
Definition: main.c:12
void start_timeout(CURLM *multi, long timeout_ms, void *userp)
Definition: main.c:103
void check_multi_info(void)
Definition: main.c:58
void curl_close_cb(uv_handle_t *handle)
Definition: main.c:30
int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp, void *socketp)
Definition: main.c:109
struct curl_context_s curl_context_t
void destroy_curl_context(curl_context_t *context)
Definition: main.c:35
CURLM * curl_handle
Definition: main.c:8
void add_download(const char *url, int num)
Definition: main.c:40
void on_timeout(uv_timer_t *req)
Definition: main.c:97
void curl_perform(uv_poll_t *req, int status, int events)
Definition: main.c:81
curl_context_t * create_curl_context(curl_socket_t sockfd)
Definition: main.c:16
uv_timer_t timeout
Definition: main.c:9
@ UV_RUN_DEFAULT
Definition: uv.h:255
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
UV_EXTERN int uv_poll_start(uv_poll_t *handle, int events, uv_poll_cb cb)
Definition: poll.c:118
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: core.c:365
UV_EXTERN int uv_timer_stop(uv_timer_t *handle)
Definition: timer.c:97
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: core.c:108
UV_EXTERN int uv_poll_init_socket(uv_loop_t *loop, uv_poll_t *handle, uv_os_sock_t socket)
Definition: poll.c:96
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:763
@ UV_WRITABLE
Definition: uv.h:801
@ UV_READABLE
Definition: uv.h:800
UV_EXTERN int uv_poll_stop(uv_poll_t *handle)
Definition: poll.c:111
static int file
Definition: z80asm.c:58