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/types.h>
15#include <sys/socket.h>
16#include <unistd.h>
17#include <poll.h>
18#include <sys/stat.h>
19
20#include <atalk/unicode.h>
21#include <atalk/bstrlib.h>
22
23/* exit error codes */
24#define EXITERR_CLNT 1  /* client related error */
25#define EXITERR_CONF 2  /* error in config files/cmd line parameters */
26#define EXITERR_SYS  3  /* local system error */
27
28/* Print a SBT and exit */
29#define AFP_PANIC(why) \
30    do {                                            \
31        netatalk_panic(why);                        \
32        abort();                                    \
33    } while(0);
34
35/* LOG assert errors */
36#ifndef NDEBUG
37#define AFP_ASSERT(b) \
38    do {                                                                \
39        if (!(b)) {                                                     \
40            AFP_PANIC(#b);                                              \
41        } \
42    } while(0);
43#else
44#define AFP_ASSERT(b)
45#endif /* NDEBUG */
46
47#ifndef MIN
48#define MIN(a, b)  ((a) < (b) ? (a) : (b))
49#endif
50
51#ifndef MAX
52#define MAX(a, b)  ((a) > (b) ? (a) : (b))
53#endif
54
55#define STRCMP(a,b,c) (strcmp(a,c) b 0)
56#define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
57#define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
58#ifndef MAX
59#define MAX(a,b) ((a) > (b) ? a : b)
60#endif
61#ifndef MIN
62#define MIN(a,b) ((a) < (b) ? a : b)
63#endif
64
65#ifdef WORDS_BIGENDIAN
66#define hton64(x)       (x)
67#define ntoh64(x)       (x)
68#else
69#define hton64(x)       ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
70                         (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
71#define ntoh64(x)       (hton64(x))
72#endif
73
74#ifdef WITH_SENDFILE
75extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
76#endif
77
78extern const int _diacasemap[], _dialowermap[];
79
80extern char **getifacelist(void);
81extern void freeifacelist(char **);
82
83#define diatolower(x)     _dialowermap[(unsigned char) (x)]
84#define diatoupper(x)     _diacasemap[(unsigned char) (x)]
85extern void bprint        (char *, int);
86extern int strdiacasecmp  (const char *, const char *);
87extern int strndiacasecmp (const char *, const char *, size_t);
88extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
89extern int check_lockfile (const char *program, const char *pidfile);
90extern int create_lockfile(const char *program, const char *pidfile);
91extern void fault_setup	  (void (*fn)(void *));
92extern void netatalk_panic(const char *why);
93#define server_unlock(x)  (unlink(x))
94
95#ifndef HAVE_DLFCN_H
96extern void *mod_open    (const char *);
97extern void *mod_symbol  (void *, const char *);
98extern void mod_close    (void *);
99#define mod_error()      ""
100#else /* ! HAVE_DLFCN_H */
101#include <dlfcn.h>
102
103#ifndef RTLD_NOW
104#define RTLD_NOW 1
105#endif /* ! RTLD_NOW */
106
107/* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
108 * OpenBSD currently does not use the second arg for dlopen(). For
109 * future compatibility we define DL_LAZY */
110#ifdef __NetBSD__
111#define mod_open(a)      dlopen(a, RTLD_LAZY)
112#elif defined(__OpenBSD__)
113#define mod_open(a)      dlopen(a, DL_LAZY)
114#else /* ! __NetBSD__ && ! __OpenBSD__ */
115#define mod_open(a)      dlopen(a, RTLD_NOW)
116#endif /* __NetBSD__ */
117
118#ifndef DLSYM_PREPEND_UNDERSCORE
119#define mod_symbol(a, b) dlsym(a, b)
120#else /* ! DLSYM_PREPEND_UNDERSCORE */
121extern void *mod_symbol  (void *, const char *);
122#endif /* ! DLSYM_PREPEND_UNDERSCORE */
123#define mod_error()      dlerror()
124#define mod_close(a)     dlclose(a)
125#endif /* ! HAVE_DLFCN_H */
126
127/******************************************************************
128 * locking.c
129 ******************************************************************/
130
131extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
132#define read_lock(fd, offset, whence, len) \
133    lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
134#define write_lock(fd, offset, whence, len) \
135    lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
136#define unlock(fd, offset, whence, len) \
137    lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
138
139/******************************************************************
140 * socket.c
141 ******************************************************************/
142
143extern int setnonblock(int fd, int cmd);
144extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
145extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
146extern const char *getip_string(const struct sockaddr *sa);
147extern unsigned int getip_port(const struct sockaddr *sa);
148extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
149extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
150extern int tokenize_ip_port(const char *ipurl, char **address, char **port);
151
152/* Structures and functions dealing with dynamic pollfd arrays */
153enum fdtype {IPC_FD, LISTEN_FD};
154struct polldata {
155    enum fdtype fdtype; /* IPC fd or listening socket fd                 */
156    void *data;         /* pointer to AFPconfig for listening socket and *
157                         * pointer to afp_child_t for IPC fd             */
158};
159
160extern void fdset_add_fd(int maxconns,
161                         struct pollfd **fdsetp,
162                         struct polldata **polldatap,
163                         int *fdset_usedp,
164                         int *fdset_sizep,
165                         int fd,
166                         enum fdtype fdtype,
167                         void *data);
168extern void fdset_del_fd(struct pollfd **fdsetp,
169                         struct polldata **polldatap,
170                         int *fdset_usedp,
171                         int *fdset_sizep,
172                         int fd);
173extern int send_fd(int socket, int fd);
174extern int recv_fd(int fd, int nonblocking);
175
176/******************************************************************
177 * unix.c
178 *****************************************************************/
179
180extern const char *getcwdpath(void);
181extern const char *fullpathname(const char *);
182extern char *stripped_slashes_basename(char *p);
183extern void randombytes(void *buf, int n);
184extern int daemonize(int nochdir, int noclose);
185extern int run_cmd(const char *cmd, char **cmd_argv);
186extern char *realpath_safe(const char *path);
187extern const char *basename_safe(const char *path);
188extern char *strtok_quote (char *s, const char *delim);
189
190extern int ochdir(const char *dir, int options);
191extern int ostat(const char *path, struct stat *buf, int options);
192extern int ostatat(int dirfd, const char *path, struct stat *st, int options);
193extern int ochown(const char *path, uid_t owner, gid_t group, int options);
194extern int ochmod(char *path, mode_t mode, const struct stat *st, int options);
195
196/******************************************************************
197 * cnid.c
198 *****************************************************************/
199
200extern bstring rel_path_in_vol(const char *path, const char *volpath);
201
202/******************************************************************
203 * cnid.c
204 *****************************************************************/
205
206extern void initline   (int, char *);
207extern int  parseline  (int, char *);
208
209#endif  /* _ATALK_UTIL_H */
210