netatalk  4.4.0dev
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
include/atalk/util.h
Go to the documentation of this file.
1
10
11#ifndef _ATALK_UTIL_H
12#define _ATALK_UTIL_H 1
13
14#include <poll.h>
15#include <stdbool.h>
16#include <sys/socket.h>
17#include <sys/stat.h>
18#include <sys/types.h>
19#include <unistd.h>
20
21#include <bstrlib.h>
22
23#include <netatalk/at.h>
24
25#include <atalk/cnid.h>
26#include <atalk/unicode.h>
27
28#ifndef RLIM_MAX
29#ifdef __APPLE__
30#define RLIM_MAX 10240
31#else
32#define RLIM_MAX 65535
33#endif
34#endif
35
36/* exit error codes */
37#define EXITERR_CLNT 1 /* client related error */
38#define EXITERR_CONF 2 /* error in config files/cmd line parameters */
39#define EXITERR_SYS 3 /* local system error */
40#define EXITERR_CLOSED 4 /* connection was immediately closed after TCP handshake */
41
42/* Print a SBT and exit */
43#define AFP_PANIC(why) \
44 do { \
45 netatalk_panic(why); \
46 abort(); \
47 } while(0);
48
49/* LOG assert errors */
50#ifndef NDEBUG
51#define AFP_ASSERT(b) \
52 do { \
53 if (!(b)) { \
54 AFP_PANIC(#b); \
55 } \
56 } while(0)
57#else
58#define AFP_ASSERT(b) do {} while(0)
59#endif /* NDEBUG */
60
61#ifndef MIN
62#define MIN(a, b) ((a) < (b) ? (a) : (b))
63#endif
64
65#ifndef MAX
66#define MAX(a, b) ((a) > (b) ? (a) : (b))
67#endif
68
69#define STRCMP(a,b,c) (strcmp(a,c) b 0)
70#define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
71#define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
72
73#ifdef WORDS_BIGENDIAN
74#define hton64(x) (x)
75#define ntoh64(x) (x)
76#else
77#define hton64(x) ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
78 (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
79#define ntoh64(x) (hton64(x))
80#endif
81
82#ifndef SAFE_FREE
83#define SAFE_FREE(x) do { if ((x) != NULL) {free(x); x=NULL;} } while(0)
84#endif
85
86#ifdef WITH_SENDFILE
87extern ssize_t sys_sendfile(int __out_fd, int __in_fd, off_t *__offset,
88 size_t __count);
89#endif
90
91extern const int _diacasemap[], _dialowermap[];
92
93extern char **getifacelist(void);
94extern void freeifacelist(char **);
95
96#define diatolower(x) _dialowermap[(unsigned char) (x)]
97#define diatoupper(x) _diacasemap[(unsigned char) (x)]
98#ifndef NO_DDP
99extern int atalk_aton(char *, struct at_addr *);
100#endif
101extern void bprint(char *, int);
102extern int strdiacasecmp(const char *, const char *);
103extern int strndiacasecmp(const char *, const char *, size_t);
104extern pid_t server_lock(char * /*program*/, char * /*file*/, int /*debug*/);
105extern int check_lockfile(const char *program, const char *pidfile);
106extern int create_lockfile(const char *program, const char *pidfile);
107extern void fault_setup(void (*fn)(void *));
108extern void netatalk_panic(const char *why);
109#define server_unlock(x) (unlink(x))
110
111#ifndef HAVE_DLFCN_H
112extern void *mod_open(const char *);
113extern void *mod_symbol(void *, const char *);
114extern void mod_close(void *);
115#define mod_error() ""
116#else /* ! HAVE_DLFCN_H */
117#include <dlfcn.h>
118
119#ifndef RTLD_NOW
120#define RTLD_NOW 1
121#endif /* ! RTLD_NOW */
122
123/* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
124 * OpenBSD currently does not use the second arg for dlopen(). For
125 * future compatibility we define DL_LAZY */
126#ifdef __NetBSD__
127#define mod_open(a) dlopen(a, RTLD_LAZY)
128#elif defined(__OpenBSD__)
129#define mod_open(a) dlopen(a, DL_LAZY)
130#else /* ! __NetBSD__ && ! __OpenBSD__ */
131#define mod_open(a) dlopen(a, RTLD_NOW)
132#endif /* __NetBSD__ */
133
134#define mod_symbol(a, b) dlsym(a, b)
135#define mod_error() dlerror()
136#define mod_close(a) dlclose(a)
137#endif /* ! HAVE_DLFCN_H */
138
139#ifndef strequal
140#define strequal(a,b) (strcmp((a),(b)) == 0)
141#endif
142
143#define cfrombstr(b) ((char *)((b)->data))
144
145/* strip slashes from end of a bstring */
146#define BSTRING_STRIP_SLASH(a) \
147 do { \
148 while (bchar((a), blength(a) - 1) == '/') \
149 bdelete((a), blength(a) - 1, 1); \
150 } while (0);
151
152/******************************************************************
153 * locking.c
154 ******************************************************************/
155
156extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence,
157 off_t len);
158#define read_lock(fd, offset, whence, len) \
159 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
160#define write_lock(fd, offset, whence, len) \
161 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
162#define unlock(fd, offset, whence, len) \
163 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
164
165/******************************************************************
166 * socket.c
167 ******************************************************************/
168
169extern int setnonblock(int fd, int cmd);
170extern ssize_t readt(int socket, void *data, const size_t length,
171 int setnonblocking, int timeout);
172extern ssize_t writet(int socket, void *data, const size_t length,
173 int setnonblocking, int timeout);
174extern const char *getip_string(const struct sockaddr *sa);
175extern unsigned int getip_port(const struct sockaddr *sa);
176extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
177extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
178extern int tokenize_ip_port(const char *ipurl, char **address, char **port);
179
180/* Structures and functions dealing with dynamic pollfd arrays */
181
183
187struct asev_data {
188 enum asev_fdtype fdtype; /* IPC fd or listening socket fd */
189 void *private; /* pointer to AFPconfig for listening socket and *
190 * pointer to afp_child_t for IPC fd */
191 int protocol; /* protocol type ASP or DSI */
192};
193
197struct asev {
198 struct pollfd *fdset; /* struct pollfd array for poll() */
199 struct asev_data *data; /* associated array of data */
200 int max;
201 int used;
202};
203
204extern struct asev *asev_init(int max);
205extern bool asev_add_fd(struct asev *sev, int fd, enum asev_fdtype fdtype,
206 void *private, int protocol);
207extern bool asev_del_fd(struct asev *sev, int fd);
208
209extern int send_fd(int socket, int fd);
210extern int recv_fd(int fd, int nonblocking);
211
212/******************************************************************
213 * unix.c
214 *****************************************************************/
215
216extern const char *getcwdpath(void);
217extern const char *fullpathname(const char *);
218extern char *stripped_slashes_basename(char *p);
219extern void randombytes(void *buf, int n);
220extern int daemonize(void);
221extern int run_cmd(const char *cmd, char **cmd_argv);
222extern char *realpath_safe(const char *path);
223extern const char *basename_safe(const char *path);
224extern char *strtok_quote(char *s, const char *delim);
225extern const char *tmpdir(void);
226
227extern int ochdir(const char *dir, int options);
228extern int ostat(const char *path, struct stat *buf, int options);
229extern int ostatat(int dirfd, const char *path, struct stat *st, int options);
230extern int ochown(const char *path, uid_t owner, gid_t group, int options);
231extern int ochmod(char *path, mode_t mode, const struct stat *st, int options);
232
233/******************************************************************
234 * cnid.c
235 *****************************************************************/
236
237extern bstring rel_path_in_vol(const char *path, const char *volpath);
238extern cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath,
239 const char *path, cnid_t *did);
240
241/******************************************************************
242 * cnid.c
243 *****************************************************************/
244
245extern void initline(int, char *);
246extern int parseline(int, char *);
247
248#endif /* _ATALK_UTIL_H */
uint32_t cnid_t
Definition adouble.h:156
static char buf[MAXPATHLEN+1]
Definition afppasswd.c:66
static struct savedt sa
Definition appl.c:32
C implementaion of bstring functions.
struct tagbstring * bstring
Definition bstrlib.h:82
static char * pidfile
Definition etc/atalkd/main.c:93
#define data
Definition hash.c:37
void freeifacelist(char **)
Definition getiface.c:141
const char * tmpdir(void)
Definition libatalk/util/unix.c:269
ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout)
Definition socket.c:88
const char * getip_string(const struct sockaddr *sa)
convert an IPv4 or IPv6 address to a static string using inet_ntop
Definition socket.c:327
const char * getcwdpath(void)
get cwd in static buffer
Definition libatalk/util/unix.c:204
int parseline(int, char *)
Definition gettok.c:32
pid_t server_lock(char *, char *, int)
Definition server_lock.c:33
bstring rel_path_in_vol(const char *path, const char *volpath)
Definition util/cnid.c:71
char * stripped_slashes_basename(char *p)
Definition libatalk/util/unix.c:251
int ochmod(char *path, mode_t mode, const struct stat *st, int options)
Definition libatalk/util/unix.c:404
int ochown(const char *path, uid_t owner, gid_t group, int options)
Definition libatalk/util/unix.c:382
int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2)
compare IP addresses for equality
Definition socket.c:460
asev_fdtype
Definition include/atalk/util.h:182
@ IPC_FD
Definition include/atalk/util.h:182
@ LISTEN_FD
Definition include/atalk/util.h:182
const int _dialowermap[]
Definition include/atalk/util.h:91
int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len)
lock a file with fctnl
Definition locking.c:86
char * realpath_safe(const char *path)
Definition libatalk/util/unix.c:594
int ochdir(const char *dir, int options)
symlink safe chdir replacement
Definition libatalk/util/unix.c:459
int ostat(const char *path, struct stat *buf, int options)
Definition libatalk/util/unix.c:373
int tokenize_ip_port(const char *ipurl, char **address, char **port)
Definition socket.c:491
const char * fullpathname(const char *)
Request absolute path.
Definition libatalk/util/unix.c:221
int strdiacasecmp(const char *, const char *)
Definition strdicasecmp.c:527
const int _diacasemap[]
Definition strdicasecmp.c:7
bool asev_add_fd(struct asev *sev, int fd, enum asev_fdtype fdtype, void *private, int protocol)
Definition socket.c:593
unsigned int getip_port(const struct sockaddr *sa)
return port number from struct sockaddr
Definition socket.c:373
char * strtok_quote(char *s, const char *delim)
Definition libatalk/util/unix.c:645
void * mod_symbol(void *, const char *)
struct asev * asev_init(int max)
Definition socket.c:563
int create_lockfile(const char *program, const char *pidfile)
Definition server_lock.c:138
int run_cmd(const char *cmd, char **cmd_argv)
Definition libatalk/util/unix.c:62
void * mod_open(const char *)
void apply_ip_mask(struct sockaddr *ai, int maskbits)
apply netmask to IP (v4 or v6)
Definition socket.c:397
int ostatat(int dirfd, const char *path, struct stat *st, int options)
Definition libatalk/util/unix.c:441
int send_fd(int socket, int fd)
Definition socket.c:745
const char * basename_safe(const char *path)
Definition libatalk/util/unix.c:634
int daemonize(void)
Definition libatalk/util/unix.c:121
void netatalk_panic(const char *why)
void initline(int, char *)
Definition gettok.c:21
void fault_setup(void(*fn)(void *))
Definition fault.c:157
int check_lockfile(const char *program, const char *pidfile)
Definition server_lock.c:111
void bprint(char *, int)
Definition bprint.c:17
cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did)
Definition util/cnid.c:170
char ** getifacelist(void)
Definition getiface.c:120
ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout)
Definition socket.c:217
void mod_close(void *)
void randombytes(void *buf, int n)
Definition libatalk/util/unix.c:549
int recv_fd(int fd, int nonblocking)
Definition socket.c:681
bool asev_del_fd(struct asev *sev, int fd)
Definition socket.c:621
int strndiacasecmp(const char *, const char *, size_t)
Definition strdicasecmp.c:540
int atalk_aton(char *, struct at_addr *)
Definition atalk_addr.c:31
int setnonblock(int fd, int cmd)
set or unset non-blocking IO on a fd
Definition socket.c:53
cnid_t did
Definition nad_cp.c:92
static enum op type
Definition nad_cp.c:95
static unsigned char port
Definition pap.c:504
Definition cnid.h:55
Definition include/atalk/util.h:187
int protocol
Definition include/atalk/util.h:191
enum asev_fdtype fdtype
Definition include/atalk/util.h:188
Definition include/atalk/util.h:197
int max
Definition include/atalk/util.h:200
int used
Definition include/atalk/util.h:201
struct asev_data * data
Definition include/atalk/util.h:199
struct pollfd * fdset
Definition include/atalk/util.h:198
Definition at.h:68
Definition include/atalk/directory.h:62
Definition include/atalk/directory.h:97
static gcry_mpi_t p
Definition uams_dhx2_pam.c:39