Rizin
unix-like reverse engineering framework and cli tools
main.c
Go to the documentation of this file.
1 #include <assert.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 
6 #include <uv.h>
7 
11 
12 void alloc_buffer(uv_handle_t *handle, size_t suggested_size, uv_buf_t *buf) {
13  buf->base = malloc(suggested_size);
14  buf->len = suggested_size;
15 }
16 
17 void on_read(uv_udp_t *req, ssize_t nread, const uv_buf_t *buf, const struct sockaddr *addr, unsigned flags) {
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 }
41 
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 }
96 
98  if (status) {
99  fprintf(stderr, "Send error %s\n", uv_strerror(status));
100  return;
101  }
102 }
103 
104 int main() {
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 }
lzma_index ** i
Definition: index.h:629
static mcore_handle handle
Definition: asm_mcore.c:8
#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
voidpf void * buf
Definition: ioapi.h:138
return memset(p, 0, total)
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 ip
static struct sockaddr static addrlen static backlog const void static flags void flags
Definition: sfsocketcall.h:123
static int
Definition: sfsocketcall.h:114
int ssize_t
Definition: sftypes.h:39
Definition: buffer.h:15
Definition: unix.h:123
Definition: uv.h:1780
Definition: uv.h:638
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
char buffer[1024]
Definition: main.c:8
struct sockaddr_in addr
Definition: main.c:10
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_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 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_udp_init(uv_loop_t *, uv_udp_t *handle)
Definition: uv-common.c:321
UV_EXTERN void uv_close(uv_handle_t *handle, uv_close_cb close_cb)
Definition: core.c:108
UV_EXTERN const char * uv_strerror(int err)
Definition: uv-common.c:212
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