-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathkademlua.c
761 lines (552 loc) · 15.1 KB
/
kademlua.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <lua.h>
#include <lualib.h>
#include <lauxlib.h>
#include <sys/socket.h>
#include <arpa/inet.h>
//#include <poll.h>
#include <sys/select.h>
#include <openssl/sha.h>
#include <sys/time.h>
#include <sys/errno.h>
#include "kademlua.h"
/* kademlua.c
* (C) 2009, Michael Meier
*/
#define DEFAULT_PORT 8000
#define HASH_SECT u_int32_t
/*lua_State *mstate;
int sock;
int port;
struct sockaddr_in mysockaddr;
int readfromstdin;
*/
void notdecoded(lua_State *L) {
lua_pushstring(L, "decoded");
lua_pushboolean(L, 0);
lua_settable(L, -3);
}
int recvpacket(lua_State *L, struct eventstate *estate) {
// TODO: better setting and handling of buffer size in recvfrom
char sbuf[16384];
//printf("socket ready to read\n");
int sock = estate->sock;
struct sockaddr_in from;
socklen_t len = sizeof(from);
int res = recvfrom(sock, sbuf, 16384, 0,
(struct sockaddr *) &from, &len);
if (res == -1) {
perror("kademlua: read");
return 0;
}
//int packlen = res;
char ascaddr[32] = {0};
inet_ntop(AF_INET, &(from.sin_addr), ascaddr, sizeof(ascaddr));
lua_newtable(L);
lua_pushstring(L, "type");
lua_pushstring(L, "sock");
lua_settable(L, -3);
lua_pushstring(L, "raw");
lua_pushlstring(L, sbuf, res);
lua_settable(L, -3);
lua_pushstring(L, "from");
lua_newtable(L);
lua_pushstring(L, "addr");
lua_pushstring(L, ascaddr);
lua_settable(L, -3);
lua_pushstring(L, "port");
lua_pushnumber(L, ntohs(from.sin_port));
lua_settable(L, -3);
lua_settable(L, -3);
//printf("addr stored\n");
/*int pos = 0;
if (packlen < 1) {
notdecoded(L);
return 1;
} else {
if (sbuf[0] != 1) {
// wrong header type
notdecoded(L);
return 1;
}
// we need 21 more bytes
pos++;
if (packlen < (pos + 21)) {
notdecoded(L);
return 1;
}
char *idptr = &sbuf[pos];
pos = pos + 20;
unsigned char call = sbuf[pos];
lua_pushstring(L, "message");
lua_newtable(L);
lua_pushstring(L, "id");
lua_pushlstring(L, idptr, 20);
lua_settable(L, -3);
lua_pushstring(L, "from");
lua_newtable(L);
lua_pushstring(L, "addr");
lua_pushstring(L, ascaddr);
lua_settable(L, -3);
lua_pushstring(L, "port");
lua_pushnumber(L, ntohs(from.sin_port));
lua_settable(L, -3);
lua_pushstring(L, "id");
lua_pushlstring(L, idptr, 20);
lua_settable(L, -3);
char unique[64];
snprintf(unique, 64, "%s:%i:", ascaddr, ntohs(from.sin_port));
{
lua_pushstring(L, "unique");
int where = strlen(unique);
if (where <= 44) {
memcpy(&unique[where], idptr, 20);
lua_pushlstring(L, unique, where + 20);
} else {
lua_pushstring(L, "xxx");
}
lua_settable(L, -3);
}
lua_settable(L, -3);
lua_pushstring(L, "call");
lua_pushnumber(L, call);
lua_settable(L, -3);
lua_settable(L, -3);
lua_pushstring(L, "decoded");
lua_pushboolean(L, 1);
lua_settable(L, -3);
}*/
return 1;
}
int getevent(lua_State *L) {
if (!lua_isuserdata(L, 1))
return 0;
struct eventstate *estate = lua_touserdata(L, 1);
/* because i remove the userdata element from the stack i certainly
do not have to change the code below. it's even not that bad as
there will only few arguments to getevent(), e.g. 1 or 2.*/
lua_remove(L, 1);
int nargs = lua_gettop(L);
double dtimeout;
if (nargs >= 1) {
dtimeout = lua_tonumber(L, 1);
} else {
dtimeout = 0;
}
struct timeval timeout;
struct timeval *timeout_ptr;
if (dtimeout == 0.0) {
timeout_ptr = NULL;
} else {
timeout.tv_sec = (int) dtimeout;
timeout.tv_usec = (int) ((dtimeout - (double) ((int) dtimeout)) * 1e6);
timeout_ptr = &timeout;
}
if (nargs >= 2) {
int packslen = lua_objlen(L, 2);
for (int i = 1; i <= packslen; i++) {
lua_pushnumber(L, i);
lua_gettable(L, 2);
// -> packet table on top of stack
//printf("got packet table\n");
lua_pushstring(L, "to");
lua_gettable(L, -2);
// -> to field on top of stack
//printf("got to field\n");
lua_pushstring(L, "addr");
lua_gettable(L, -2);
// -> ip addr on top of stack
//printf("ip addr string on top of stack\n");
const char *ascaddr = lua_tostring(L, -1);
if (ascaddr == NULL) {
lua_pushstring(L, "need a string to represent and IP address");
lua_error(L);
}
struct sockaddr_in addr;
addr.sin_family = AF_INET;
if (inet_pton(AF_INET, ascaddr, &(addr.sin_addr)) == -1) {
lua_pushstring(L, "invalid IP address");
lua_error(L);
}
//printf("got addr\n");
lua_pop(L, 1);
// -> to field on top of stack
lua_pushstring(L, "port");
lua_gettable(L, -2);
// -> port on top of stack
addr.sin_port = htons(lua_tonumber(L, -1));
lua_pop(L, 1);
// -> to field on top of stack
lua_pop(L, 1);
// -> packet table on top of stack
lua_pushstring(L, "raw");
lua_gettable(L, -2);
int sendres;
if (!(lua_isnil(L, -1))) {
size_t len;
const char *raw = lua_tolstring(L, -1, &len);
sendres = sendto(estate->sock, raw, len, 0,
(struct sockaddr*) &addr, sizeof(addr));
} else {
sendres = sendto(estate->sock, "yeehaw!", 7, 0,
(struct sockaddr*) &addr, sizeof(addr));
}
lua_pop(L, 1);
if (sendres == -1) {
char *errmsg = strerror(errno);
lua_pushstring(L, "errmsg");
lua_pushstring(L, errmsg);
lua_settable(L, -3);
lua_pushstring(L, "errno");
lua_pushnumber(L, errno);
lua_settable(L, -3);
}
lua_pushstring(L, "res");
lua_pushnumber(L, sendres);
lua_settable(L, -3);
lua_pop(L, 1);
// -> last arg on top of stack
}
}
fd_set rset;
FD_ZERO(&rset);
if (estate->readfromstdin)
FD_SET(0, &rset);
FD_SET(estate->sock, &rset);
// determining max fd = argh! i hate apple for not being able to produce
// a poll() which works on a terminal.
select(estate->sock + 1, &rset, NULL, NULL, timeout_ptr);
if (FD_ISSET(0, &rset)) {
char buf[1024];
int bytesread = read(0, buf, 1023);
if (bytesread == -1) {
lua_newtable(L);
lua_pushstring(L, "type");
lua_pushstring(L, "stdin");
lua_settable(L, -3);
lua_pushstring(L, "errno");
lua_pushnumber(L, (double) errno);
lua_settable(L, -3);
lua_pushstring(L, "errormessage");
lua_pushstring(L, strerror(errno));
lua_settable(L, -3);
return 1;
}
buf[bytesread] = 0;
if (bytesread == 0)
estate->readfromstdin = 0;
//printf("read: %s", buf);
//lua_pushstring(mstate, buf);
lua_newtable(L);
lua_pushstring(L, "type");
lua_pushstring(L, "stdin");
lua_settable(L, -3);
lua_pushstring(L, "raw");
lua_pushstring(L, buf);
lua_settable(L, -3);
return 1;
}
if (FD_ISSET(estate->sock, &rset)) {
return recvpacket(L, estate);
}
return 0;
}
int sha1(lua_State* L) {
int nargs = lua_gettop(L);
if (nargs != 1) {
lua_pushstring(L, "sha1() needs exactly one argument");
lua_error(L);
}
if (!(lua_isstring(L, 1))) {
lua_pushstring(L, "argument needs to be a string");
lua_error(L);
}
size_t len;
const unsigned char *buf = (const unsigned char*) lua_tolstring(L, 1, &len);
unsigned char *res = SHA1(buf, len, NULL);
lua_pushlstring(L, (char *) res, 20);
return 1;
}
char nibbledigit(char digit) {
if ((digit >= 0) & (digit < 10)) {
return digit + 48;
} else if ((digit >= 10) & (digit < 16)) {
return digit + 87;
} else {
return 'x';
}
}
int tohex(lua_State *L) {
int nargs = lua_gettop(L);
if (nargs != 1) {
lua_pushstring(L, "tohex() needs exactly one argument");
lua_error(L);
}
if (!(lua_isstring(L, 1))) {
lua_pushstring(L, "argument needs to be a string");
lua_error(L);
}
size_t len;
const unsigned char *inbuf = (const unsigned char*) lua_tolstring(L, 1, &len);
char* outbuf = malloc(len * 2);
if (outbuf == NULL) {
lua_pushstring(L, "could not allocate memory");
lua_error(L);
}
for (int i = 0; i < len; i++) {
outbuf[i*2 + 1] = nibbledigit(inbuf[i] & 0xf);
outbuf[i*2] = nibbledigit((inbuf[i] >> 4) & 0xf);
}
lua_pushlstring(L, outbuf, len*2);
free(outbuf);
return 1;
}
char fromdigit(char digit) {
if ((digit >= 48) & (digit < 58)) {
return digit -48;
} else if ((digit >= 97) & (digit <= 102)) {
return digit - 87;
} else {
return 0;
}
}
int fromhex(lua_State *L) {
int nargs = lua_gettop(L);
if (nargs != 1) {
lua_pushstring(L, "fromhex() needs exactly one argument");
lua_error(L);
}
if (!(lua_isstring(L, 1))) {
lua_pushstring(L, "argument needs to be a string");
lua_error(L);
}
size_t len;
const unsigned char *inbuf = (const unsigned char*) lua_tolstring(L, 1, &len);
unsigned char* outbuf = calloc((len / 2) + (len % 2), sizeof(unsigned char));
if (outbuf == NULL) {
lua_pushstring(L, "could not allocate memory");
lua_error(L);
}
unsigned char shift;
int offset;
if (len % 2) {
outbuf[0] = '0';
shift = 0;
offset = 1;
} else {
shift = 1;
offset = 0;
}
for (int i = 0; i < len; i++) {
unsigned char nibble = fromdigit(inbuf[i]);
if (shift) {
outbuf[(i + offset) / 2] |= (nibble << 4);
} else {
outbuf[(i + offset) / 2] |= (nibble);
}
shift = shift ^ 1;
}
lua_pushlstring(L, (char *) outbuf, (len/2) + (len % 2));
free(outbuf);
return 1;
}
int xor(lua_State *L) {
int nargs = lua_gettop(L);
if (nargs != 2) {
lua_pushstring(L, "xor() needs exactly two arguments");
lua_error(L);
}
if (!(lua_isstring(L, 1))) {
lua_pushstring(L, "argument 1 needs to be a string");
lua_error(L);
}
if (!(lua_isstring(L, 2))) {
lua_pushstring(L, "argument 2 needs to be a string");
lua_error(L);
}
size_t len1;
const unsigned char *inbuf1 = (const unsigned char*) lua_tolstring(L, 1, &len1);
size_t len2;
const unsigned char *inbuf2 = (const unsigned char*) lua_tolstring(L, 2, &len2);
if (len1 != len2) {
lua_pushstring(L, "the strings need to be of the same length");
lua_error(L);
}
if ((len1 % sizeof(HASH_SECT)) != 0) {
lua_pushstring(L, "the strings need to be divideable by sizeof(HASH_SECT)");
lua_error(L);
}
HASH_SECT *sect1 = (HASH_SECT*) inbuf1;
HASH_SECT *sect2 = (HASH_SECT*) inbuf2;
unsigned char* outbuf = malloc(len1);
if (outbuf == NULL) {
lua_pushstring(L, "could not allocate memory");
lua_error(L);
}
HASH_SECT *outsect = (HASH_SECT*) outbuf;
for (int i = 0; i < (len1 / sizeof(HASH_SECT)); i++) {
outsect[i] = sect1[i] ^ sect2[i];
//printf("%x ^ %x == %x\n", sect1[i], sect2[i], sect1[i] ^ sect2[i]);
}
lua_pushlstring(L, (char*) outbuf, len1);
free(outbuf);
return 1;
}
int getbucketno(lua_State* L) {
int nargs = lua_gettop(L);
if (nargs != 1) {
lua_pushstring(L, "getbucketno() needs exactly one argument");
lua_error(L);
}
if (!(lua_isstring(L, 1))) {
lua_pushstring(L, "argument needs to be a string");
lua_error(L);
}
size_t len;
const unsigned char *buf = (const unsigned char*) lua_tolstring(L, 1, &len);
for (int i = 0; i < len; i++) {
unsigned char bit = 1;
// we have found the byte with the first bit set (in the string as a whole)
unsigned char byte = buf[i];
if (byte) {
while (!(byte & 0x80)) {
byte = byte << 1;
bit++;
}
lua_pushnumber(L, (double) (i * 8) + (double) bit);
return 1;
}
}
lua_pushnumber(L, 161);
return 1;
// the error will not be in your face
//lua_pushstring(L, "no bit set to one found");
//lua_error(L);
return 0;
}
int ectime(lua_State *L) {
struct timeval t;
gettimeofday(&t, NULL);
double tm = t.tv_sec + (1e-6 * t.tv_usec);
lua_pushnumber(L, tm);
return 1;
}
int initestate(lua_State *mstate) {
if (lua_gettop(mstate) != 1) {
lua_pushstring(mstate, "initestate() needs one argument table");
lua_error(mstate);
}
// on stack position 2
struct eventstate *estate = lua_newuserdata(mstate, sizeof(struct eventstate));
if (!estate)
return 0;
estate->mstate = mstate;
estate->readfromstdin = 1;
estate->port = DEFAULT_PORT;
//lua_getglobal(mstate, "port");
lua_getfield(mstate, 1, "port");
if (!lua_isnumber(mstate, -1)) {
printf("port variable not correctly set in params.lua");
exit(1);
}
estate->port = (u_int16_t) lua_tonumber(mstate, -1);
lua_settop(mstate, 2);
// => only argtable and userdata
if ((estate->sock = socket(PF_INET, SOCK_DGRAM, 0)) == -1) {
perror("kademlua: socket");
exit(1);
}
{
int res;
int on = 1;
if ((res = setsockopt(estate->sock, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on))) == -1) {
perror("kademlua: setsockopt");
exit(1);
}
}
/* snip */
// remove the argtable
lua_remove(mstate, 1);
// and be sure...
lua_settop(mstate, 1);
{
int res;
estate->mysockaddr.sin_family = AF_INET;
estate->mysockaddr.sin_addr.s_addr = INADDR_ANY;
// TODO: move somewhere else
//mysockaddr.sin_addr.s_addr = inet_addr("192.168.1.5");
estate->mysockaddr.sin_port = htons(estate->port);
if ((res = bind(estate->sock,
(struct sockaddr*) &(estate->mysockaddr),
sizeof(estate->mysockaddr))) == -1) {
perror("kademlua: bind");
exit(1);
}
socklen_t len = sizeof(estate->mysockaddr);
res = getsockname(estate->sock,
(struct sockaddr*) &(estate->mysockaddr),
&len);
if (res == -1) {
perror("kademlua: getsockname");
}
char ascaddr[32];
inet_ntop(AF_INET, &(estate->mysockaddr.sin_addr), ascaddr, 32);
printf("bound to %s:%i\n", ascaddr, ntohs(estate->mysockaddr.sin_port));
}
//return estate;
return 1;
}
int initec(lua_State *L) {
luaL_openlib(L, "ec", eclib, 0);
return 1;
}
static const struct luaL_reg eclib[] = {
{"getevent", getevent},
{"sha1", sha1},
{"tohex", tohex},
{"fromhex", fromhex},
{"xor", xor},
{"getbucketno", getbucketno},
{"time", ectime},
{"initestate", initestate},
{NULL, NULL}
};
struct eventstate* init(int argc, char **argv) {
lua_State *mstate = lua_open();
if (!mstate)
return NULL;
luaL_openlibs(mstate);
luaL_openlib(mstate, "ec", eclib, 0);
// clear stack after openlib pushed the ec lib
lua_settop(mstate, 0);
pushargv(mstate, argc, argv);
int res = luaL_loadfile(mstate, "params.lua");
if (res != 0) {
printf("error loading params.lua\n");
exit(1);
}
lua_call(mstate, 0, 0);
lua_newtable(mstate);
lua_pushstring(mstate, "port");
lua_getglobal(mstate, "port");
lua_settable(mstate, -3);
int ret = initestate(mstate);
if (ret == 0) {
printf("initestate failed.");
exit(1);
}
// initestate pushed userdata
return (struct eventstate*) lua_touserdata(mstate, 1);
}
void pushargv(lua_State *L, int argc, char **argv) {
lua_newtable(L);
for (int i = 0; i < argc; i++) {
lua_pushnumber(L, i);
lua_pushstring(L, argv[i]);
lua_settable(L, -3);
}
lua_setglobal(L, "argv");
}