3 * Netatalk utility functions
5 * Utility functions for these areas: \n
8 * * misc UNIX function wrappers, eg for getcwd
12 #define _ATALK_UTIL_H 1
14 #include <sys/types.h>
15 #include <sys/socket.h>
19 #include <atalk/unicode.h>
20 #include <atalk/bstrlib.h>
22 /* exit error codes */
23 #define EXITERR_CLNT 1 /* client related error */
24 #define EXITERR_CONF 2 /* error in config files/cmd line parameters */
25 #define EXITERR_SYS 3 /* local system error */
27 /* Print a SBT and exit */
28 #define AFP_PANIC(why) \
30 netatalk_panic(why); \
34 /* LOG assert errors */
36 #define AFP_ASSERT(b) \
47 #define MIN(a, b) ((a) < (b) ? (a) : (b))
51 #define MAX(a, b) ((a) > (b) ? (a) : (b))
54 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
55 #define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
56 #define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
58 #if BYTE_ORDER == BIG_ENDIAN
61 #else /* BYTE_ORDER == BIG_ENDIAN */
62 #define hton64(x) ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
63 (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
64 #define ntoh64(x) (hton64(x))
65 #endif /* BYTE_ORDER == BIG_ENDIAN */
68 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
71 extern const int _diacasemap[], _dialowermap[];
73 extern char **getifacelist(void);
74 extern void freeifacelist(char **);
76 #define diatolower(x) _dialowermap[(unsigned char) (x)]
77 #define diatoupper(x) _diacasemap[(unsigned char) (x)]
78 extern void bprint (char *, int);
79 extern int strdiacasecmp (const char *, const char *);
80 extern int strndiacasecmp (const char *, const char *, size_t);
81 extern pid_t server_lock (char * /*program*/, char * /*file*/, int /*debug*/);
82 extern void fault_setup (void (*fn)(void *));
83 extern void netatalk_panic(const char *why);
84 #define server_unlock(x) (unlink(x))
87 extern void *mod_open (const char *);
88 extern void *mod_symbol (void *, const char *);
89 extern void mod_close (void *);
90 #define mod_error() ""
91 #else /* ! HAVE_DLFCN_H */
96 #endif /* ! RTLD_NOW */
98 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
99 * OpenBSD currently does not use the second arg for dlopen(). For
100 * future compatibility we define DL_LAZY */
102 #define mod_open(a) dlopen(a, RTLD_LAZY)
103 #elif defined(__OpenBSD__)
104 #define mod_open(a) dlopen(a, DL_LAZY)
105 #else /* ! __NetBSD__ && ! __OpenBSD__ */
106 #define mod_open(a) dlopen(a, RTLD_NOW)
107 #endif /* __NetBSD__ */
109 #ifndef DLSYM_PREPEND_UNDERSCORE
110 #define mod_symbol(a, b) dlsym(a, b)
111 #else /* ! DLSYM_PREPEND_UNDERSCORE */
112 extern void *mod_symbol (void *, const char *);
113 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
114 #define mod_error() dlerror()
115 #define mod_close(a) dlclose(a)
116 #endif /* ! HAVE_DLFCN_H */
118 /******************************************************************
120 ******************************************************************/
122 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
123 #define read_lock(fd, offset, whence, len) \
124 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
125 #define write_lock(fd, offset, whence, len) \
126 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
127 #define unlock(fd, offset, whence, len) \
128 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
130 /******************************************************************
132 ******************************************************************/
134 extern int setnonblock(int fd, int cmd);
135 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
136 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
137 extern const char *getip_string(const struct sockaddr *sa);
138 extern unsigned int getip_port(const struct sockaddr *sa);
139 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
140 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
142 /* Structures and functions dealing with dynamic pollfd arrays */
143 enum fdtype {IPC_FD, LISTEN_FD, DISASOCIATED_IPC_FD};
145 enum fdtype fdtype; /* IPC fd or listening socket fd */
146 void *data; /* pointer to AFPconfig for listening socket and *
147 * pointer to afp_child_t for IPC fd */
150 extern void fdset_add_fd(int maxconns,
151 struct pollfd **fdsetp,
152 struct polldata **polldatap,
158 extern void fdset_del_fd(struct pollfd **fdsetp,
159 struct polldata **polldatap,
163 extern int send_fd(int socket, int fd);
164 extern int recv_fd(int fd, int nonblocking);
166 /******************************************************************
168 *****************************************************************/
170 extern const char *abspath(const char *name);
171 extern const char *getcwdpath(void);
172 extern char *stripped_slashes_basename(char *p);
173 extern int lchdir(const char *dir);
174 extern void randombytes(void *buf, int n);
175 #endif /* _ATALK_UTIL_H */
177 /******************************************************************
179 *****************************************************************/
181 extern bstring rel_path_in_vol(const char *path, const char *volpath);