netatalk 4.5.0
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
38#define EXITERR_CONF 2
39#define EXITERR_SYS 3
40#define EXITERR_CLOSED 4
41
43#define AFP_PANIC(why) \
44 do { \
45 netatalk_panic(why); \
46 abort(); \
47 } while(0)
48
49#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
87 extern ssize_t sys_sendfile(int __out_fd, int __in_fd, off_t *__offset,
88 size_t __count);
89#endif
90
91 extern const int _diacasemap[], _dialowermap[];
92
93 extern char **getifacelist(void);
94 extern void freeifacelist(char **);
95
96#define diatolower(x) _dialowermap[(unsigned char) (x)]
97#define diatoupper(x) _diacasemap[(unsigned char) (x)]
98#ifndef NO_DDP
99 extern int atalk_aton(char *, struct at_addr *);
100#endif
101 extern void bprint(char *, int);
102 extern int strdiacasecmp(const char *, const char *);
103 extern int strndiacasecmp(const char *, const char *, size_t);
104 extern pid_t server_lock(char * /*program*/, char * /*file*/, int /*debug*/);
105 extern int check_lockfile(const char *program, const char *pidfile);
106 extern int create_lockfile(const char *program, const char *pidfile);
107 extern void fault_setup(void (*fn)(void *));
108 extern void netatalk_panic(const char *why);
109#define server_unlock(x) (unlink(x))
110
111#ifndef HAVE_DLFCN_H
112 extern void *mod_open(const char *);
113 extern void *mod_symbol(void *, const char *);
114 extern 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#define mod_open(a) dlopen(a, RTLD_NOW | RTLD_GLOBAL)
124
125#define mod_symbol(a, b) dlsym(a, b)
126#define mod_error() dlerror()
127#define mod_close(a) dlclose(a)
128#endif /* ! HAVE_DLFCN_H */
129
130#ifndef strequal
131#define strequal(a,b) (strcmp((a),(b)) == 0)
132#endif
133
134#define cfrombstr(b) ((char *)((b)->data))
135
137#define BSTRING_STRIP_SLASH(a) \
138 do { \
139 while (bchar((a), blength(a) - 1) == '/') \
140 bdelete((a), blength(a) - 1, 1); \
141 } while (0);
142
143/******************************************************************
144 * locking.c
145 ******************************************************************/
146
147 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence,
148 off_t len);
149#define read_lock(fd, offset, whence, len) \
150 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
151#define write_lock(fd, offset, whence, len) \
152 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
153#define unlock(fd, offset, whence, len) \
154 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
155
156/******************************************************************
157 * socket.c
158 ******************************************************************/
159
160 extern int setnonblock(int fd, int cmd);
161 extern ssize_t readt(int socket, void *data, const size_t length,
162 int setnonblocking, int timeout);
163 extern ssize_t writet(int socket, void *data, const size_t length,
164 int setnonblocking, int timeout);
165 extern const char *getip_string(const struct sockaddr *sa);
166 extern unsigned int getip_port(const struct sockaddr *sa);
167 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
168 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
169 extern int tokenize_ip_port(const char *ipurl, char **address, char **port);
170
171/* Structures and functions dealing with dynamic pollfd arrays */
172
173 enum asev_fdtype {IPC_FD, LISTEN_FD, STATS_FD};
174
178 struct asev_data {
179 enum asev_fdtype fdtype;
180 void *private;
182 int protocol;
183};
184
188 struct asev {
189 struct pollfd *fdset;
190 struct asev_data *data;
191 int max;
192 int used;
193};
194
195 extern struct asev *asev_init(int max);
196 extern bool asev_add_fd(struct asev *sev, int fd, enum asev_fdtype fdtype,
197 void *private, int protocol);
198 extern bool asev_del_fd(struct asev *sev, int fd);
199
200 extern int send_fd(int socket, int fd);
201 extern int recv_fd(int fd, int nonblocking);
202
203/******************************************************************
204 * unix.c
205 *****************************************************************/
206
207 extern const char *getcwdpath(void);
208 extern const char *fullpathname(const char *);
209 extern char *stripped_slashes_basename(char *p);
210 extern void randombytes(void *buf, int n);
211 extern int daemonize(void);
212 extern int run_cmd(const char *cmd, char **cmd_argv);
213 extern char *realpath_safe(const char *path);
214 extern const char *basename_safe(const char *path);
215 extern char *strtok_quote(char *s, const char *delim);
216 extern const char *tmpdir(void);
217 extern int safe_atoi(const char *str, const char *param_name,
218 int min_val, int max_val, int default_val);
219
220 extern int ochdir(const char *dir, int options);
221 extern int ostat(const char *path, struct stat *buf, int options);
222 extern int ostatat(int dirfd, const char *path, struct stat *st, int options);
223 extern int ochown(const char *path, uid_t owner, gid_t group, int options);
224 extern int ochmod(char *path, mode_t mode, const struct stat *st, int options);
225
226/******************************************************************
227 * cnid.c
228 *****************************************************************/
229
230 extern bstring rel_path_in_vol(const char *path, const char *volpath);
231 extern cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath,
232 const char *path, cnid_t *did);
233 extern char *uuid_strip_dashes(const char *uuid);
234
235/******************************************************************
236 * gettok.c
237 *****************************************************************/
238
239 extern void initline(int, char *);
240 extern int parseline(int, char *);
241
242/******************************************************************
243 * pathconv.c
244 *****************************************************************/
245
246 extern char *convert_utf8_to_mac(const struct vol *vol, const char *upath);
247
248#endif /* _ATALK_UTIL_H */
cnid_t
uint32_t cnid_t
Definition adouble.h:156
buf
static char buf[MAXPATHLEN+1]
Definition afppasswd.c:113
sa
static struct savedt sa
Definition appl.c:32
at.h
uuid
uid_t uuid
bstrlib.h
C implementaion of bstring functions.
bstring
struct tagbstring * bstring
Definition bstrlib.h:86
cnid.h
p
size_t p
Definition dircache.c:256
pidfile
static char * pidfile
Definition etc/atalkd/main.c:93
data
#define data
Definition hash.c:38
freeifacelist
void freeifacelist(char **)
Definition getiface.c:157
tmpdir
const char * tmpdir(void)
Find a suitable temporary directory for Netatalk.
Definition libatalk/util/unix.c:288
readt
ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout)
non-blocking drop-in replacement for read with timeout using select
Definition socket.c:88
getip_string
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
getcwdpath
const char * getcwdpath(void)
get cwd in static buffer
Definition libatalk/util/unix.c:218
parseline
int parseline(int, char *)
Definition gettok.c:32
server_lock
pid_t server_lock(char *, char *, int)
Definition server_lock.c:33
rel_path_in_vol
bstring rel_path_in_vol(const char *path, const char *volpath)
Build path relative to volume root.
Definition util/cnid.c:72
stripped_slashes_basename
char * stripped_slashes_basename(char *p)
Takes a buffer with a path, strips slashs, returns basename.
Definition libatalk/util/unix.c:270
ochmod
int ochmod(char *path, mode_t mode, const struct stat *st, int options)
chmod() wrapper for symlink and ACL handling
Definition libatalk/util/unix.c:424
ochown
int ochown(const char *path, uid_t owner, gid_t group, int options)
Definition libatalk/util/unix.c:401
compare_ip
int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2)
compare IP addresses for equality
Definition socket.c:460
asev_fdtype
asev_fdtype
Definition include/atalk/util.h:173
STATS_FD
@ STATS_FD
Definition include/atalk/util.h:173
IPC_FD
@ IPC_FD
Definition include/atalk/util.h:173
LISTEN_FD
@ LISTEN_FD
Definition include/atalk/util.h:173
_dialowermap
const int _dialowermap[]
Definition include/atalk/util.h:91
lock_reg
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
safe_atoi
int safe_atoi(const char *str, const char *param_name, int min_val, int max_val, int default_val)
Safe string to integer conversion with bounds checking.
Definition netatalk_conf.c:890
realpath_safe
char * realpath_safe(const char *path)
Definition libatalk/util/unix.c:615
ochdir
int ochdir(const char *dir, int options)
symlink safe chdir replacement
Definition libatalk/util/unix.c:480
ostat
int ostat(const char *path, struct stat *buf, int options)
Definition libatalk/util/unix.c:392
uuid_strip_dashes
char * uuid_strip_dashes(const char *uuid)
Definition util/cnid.c:218
tokenize_ip_port
int tokenize_ip_port(const char *ipurl, char **address, char **port)
Tokenize IP(4/6) addresses with an optional port into address and port.
Definition socket.c:492
fullpathname
const char * fullpathname(const char *)
Request absolute path.
Definition libatalk/util/unix.c:235
strdiacasecmp
int strdiacasecmp(const char *, const char *)
Definition strdicasecmp.c:527
_diacasemap
const int _diacasemap[]
Definition strdicasecmp.c:7
asev_add_fd
bool asev_add_fd(struct asev *sev, int fd, enum asev_fdtype fdtype, void *private, int protocol)
Add a fd to a dynamic pollfd array and associated data array.
Definition socket.c:594
getip_port
unsigned int getip_port(const struct sockaddr *sa)
return port number from struct sockaddr
Definition socket.c:373
strtok_quote
char * strtok_quote(char *s, const char *delim)
extended strtok allows the quoted strings
Definition libatalk/util/unix.c:668
mod_symbol
void * mod_symbol(void *, const char *)
asev_init
struct asev * asev_init(int max)
Definition socket.c:564
convert_utf8_to_mac
char * convert_utf8_to_mac(const struct vol *vol, const char *upath)
Convert a UTF-8 filename to a mangled Mac filename.
Definition pathconv.c:25
sys_sendfile
ssize_t sys_sendfile(int __out_fd, int __in_fd, off_t *__offset, size_t __count)
Definition ad_sendfile.c:78
create_lockfile
int create_lockfile(const char *program, const char *pidfile)
Definition server_lock.c:138
run_cmd
int run_cmd(const char *cmd, char **cmd_argv)
Definition libatalk/util/unix.c:62
mod_open
void * mod_open(const char *)
apply_ip_mask
void apply_ip_mask(struct sockaddr *ai, int maskbits)
apply netmask to IP (v4 or v6)
Definition socket.c:397
ostatat
int ostatat(int dirfd, const char *path, struct stat *st, int options)
ostat/fsstatat multiplexer
Definition libatalk/util/unix.c:462
send_fd
int send_fd(int socket, int fd)
Definition socket.c:746
basename_safe
const char * basename_safe(const char *path)
safe basename() replacement
Definition libatalk/util/unix.c:656
daemonize
int daemonize(void)
Fork, exit parent, setsid(), chdir("/"), close all fds.
Definition libatalk/util/unix.c:119
netatalk_panic
void netatalk_panic(const char *why)
Definition fault.c:84
initline
void initline(int, char *)
Definition gettok.c:21
fault_setup
void fault_setup(void(*fn)(void *))
Definition fault.c:155
check_lockfile
int check_lockfile(const char *program, const char *pidfile)
Definition server_lock.c:111
bprint
void bprint(char *, int)
Definition bprint.c:17
cnid_for_path
cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did)
Resolves CNID of a given path.
Definition util/cnid.c:171
getifacelist
char ** getifacelist(void)
Get interfaces from the kernel.
Definition getiface.c:136
writet
ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout)
non-blocking drop-in replacement for read with timeout using select
Definition socket.c:217
mod_close
void mod_close(void *)
randombytes
void randombytes(void *buf, int n)
Definition libatalk/util/unix.c:570
recv_fd
int recv_fd(int fd, int nonblocking)
Receive a fd on a suitable socket.
Definition socket.c:682
asev_del_fd
bool asev_del_fd(struct asev *sev, int fd)
Remove fd from asev.
Definition socket.c:622
strndiacasecmp
int strndiacasecmp(const char *, const char *, size_t)
Definition strdicasecmp.c:540
atalk_aton
int atalk_aton(char *, struct at_addr *)
Check whether "cp" is a valid ascii representation of an AppleTalk address and convert to a binary ad...
Definition atalk_addr.c:32
setnonblock
int setnonblock(int fd, int cmd)
set or unset non-blocking IO on a fd
Definition socket.c:53
did
cnid_t did
Definition nad_cp.c:93
type
static enum op type
Definition nad_cp.c:96
port
static unsigned char port
Definition pap.c:504
_cnid_db
Definition cnid.h:65
asev_data
atalk socket event data
Definition include/atalk/util.h:178
asev_data::protocol
int protocol
Definition include/atalk/util.h:182
asev_data::fdtype
enum asev_fdtype fdtype
Definition include/atalk/util.h:179
asev
atalk socket event
Definition include/atalk/util.h:188
asev::max
int max
Definition include/atalk/util.h:191
asev::used
int used
Definition include/atalk/util.h:192
asev::data
struct asev_data * data
Definition include/atalk/util.h:190
asev::fdset
struct pollfd * fdset
Definition include/atalk/util.h:189
at_addr
Definition at.h:68
dir
Definition include/atalk/directory.h:56
path
Definition include/atalk/directory.h:123
vol
Definition include/atalk/volume.h:33
unicode.h