Skip to content

Commit

Permalink
More admin stuff
Browse files Browse the repository at this point in the history
  • Loading branch information
cjdelisle committed Jan 26, 2012
1 parent 970cabf commit dd29ed3
Show file tree
Hide file tree
Showing 7 changed files with 121 additions and 58 deletions.
46 changes: 31 additions & 15 deletions admin/Admin.c
Original file line number Diff line number Diff line change
Expand Up @@ -161,12 +161,11 @@ void incomingFromClient(evutil_socket_t socket, short eventType, void* vconn)
struct Connection* conn = (struct Connection*) vconn;
struct ChildContext* context = conn->context;
eventType = eventType;
perror("incomingFromClient");
errno = 0;
char buf[MAX_API_REQUEST_SIZE];
ssize_t result = recv(socket, buf, MAX_API_REQUEST_SIZE, 0);
if (result > 0) {
size_t amountWritten = write(context->outFd, buf, result);
perror("wrote to parent pipe");
if (amountWritten != (size_t)result) {
exit(0);
}
Expand All @@ -192,7 +191,7 @@ struct Connection* newConnection(struct ChildContext* context, evutil_socket_t f
if (!conn) {
return NULL;
}
perror("new connection");

conn->read = event_new(context->eventBase, fd, EV_READ | EV_PERSIST, incomingFromClient, conn);
conn->socket = fd;
conn->context = context;
Expand Down Expand Up @@ -228,8 +227,10 @@ void acceptConn(evutil_socket_t socket, short eventType, void* vcontext)
}
}

#define BSTR(x) (&(String) { .bytes = x, .len = strlen(x) })

// only in child
void child(struct ChildContext* context)
void child(Dict* config, struct ChildContext* context)
{
context->dataFromParent =
event_new(context->eventBase,
Expand All @@ -242,9 +243,23 @@ void child(struct ChildContext* context)

struct sockaddr_storage addr;
int addrLen = sizeof(struct sockaddr_storage);
evutil_parse_sockaddr_port("127.0.0.1:9999", (struct sockaddr*) &addr, &addrLen);
char* bindTo = "127.0.0.1:9999";
String* bindStr = Dict_getString(config, BSTR("bind"));
if (bindStr) {
fprintf(stderr, "Admin: Binding to %s\n", bindStr->bytes);
if (evutil_parse_sockaddr_port(bindStr->bytes, (struct sockaddr*) &addr, &addrLen)) {
fprintf(stderr, "Admin: admin.bind parse failed, calling back on %s\n", bindTo);
bindStr = NULL;
}
}
if (!bindStr) {
fprintf(stderr, "Admin: Binding to %s\n", bindTo);
evutil_parse_sockaddr_port(bindTo, (struct sockaddr*) &addr, &addrLen);
}

evutil_socket_t listener = socket(addr.ss_family, SOCK_STREAM, 0);
evutil_make_socket_nonblocking(listener);
evutil_make_listen_socket_reuseable(listener);

if (bind(listener, (struct sockaddr*)&addr, sizeof(addr)) < 0) {
perror("bind");
Expand Down Expand Up @@ -310,13 +325,12 @@ void setUser(String* name)
}
}

#define BSTR(x) (&(String) { .bytes = x, .len = strlen(x) })
struct Admin* Admin_new(Dict* config,
struct event_base** eventBase,
char* user,
struct event_base* eventBase,
struct ExceptionHandler* eh,
struct Allocator* allocator)
{
Dict_getString(config, BSTR("bind"));
errno = 0;
int pipes[2][2];
if (pipe(pipes[0]) || pipe(pipes[1])) {
Expand All @@ -342,15 +356,18 @@ struct Admin* Admin_new(Dict* config,
// become orphaned if the parent gets signal 11 err um 9.
setpgid(0, pgid);

setUser(Dict_getString(config, BSTR("setuser")));
if (user) {
Security_setUser(user, NULL, AbortHandler_INSTANCE);
}

struct ChildContext context;
memset(&context, 0, sizeof(struct ChildContext));
context.inFd = inFd;
context.outFd = outFd;
context.allocator = allocator;
//event_reinit(eventBase);
context.eventBase = event_base_new();//eventBase;
child(&context);
event_reinit(eventBase);
context.eventBase = eventBase;
child(config, &context);
exit(0);
}

Expand All @@ -361,9 +378,8 @@ struct Admin* Admin_new(Dict* config,
admin->outFd = outFd;
admin->allocator = allocator;
admin->functionCount = 0;
*eventBase = event_base_new();
admin->eventBase = *eventBase;
admin->pipeEv = event_new(admin->eventBase, inFd, EV_READ | EV_PERSIST, inFromChild, admin);
admin->eventBase = eventBase;
admin->pipeEv = event_new(eventBase, inFd, EV_READ | EV_PERSIST, inFromChild, admin);
event_add(admin->pipeEv, NULL);

return admin;
Expand Down
3 changes: 2 additions & 1 deletion admin/Admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ void Admin_registerFunction(char* name,
void Admin_sendMessage(Dict* message, struct Admin* admin);

struct Admin* Admin_new(Dict* config,
struct event_base** eventBase,
char* user,
struct event_base* eventBase,
struct ExceptionHandler* eh,
struct Allocator* allocator);
#endif
14 changes: 6 additions & 8 deletions admin/http/HttpServer.c
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,7 @@ void apiHandler(struct evhttp_request* req, void* vcontext)
struct Context* context = (struct Context*) vcontext;
context=context;
const char* uri = evhttp_decode_uri(evhttp_request_get_uri(req));
printf("%s\n", uri);

#define KEY "/?q="
const char* q = strstr(uri, "/?q=");
if (!q) {
Expand Down Expand Up @@ -267,16 +267,14 @@ static void handleApiEvent(evutil_socket_t socket, short eventType, void* vconte
event_free(context->apiEvent);
return;
}
fprintf(stderr, "overlong message\n");
perror("overlong message");
return;
}
context->messageBuffer[length] = '\0';
fprintf(stderr, "incoming from api server [%s]\n", context->messageBuffer);
struct Reader* reader =
ArrayReader_new(context->messageBuffer, MAX_API_REPLY_SIZE, context->allocator);
Dict message;
if (List_getStandardBencSerializer()->parseDictionary(reader, context->allocator, &message)) {
fprintf(stderr, "unparsable\n");
return;
}

Expand Down Expand Up @@ -311,23 +309,23 @@ void setupApi(char* ipAndPort, struct Context* context)
int main(int argc, char **argv)
{
assert(argc > 0);
if (argc < 2) {
printf("Usage: %s address.and.port.of.api:server /full/path/to/directory/\n", argv[0]);
if (argc < 3) {
printf("Usage: %s address.of.api.server:port /full/path/to/directory/\n", argv[0]);
return 0;
}
Crypto_init();

struct Context context;
memset(&context, 0, sizeof(struct Context));
context.workingDir = argv[1];
context.workingDir = argv[2];
randombytes((uint8_t*) &context.txidBaseline, 4);

char alloc[1<<16];
context.allocator = BufferAllocator_new(alloc, 1<<16);

context.eventBase = event_base_new();

setupApi("127.0.0.1:9999", &context);
setupApi(argv[1], &context);

struct evhttp* httpd = evhttp_new(context.eventBase);
evhttp_bind_socket(httpd, "127.0.0.1", 51902);
Expand Down
19 changes: 19 additions & 0 deletions admin/http/text/html/index.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
<html>
<head>
</head>
<script src="/text/javascript/bencode.js"></script>
<script>
var xmlhttp = new XMLHttpRequest();
xmlhttp.open("GET", "/api/?q=d1:q19:NodeStore_dumpTablee", true);
xmlhttp.onreadystatechange = function() {
if (xmlhttp.readyState==4) {
var obj = bdecode(xmlhttp.responseText);
var table = obj.routingTable;
document.body.innerHTML = JSON.stringify(obj);
}
};
xmlhttp.send(null);
</script>
<body>
</body>
</html>
44 changes: 28 additions & 16 deletions cjdroute.c
Original file line number Diff line number Diff line change
Expand Up @@ -534,6 +534,20 @@ static void registerRouter(Dict* config, uint8_t myPubKey[32], struct Context* c
context->admin);
}

static char* setUser(List* config)
{
for (int i = 0; i < List_size(config); i++) {
Dict* d = List_getDict(config, i);
if (d) {
String* uname = Dict_getString(d, BSTR("setuser"));
if (uname) {
return uname->bytes;
}
}
}
return NULL;
}

static void security(List* config, struct Log* logger, struct ExceptionHandler* eh)
{
if (!config) {
Expand All @@ -544,18 +558,13 @@ static void security(List* config, struct Log* logger, struct ExceptionHandler*
String* s = List_getString(config, i);
if (s && String_equals(s, BSTR("nofiles"))) {
nofiles = true;
continue;
}
Dict* d = List_getDict(config, i);
if (d) {
String* uname = Dict_getString(d, BSTR("setuser"));
if (uname) {
Log_info1(logger, "Changing user to [%s]\n", uname->bytes);
Security_setUser(uname->bytes, logger, eh);
continue;
}
}
}
char* user = setUser(config);
if (user) {
Log_info1(logger, "Changing user to [%s]\n", user);
Security_setUser(user, logger, eh);
}
if (nofiles) {
Log_info(logger, "Setting max open files to zero.\n");
Security_noFiles(eh);
Expand All @@ -570,9 +579,11 @@ static void adminPing(Dict* input, void* vadmin, struct Allocator* alloc)
Admin_sendMessage(input, (struct Admin*) vadmin);
}

static void admin(Dict* adminConf, struct Context* context)
static void admin(Dict* adminConf, char* user, struct Context* context)
{
context->admin = Admin_new(adminConf, &context->base, context->eHandler, context->allocator);
context->admin =
Admin_new(adminConf, user, context->base, context->eHandler, context->allocator);

Admin_registerFunction("ping", adminPing, context->admin, context->admin);
}

Expand Down Expand Up @@ -616,6 +627,7 @@ int main(int argc, char** argv)

struct Context context;
memset(&context, 0, sizeof(struct Context));
context.base = event_base_new();

// Allow it to allocate 4MB
context.allocator = MallocAllocator_new(1<<22);
Expand All @@ -632,12 +644,12 @@ int main(int argc, char** argv)

printf("Version: " Version_STRING "\n");

char* user = setUser(Dict_getList(&config, BSTR("security")));

// Admin
Dict* adminConf = Dict_getDict(&config, BSTR("admin"));
if (adminConf) {
admin(adminConf, &context);
} else {
context.base = event_base_new();
admin(adminConf, user, &context);
}

// Logging
Expand Down Expand Up @@ -699,7 +711,7 @@ int main(int argc, char** argv)
Log_info1(context.logger, "Your IPv6 address is: %s\n", myIp);

// Security.
security(NULL/*Dict_getList(&config, BSTR("security"))*/, context.logger, context.eHandler);
security(Dict_getList(&config, BSTR("security")), context.logger, context.eHandler);

event_base_loop(context.base, 0);
}
4 changes: 0 additions & 4 deletions dht/dhtcore/Janitor.c
Original file line number Diff line number Diff line change
Expand Up @@ -115,10 +115,6 @@ static void maintanenceCycle(void* vcontext)
(unsigned int) AverageRoller_getAverage(janitor->routerModule->gmrtRoller),
(unsigned int) nonZeroNodes);

Log_debug(janitor->routerModule->logger, "Routing table dump:\n");
NodeStore_dumpTables(janitor->routerModule->logger->writer, janitor->routerModule->nodeStore);
janitor->routerModule->logger->writer->write("\n", 1, janitor->routerModule->logger->writer);

size_t bytes = MallocAllocator_bytesAllocated(janitor->allocator);
Log_debug1(janitor->routerModule->logger,
"Using %u bytes of memory.\n",
Expand Down
49 changes: 35 additions & 14 deletions dht/dhtcore/NodeStore.c
Original file line number Diff line number Diff line change
Expand Up @@ -401,7 +401,7 @@ void NodeStore_remove(struct Node* node, struct NodeStore* store)
memcpy(header, &store->headers[store->size], sizeof(struct NodeHeader));
}

static void sendEntries(struct NodeStore* store, struct List_Item* last, String* txid)
static void sendEntries(struct NodeStore* store, struct List_Item* last, String* txid, bool isMore)
{
struct Dict_Entry txidEntry = {
.next = NULL,
Expand All @@ -413,22 +413,28 @@ static void sendEntries(struct NodeStore* store, struct List_Item* last, String*
.key = &(String) { .len = 12, .bytes = "routingTable" },
.val = &(Object) { .type = Object_LIST, .as.list = &last }
};
Dict d = &tableEntry;
Dict d;
if (isMore) {
struct Dict_Entry more = {
.next = &tableEntry,
.key = &(String) { .len = 4, .bytes = "more" },
.val = &(Object) { .type = Object_INTEGER, .as.number = 1 }
};
d = &more;
} else {
d = &tableEntry;
}
Admin_sendMessage(&d, store->admin);
}

static void addRoutingTableEntries(struct NodeStore* store,
uint32_t i,
uint32_t j,
struct List_Item* last,
String* txid)
{
if (i >= store->size) {
sendEntries(store, last, txid);
return;
}

uint8_t path[20];
String pathStr = { .len = 20, .bytes = (char*)path };
uint8_t path[19];
String pathStr = { .len = 19, .bytes = (char*)path };
struct Dict_Entry pathEntry = {
.next = NULL,
.key = &(String) { .len = 4, .bytes = "path" },
Expand All @@ -438,11 +444,11 @@ static void addRoutingTableEntries(struct NodeStore* store,
struct Dict_Entry linkStateEntry = {
.next = &pathEntry,
.key = &(String) { .len = 4, .bytes = "linkState" },
.val = &(Object) { .type = Object_INTEGER, .as.number = store->headers[i].reach }
.val = &(Object) { .type = Object_INTEGER }
};

uint8_t ip[40];
String ipStr = { .len = 40, .bytes = (char*)ip };
uint8_t ip[39];
String ipStr = { .len = 39, .bytes = (char*)ip };
struct Dict_Entry entry = {
.next = &linkStateEntry,
.key = &(String) { .len = 2, .bytes = "ip" },
Expand All @@ -455,10 +461,20 @@ static void addRoutingTableEntries(struct NodeStore* store,
.elem = &(Object) { .type = Object_DICT, .as.dictionary = &d }
};

if (i >= store->size || j > 900) {
linkStateEntry.val->as.number = 0xFFFFFFFF;
Address_printIp(ip, store->thisNodeAddress);
strcpy((char*)path, "0000.0000.0000.0001");

sendEntries(store, &next, txid, (j > 900));
return;
}

linkStateEntry.val->as.number = store->headers[i].reach;
Address_printIp(ip, &store->nodes[i].address);
Address_printNetworkAddress(path, &store->nodes[i].address);

addRoutingTableEntries(store, i + 1, &next, txid);
addRoutingTableEntries(store, i + 1, j + 1, &next, txid);
}

static void dumpTable(Dict* req, void* vnodeStore, struct Allocator* tempAlloc)
Expand All @@ -468,5 +484,10 @@ static void dumpTable(Dict* req, void* vnodeStore, struct Allocator* tempAlloc)
if (!txid) {
return;
}
addRoutingTableEntries(store, 0, NULL, txid);
uint32_t i = 0;
int64_t* iPtr = Dict_getInt(req, &(String) { .len = 5, .bytes = "start" });
if (iPtr && *iPtr > 0 && *iPtr < UINT32_MAX) {
i = *iPtr;
}
addRoutingTableEntries(store, i, 0, NULL, txid);
}

0 comments on commit dd29ed3

Please sign in to comment.