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

Go to the source code of this file.

Functions

void alloc_buffer (uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
 
void on_read (uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
 
void on_connect (uv_connect_t *req, int status)
 
void on_resolved (uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
 
int main ()
 

Variables

uv_loop_tloop
 

Function Documentation

◆ alloc_buffer()

void alloc_buffer ( uv_handle_t handle,
size_t  suggested_size,
uv_buf_t buf 
)

Definition at line 8 of file main.c.

8  {
9  buf->base = malloc(suggested_size);
10  buf->len = suggested_size;
11 }
voidpf void * buf
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123

References malloc().

Referenced by main(), make_discover_msg(), on_connect(), and on_new_connection().

◆ main()

int main ( void  )

Definition at line 62 of file main.c.

62  {
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 }
#define r
Definition: crypto_rc6.c:12
#define PF_INET
Definition: sftypes.h:248
@ SOCK_STREAM
Definition: sftypes.h:224
uv_loop_t * loop
Definition: main.c:7
void on_resolved(uv_getaddrinfo_t *resolver, int status, struct addrinfo *res)
Definition: main.c:43
@ UV_RUN_DEFAULT
Definition: uv.h:255
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 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

References loop, on_resolved(), PF_INET, r, SOCK_STREAM, uv_default_loop(), uv_err_name(), uv_getaddrinfo(), uv_run(), and UV_RUN_DEFAULT.

◆ on_connect()

void on_connect ( uv_connect_t req,
int  status 
)

Definition at line 32 of file main.c.

32  {
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 }
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
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
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
UV_EXTERN int uv_read_start(uv_stream_t *, uv_alloc_cb alloc_cb, uv_read_cb read_cb)
Definition: stream.c:1555

References alloc_buffer(), free(), on_read(), req, status, uv_err_name(), and uv_read_start().

Referenced by on_resolved().

◆ on_read()

void on_read ( uv_stream_t client,
ssize_t  nread,
const uv_buf_t buf 
)

Definition at line 13 of file main.c.

13  {
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 }
#define NULL
Definition: cris-opc.c:27
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: core.c:108

References free(), malloc(), NULL, uv_close(), and uv_err_name().

Referenced by main(), on_connect(), on_open(), and on_write().

◆ on_resolved()

void on_resolved ( uv_getaddrinfo_t resolver,
int  status,
struct addrinfo *  res 
)

Definition at line 43 of file main.c.

43  {
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 }
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
Definition: uv.h:547
void on_connect(uv_connect_t *req, int status)
Definition: main.c:32
struct sockaddr_in addr
Definition: main.c:10
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_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_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: tcp.c:143

References addr, loop, malloc(), on_connect(), socket, status, uv_err_name(), uv_freeaddrinfo(), uv_ip4_name(), uv_tcp_connect(), and uv_tcp_init().

Referenced by main().

Variable Documentation

◆ loop

uv_loop_t* loop

Definition at line 6 of file main.c.