Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

Standardized creation of constant strings using String_CONST

  • Loading branch information...
commit c02de07b0b7f3a6d1d293ee64730309edfecbdae 1 parent 25447c9
Caleb James DeLisle authored
Showing with 42 additions and 42 deletions.
  1. +19 −18 admin/Admin.c
  2. +21 −22 cjdroute.c
  3. +2 −2 dht/dhtcore/RouterModule.c
37 admin/Admin.c
View
@@ -43,15 +43,13 @@
#define MAX_API_REQUEST_SIZE (1<<16)
-#define BSTR(x) (&(String) { .bytes = x, .len = strlen(x) })
-
-static String* TYPE = &(String) { .bytes = "type", .len = 4 };
-static String* REQUIRED = &(String) { .bytes = "required", .len = 8 };
-static String* STRING = &(String) { .bytes = "String", .len = 6 };
-static String* INT = &(String) { .bytes = "Int", .len = 3 };
-static String* DICT = &(String) { .bytes = "Dict", .len = 4 };
-static String* LIST = &(String) { .bytes = "List", .len = 4 };
-static String* NONE = &(String) { .bytes = "none", .len = 4 };
+static String* TYPE = String_CONST_SO("type");
+static String* REQUIRED = String_CONST_SO("required");
+static String* STRING = String_CONST_SO("String");
+static String* INT = String_CONST_SO("Int");
+static String* DICT = String_CONST_SO("Dict");
+static String* LIST = String_CONST_SO("List");
+static String* NONE = String_CONST_SO("none");
struct Function
{
@@ -82,14 +80,14 @@ struct Admin
static inline bool authValid(Dict* message, uint8_t* buffer, uint32_t length, struct Admin* admin)
{
- String* cookieStr = Dict_getString(message, BSTR("cookie"));
+ String* cookieStr = Dict_getString(message, String_CONST("cookie"));
uint32_t cookie = (cookieStr != NULL) ? strtoll(cookieStr->bytes, NULL, 10) : 0;
if (!cookie) {
- int64_t* cookieInt = Dict_getInt(message, BSTR("cookie"));
+ int64_t* cookieInt = Dict_getInt(message, String_CONST("cookie"));
cookie = (cookieInt) ? *cookieInt : 0;
}
uint64_t nowSecs = Time_currentTimeSeconds(admin->eventBase);
- String* submittedHash = Dict_getString(message, BSTR("hash"));
+ String* submittedHash = Dict_getString(message, String_CONST("hash"));
if (cookie > nowSecs || cookie < nowSecs - 20 || !submittedHash || submittedHash->len != 64) {
return false;
}
@@ -174,7 +172,7 @@ static void handleRequestFromChild(struct Admin* admin,
}
// If they're asking for a cookie then lets give them one.
- String* cookie = BSTR("cookie");
+ String* cookie = String_CONST("cookie");
if (String_equals(query, cookie)) {
Dict* d = Dict_new(allocator);
char bytes[32];
@@ -186,16 +184,16 @@ static void handleRequestFromChild(struct Admin* admin,
}
// If this is a permitted query, make sure the cookie is right.
- String* auth = BSTR("auth");
+ String* auth = String_CONST("auth");
bool authed = false;
if (String_equals(query, auth)) {
if (!authValid(&message, buffer + skip, reader->bytesRead(reader), admin)) {
Dict* d = Dict_new(allocator);
- Dict_putString(d, BSTR("error"), BSTR("Auth failed."), allocator);
+ Dict_putString(d, String_CONST("error"), String_CONST("Auth failed."), allocator);
Admin_sendMessage(d, txid, admin);
return;
}
- query = Dict_getString(&message, BSTR("aq"));
+ query = Dict_getString(&message, String_CONST("aq"));
authed = true;
}
@@ -214,13 +212,16 @@ static void handleRequestFromChild(struct Admin* admin,
if (noFunctionsCalled) {
Dict* d = Dict_new(allocator);
- Dict_putString(d, BSTR("error"), BSTR("No functions matched your request."), allocator);
+ Dict_putString(d,
+ String_CONST("error"),
+ String_CONST("No functions matched your request."),
+ allocator);
Dict* functions = Dict_new(allocator);
for (int i = 0; i < admin->functionCount; i++) {
Dict_putDict(functions, admin->functions[i].name, admin->functions[i].args, allocator);
}
if (functions) {
- Dict_putDict(d, BSTR("availableFunctions"), functions, allocator);
+ Dict_putDict(d, String_CONST("availableFunctions"), functions, allocator);
}
Admin_sendMessage(d, txid, admin);
return;
43 cjdroute.c
View
@@ -244,10 +244,9 @@ static int genconf()
return 0;
}
-#define BSTR(x) (&(String) { .bytes = x, .len = strlen(x) })
static void parsePrivateKey(Dict* config, struct Address* addr, uint8_t privateKey[32])
{
- String* privateKeyStr = Dict_getString(config, BSTR("privateKey"));
+ String* privateKeyStr = Dict_getString(config, String_CONST("privateKey"));
if (privateKeyStr == NULL) {
fprintf(stderr, "Could not extract private key from configuration.\n");
} else if (privateKeyStr->len != 64) {
@@ -309,11 +308,11 @@ static int getcmds(Dict* config)
uint8_t myIp[40];
Address_printIp(myIp, &addr);
- Dict* router = Dict_getDict(config, BSTR("router"));
- Dict* iface = Dict_getDict(router, BSTR("interface"));
- String* type = Dict_getString(iface, BSTR("type"));
- String* tunDevice = Dict_getString(iface, BSTR("tunDevice"));
- if (!String_equals(type, BSTR("TUNInterface"))) {
+ Dict* router = Dict_getDict(config, String_CONST("router"));
+ Dict* iface = Dict_getDict(router, String_CONST("interface"));
+ String* type = Dict_getString(iface, String_CONST("type"));
+ String* tunDevice = Dict_getString(iface, String_CONST("tunDevice"));
+ if (!String_equals(type, String_CONST("TUNInterface"))) {
fprintf(stderr, "router.interface.type is not recognized.\n");
return -1;
}
@@ -364,9 +363,9 @@ static void reconf(struct Context* ctx, Dict* mainConf)
static void registerRouter(Dict* config, struct Address *addr, struct Context* context)
{
- Dict* iface = Dict_getDict(config, BSTR("interface"));
- if (String_equals(Dict_getString(iface, BSTR("type")), BSTR("TUNInterface"))) {
- String* tunPath = Dict_getString(iface, BSTR("tunDevice"));
+ Dict* iface = Dict_getDict(config, String_CONST("interface"));
+ if (String_equals(Dict_getString(iface, String_CONST("type")), String_CONST("TUNInterface"))) {
+ String* tunPath = Dict_getString(iface, String_CONST("tunDevice"));
struct TUNInterface* tun = TUNInterface_new(tunPath, context->base, context->allocator);
if (TUNConfigurator_configure(tun, addr->ip6.bytes, 8) != 0) {
fprintf(stderr, "Couldn't configure TUN interface\n");
@@ -388,7 +387,7 @@ 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"));
+ String* uname = Dict_getString(d, String_CONST("setuser"));
if (uname) {
return uname->bytes;
}
@@ -405,7 +404,7 @@ static void security(List* config, struct Log* logger, struct ExceptionHandler*
bool nofiles = false;
for (int i = 0; i < List_size(config); i++) {
String* s = List_getString(config, i);
- if (s && String_equals(s, BSTR("nofiles"))) {
+ if (s && String_equals(s, String_CONST("nofiles"))) {
nofiles = true;
}
}
@@ -425,7 +424,7 @@ static void adminPing(Dict* input, void* vadmin, String* txid)
uint8_t buffer[256];
struct Allocator* alloc = BufferAllocator_new(buffer, 256);
- String* pong = BSTR("pong");
+ String* pong = String_CONST("pong");
Dict* d = Dict_new(alloc);
Dict_putString(d, CJDHTConstants_QUERY, pong, alloc);
@@ -438,7 +437,7 @@ static void adminMemory(Dict* input, void* vcontext, String* txid)
uint8_t buffer[256];
struct Allocator* alloc = BufferAllocator_new(buffer, 256);
- String* bytes = BSTR("bytes");
+ String* bytes = String_CONST("bytes");
Dict* d = Dict_new(alloc);
Dict_putInt(d, bytes, MallocAllocator_bytesAllocated(context->allocator), alloc);
@@ -491,7 +490,7 @@ static void admin(Dict* mainConf, char* user, struct Log* logger, struct Context
static void pidfile(Dict* config)
{
- String* pidFile = Dict_getString(config, BSTR("pidFile"));
+ String* pidFile = Dict_getString(config, String_CONST("pidFile"));
if (pidFile) {
printf("%s", pidFile->bytes);
}
@@ -576,7 +575,7 @@ int main(int argc, char** argv)
CryptoAuth_new(&config, context.allocator, privateKey, context.base, context.logger);
// Admin
- char* user = setUser(Dict_getList(&config, BSTR("security")));
+ char* user = setUser(Dict_getList(&config, String_CONST("security")));
admin(&config, user, &logger, &context);
context.eHandler = AbortHandler_INSTANCE;
@@ -585,7 +584,7 @@ int main(int argc, char** argv)
ReplyModule_register(context.registry, context.allocator);
// Router
- Dict* routerConf = Dict_getDict(&config, BSTR("router"));
+ Dict* routerConf = Dict_getDict(&config, String_CONST("router"));
registerRouter(routerConf, &myAddr, &context);
SerializationModule_register(context.registry, context.allocator);
@@ -599,11 +598,11 @@ int main(int argc, char** argv)
context.base,
context.allocator);
- Dict* interfaces = Dict_getDict(&config, BSTR("interfaces"));
+ Dict* interfaces = Dict_getDict(&config, String_CONST("interfaces"));
- Dict* udpConf = Dict_getDict(interfaces, BSTR("UDPInterface"));
+ Dict* udpConf = Dict_getDict(interfaces, String_CONST("UDPInterface"));
if (udpConf) {
- String* bindStr = Dict_getString(udpConf, BSTR("bind"));
+ String* bindStr = Dict_getString(udpConf, String_CONST("bind"));
UDPInterface_new(context.base,
(bindStr) ? bindStr->bytes : NULL,
context.allocator,
@@ -619,7 +618,7 @@ int main(int argc, char** argv)
}
// pid file
- String* pidFile = Dict_getString(&config, BSTR("pidFile"));
+ String* pidFile = Dict_getString(&config, String_CONST("pidFile"));
if (pidFile) {
Log_info1(context.logger, "Writing pid of process to [%s].\n", pidFile->bytes);
FILE* pf = fopen(pidFile->bytes, "w");
@@ -665,7 +664,7 @@ int main(int argc, char** argv)
Log_info1(context.logger, "Your IPv6 address is: %s\n", myIp);
// Security.
- security(Dict_getList(&config, BSTR("security")), context.logger, context.eHandler);
+ security(Dict_getList(&config, String_CONST("security")), context.logger, context.eHandler);
event_base_loop(context.base, 0);
abort();
4 dht/dhtcore/RouterModule.c
View
@@ -272,11 +272,11 @@ struct RouterModule_Ping
struct Allocator* allocator;
};
-#define BSTR(x) (&(String) { .bytes = x, .len = strlen(x) })
+
static void pingNode(Dict* args, void* vrouter, String* txid)
{
struct RouterModule* router = (struct RouterModule*) vrouter;
- String* pathStr = Dict_getString(args, BSTR("path"));
+ String* pathStr = Dict_getString(args, String_CONST("path"));
int64_t* timeoutPtr = Dict_getInt(args, String_CONST("timeout"));
uint32_t timeout = (timeoutPtr && *timeoutPtr > 0) ? *timeoutPtr : 0;
Please sign in to comment.
Something went wrong with that request. Please try again.