Rizin
unix-like reverse engineering framework and cli tools
main.c
Go to the documentation of this file.
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <uv.h>
5 
7 
8 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
9  buf->base = malloc(suggested_size);
10  buf->len = suggested_size;
11 }
12 
13 void on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf) {
14  if (nread < 0) {
15  if (nread != UV_EOF)
16  fprintf(stderr, "Read error %s\n", uv_err_name(nread));
17  uv_close((uv_handle_t*) client, NULL);
18  free(buf->base);
19  free(client);
20  return;
21  }
22 
23  char *data = (char*) malloc(sizeof(char) * (nread+1));
24  data[nread] = '\0';
25  strncpy(data, buf->base, nread);
26 
27  fprintf(stderr, "%s", data);
28  free(data);
29  free(buf->base);
30 }
31 
33  if (status < 0) {
34  fprintf(stderr, "connect failed error %s\n", uv_err_name(status));
35  free(req);
36  return;
37  }
38 
40  free(req);
41 }
42 
43 void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res) {
44  if (status < 0) {
45  fprintf(stderr, "getaddrinfo callback error %s\n", uv_err_name(status));
46  return;
47  }
48 
49  char addr[17] = {'\0'};
50  uv_ip4_name((struct sockaddr_in*) res->ai_addr, addr, 16);
51  fprintf(stderr, "%s\n", addr);
52 
53  uv_connect_t *connect_req = (uv_connect_t*) malloc(sizeof(uv_connect_t));
54  uv_tcp_t *socket = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
56 
57  uv_tcp_connect(connect_req, socket, (const struct sockaddr*) res->ai_addr, on_connect);
58 
59  uv_freeaddrinfo(res);
60 }
61 
62 int main() {
64 
65  struct addrinfo hints;
66  hints.ai_family = PF_INET;
67  hints.ai_socktype = SOCK_STREAM;
68  hints.ai_protocol = IPPROTO_TCP;
69  hints.ai_flags = 0;
70 
71  uv_getaddrinfo_t resolver;
72  fprintf(stderr, "irc.freenode.net is... ");
73  int r = uv_getaddrinfo(loop, &resolver, on_resolved, "irc.freenode.net", "6667", &hints);
74 
75  if (r) {
76  fprintf(stderr, "getaddrinfo call error %s\n", uv_err_name(r));
77  return 1;
78  }
79  return uv_run(loop, UV_RUN_DEFAULT);
80 }
static mcore_handle handle
Definition: asm_mcore.c:8
#define NULL
Definition: cris-opc.c:27
#define r
Definition: crypto_rc6.c:12
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 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 socket
Definition: sflib.h:79
RZ_API void Ht_() free(HtName_(Ht) *ht)
Definition: ht_inc.c:130
voidpf void * buf
Definition: ioapi.h:138
int main(int argc, const char **argv)
Definition: main.c:340
void * malloc(size_t size)
Definition: malloc.c:123
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
#define PF_INET
Definition: sftypes.h:248
@ SOCK_STREAM
Definition: sftypes.h:224
int ssize_t
Definition: sftypes.h:39
Definition: unix.h:123
Definition: uv.h:1780
Definition: uv.h:547
uv_loop_t * loop
Definition: main.c:7
void on_connect(uv_connect_t *req, int status)
Definition: main.c:32
void on_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
Definition: main.c:13
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: main.c:8
void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
Definition: main.c:43
struct sockaddr_in addr
Definition: main.c:10
@ UV_RUN_DEFAULT
Definition: uv.h:255
UV_EXTERN int uv_tcp_connect(uv_connect_t *req, uv_tcp_t *handle, const struct sockaddr *addr, uv_connect_cb cb)
Definition: uv-common.c:345
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: stream.c:1555
UV_EXTERN int uv_ip4_name(const struct sockaddr_in *src, char *dst, size_t size)
Definition: uv-common.c:267
UV_EXTERN void uv_freeaddrinfo(struct addrinfo *ai)
Definition: getaddrinfo.c:223
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: core.c:365
UV_EXTERN const char * uv_err_name(int err)
Definition: uv-common.c:189
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: core.c:108
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: tcp.c:143
UV_EXTERN int uv_getaddrinfo(uv_loop_t *loop, uv_getaddrinfo_t *req, uv_getaddrinfo_cb getaddrinfo_cb, const char *node, const char *service, const struct addrinfo *hints)
Definition: getaddrinfo.c:141
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:763