summaryrefslogtreecommitdiffstats
path: root/src/server/helper.h
blob: 78a1c41d31bc1c2321dcd313f931874674f48c70 (plain) (blame)
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
#ifndef HELPER_H_
#define HELPER_H_

#include "server.h"
#include "../shared/log.h"
#include "../types.h"
#include <netinet/in.h>
#include <string.h>
#include <unistd.h>

#define ERROR_GOTO(jumplabel, ...) do { logadd(LOG_ERROR, __VA_ARGS__); goto jumplabel; } while (0);

bool parse_address(char *string, dnbd3_host_t *host);
bool host_to_string(const dnbd3_host_t *host, char *target, size_t targetlen);
void remove_trailing_slash(char *string);
void trim_right(char * const string);
void setThreadName(const char *name);
void blockNoncriticalSignals();

static inline bool isSameAddress(const dnbd3_host_t * const a, const dnbd3_host_t * const b)
{
	return (a->type == b->type) && (0 == memcmp( a->addr, b->addr, (a->type == HOST_IP4 ? 4 : 16) ));
}

static inline bool isSameAddressPort(const dnbd3_host_t * const a, const dnbd3_host_t * const b)
{
	return (a->type == b->type) && (a->port == b->port) && (0 == memcmp( a->addr, b->addr, (a->type == HOST_IP4 ? 4 : 16) ));
}

/**
 * Test whether string ends in suffix.
 * @return true if string =~ /suffix$/
 */
static inline bool strend(char *string, char *suffix)
{
	if ( string == NULL ) return false;
	if ( suffix == NULL || *suffix == '\0' ) return true;
	const size_t len1 = strlen( string );
	const size_t len2 = strlen( suffix );
	if ( len2 > len1 ) return false;
	return strcmp( string + len1 - len2, suffix ) == 0;
}

#endif