Skip to content

Commit

Permalink
Major code cleanup to allow smoother gcc compilation using alt options.
Browse files Browse the repository at this point in the history
  • Loading branch information
ladar committed Feb 9, 2017
1 parent dc4f494 commit 3af9324
Show file tree
Hide file tree
Showing 31 changed files with 119 additions and 109 deletions.
2 changes: 1 addition & 1 deletion check/magma/magma_check.c
Expand Up @@ -52,7 +52,7 @@ Suite * suite_check_barrister(chr_t *testname) {

TCase *tc;
void *handle;
void (*unittest)(int);
void *unittest;
Suite *s = suite_create("\tBarrister");;

// By calling dlopen() with NULL for the filename, we are attempting to establish a handle for the currently running executable image.
Expand Down
6 changes: 3 additions & 3 deletions check/magma/providers/compress_check.c
Expand Up @@ -10,9 +10,9 @@
bool_t check_compress_sthread(check_compress_opt_t *opts) {

size_t rlen;
stringer_t *output;
compress_t *compress;
unsigned char *original;
stringer_t *output = NULL;
compress_t *compress = NULL;
unsigned char *original = NULL;

for (uint64_t i = 0; status() && i < COMPRESS_CHECK_ITERATIONS; i++) {

Expand Down
2 changes: 1 addition & 1 deletion check/magma/providers/provide_check.c
Expand Up @@ -44,7 +44,7 @@ END_TEST
//! Compression Engine Tests
START_TEST (check_compress_lzo_s) {

bool_t outcome;
bool_t outcome = true;
check_compress_opt_t opts = {
.engine = COMPRESS_ENGINE_LZO
};
Expand Down
4 changes: 2 additions & 2 deletions src/core/host/spool.c
Expand Up @@ -110,8 +110,8 @@ int_t spool_check(stringer_t *path) {
int_t spool_mktemp(int_t spool, chr_t *prefix) {

time_t now;
int_t fd, err_info;
stringer_t *path, *template, *base = NULL;
int_t fd = -1, err_info;
stringer_t *path = NULL, *template = NULL, *base = NULL;

if (!prefix) prefix = "magma";

Expand Down
2 changes: 1 addition & 1 deletion src/core/strings/allocation.c
Expand Up @@ -483,8 +483,8 @@ int_t st_append_out(size_t align, stringer_t **s, stringer_t *append) {
*/
stringer_t * st_alloc_opts(uint32_t opts, size_t len) {

int handle;
void *joint;
int handle = -1;
size_t avail = 0;
stringer_t *result = NULL;
void (*release)(void *buffer) = opts & SECURE ? &mm_sec_free : &mm_free;
Expand Down
27 changes: 18 additions & 9 deletions src/engine/context/signal.c
Expand Up @@ -56,9 +56,9 @@ void signal_shutdown(int signal) {
char working[64];
struct stat64 info;
struct rlimit64 limits;
struct sockaddr_in6 saddr;
socklen_t len = sizeof(struct sockaddr_in6);
pthread_t status_thread;
struct sockaddr *saddr = MEMORYBUF(sizeof(struct sockaddr_in6));
socklen_t len = sizeof(struct sockaddr_in6);
const struct timespec split = { .tv_sec = 0, .tv_nsec = 100000000 }, single = { .tv_sec = 1, .tv_nsec = 0 };

// We assume the server is being shutdown for a good reason.
Expand Down Expand Up @@ -95,19 +95,28 @@ void signal_shutdown(int signal) {
mm_wipe(&info, sizeof(struct stat64));
mm_wipe(&saddr, sizeof(struct sockaddr_in6));

/// LOW: This only compares the port number for the sockets. We should also ensure the socket is owned by magmad, and/or
/// that the server used INADDR_ANY or IN6ADDR_ANY_INIT, otherwise the logic below will close sockets that could be owned by
/// other processes on the system that are using the same port number, while bound to a different IP interface.
// Look for socket descriptors using ports assigned to server instances and close them.
if (!fstat64(fd, &info) && S_ISSOCK(info.st_mode) && !getsockname(fd, &saddr, &len)) {
if (!fstat64(fd, &info) && S_ISSOCK(info.st_mode) && !getsockname(fd, saddr, &len)) {

if (len == sizeof(struct sockaddr_in6) && saddr.sin6_family == AF_INET6 && servers_get_count_using_port(ntohs(saddr.sin6_port))) {
mm_copy(&(ip.ip6), &(saddr.sin6_addr), sizeof(struct in6_addr));
if (len == sizeof(struct sockaddr_in6) && ((struct sockaddr_in6 *)saddr)->sin6_family == AF_INET6 &&
servers_get_count_using_port(ntohs(((struct sockaddr_in6 *)saddr)->sin6_port))) {

mm_copy(&(ip.ip6), &(((struct sockaddr_in6 *)saddr)->sin6_addr), sizeof(struct in6_addr));
ip.family = AF_INET6;
log_info("%s:%u is being shutdown.", st_char_get(ip_presentation(&ip, PLACER(working, 64))), ntohs(saddr.sin6_port));
log_info("%s:%u is being shutdown.", st_char_get(ip_presentation(&ip, PLACER(working, 64))),
ntohs(((struct sockaddr_in6 *)saddr)->sin6_port));
close(fd);
}
else if (len == sizeof(struct sockaddr_in) && ((struct sockaddr_in *)&saddr)->sin_family == AF_INET && servers_get_count_using_port(ntohs(((struct sockaddr_in *)&saddr)->sin_port))) {
mm_copy(&(ip.ip4), &(((struct sockaddr_in *)&saddr)->sin_addr), sizeof(struct in_addr));
else if (len == sizeof(struct sockaddr_in) && (((struct sockaddr_in *)saddr)->sin_family == AF_INET &&
servers_get_count_using_port(ntohs(((struct sockaddr_in *)saddr)->sin_port)))) {

mm_copy(&(ip.ip4), &(((struct sockaddr_in *)saddr)->sin_addr), sizeof(struct in_addr));
ip.family = AF_INET;
log_info("%s:%u is being shutdown.", st_char_get(ip_presentation(&ip, PLACER(working, 64))), ntohs(((struct sockaddr_in *)&saddr)->sin_port));
log_info("%s:%u is being shutdown.", st_char_get(ip_presentation(&ip, PLACER(working, 64))),
ntohs(((struct sockaddr_in *)saddr)->sin_port));
close(fd);
}
}
Expand Down
15 changes: 7 additions & 8 deletions src/network/addresses.c
Expand Up @@ -36,7 +36,6 @@
* @param src a pointer to the source IP address object to be copied.
* @param NULL on failure, or a pointer to the destination IP address buffer on success.
*/
// QUESTION: Should return NULL if !src?
ip_t * ip_copy(ip_t *dst, ip_t *src) {

if (dst && src) {
Expand Down Expand Up @@ -390,7 +389,7 @@ bool_t ip_address_equal(ip_t *ip1, ip_t *ip2) {
ip_t * con_addr(connection_t *con, ip_t *output) {

ip_t *result;
struct sockaddr_in6 address;
struct sockaddr *address = MEMORYBUF(sizeof(struct sockaddr_in6));
socklen_t len = sizeof(struct sockaddr_in6);

if (!(result = output) && !(result = mm_alloc(sizeof(ip_t)))) {
Expand All @@ -399,7 +398,7 @@ ip_t * con_addr(connection_t *con, ip_t *output) {
}

// Extract the socket structure.
else if (getpeername(con->network.sockd, &(address), &len)) {
else if (getpeername(con->network.sockd, address, &len)) {

if (!output) {
mm_free(result);
Expand All @@ -409,12 +408,12 @@ ip_t * con_addr(connection_t *con, ip_t *output) {
}

// Classify and copy to the result.
else if (len == sizeof(struct sockaddr_in6) && address.sin6_family == AF_INET6) {
mm_copy(&(result->ip6), &(address.sin6_addr), sizeof(struct in6_addr));
else if (len == sizeof(struct sockaddr_in6) && ((struct sockaddr_in6 *)address)->sin6_family == AF_INET6) {
mm_copy(&(result->ip6), &(((struct sockaddr_in6 *)address)->sin6_addr), sizeof(struct in6_addr));
result->family = AF_INET6;
}
else if (len == sizeof(struct sockaddr_in) && ((struct sockaddr_in *)&address)->sin_family == AF_INET) {
mm_copy(&(result->ip4), &(((struct sockaddr_in *)&address)->sin_addr), sizeof(struct in_addr));
else if (len == sizeof(struct sockaddr_in) && ((struct sockaddr_in *)address)->sin_family == AF_INET) {
mm_copy(&(result->ip4), &(((struct sockaddr_in *)address)->sin_addr), sizeof(struct in_addr));
result->family = AF_INET;
}

Expand Down Expand Up @@ -665,8 +664,8 @@ bool_t ip_str_subnet(chr_t *substr, subnet_t *out) {
*/
bool_t ip_matches_subnet(subnet_t *subnet, ip_t *addr) {

uint_t byteno = 0, bitno;
uchr_t this_mask;
uint_t byteno = 0, bitno = 0;

if (subnet->address.family != addr->family) {
return false;
Expand Down
7 changes: 2 additions & 5 deletions src/network/read.c
Expand Up @@ -213,8 +213,8 @@ int64_t con_read(connection_t *con) {
int64_t client_read_line(client_t *client) {

ssize_t bytes;
int_t sslerr = -1;
bool_t line = false;
int sslerr;

if (!client || client->sockd == -1) {
client->status = 1;
Expand Down Expand Up @@ -295,14 +295,11 @@ int64_t client_read_line(client_t *client) {
return pl_length_get(client->line);
}

/**
*/
int64_t client_read(client_t *client) {

ssize_t bytes;
bool_t blocking;
int sslerr;
int_t sslerr = -1;

if (!client || client->sockd == -1) {
client->status = -1;
Expand Down
29 changes: 17 additions & 12 deletions src/network/reverse.c
Expand Up @@ -106,31 +106,37 @@ stringer_t * con_reverse_check(connection_t *con, uint32_t timeout) {
*/
void con_reverse_lookup(connection_t *con) {

void *addr;
int_t ecode;
void *addr = NULL;
stringer_t *domain;
sa_family_t family;
struct sockaddr_in6 address;
struct hostent host, *hostp;
sa_family_t family = AF_UNSPEC;
socklen_t len = sizeof(struct sockaddr_in6);
struct sockaddr *address = MEMORYBUF(sizeof(struct sockaddr_in6));

// Perform the lookup.
if (getpeername(con->network.sockd, &address, &len)) {
if (getpeername(con->network.sockd, address, &len)) {
con_reverse_status(con, REVERSE_ERROR);
con_destroy(con);
return;
}

if (address.sin6_family == AF_INET6) {
family = address.sin6_family;
addr = (void *)&(address.sin6_addr);
else if (len == sizeof(struct sockaddr_in6) && ((struct sockaddr_in6 *)address)->sin6_family == AF_INET6) {
family = ((struct sockaddr_in6 *)address)->sin6_family;
addr = &(((struct sockaddr_in6 *)address)->sin6_addr);
}
else if (address.sin6_family == AF_INET) {
family = address.sin6_family;
addr = (void *)&(((struct sockaddr_in *)&address)->sin_addr);
else if (len == sizeof(struct sockaddr_in) && ((struct sockaddr_in *)address)->sin_family == AF_INET) {
family = ((struct sockaddr_in *)address)->sin_family;
addr = &(((struct sockaddr_in *)address)->sin_addr);
}
else {
con_reverse_status(con, REVERSE_ERROR);
con_destroy(con);
return;
}

if (!(gethostbyaddr_r(addr, len, family, &host, bufptr, buflen, &hostp, &ecode)) &&
// Only perform the reverse address lookup if the socket IP address is determined above.
if (!(gethostbyaddr_r(addr, len, family, &host, MEMORYBUF(1024), 1024, &hostp, &ecode)) &&
(len = ns_length_get(host.h_name)) && (domain = st_import(host.h_name, len))) {
con_reverse_domain(con, domain, REVERSE_COMPLETE);
}
Expand All @@ -139,6 +145,5 @@ void con_reverse_lookup(connection_t *con) {
}

con_destroy(con);

return;
}
6 changes: 3 additions & 3 deletions src/network/write.c
Expand Up @@ -23,7 +23,7 @@
int64_t con_write_bl(connection_t *con, char *block, size_t length) {

ssize_t written, position = 0;
int sslerr;
int sslerr = -1;

if (!con || con->network.sockd == -1) {
con->network.status = -1;
Expand Down Expand Up @@ -195,8 +195,8 @@ int64_t client_write(client_t *client, stringer_t *s) {

uchr_t *block;
size_t length;
int sslerr = -1;
ssize_t written, position = 0;
int sslerr;

if (!client || client->sockd == -1) {
client->status = -1;
Expand Down Expand Up @@ -274,8 +274,8 @@ int64_t client_write(client_t *client, stringer_t *s) {
int64_t client_print(client_t *client, chr_t *format, ...) {

va_list args;
int64_t result;
stringer_t *buffer;
int64_t result = -1;

if (!client || client->sockd == -1) {
return -1;
Expand Down
2 changes: 1 addition & 1 deletion src/objects/contacts/contacts.c
Expand Up @@ -224,7 +224,7 @@ int_t contact_remove(contact_t *contact, uint64_t usernum, uint64_t foldernum) {
int_t contact_move(contact_folder_t *source, contact_folder_t *target, contact_t *contact, uint64_t usernum) {

inx_t *swap;
contact_t *dupe;
contact_t *dupe = NULL;
multi_t multi = { .type = M_TYPE_UINT64, .val.u64 = 0 };

if (!contact || !contact->contactnum || !usernum || !source || !source->foldernum || !target || !target->foldernum) {
Expand Down
6 changes: 3 additions & 3 deletions src/providers/compress/bzip.c
Expand Up @@ -61,10 +61,10 @@ bool_t lib_load_bzip(void) {
stringer_t * decompress_bzip(compress_t *compressed) {

int ret;
void *bptr;
uint64_t hash, rlen, blen;
void *bptr = NULL;
stringer_t *result = NULL;
compress_head_t *head;
compress_head_t *head = NULL;
uint64_t hash = 0, rlen = 0, blen = 0;

if (!(head = (compress_head_t *)compressed)) {
log_info("Invalid compression header. {compress_head = NULL}");
Expand Down
8 changes: 4 additions & 4 deletions src/providers/compress/lzo.c
Expand Up @@ -85,11 +85,11 @@ stringer_t * decompress_block_lzo(stringer_t *block) {
*/
stringer_t * decompress_lzo(compress_t *compressed) {

int ret;
void *bptr;
uint64_t hash, rlen, blen;
int_t ret = 0;
void *bptr = NULL;
stringer_t *result = NULL;
compress_head_t *head;
compress_head_t *head = NULL;
uint64_t hash = 0, rlen = 0, blen = 0;

if (!(head = (compress_head_t *)compressed)) {
log_info("Invalid compression header. {compress_head = NULL}");
Expand Down
9 changes: 4 additions & 5 deletions src/providers/compress/zlib.c
Expand Up @@ -11,7 +11,6 @@ int (*deflateEnd_d)(z_streamp strm) = NULL;
int (*deflate_d)(z_streamp strm, int flush) = NULL;
int (*deflateInit2__d)(z_streamp strm, int level, int method, int windowBits, int memLevel, int strategy, const char *version, int stream_size) = NULL;


/**
* @brief Return the version string of zlib.
* @return a pointer to a character string containing the zlib version information.
Expand Down Expand Up @@ -44,11 +43,11 @@ bool_t lib_load_zlib(void) {
*/
stringer_t * decompress_zlib(compress_t *compressed) {

int ret;
void *bptr;
uint64_t hash, rlen, blen;
int_t ret = 0;
void *bptr = NULL;
stringer_t *result = NULL;
compress_head_t *head;
compress_head_t *head = NULL;
uint64_t hash = 0, rlen = 0, blen = 0;

if (!(head = (compress_head_t *)compressed)) {
log_info("Invalid compression header. {compress_head = NULL}");
Expand Down
2 changes: 1 addition & 1 deletion src/providers/cryptography/hash.c
Expand Up @@ -11,7 +11,7 @@ stringer_t * hash_digest(digest_t *digest, stringer_t *s, stringer_t *output) {

int_t olen;
uint_t rlen;
uint32_t opts;
uint32_t opts = 0;
EVP_MD_CTX ctx;
stringer_t *result = NULL;

Expand Down
2 changes: 1 addition & 1 deletion src/providers/cryptography/hmac.c
Expand Up @@ -21,7 +21,7 @@ stringer_t * deprecated_hmac_digest(digest_t *digest, stringer_t *s, stringer_t

int_t olen;
uint_t rlen;
uint32_t opts;
uint32_t opts = 0;
HMAC_CTX hmac;
stringer_t *result = NULL;

Expand Down
3 changes: 2 additions & 1 deletion src/providers/cryptography/scramble.c
Expand Up @@ -103,8 +103,9 @@ scramble_t * deprecated_scramble_import(stringer_t *s) {

void *bptr;
scramble_t *buffer;
uint64_t blen, vlen;
scramble_head_t *head;
uint64_t blen = 0, vlen = 0;

#ifdef MAGMA_PEDANTIC
uint32_t hash = 0;
#endif
Expand Down
2 changes: 1 addition & 1 deletion src/providers/cryptography/stacie.c
Expand Up @@ -397,7 +397,7 @@ stringer_t * stacie_hashed_key_derive(stringer_t *base, uint32_t rounds, stringe
// To help mitigate the load generated by processing very short passwords, we sleep for a microsecond, with the number
// gradually increasing, as the number of rounds increases. Unfortunately this means a password with a very large number
// rounds may not finish in time to satisfy the user.
usleep(count);
if (0xFFFF0000 & count) usleep(count & 0xFFFF0000);
#endif

}
Expand Down
4 changes: 2 additions & 2 deletions src/providers/deprecated/hmac.c
Expand Up @@ -22,8 +22,8 @@ stringer_t * hmac_digest(digest_t *digest, stringer_t *s, stringer_t *key, strin

int_t olen;
uint_t rlen;
uint32_t opts;
HMAC_CTX hmac;
uint32_t opts = 0;
stringer_t *result = NULL;

// Ensure a digest pointer was passed in and that we can retrieve the output length.
Expand All @@ -36,7 +36,7 @@ stringer_t * hmac_digest(digest_t *digest, stringer_t *s, stringer_t *key, strin
return NULL;
}
else if (st_empty(s)) {
log_pedantic("The input string does not appear to have any data ready for encoding. {%slen = %zu}", s ? "" : "s = NULL / ", s ? st_length_get(s) : 0);
log_pedantic("The input string does not appear to have any data ready for encoding. { %slen = %zu }", s ? "" : "s = NULL / ", s ? st_length_get(s) : 0);
return NULL;
}

Expand Down
2 changes: 1 addition & 1 deletion src/providers/deprecated/scramble.c
Expand Up @@ -104,7 +104,7 @@ scramble_t * scramble_import(stringer_t *s) {

void *bptr;
scramble_t *buffer;
uint64_t blen, vlen;
uint64_t blen = 0, vlen = 0;
scramble_head_t *head;
#ifdef MAGMA_PEDANTIC
uint32_t hash = 0;
Expand Down

0 comments on commit 3af9324

Please sign in to comment.