Rizin
unix-like reverse engineering framework and cli tools
main.c File Reference
#include <assert.h>
#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_udp_t *req, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags)
 
uv_buf_t make_discover_msg ()
 
void on_send (uv_udp_send_t *req, int status)
 
int main ()
 

Variables

uv_loop_tloop
 
uv_udp_t send_socket
 
uv_udp_t recv_socket
 

Function Documentation

◆ alloc_buffer()

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

Definition at line 12 of file main.c.

12  {
13  buf->base = malloc(suggested_size);
14  buf->len = suggested_size;
15 }
voidpf void * buf
Definition: ioapi.h:138
void * malloc(size_t size)
Definition: malloc.c:123

References malloc().

◆ main()

int main ( void  )

Definition at line 104 of file main.c.

104  {
105  loop = uv_default_loop();
106 
108  struct sockaddr_in recv_addr;
109  uv_ip4_addr("0.0.0.0", 68, &recv_addr);
110  uv_udp_bind(&recv_socket, (const struct sockaddr *)&recv_addr, UV_UDP_REUSEADDR);
112 
114  struct sockaddr_in broadcast_addr;
115  uv_ip4_addr("0.0.0.0", 0, &broadcast_addr);
116  uv_udp_bind(&send_socket, (const struct sockaddr *)&broadcast_addr, 0);
118 
119  uv_udp_send_t send_req;
120  uv_buf_t discover_msg = make_discover_msg();
121 
122  struct sockaddr_in send_addr;
123  uv_ip4_addr("255.255.255.255", 67, &send_addr);
124  uv_udp_send(&send_req, &send_socket, &discover_msg, 1, (const struct sockaddr *)&send_addr, on_send);
125 
126  return uv_run(loop, UV_RUN_DEFAULT);
127 }
Definition: unix.h:123
uv_loop_t * loop
Definition: main.c:7
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_send(uv_udp_send_t *req, int status)
Definition: main.c:97
uv_udp_t recv_socket
Definition: main.c:10
uv_udp_t send_socket
Definition: main.c:9
uv_buf_t make_discover_msg()
Definition: main.c:42
UV_EXTERN int uv_ip4_addr(const char *ip, int port, struct sockaddr_in *addr)
Definition: uv-common.c:221
UV_EXTERN int uv_udp_bind(uv_udp_t *handle, const struct sockaddr *addr, unsigned int flags)
Definition: uv-common.c:326
UV_EXTERN int uv_udp_recv_start(uv_udp_t *handle, uv_alloc_cb alloc_cb, uv_udp_recv_cb recv_cb)
Definition: uv-common.c:468
@ UV_RUN_DEFAULT
Definition: uv.h:255
@ UV_UDP_REUSEADDR
Definition: uv.h:611
UV_EXTERN int uv_udp_send(uv_udp_send_t *req, uv_udp_t *handle, const uv_buf_t bufs[], unsigned int nbufs, const struct sockaddr *addr, uv_udp_send_cb send_cb)
Definition: uv-common.c:438
UV_EXTERN int uv_run(uv_loop_t *, uv_run_mode mode)
Definition: core.c:365
UV_EXTERN int uv_udp_init(uv_loop_t *, uv_udp_t *handle)
Definition: uv-common.c:321
UV_EXTERN uv_loop_t * uv_default_loop(void)
Definition: uv-common.c:763
UV_EXTERN int uv_udp_set_broadcast(uv_udp_t *handle, int on)
Definition: udp.c:1128

References alloc_buffer(), loop, make_discover_msg(), on_read(), on_send(), recv_socket, send_socket, uv_default_loop(), uv_ip4_addr(), uv_run(), UV_RUN_DEFAULT, uv_udp_bind(), uv_udp_init(), uv_udp_recv_start(), UV_UDP_REUSEADDR, uv_udp_send(), and uv_udp_set_broadcast().

◆ make_discover_msg()

uv_buf_t make_discover_msg ( )

Definition at line 42 of file main.c.

42  {
44  alloc_buffer(NULL, 256, &buffer);
45  memset(buffer.base, 0, buffer.len);
46 
47  // BOOTREQUEST
48  buffer.base[0] = 0x1;
49  // HTYPE ethernet
50  buffer.base[1] = 0x1;
51  // HLEN
52  buffer.base[2] = 0x6;
53  // HOPS
54  buffer.base[3] = 0x0;
55  // XID 4 bytes
56  buffer.base[4] = (unsigned int) random();
57  // SECS
58  buffer.base[8] = 0x0;
59  // FLAGS
60  buffer.base[10] = 0x80;
61  // CIADDR 12-15 is all zeros
62  // YIADDR 16-19 is all zeros
63  // SIADDR 20-23 is all zeros
64  // GIADDR 24-27 is all zeros
65  // CHADDR 28-43 is the MAC address, use your own
66  buffer.base[28] = 0xe4;
67  buffer.base[29] = 0xce;
68  buffer.base[30] = 0x8f;
69  buffer.base[31] = 0x13;
70  buffer.base[32] = 0xf6;
71  buffer.base[33] = 0xd4;
72  // SNAME 64 bytes zero
73  // FILE 128 bytes zero
74  // OPTIONS
75  // - magic cookie
76  buffer.base[236] = 99;
77  buffer.base[237] = 130;
78  buffer.base[238] = 83;
79  buffer.base[239] = 99;
80 
81  // DHCP Message type
82  buffer.base[240] = 53;
83  buffer.base[241] = 1;
84  buffer.base[242] = 1; // DHCPDISCOVER
85 
86  // DHCP Parameter request list
87  buffer.base[243] = 55;
88  buffer.base[244] = 4;
89  buffer.base[245] = 1;
90  buffer.base[246] = 3;
91  buffer.base[247] = 15;
92  buffer.base[248] = 6;
93 
94  return buffer;
95 }
#define NULL
Definition: cris-opc.c:27
return memset(p, 0, total)
static int
Definition: sfsocketcall.h:114
Definition: buffer.h:15
char buffer[1024]
Definition: main.c:8

References alloc_buffer(), buffer, int, memset(), and NULL.

Referenced by main().

◆ on_read()

void on_read ( uv_udp_t req,
ssize_t  nread,
const uv_buf_t buf,
const struct sockaddr addr,
unsigned  flags 
)

Definition at line 17 of file main.c.

17  {
18  if (nread < 0) {
19  fprintf(stderr, "Read error %s\n", uv_err_name(nread));
21  free(buf->base);
22  return;
23  }
24 
25  char sender[17] = { 0 };
26  uv_ip4_name((const struct sockaddr_in*) addr, sender, 16);
27  fprintf(stderr, "Recv from %s\n", sender);
28 
29  // ... DHCP specific code
30  unsigned int *as_integer = (unsigned int*)buf->base;
31  unsigned int ipbin = ntohl(as_integer[4]);
32  unsigned char ip[4] = {0};
33  int i;
34  for (i = 0; i < 4; i++)
35  ip[i] = (ipbin >> i*8) & 0xff;
36  fprintf(stderr, "Offered IP %d.%d.%d.%d\n", ip[3], ip[2], ip[1], ip[0]);
37 
38  free(buf->base);
40 }
lzma_index ** i
Definition: index.h:629
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
#define ip
struct sockaddr_in addr
Definition: main.c:10
UV_EXTERN int uv_ip4_name(const struct sockaddr_in *src, char *dst, size_t size)
Definition: uv-common.c:267
UV_EXTERN int uv_udp_recv_stop(uv_udp_t *handle)
Definition: uv-common.c:478
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 addr, free(), i, ip, NULL, req, uv_close(), uv_err_name(), uv_ip4_name(), and uv_udp_recv_stop().

◆ on_send()

void on_send ( uv_udp_send_t req,
int  status 
)

Definition at line 97 of file main.c.

97  {
98  if (status) {
99  fprintf(stderr, "Send error %s\n", uv_strerror(status));
100  return;
101  }
102 }
static const char struct stat static buf struct stat static buf static vhangup int status
Definition: sflib.h:145
UV_EXTERN const char * uv_strerror(int err)
Definition: uv-common.c:212

References status, and uv_strerror().

Referenced by main().

Variable Documentation

◆ loop

uv_loop_t* loop

Definition at line 8 of file main.c.

◆ recv_socket

uv_udp_t recv_socket

Definition at line 10 of file main.c.

Referenced by main().

◆ send_socket

uv_udp_t send_socket

Definition at line 9 of file main.c.

Referenced by main().