-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathservice.cpp
660 lines (618 loc) · 21.2 KB
/
service.cpp
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
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
#include <service>
#include <timers>
#include <os> // Literally! \o/
#include <cstdio>
#include <elle/log.hh>
ELLE_LOG_COMPONENT("foo");
#include <reactor/scheduler.hh>
#include <reactor/mutex.hh>
#include <reactor/backend/boost_context/backend.hh>
#include <reactor/network/udp-socket.hh>
#include <reactor/network/buffer.hh>
#include <boost/filesystem.hpp>
#include <boost/algorithm/string.hpp>
#include <infinit/model/doughnut/Doughnut.hh>
#include <infinit/model/doughnut/consensus/Paxos.hh>
#include <infinit/overlay/kelips/Kelips.hh>
#include <infinit/storage/Memory.hh>
reactor::Scheduler* sched = 0;
bool scheduler_sleeping = false;
void sched_step()
{
scheduler_sleeping = false;
while (sched->step())
;
scheduler_sleeping = true;
//Timers::oneshot(std::chrono::milliseconds(100), [](int) { sched_step();});
}
void checked_step(int)
{
if (!reactor::scheduler().current())
sched_step();
else
{
Timers::oneshot(std::chrono::microseconds(100), checked_step);
}
}
void wake_scheduler()
{
if (true) // scheduler_sleeping)
{
scheduler_sleeping = false;
Timers::oneshot(std::chrono::microseconds(100), checked_step);
}
}
static const char userkey[] = "{\"rsa\" : \"MIIEowIBAAKCAQEAxAtcxUwkLs1m2cDOe7WtFxAr2BR4rsDLC9UlMlW1e/DJi4ShgGI8mx7pe7i1EvpYc0H3vQ/c01Q0CppZcZCRPFA3yhpYVjsPYDktesXG4OtHHGe5KDo+U+nFBY6lcBFqeKTZdKmFHNaNy+JhOHLjPPrJrqamsac/S1B8GGPm9Z3hqmfNkvOalgXfao+KLLnJLB48in6QvUMlNIplzI8ZM5/2D8jBm12jybrkaWNmD0Dq1Bx+PGwGgpeR29un09pyg4a/7AsZeJdO0VXKDZUpRR4SHaXFMkG3yiOO2iR67pOkteq1VtXEQPLaIl9aN5fo9NcQQIKrhce3gpO17lA9owIDAQABAoIBAQCvIPtZ4N/9003Kvnt2deBPVwnjuL3qVp3MTzcwVPKP6pURBoWDe75qUF4BQQq5DlzPcaHPCgmZ24G16xZ15dBoUbzU1V4OgioFKm7fWyiDqopW7K2yKv2c1ptDkJ9nkpdLePAtUHZyQZRgCzYQSEmJIvviAkutLhvTuu1wmGYtCLG9CRQJ2i0M1e3y54JVavr/JX0VRZtqvWkN6s9pcXKfz86buNEI8jU0EPq2gO0YCT9eahdIaZtkn4vmRluAMnYfs+Cacc8e8ZvglvH+fDvLCSlrK96/tG0dRIAzfN+A8PJ82ojkyhfBVSKFHYK/8GyIrEtBdU0v/bOTwM8gvS35AoGBAOV8ZdXyscSIe+tmpHiEIf3+R5AdRAAbd66tyjiTg1WyFpKsOq7O9NoxxCcw/5Jc1IfGtZ7FmjWCp5coIbcRkOXza84Q+apGzMK7zeWfeCsbs55mQ5aRDnHYx4/G0lonoPQNv7RVxiaEzm+VgeXvqHBZYKUscf8A9LpMhjOwlML1AoGBANqx2ZOH3YN7qUIhSaZIYiFJKnU7J2aYfqqEMpdmVazbzyy9qnLwE5AUBoDy/OrGuW/D6YNeT6ISJUfBF7JU71NeAdXY3wcYN2gOeD1mUNNdbMs2c/vY0gDQ6USG9gaaWmrgzrpTop2ILFKc6+b4gAfaL3Zz7yrbrgCVuvCCdg83AoGAaasjNSXAZ0+1R8qGlxu4jzzj9N8U7bu4G03Y3L5H7lDHhhgaGV4gbswVlzo/pERsdGyyOn6gqF0WEEshYyuKfefdTxCP9bEOHejeQQpyCd+CkMBkBNOcRB3enjydpXez7EzcZgxM5nWmnMjJ/HejJsBw+P2DLDljdtk/vlNj3HUCgYAqplalU/DaTIqU0AMZ/7HLhgZWuIOVmZXSUVfAeP+qZ2++7PpJ0hIungkqqriyXLEbX9yxdvoWxG0q0jh52eCWpJW9C79rFcjwbSn753FJ10V5WBREgGNsL1HewGdIoF+TymXmpprnGAB02A+Vis8FOQLamf+BnzgO+yRq9TZq/wKBgFhGMzVF24B13mZBiuHafBgtypl0NlkaABQMKP/YQxMe50UbowX8OpWQh4/vz+Zp5kWWSe8FnRMyIw0A5g4+oFXBSl6NQsYLqoY83vDRGoN4sMMJboJa5RzoPk8uIPmXS54KgmNGlSgWUmSkNB8sNX9DI87zfJk9wDmsuvZO8TN8\"}";
void init_infinit()
{
reactor::sleep(1_sec);
/*while (true)
{
printf("CANARD\n");
reactor::sleep(500_ms);
}*/
printf("infinit starter thread\n");
namespace dht = infinit::model::doughnut;
auto priv = elle::serialization::json::deserialize
<infinit::cryptography::rsa::PrivateKey>(elle::Buffer(userkey), false);
auto pub = infinit::cryptography::rsa::PublicKey(priv);
printf("keypair\n");
auto keys = std::make_shared<infinit::cryptography::rsa::KeyPair>(
pub, priv);
auto consensus = [&] (dht::Doughnut& dht)
{
return elle::make_unique<dht::consensus::Paxos>(
dht::consensus::doughnut = dht,
dht::consensus::replication_factor = 1);
};
printf("passport\n");
auto passport = new dht::Passport(keys->K(), "network-", *keys);
infinit::overlay::kelips::Configuration conf;
auto overlay = [&](
dht::Doughnut& d,
std::shared_ptr<dht::Local> local)
-> std::unique_ptr<infinit::overlay::Overlay> {
return std::unique_ptr<infinit::overlay::Overlay>(
new infinit::overlay::kelips::Node(conf, std::move(local),&d));
};
printf("storage\n");
auto storage = std::unique_ptr<infinit::storage::Storage>(new infinit::storage::Memory());
printf("dht\n");
auto d = new dht::Doughnut(
dht::id = infinit::model::Address::random(),
dht::keys = keys,
dht::owner = keys->public_key(),
dht::passport = *passport,
dht::consensus_builder = consensus,
dht::overlay_builder = overlay,
dht::port = 6666,
dht::storage = std::move(storage),
dht::protocol = dht::Protocol::all,
dht::rdv_host = std::string("198.211.96.217:7890")
);
printf("DONE\n");
}
void init_tcp()
{
reactor::sleep(1_sec);
auto s = new reactor::network::TCPServer();
s->listen(4444);
while (true)
{
auto tt = s->accept();
ELLE_LOG("new connection %s", tt->peer());
new reactor::Thread("hs", [t=tt.release()] {
try
{
while (true)
{
auto data = t->read(5);
t->write(data);
}
}
catch(elle::Error const&e)
{
ELLE_LOG("tcp end: %s", e);
}
}, true);
}
}
void init_udp()
{
printf("INIT UDP\n");
auto u = new reactor::network::UDPSocket(*sched);
u->bind(boost::asio::ip::udp::endpoint(
boost::asio::ip::address_v4(), 5555));
while (true)
{
boost::asio::ip::udp::endpoint endpoint;
char buffer[4096];
printf("RECVFROM\n");
auto sz = u->receive_from(reactor::network::Buffer(buffer, 4096), endpoint);
printf("SENDTO %d\n", sz);
u->send_to(reactor::network::Buffer(buffer, sz), endpoint);
printf("SENDTO RETURNED\n");
}
}
class StaticInit
{
public:
NAMED_ARGUMENT(nacanard);
StaticInit()
{
initialized = 4242;
sinit = "some allocated string";
}
int initialized;
std::string sinit;
};
void heapstats()
{
//printf("heap %d\n", OS::heap_usage());
wake_scheduler();
Timers::oneshot(std::chrono::seconds(1), [](int) { heapstats();});
}
// OK
NAMED_ARGUMENT_DEFINE(nacanard, StaticInit);
//extern "C" void __cxx_global_var_init64() {}
//extern "C" void __cxx_global_var_init57() {}
extern "C" void __cxx_global_var_init();
//extern "C" void __do_global_ctors_aux();
extern "C" void _init();
StaticInit test;
namespace elle
{
namespace log
{
std::string mirage_log_level(std::string const&);
}
}
extern "C" void __cxx_global_var_init45 ();
extern "C" void __cxx_global_var_init46 ();
extern "C" void __cxx_global_var_init47 ();
extern "C" void __cxx_global_var_init48 ();
extern "C" void __cxx_global_var_init49 ();
extern "C" void __cxx_global_var_init ();
extern "C" void __cxx_global_var_init1 ();
extern "C" void __cxx_global_var_init2 ();
extern "C" void __cxx_global_var_init21 ();
extern "C" void __cxx_global_var_init22 ();
extern "C" void __cxx_global_var_init23 ();
extern "C" void __cxx_global_var_init3 ();
extern "C" void __cxx_global_var_init39 ();
extern "C" void __cxx_global_var_init4 ();
extern "C" void __cxx_global_var_init5 ();
extern "C" void __cxx_global_var_init6 ();
extern "C" void __cxx_global_var_init7 ();
extern "C" void __cxx_global_var_init120();
extern "C" void __cxx_global_var_init121();
extern "C" void __cxx_global_var_init122();
extern "C" void __cxx_global_var_init15 ();
extern "C" void __cxx_global_var_init17 ();
extern "C" void __cxx_global_var_init13 ();
extern "C" void __cxx_global_var_init14 ();
extern "C" void __cxx_global_var_init25 ();
extern "C" void __cxx_global_var_init55 ();
extern "C" void __sinit();
void invoke_ctors()
{
unsigned char* cinit = (unsigned char*)&_init;
printf("init at %x\n", cinit);
cinit += 3;
if (*cinit != 0xe8)
{
printf("BAD INSTRUCTION\n");
return;
}
unsigned int raddr = cinit[1] + (cinit[2]<<8) + (cinit[3] << 16) + (cinit[4] << 24);
unsigned int tgt = (unsigned int)cinit + raddr + 5;
printf("dgct is at %x\n", tgt);
unsigned char* ctcode = (unsigned char*)tgt;//(unsigned char*)&__do_global_ctors_aux;
if (ctcode[0] != 0xa1)
{
printf("BAD INSTRUCTION 2\n");
return;
}
unsigned int secstart = ctcode[1] + (ctcode[2] << 8) + (ctcode[3] << 16) + (ctcode[4] << 24);
printf(".ctors is at %x\n", secstart);
void** ctors = (void**)secstart;
void* end = (void*)-1;
/*
int idx = 0;
while(*ctors != end)
{
printf(" invoking ctor %d at %x\n", idx, *ctors);
//if (idx != 7)
{
void (*f)(void) = (void(*)())*ctors;
f();
}
--ctors;
++idx;
}*/
while (*ctors != end)
--ctors;
++ctors;
int idx = 0;
while (*ctors)
{
printf(" invoking ctor %d at %x\n", idx, *ctors);
//if (idx != 7)
{
void (*f)(void) = (void(*)())*ctors;
f();
}
++ctors;
++idx;
}
}
void Service::start(const std::string&)
{
printf("INVOKING INIT\n");
//invoke_ctors();
_init();
//__sinit();
//std::__1::ctype<char>
printf("initialized? %d\n", test.initialized);
printf("init? %s\n", test.sinit.c_str());
elle::log::mirage_log_level("DEBUG");
printf("%d %d %d %d\n", (int)ntohl(1), (int)ntohl(0x100), (int)ntohs(1), (int)htons(0x100));
#if 0
__cxx_global_var_init45 ();
__cxx_global_var_init46 ();
__cxx_global_var_init47 ();
__cxx_global_var_init48 ();
__cxx_global_var_init49 ();
__cxx_global_var_init ();
__cxx_global_var_init1 ();
__cxx_global_var_init2 ();
//__cxx_global_var_init21 ();
//__cxx_global_var_init22 ();
//__cxx_global_var_init23 ();
__cxx_global_var_init3 ();
//__cxx_global_var_init39 ();
__cxx_global_var_init4 ();
__cxx_global_var_init5 ();
__cxx_global_var_init6 ();
__cxx_global_var_init7 ();
__cxx_global_var_init120 ();
__cxx_global_var_init121 ();
__cxx_global_var_init122 ();
__cxx_global_var_init15 ();
__cxx_global_var_init17 ();
__cxx_global_var_init13 ();
__cxx_global_var_init14 ();
__cxx_global_var_init25 ();
__cxx_global_var_init55 ();
#endif
setenv("ELLE_LOG_LEVEL", "DUMP", 1);
//setenv("INFINIT_RDV", "", 1);
setenv("INFINIT_NO_IPV6", "1", 1);
printf("Hello world - OS included!\n");
write(1, "COIN\n", 5);
//__cxx_global_var_init();
//__do_global_ctors_aux();
//_init(); // causes exit
ELLE_LOG("canard");
ELLE_TRACE("trace level");
// OK
reactor::backend::boost_context::Backend b;
// OK
boost::filesystem::path p("/tmp/coin");
boost::filesystem::exists(p);
std::cerr << "canard" << std::endl;
std::cout << "coincoin" << std::endl;
std::cin.good();
// ok
//boost::asio::io_service io;
// runtime crash
//boost::asio::ip::tcp::socket s(io);
// BAD // passes with non debug build
//reactor::Mutex m;
// OK
// exception.o
sched = new reactor::Scheduler();
auto t = new reactor::Thread(*sched, "init_infinit", init_infinit, true);
printf("Hello world: arming... !\n");
Timers::oneshot(std::chrono::milliseconds(100), [](int) { sched_step();});
//new reactor::Thread(*sched, "init_udp", [] {init_udp();}, true);
//new reactor::Thread(*sched, "init_tcp", [] {init_tcp();}, true);
Timers::oneshot(std::chrono::milliseconds(100), [](int) { heapstats();});
}
#define NS_INADDRSZ 4
static int
inet_pton4(const char* src, u_char* dst)
{
int saw_digit, octets, ch;
u_char tmp[NS_INADDRSZ], *tp;
saw_digit = 0;
octets = 0;
*(tp = tmp) = 0;
while ((ch = *src++) != '\0') {
if (ch >= '0' && ch <= '9') {
u_int neww = *tp * 10 + (ch - '0');
if (saw_digit && *tp == 0)
return (0);
if (neww > 255)
return (0);
*tp = neww;
if (! saw_digit) {
if (++octets > 4)
return (0);
saw_digit = 1;
}
} else if (ch == '.' && saw_digit) {
if (octets == 4)
return (0);
*++tp = 0;
saw_digit = 0;
} else
return (0);
}
if (octets < 4)
return (0);
memcpy(dst, tmp, NS_INADDRSZ);
return (1);
}
#define NS_IN6ADDRSZ 16
#define NS_INT16SZ 2
static int
inet_pton6(const char*src, unsigned char*dst)
{
static const char xdigits[] = "0123456789abcdef";
u_char tmp[NS_IN6ADDRSZ], *tp, *endp, *colonp;
const char *curtok;
int ch, saw_xdigit;
u_int val;
tp = (unsigned char*)memset(tmp, '\0', NS_IN6ADDRSZ);
endp = tp + NS_IN6ADDRSZ;
colonp = NULL;
/* Leading :: requires some special handling. */
if (*src == ':')
if (*++src != ':')
return (0);
curtok = src;
saw_xdigit = 0;
val = 0;
while ((ch = tolower (*src++)) != '\0') {
const char *pch;
pch = strchr(xdigits, ch);
if (pch != NULL) {
val <<= 4;
val |= (pch - xdigits);
if (val > 0xffff)
return (0);
saw_xdigit = 1;
continue;
}
if (ch == ':') {
curtok = src;
if (!saw_xdigit) {
if (colonp)
return (0);
colonp = tp;
continue;
} else if (*src == '\0') {
return (0);
}
if (tp + NS_INT16SZ > endp)
return (0);
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
saw_xdigit = 0;
val = 0;
continue;
}
if (ch == '.' && ((tp + NS_INADDRSZ) <= endp) &&
inet_pton4(curtok, tp) > 0) {
tp += NS_INADDRSZ;
saw_xdigit = 0;
break; /* '\0' was seen by inet_pton4(). */
}
return (0);
}
if (saw_xdigit) {
if (tp + NS_INT16SZ > endp)
return (0);
*tp++ = (u_char) (val >> 8) & 0xff;
*tp++ = (u_char) val & 0xff;
}
if (colonp != NULL) {
/*
* Since some memmove()'s erroneously fail to handle
* overlapping regions, we'll do the shift by hand.
*/
const int n = tp - colonp;
int i;
if (tp == endp)
return (0);
for (i = 1; i <= n; i++) {
endp[- i] = colonp[n - i];
colonp[n - i] = 0;
}
tp = endp;
}
if (tp != endp)
return (0);
memcpy(dst, tmp, NS_IN6ADDRSZ);
return (1);
}
extern "C" int
inet_pton(int af, const char*src, void* dst)
{
switch (af) {
case AF_INET:
return (inet_pton4(src, (unsigned char*)dst));
case AF_INET6:
return (inet_pton6(src, (unsigned char*)dst));
default:
// __set_errno (EAFNOSUPPORT);
return (-1);
}
/* NOTREACHED */
}
extern "C" const char *inet_ntop(int af, const void *src,
char *dst, socklen_t size);
static const char *
inet_ntop6(const unsigned char *src, char* dst, socklen_t size)
{
/*
* Note that int32_t and int16_t need only be "at least" large enough
* to contain a value of the specified size. On some systems, like
* Crays, there is no such thing as an integer variable with 16 bits.
* Keep this in mind if you think this function should have been coded
* to use pointer overlays. All the world's not a VAX.
*/
char tmp[sizeof "ffff:ffff:ffff:ffff:ffff:ffff:255.255.255.255"], *tp;
struct { int base, len; } best, cur;
u_int words[NS_IN6ADDRSZ / NS_INT16SZ];
int i;
/*
* Preprocess:
* Copy the input (bytewise) array into a wordwise array.
* Find the longest run of 0x00's in src[] for :: shorthanding.
*/
memset(words, '\0', sizeof words);
for (i = 0; i < NS_IN6ADDRSZ; i += 2)
words[i / 2] = (src[i] << 8) | src[i + 1];
best.base = -1;
cur.base = -1;
best.len = 0;
cur.len = 0;
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
if (words[i] == 0) {
if (cur.base == -1)
cur.base = i, cur.len = 1;
else
cur.len++;
} else {
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
cur.base = -1;
}
}
}
if (cur.base != -1) {
if (best.base == -1 || cur.len > best.len)
best = cur;
}
if (best.base != -1 && best.len < 2)
best.base = -1;
/*
* Format the result.
*/
tp = tmp;
for (i = 0; i < (NS_IN6ADDRSZ / NS_INT16SZ); i++) {
/* Are we inside the best run of 0x00's? */
if (best.base != -1 && i >= best.base &&
i < (best.base + best.len)) {
if (i == best.base)
*tp++ = ':';
continue;
}
/* Are we following an initial run of 0x00s or any real hex? */
if (i != 0)
*tp++ = ':';
/* Is this address an encapsulated IPv4? */
if (i == 6 && best.base == 0 &&
(best.len == 6 || (best.len == 5 && words[5] == 0xffff))) {
if (!inet_ntop(AF_INET, src+12, tp, sizeof tmp - (tp - tmp)))
return (NULL);
tp += strlen(tp);
break;
}
tp += sprintf(tp, "%x", words[i]);
}
/* Was it a trailing run of 0x00's? */
if (best.base != -1 && (best.base + best.len) ==
(NS_IN6ADDRSZ / NS_INT16SZ))
*tp++ = ':';
*tp++ = '\0';
/*
* Check for overflow, copy, and we're done.
*/
if ((socklen_t)(tp - tmp) > size) {
//__set_errno (ENOSPC);
return (NULL);
}
return strcpy(dst, tmp);
}
extern "C" const char *inet_ntop(int af, const void *src,
char *dst, socklen_t size)
{
if (af == AF_INET6)
return inet_ntop6((const unsigned char*)src, dst, size);
const unsigned char* csrc = (const unsigned char*)src;
sprintf(dst, "%d.%d.%d.%d",
(unsigned int)csrc[0],
(unsigned int)csrc[1],
(unsigned int)csrc[2],
(unsigned int)csrc[3]);
return dst;
}
extern "C" int aainet_pton(int af, const char *src, void *dst)
{
printf("inet_pton %s\n", src);
unsigned char* cdst = (unsigned char*)dst;
return 1;
/*
std::vector<std::string> elems;
boost::algorithm::split(elems, src, boost::is_any_of("."));
unsigned char* cdst = (unsigned char*)dst;
for (int i=0; i<4; ++i)
cdst[i] = std::stoi(elems[i]);
return 1;*/
}
static const int FNM_NOMATCH = 1;
extern "C" int fnmatch(const char *pattern, const char *string, int flags)
{
if (!pattern || !string)
return -1;
//printf("fnmatch %s %s\n", pattern, string);
std::string pat(pattern);
std::string str(string);
int res = FNM_NOMATCH;
if (pat == "*")
res= 0;
else if (pat[0] == '*' && pat[pat.size()-1] == '*')
res = (str.find(pat.substr(1, pat.size()-2)) == str.npos) ? FNM_NOMATCH : 0;
else if (pat[0] == '*')
res = (str.find(pat.substr(1)) == str.size() - pat.size()-1) ? 0 : FNM_NOMATCH;
else if (pat[pat.size()-1] == '*')
res = (str.find(pat.substr(0, pat.size()-1)) == 0) ? 0 : FNM_NOMATCH;
else
res = (pat == str) ? 0 : FNM_NOMATCH;
printf("fnmatch %s %s -> %d\n", pattern, string, res);
return res;
}
namespace boost
{
namespace posix_time
{
boost::posix_time::ptime microsec_clock::local_time()
{
return ptime() + boost::posix_time::microseconds(OS::micros_since_boot());
}
boost::posix_time::ptime microsec_clock::universal_time()
{
return local_time();
}
}
}