Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
Browse files

Switching to static allocations in kite and kite request structures

  • Loading branch information...
commit 1c59cd63426efaeb96c8d2175056a03a61c153d7 1 parent 7645624
@BjarniRunar BjarniRunar authored
View
11 httpkite.c
@@ -29,6 +29,7 @@ Note: For alternate license terms, see the file COPYING.md.
#include "pkconn.h"
#include "pkproto.h"
#include "pklogging.h"
+#include "utils.h"
struct pk_global_state pk_state;
@@ -89,13 +90,13 @@ int main(int argc, char **argv) {
PKS_SSL_INIT(ctx);
kite_r.kite = &kite;
- kite.protocol = "http";
- kite.public_domain = argv[1];
+ strcpy(kite.protocol, "http");
+ strncpyz(kite.public_domain, argv[1], PK_DOMAIN_LENGTH);
kite.public_port = 0;
- kite.auth_secret = argv[2];
+ strncpyz(kite.auth_secret, argv[2], PK_SECRET_LENGTH);
- kite_r.bsalt = NULL;
- kite_r.fsalt = NULL;
+ kite_r.bsalt[0] = '\0';
+ kite_r.fsalt[0] = '\0';
kite_rp = &kite_r;
srand(time(0) ^ getpid());
View
2  pkblocker.c
@@ -279,7 +279,7 @@ int pkb_update_dns(struct pk_manager* pkm)
if (!bogus) return 0;
for (j = 0, kite = pkm->kites; j < pkm->kite_max; kite++, j++) {
- if (kite->protocol != NULL) {
+ if (kite->protocol[0] != '\0') {
PKS_STATE(pkm->status = PK_STATUS_DYNDNS);
sprintf(payload, "%s:%s", kite->public_domain, address_list);
pk_sign(NULL, kite->auth_secret, payload, 100, signature);
View
12 pkmanager.c
@@ -963,7 +963,7 @@ struct pk_pagekite* pkm_find_kite(struct pk_manager* pkm,
found = NULL;
for (which = 0; which < pkm->kite_max; which++) {
kite = pkm->kites+which;
- if (kite->protocol != NULL) {
+ if (kite->protocol[0] != '\0') {
if ((0 == strcasecmp(domain, kite->public_domain)) &&
(0 == strcasecmp(protocol, kite->protocol))) {
if (kite->public_port <= 0)
@@ -988,16 +988,16 @@ struct pk_pagekite* pkm_add_kite(struct pk_manager* pkm,
/* FIXME: This is O(N), we'll need a nicer data structure for frontends */
for (which = 0; which < pkm->kite_max; which++) {
kite = pkm->kites+which;
- if (kite->protocol == NULL) break;
+ if (kite->protocol[0] == '\0') break;
}
if (which >= pkm->kite_max)
return pk_err_null(ERR_NO_MORE_KITES);
- kite->protocol = strdup(protocol);
- kite->auth_secret = strdup(auth_secret);
- kite->public_domain = strdup(public_domain);
+ strncpyz(kite->protocol, protocol, PK_PROTOCOL_LENGTH);
+ strncpyz(kite->auth_secret, auth_secret, PK_SECRET_LENGTH);
+ strncpyz(kite->public_domain, public_domain, PK_DOMAIN_LENGTH);
kite->public_port = public_port;
- kite->local_domain = strdup(local_domain);
+ strncpyz(kite->local_domain, local_domain, PK_DOMAIN_LENGTH);
kite->local_port = local_port;
return kite;
View
93 pkproto.c
@@ -34,12 +34,12 @@ Note: For alternate license terms, see the file COPYING.md.
void pk_reset_pagekite(struct pk_pagekite* kite)
{
- kite->protocol = NULL;
- kite->public_domain = NULL;
+ kite->protocol[0] = '\0';
+ kite->public_domain[0] = '\0';
kite->public_port = 0;
- kite->local_domain = NULL;
+ kite->local_domain[0] = '\0';
kite->local_port = 0;
- kite->auth_secret = NULL;
+ kite->auth_secret[0] = '\0';
}
void frame_reset_values(struct pk_frame* frame)
@@ -312,12 +312,7 @@ size_t pk_format_pong(char* buf)
int pk_make_bsalt(struct pk_kite_request* kite_r) {
uint8_t buffer[1024];
-
- if (kite_r->bsalt == NULL) kite_r->bsalt = malloc(41);
- if (kite_r->bsalt == NULL) {
- pk_log(PK_LOG_TUNNEL_CONNS, "WARNING: Failed to malloc() for bsalt");
- return -1;
- }
+ char digest[41];
/* FIXME: This is not very random. */
sprintf((char*) buffer, "%x %x %x %x",
@@ -334,8 +329,8 @@ int pk_make_bsalt(struct pk_kite_request* kite_r) {
pd_sha1_update(&context, buffer, strlen((const char*) buffer));
pd_sha1_final(&context, buffer);
#endif
- digest_to_hex(buffer, kite_r->bsalt);
- kite_r->bsalt[36] = '\0';
+ digest_to_hex(buffer, digest);
+ strncpyz(kite_r->bsalt, digest, PK_SALT_LENGTH);
return 1;
}
@@ -392,11 +387,10 @@ int pk_sign_kite_request(char *buffer, struct pk_kite_request* kite_r, int salt)
char request_sign[1024];
char request_salt[1024];
char proto[64];
- char* fsalt;
struct pk_pagekite* kite;
kite = kite_r->kite;
- if (kite_r->bsalt == NULL)
+ if (kite_r->bsalt[0] == '\0')
if (pk_make_bsalt(kite_r) < 0)
return 0;
@@ -405,12 +399,8 @@ int pk_sign_kite_request(char *buffer, struct pk_kite_request* kite_r, int salt)
else
strcpy(proto, kite->protocol);
- if (kite_r->fsalt != NULL)
- fsalt = kite_r->fsalt;
- else
- fsalt = "";
-
- sprintf(request, "%s:%s:%s:%s", proto, kite->public_domain, kite_r->bsalt, fsalt);
+ sprintf(request, "%s:%s:%s:%s", proto, kite->public_domain,
+ kite_r->bsalt, kite_r->fsalt);
sprintf(request_salt, "%8.8x", salt);
pk_sign(request_salt, kite->auth_secret, request, 36, request_sign);
@@ -424,27 +414,43 @@ char *pk_parse_kite_request(struct pk_kite_request* kite_r, const char *line)
{
char* copy;
char* p;
+ char* public_domain;
+ char* bsalt;
+ char* fsalt;
+ char* protocol;
struct pk_pagekite* kite = kite_r->kite;
copy = malloc(strlen(line)+1);
strcpy(copy, line);
- kite->protocol = strchr(copy, ' ');
- if (kite->protocol == NULL)
- kite->protocol = copy;
+ protocol = strchr(copy, ' ');
+ if (protocol == NULL)
+ protocol = copy;
else
- kite->protocol++;
+ protocol++;
- if (NULL == (kite->public_domain = strchr(kite->protocol, ':')))
+ if (NULL == (public_domain = strchr(protocol, ':'))) {
+ free(copy);
return pk_err_null(ERR_PARSE_NO_KITENAME);
- if (NULL == (kite_r->bsalt = strchr(kite->public_domain+1, ':')))
+ }
+ *(public_domain++) = '\0';
+
+ if (NULL == (bsalt = strchr(public_domain, ':'))) {
+ free(copy);
return pk_err_null(ERR_PARSE_NO_BSALT);
- if (NULL == (kite_r->fsalt = strchr(kite_r->bsalt+1, ':')))
+ }
+ *(bsalt++) = '\0';
+
+ if (NULL == (fsalt = strchr(bsalt, ':'))) {
+ free(copy);
return pk_err_null(ERR_PARSE_NO_FSALT);
+ }
+ *(fsalt++) = '\0';
- *(kite->public_domain++) = '\0';
- *(kite_r->bsalt++) = '\0';
- *(kite_r->fsalt++) = '\0';
+ strncpyz(kite->protocol, protocol, PK_PROTOCOL_LENGTH);
+ strncpyz(kite->public_domain, public_domain, PK_DOMAIN_LENGTH);
+ strncpyz(kite_r->bsalt, bsalt, PK_SALT_LENGTH);
+ strncpyz(kite_r->fsalt, fsalt, PK_SALT_LENGTH);
if (NULL != (p = strchr(kite->protocol, '-'))) {
*p++ = '\0';
@@ -453,7 +459,8 @@ char *pk_parse_kite_request(struct pk_kite_request* kite_r, const char *line)
else
kite->public_port = 0;
- return copy;
+ free(copy);
+ return kite->public_domain;
}
int pk_connect_ai(struct pk_conn* pkc, struct addrinfo* ai, int reconnecting,
@@ -486,7 +493,7 @@ int pk_connect_ai(struct pk_conn* pkc, struct addrinfo* ai, int reconnecting,
}
for (i = 0; i < n; i++) {
- if (requests[i].kite->protocol != NULL) {
+ if (requests[i].kite->protocol[0] != '\0') {
requests[i].status = PK_KITE_UNKNOWN;
bytes = pk_sign_kite_request(buffer, &(requests[i]), rand());
pk_log(PK_LOG_TUNNEL_DATA, " * %s", requests[i].kite->public_domain);
@@ -547,23 +554,23 @@ int pk_connect_ai(struct pk_conn* pkc, struct addrinfo* ai, int reconnecting,
if (strncasecmp(p, "X-PageKite-SignThis:", 20) == 0) {
pk_log(PK_LOG_TUNNEL_DATA, "%s", p);
tkite_r.kite = &tkite;
- pk_parse_kite_request(&tkite_r, p);
- for (j = 0; j < n; j++) {
- if ((requests[j].kite->protocol != NULL) &&
- (requests[j].kite->public_port == tkite.public_port) &&
- (0 == strcmp(requests[j].kite->public_domain, tkite.public_domain)) &&
- (0 == strcmp(requests[j].kite->protocol, tkite.protocol)))
- {
- requests[j].fsalt = strdup(tkite_r.fsalt);
- i++;
+ if (NULL != pk_parse_kite_request(&tkite_r, p)) {
+ for (j = 0; j < n; j++) {
+ if ((requests[j].kite->protocol[0] != '\0') &&
+ (requests[j].kite->public_port == tkite.public_port) &&
+ (0 == strcmp(requests[j].kite->public_domain, tkite.public_domain)) &&
+ (0 == strcmp(requests[j].kite->protocol, tkite.protocol)))
+ {
+ strncpyz(requests[j].fsalt, tkite_r.fsalt, PK_SALT_LENGTH);
+ i++;
+ }
}
}
}
else if (session_id && /* 123456789012345678901 = 21 bytes */
(strncasecmp(p, "X-PageKite-SessionID:", 21) == 0)) {
- strncpy(session_id, p+22, PK_HANDSHAKE_SESSIONID_MAX);
+ strncpyz(session_id, p+22, PK_HANDSHAKE_SESSIONID_MAX-1);
pk_log(PK_LOG_TUNNEL_DATA, "Session ID is: %s", session_id);
- session_id[PK_HANDSHAKE_SESSIONID_MAX-1] = '\0';
}
p += bytes;
} while (bytes);
View
18 pkproto.h
@@ -66,23 +66,27 @@ Note: For alternate license terms, see the file COPYING.md.
#define PK_EOF (PK_EOF_READ | PK_EOF_WRITE)
/* Data structure describing a kite */
-struct pk_pagekite {
- char* protocol;
- char* public_domain;
+#define PK_PROTOCOL_LENGTH 24
+#define PK_DOMAIN_LENGTH 1024
+#define PK_SECRET_LENGTH 256
+struct pk_pagekite {
+ char protocol[PK_PROTOCOL_LENGTH+1];
+ char public_domain[PK_DOMAIN_LENGTH+1];
int public_port;
- char* local_domain;
+ char local_domain[PK_DOMAIN_LENGTH+1];
int local_port;
- char* auth_secret;
+ char auth_secret[PK_SECRET_LENGTH+1];
};
/* Data structure describing a kite request */
#define PK_KITE_UNKNOWN 0x0000
#define PK_KITE_FLYING 0x0001
#define PK_KITE_REJECTED 0x0002
+#define PK_SALT_LENGTH 36
struct pk_kite_request {
struct pk_pagekite* kite;
- char* bsalt;
- char* fsalt;
+ char bsalt[PK_SALT_LENGTH+1];
+ char fsalt[PK_SALT_LENGTH+1];
int status;
};
View
11 pkproto_test.c
@@ -139,7 +139,6 @@ int pkproto_test_alloc(unsigned int buf_len, char *buffer, struct pk_parser* p)
int pkproto_test_make_bsalt(void) {
struct pk_kite_request kite;
- kite.bsalt = NULL;
pk_make_bsalt(&kite);
assert(strlen(kite.bsalt) == 36);
return 1;
@@ -153,12 +152,12 @@ int pkproto_test_sign_kite_request(void) {
char buffer[120];
kite_r.kite = &kite;
- kite.public_domain = "testkite.com";
+ strcpy(kite.public_domain, "testkite.com");
kite.public_port = 99;
- kite.auth_secret = "wigglybop";
- kite.protocol = "http";
- kite_r.bsalt = "123456789012345678901234567890123456";
- kite_r.fsalt = NULL;
+ strcpy(kite.auth_secret, "wigglybop");
+ strcpy(kite.protocol, "http");
+ strcpy(kite_r.bsalt, "123456789012345678901234567890123456");
+ strcpy(kite_r.fsalt, "");
bytes = pk_sign_kite_request(buffer, &kite_r, 1);
assert(bytes == strlen(expected));
View
2  utils.h
@@ -19,6 +19,8 @@ Note: For alternate license terms, see the file COPYING.md.
******************************************************************************/
+#define strncpyz(dest, src, len) { strncpy(dest, src, len); dest[len] = '\0'; }
+
int zero_first_crlf(int, char*);
int dbg_write(int, char *, int);
int set_non_blocking(int);
Please sign in to comment.
Something went wrong with that request. Please try again.