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.

Classes

struct  write_req_t
 

Macros

#define DEFAULT_PORT   7000
 
#define DEFAULT_BACKLOG   128
 

Functions

void free_write_req (uv_write_t *req)
 
void alloc_buffer (uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
 
void on_close (uv_handle_t *handle)
 
void echo_write (uv_write_t *req, int status)
 
void echo_read (uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
 
void on_new_connection (uv_stream_t *server, int status)
 
int main ()
 

Variables

uv_loop_tloop
 
struct sockaddr_in addr
 

Macro Definition Documentation

◆ DEFAULT_BACKLOG

#define DEFAULT_BACKLOG   128

Definition at line 7 of file main.c.

◆ DEFAULT_PORT

#define DEFAULT_PORT   7000

Definition at line 6 of file main.c.

Function Documentation

◆ alloc_buffer()

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

Definition at line 23 of file main.c.

23  {
24  buf->base = (char*) malloc(suggested_size);
25  buf->len = suggested_size;
26 }
voidpf void * buf
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123

References malloc().

◆ echo_read()

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

Definition at line 39 of file main.c.

39  {
40  if (nread > 0) {
42  req->buf = uv_buf_init(buf->base, nread);
43  uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write);
44  return;
45  }
46  if (nread < 0) {
47  if (nread != UV_EOF)
48  fprintf(stderr, "Read error %s\n", uv_err_name(nread));
49  uv_close((uv_handle_t*) client, on_close);
50  }
51 
52  free(buf->base);
53 }
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
Definition: uv.h:525
void echo_write(uv_write_t *req, int status)
Definition: main.c:30
void on_close(uv_handle_t *handle)
Definition: main.c:28
UV_EXTERN int uv_write(uv_write_t *req, uv_stream_t *handle, const uv_buf_t bufs[], unsigned int nbufs, uv_write_cb cb)
Definition: stream.c:1492
UV_EXTERN uv_buf_t uv_buf_init(char *base, unsigned int len)
Definition: uv-common.c:157
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

References echo_write(), free(), malloc(), on_close(), req, uv_buf_init(), uv_close(), uv_err_name(), and uv_write().

◆ echo_write()

void echo_write ( uv_write_t req,
int  status 
)

Definition at line 32 of file main.c.

32  {
33  if (status) {
34  fprintf(stderr, "Write error %s\n", uv_strerror(status));
35  }
37 }
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
void free_write_req(uv_write_t *req)
Definition: main.c:19
UV_EXTERN const char * uv_strerror(int err)
Definition: uv-common.c:212

References free_write_req(), req, status, and uv_strerror().

◆ free_write_req()

void free_write_req ( uv_write_t req)

Definition at line 17 of file main.c.

17  {
18  write_req_t *wr = (write_req_t*) req;
19  free(wr->buf.base);
20  free(wr);
21 }
char * base
Definition: unix.h:124
uv_buf_t buf
Definition: worker.c:13

References uv_buf_t::base, write_req_t::buf, free(), and req.

◆ main()

int main ( void  )

Definition at line 72 of file main.c.

72  {
74 
75  uv_tcp_t server;
76  uv_tcp_init(loop, &server);
77 
78  uv_ip4_addr("0.0.0.0", DEFAULT_PORT, &addr);
79 
80  uv_tcp_bind(&server, (const struct sockaddr*)&addr, 0);
82  if (r) {
83  fprintf(stderr, "Listen error %s\n", uv_strerror(r));
84  return 1;
85  }
86  return uv_run(loop, UV_RUN_DEFAULT);
87 }
#define r
Definition: crypto_rc6.c:12
Definition: uv.h:547
void on_new_connection(uv_stream_t *server, int status)
Definition: main.c:50
uv_loop_t * loop
Definition: main.c:7
#define DEFAULT_PORT
Definition: main.c:6
#define DEFAULT_BACKLOG
Definition: main.c:7
struct sockaddr_in addr
Definition: main.c:10
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
@ UV_RUN_DEFAULT
Definition: uv.h:255
UV_EXTERN int uv_listen(uv_stream_t *stream, int backlog, uv_connection_cb cb)
Definition: stream.c:656
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: core.c:365
UV_EXTERN int uv_tcp_bind(uv_tcp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:277
UV_EXTERN int uv_tcp_init(uv_loop_t *, uv_tcp_t *handle)
Definition: tcp.c:143
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:763

References addr, DEFAULT_BACKLOG, DEFAULT_PORT, loop, on_new_connection(), r, uv_default_loop(), uv_ip4_addr(), uv_listen(), uv_run(), UV_RUN_DEFAULT, uv_strerror(), uv_tcp_bind(), and uv_tcp_init().

◆ on_close()

void on_close ( uv_handle_t handle)

Definition at line 28 of file main.c.

28  {
29  free(handle);
30 }
static mcore_handle handle
Definition: asm_mcore.c:8

References free(), and handle.

Referenced by echo_read(), and on_new_connection().

◆ on_new_connection()

void on_new_connection ( uv_stream_t server,
int  status 
)

Definition at line 55 of file main.c.

55  {
56  if (status < 0) {
57  fprintf(stderr, "New connection error %s\n", uv_strerror(status));
58  // error!
59  return;
60  }
61 
62  uv_tcp_t *client = (uv_tcp_t*) malloc(sizeof(uv_tcp_t));
63  uv_tcp_init(loop, client);
64  if (uv_accept(server, (uv_stream_t*) client) == 0) {
66  }
67  else {
68  uv_close((uv_handle_t*) client, on_close);
69  }
70 }
void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf)
Definition: main.c:8
void echo_read(uv_stream_t *client, ssize_t nread, const uv_buf_t *buf)
Definition: main.c:37
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_accept(uv_stream_t *server, uv_stream_t *client)
Definition: stream.c:591

References alloc_buffer(), echo_read(), loop, malloc(), on_close(), status, uv_accept(), uv_close(), uv_read_start(), uv_strerror(), and uv_tcp_init().

Variable Documentation

◆ addr

struct sockaddr_in addr

Definition at line 9 of file main.c.

Referenced by main(), on_read(), and on_resolved().

◆ loop

uv_loop_t* loop

Definition at line 9 of file main.c.