summaryrefslogblamecommitdiffstats
path: root/src/server/helper.h
blob: e108d8158f5a02427b511a5c8945dbb5ff0c6f3a (plain) (tree)
1
2
3
4
5
6
7
8
9



                   
                

                       

                   
                     
 
                                                                                                    
 

                                                                              
                              
                                         
                                     
                               
                               
 
                                                                                            



                                                                                                        
                                                                                                
 
                                                                                                                                


   


                                      

                                                    

                                                             

                                             
                                        
                                                           

 
      
#ifndef HELPER_H_
#define HELPER_H_

#include "server.h"
#include "log.h"
#include <netinet/in.h>
#include <string.h>
#include <errno.h>
#include <unistd.h>
#include "../types.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 strtolower(char *string);
void remove_trailing_slash(char *string);
void trim_right(char * const string);
void setThreadName(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 == AF_INET ? 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 == AF_INET ? 4 : 16) ));
}

/**
 * Test whether string ends in suffix.
 * @return true if string =~ /suffix$/
 */
static inline int 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