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 PIPENAME   "/tmp/echo.sock"
 

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 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)
 
void remove_sock (int sig)
 
int main ()
 

Variables

uv_loop_tloop
 

Macro Definition Documentation

◆ PIPENAME

#define PIPENAME   "/tmp/echo.sock"

Definition at line 9 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 25 of file main.c.

25  {
26  buf->base = malloc(suggested_size);
27  buf->len = suggested_size;
28 }
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 37 of file main.c.

37  {
38  if (nread > 0) {
40  req->buf = uv_buf_init(buf->base, nread);
41  uv_write((uv_write_t*) req, client, &req->buf, 1, echo_write);
42  return;
43  }
44 
45  if (nread < 0) {
46  if (nread != UV_EOF)
47  fprintf(stderr, "Read error %s\n", uv_err_name(nread));
48  uv_close((uv_handle_t*) client, NULL);
49  }
50 
51  free(buf->base);
52 }
#define NULL
Definition: cris-opc.c:27
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
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(), NULL, req, uv_buf_init(), uv_close(), uv_err_name(), and uv_write().

Referenced by on_new_connection().

◆ echo_write()

void echo_write ( uv_write_t req,
int  status 
)

Definition at line 30 of file main.c.

30  {
31  if (status < 0) {
32  fprintf(stderr, "Write error %s\n", uv_err_name(status));
33  }
35 }
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

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

Referenced by echo_read().

◆ free_write_req()

void free_write_req ( uv_write_t req)

Definition at line 19 of file main.c.

19  {
20  write_req_t *wr = (write_req_t*) req;
21  free(wr->buf.base);
22  free(wr);
23 }
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.

Referenced by echo_write(), on_file_write(), and on_stdout_write().

◆ main()

int main ( void  )

Definition at line 76 of file main.c.

76  {
78 
79  uv_pipe_t server;
80  uv_pipe_init(loop, &server, 0);
81 
82  signal(SIGINT, remove_sock);
83 
84  int r;
85  if ((r = uv_pipe_bind(&server, PIPENAME))) {
86  fprintf(stderr, "Bind error %s\n", uv_err_name(r));
87  return 1;
88  }
89  if ((r = uv_listen((uv_stream_t*) &server, 128, on_new_connection))) {
90  fprintf(stderr, "Listen error %s\n", uv_err_name(r));
91  return 2;
92  }
93  return uv_run(loop, UV_RUN_DEFAULT);
94 }
#define r
Definition: crypto_rc6.c:12
static static fork const void static count static fd const char static mode const char static pathname const char static path const char static dev const char static group static getpid static getuid void void static data static pause const char static mode static sync const char const char static newpath const char static pathname unsigned long static filedes void static end_data_segment signal
Definition: sflib.h:79
Definition: uv.h:767
void on_new_connection(uv_stream_t *server, int status)
Definition: main.c:50
uv_loop_t * loop
Definition: main.c:7
void remove_sock(int sig)
Definition: main.c:70
#define PIPENAME
Definition: main.c:9
@ 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_pipe_init(uv_loop_t *, uv_pipe_t *handle, int ipc)
Definition: pipe.c:33
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: core.c:365
UV_EXTERN int uv_pipe_bind(uv_pipe_t *handle, const char *name)
Definition: pipe.c:43
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:763

References loop, on_new_connection(), PIPENAME, r, remove_sock(), signal, uv_default_loop(), uv_err_name(), uv_listen(), uv_pipe_bind(), uv_pipe_init(), uv_run(), and UV_RUN_DEFAULT.

◆ on_new_connection()

void on_new_connection ( uv_stream_t server,
int  status 
)

Definition at line 54 of file main.c.

54  {
55  if (status == -1) {
56  // error!
57  return;
58  }
59 
60  uv_pipe_t *client = (uv_pipe_t*) malloc(sizeof(uv_pipe_t));
61  uv_pipe_init(loop, client, 0);
62  if (uv_accept(server, (uv_stream_t*) client) == 0) {
64  }
65  else {
66  uv_close((uv_handle_t*) client, NULL);
67  }
68 }
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(), NULL, status, uv_accept(), uv_close(), uv_pipe_init(), and uv_read_start().

◆ remove_sock()

void remove_sock ( int  sig)

Definition at line 70 of file main.c.

70  {
71  uv_fs_t req;
73  exit(0);
74 }
Definition: uv.h:1306
UV_EXTERN int uv_fs_unlink(uv_loop_t *loop, uv_fs_t *req, const char *path, uv_fs_cb cb)
Definition: fs.c:2020

References test-lz4-list::exit, loop, NULL, PIPENAME, req, and uv_fs_unlink().

Referenced by main().

Variable Documentation

◆ loop

uv_loop_t* loop

Definition at line 12 of file main.c.