Rizin
unix-like reverse engineering framework and cli tools
main.c File Reference
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <uv.h>
#include <curl/curl.h>

Go to the source code of this file.

Classes

struct  curl_context_s
 

Typedefs

typedef struct curl_context_s curl_context_t
 

Functions

curl_context_tcreate_curl_context (curl_socket_t sockfd)
 
void curl_close_cb (uv_handle_t *handle)
 
void destroy_curl_context (curl_context_t *context)
 
void add_download (const char *url, int num)
 
void check_multi_info (void)
 
void curl_perform (uv_poll_t *req, int status, int events)
 
void on_timeout (uv_timer_t *req)
 
void start_timeout (CURLM *multi, long timeout_ms, void *userp)
 
int handle_socket (CURL *easy, curl_socket_t s, int action, void *userp, void *socketp)
 
int main (int argc, char **argv)
 

Variables

uv_loop_tloop
 
CURLM * curl_handle
 
uv_timer_t timeout
 

Typedef Documentation

◆ curl_context_t

Function Documentation

◆ add_download()

void add_download ( const char *  url,
int  num 
)

Definition at line 40 of file main.c.

40  {
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 }
static mcore_handle handle
Definition: asm_mcore.c:8
#define NULL
Definition: cris-opc.c:27
const char * filename
Definition: ioapi.h:137
sprintf
Definition: kernel.h:365
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
string FILE
Definition: benchmark.py:21
url
Definition: setup.py:262
Definition: gzappend.c:170
CURLM * curl_handle
Definition: main.c:8
static int file
Definition: z80asm.c:58

References curl_handle, file, benchmark::FILE, handle, NULL, num, sprintf, and setup::url.

Referenced by main().

◆ check_multi_info()

void check_multi_info ( void  )

Definition at line 58 of file main.c.

58  {
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 }
_Use_decl_annotations_ int __cdecl printf(const char *const _Format,...)
Definition: cs_driver.c:93
char * message
Definition: main.c:12

References curl_handle, message, and printf().

Referenced by curl_perform(), and on_timeout().

◆ create_curl_context()

curl_context_t* create_curl_context ( curl_socket_t  sockfd)

Definition at line 16 of file main.c.

16  {
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 }
#define r
Definition: crypto_rc6.c:12
void * malloc(size_t size)
Definition: malloc.c:123
assert(limit<=UINT32_MAX/2)
static sockfd
Definition: sfsocketcall.h:114
uv_loop_t * loop
Definition: main.c:7
UV_EXTERN int uv_poll_init_socket(uv_loop_t *loop, uv_poll_t *handle, uv_os_sock_t socket)
Definition: poll.c:96

References assert(), loop, malloc(), r, sockfd, and uv_poll_init_socket().

Referenced by handle_socket().

◆ curl_close_cb()

void curl_close_cb ( uv_handle_t handle)

Definition at line 30 of file main.c.

30  {
32  free(context);
33 }
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130

References free(), and handle.

Referenced by destroy_curl_context().

◆ curl_perform()

void curl_perform ( uv_poll_t req,
int  status,
int  events 
)

Definition at line 81 of file main.c.

81  {
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 }
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
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
void check_multi_info(void)
Definition: main.c:58
uv_timer_t timeout
Definition: main.c:9
UV_EXTERN int uv_timer_stop(uv_timer_t *handle)
Definition: timer.c:97
@ UV_WRITABLE
Definition: uv.h:801
@ UV_READABLE
Definition: uv.h:800

References check_multi_info(), curl_handle, flags, req, status, timeout, UV_READABLE, uv_timer_stop(), and UV_WRITABLE.

Referenced by handle_socket().

◆ destroy_curl_context()

void destroy_curl_context ( curl_context_t context)

Definition at line 35 of file main.c.

35  {
36  uv_close((uv_handle_t*) &context->poll_handle, curl_close_cb);
37 }
void curl_close_cb(uv_handle_t *handle)
Definition: main.c:30
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: core.c:108

References curl_close_cb(), and uv_close().

Referenced by handle_socket().

◆ handle_socket()

int handle_socket ( CURL *  easy,
curl_socket_t  s,
int  action,
void *  userp,
void *  socketp 
)

Definition at line 109 of file main.c.

109  {
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 }
static Sdb * s
Definition: main.c:15
uv_poll_t poll_handle
Definition: main.c:12
void destroy_curl_context(curl_context_t *context)
Definition: main.c:35
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_EXTERN int uv_poll_start(uv_poll_t *handle, int events, uv_poll_cb cb)
Definition: poll.c:118
UV_EXTERN int uv_poll_stop(uv_poll_t *handle)
Definition: poll.c:111

References test-lz4-speed::action, create_curl_context(), curl_handle, curl_perform(), destroy_curl_context(), NULL, curl_context_s::poll_handle, s, uv_poll_start(), uv_poll_stop(), UV_READABLE, and UV_WRITABLE.

Referenced by main().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 142 of file main.c.

142  {
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 static fork const void static count static fd const char const char static newpath char char argv
Definition: sflib.h:40
void start_timeout(CURLM *multi, long timeout_ms, void *userp)
Definition: main.c:103
int handle_socket(CURL *easy, curl_socket_t s, int action, void *userp, void *socketp)
Definition: main.c:109
void add_download(const char *url, int num)
Definition: main.c:40
@ UV_RUN_DEFAULT
Definition: uv.h:255
UV_EXTERN int uv_timer_init(uv_loop_t *, uv_timer_t *handle)
Definition: timer.c:58
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: core.c:365
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:763

References add_download(), argv, curl_handle, handle_socket(), loop, start_timeout(), timeout, uv_default_loop(), uv_run(), UV_RUN_DEFAULT, and uv_timer_init().

◆ on_timeout()

void on_timeout ( uv_timer_t req)

Definition at line 97 of file main.c.

97  {
98  int running_handles;
99  curl_multi_socket_action(curl_handle, CURL_SOCKET_TIMEOUT, 0, &running_handles);
101 }

References check_multi_info(), and curl_handle.

Referenced by start_timeout().

◆ start_timeout()

void start_timeout ( CURLM *  multi,
long  timeout_ms,
void *  userp 
)

Definition at line 103 of file main.c.

103  {
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 }
void on_timeout(uv_timer_t *req)
Definition: main.c:97
UV_EXTERN int uv_timer_start(uv_timer_t *handle, uv_timer_cb cb, uint64_t timeout, uint64_t repeat)
Definition: timer.c:66

References on_timeout(), timeout, and uv_timer_start().

Referenced by main().

Variable Documentation

◆ curl_handle

CURLM* curl_handle

Definition at line 8 of file main.c.

Referenced by add_download(), check_multi_info(), curl_perform(), handle_socket(), main(), and on_timeout().

◆ loop

uv_loop_t* loop

Definition at line 7 of file main.c.

◆ timeout