1/*! 2 * @file 3 * Netatalk utility functions 4 * 5 * Utility functions for these areas: \n 6 * * sockets \n 7 * * locking \n 8 * * misc UNIX function wrappers, eg for getcwd 9 */ 10 11#ifndef _ATALK_UTIL_H 12#define _ATALK_UTIL_H 1 13 14#include <sys/cdefs.h> 15#include <sys/types.h> 16#ifdef HAVE_UNISTD_H 17#include <unistd.h> 18#endif /* HAVE_UNISTD_H */ 19#include <poll.h> 20#include <netatalk/at.h> 21 22#include <atalk/unicode.h> 23#include <atalk/bstrlib.h> 24 25/* exit error codes */ 26#define EXITERR_CLNT 1 /* client related error */ 27#define EXITERR_CONF 2 /* error in config files/cmd line parameters */ 28#define EXITERR_SYS 3 /* local system error */ 29 30/* Print a SBT and exit */ 31#define AFP_PANIC(why) \ 32 do { \ 33 netatalk_panic(why); \ 34 abort(); \ 35 } while(0); 36 37/* LOG assert errors */ 38#ifndef NDEBUG 39#define AFP_ASSERT(b) \ 40 do { \ 41 if (!(b)) { \ 42 AFP_PANIC(#b); \ 43 } \ 44 } while(0); 45#else 46#define AFP_ASSERT(b) 47#endif /* NDEBUG */ 48 49#define STRCMP(a,b,c) (strcmp(a,c) b 0) 50 51#if BYTE_ORDER == BIG_ENDIAN 52#define hton64(x) (x) 53#define ntoh64(x) (x) 54#else /* BYTE_ORDER == BIG_ENDIAN */ 55#define hton64(x) ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \ 56 (uint64_t) ((htonl(x) & 0xffffffffLL) << 32)) 57#define ntoh64(x) (hton64(x)) 58#endif /* BYTE_ORDER == BIG_ENDIAN */ 59 60#ifdef WITH_SENDFILE 61extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count); 62#endif 63 64extern const int _diacasemap[], _dialowermap[]; 65 66extern char **getifacelist(void); 67extern void freeifacelist(char **); 68 69#define diatolower(x) _dialowermap[(unsigned char) (x)] 70#define diatoupper(x) _diacasemap[(unsigned char) (x)] 71extern int atalk_aton (char *, struct at_addr *); 72extern void bprint (char *, int); 73extern int strdiacasecmp (const char *, const char *); 74extern int strndiacasecmp (const char *, const char *, size_t); 75extern pid_t server_lock (char * /*program*/, char * /*file*/, int /*debug*/); 76extern void fault_setup (void (*fn)(void *)); 77extern void netatalk_panic(const char *why); 78#define server_unlock(x) (unlink(x)) 79 80/* strlcpy and strlcat are used by pam modules */ 81#ifndef UAM_MODULE_EXPORT 82#define UAM_MODULE_EXPORT 83#endif 84 85#ifndef HAVE_STRLCPY 86UAM_MODULE_EXPORT size_t strlcpy (char *, const char *, size_t); 87#endif 88 89#ifndef HAVE_STRLCAT 90UAM_MODULE_EXPORT size_t strlcat (char *, const char *, size_t); 91#endif 92 93#ifndef HAVE_DLFCN_H 94extern void *mod_open (const char *); 95extern void *mod_symbol (void *, const char *); 96extern void mod_close (void *); 97#define mod_error() "" 98#else /* ! HAVE_DLFCN_H */ 99#include <dlfcn.h> 100 101#ifndef RTLD_NOW 102#define RTLD_NOW 1 103#endif /* ! RTLD_NOW */ 104 105/* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY. 106 * OpenBSD currently does not use the second arg for dlopen(). For 107 * future compatibility we define DL_LAZY */ 108#ifdef __NetBSD__ 109#define mod_open(a) dlopen(a, RTLD_LAZY) 110#elif defined(__OpenBSD__) 111#define mod_open(a) dlopen(a, DL_LAZY) 112#else /* ! __NetBSD__ && ! __OpenBSD__ */ 113#define mod_open(a) dlopen(a, RTLD_NOW) 114#endif /* __NetBSD__ */ 115 116#ifndef DLSYM_PREPEND_UNDERSCORE 117#define mod_symbol(a, b) dlsym(a, b) 118#else /* ! DLSYM_PREPEND_UNDERSCORE */ 119extern void *mod_symbol (void *, const char *); 120#endif /* ! DLSYM_PREPEND_UNDERSCORE */ 121#define mod_error() dlerror() 122#define mod_close(a) dlclose(a) 123#endif /* ! HAVE_DLFCN_H */ 124 125/****************************************************************** 126 * locking.c 127 ******************************************************************/ 128 129extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len); 130#define read_lock(fd, offset, whence, len) \ 131 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len)) 132#define write_lock(fd, offset, whence, len) \ 133 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len)) 134#define unlock(fd, offset, whence, len) \ 135 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len)) 136 137/****************************************************************** 138 * socket.c 139 ******************************************************************/ 140 141extern int setnonblock(int fd, int cmd); 142extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout); 143extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout); 144extern const char *getip_string(const struct sockaddr *sa); 145extern unsigned int getip_port(const struct sockaddr *sa); 146extern void apply_ip_mask(struct sockaddr *ai, int maskbits); 147extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2); 148 149/* Structures and functions dealing with dynamic pollfd arrays */ 150enum fdtype {IPC_FD, LISTEN_FD, DISASOCIATED_IPC_FD}; 151struct polldata { 152 enum fdtype fdtype; /* IPC fd or listening socket fd */ 153 void *data; /* pointer to AFPconfig for listening socket and * 154 * pointer to afp_child_t for IPC fd */ 155}; 156 157extern void fdset_add_fd(struct pollfd **fdsetp, 158 struct polldata **polldatap, 159 int *fdset_usedp, 160 int *fdset_sizep, 161 int fd, 162 enum fdtype fdtype, 163 void *data); 164extern void fdset_del_fd(struct pollfd **fdsetp, 165 struct polldata **polldatap, 166 int *fdset_usedp, 167 int *fdset_sizep, 168 int fd); 169extern int send_fd(int socket, int fd); 170extern int recv_fd(int fd, int nonblocking); 171 172/****************************************************************** 173 * unix.c 174 *****************************************************************/ 175 176extern const char *getcwdpath(void); 177extern char *stripped_slashes_basename(char *p); 178extern int lchdir(const char *dir); 179extern void randombytes(void *buf, int n); 180#endif /* _ATALK_UTIL_H */ 181 182/****************************************************************** 183 * cnid.c 184 *****************************************************************/ 185 186extern bstring rel_path_in_vol(const char *path, const char *volpath); 187