]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
Merge master
[netatalk.git] / include / atalk / util.h
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 <atalk/unicode.h>
19
20 /* exit error codes */
21 #define EXITERR_CLNT 1  /* client related error */
22 #define EXITERR_CONF 2  /* error in config files/cmd line parameters */
23 #define EXITERR_SYS  3  /* local system error */
24
25 /* Print a SBT and exit */
26 #define AFP_PANIC(why) \
27     do {                                            \
28         netatalk_panic(why);                        \
29         abort();                                    \
30     } while(0);
31
32 /* LOG assert errors */
33 #ifndef NDEBUG
34 #define AFP_ASSERT(b) \
35     do {                                                                \
36         if (!(b)) {                                                     \
37             AFP_PANIC(#b);                                              \
38         } \
39     } while(0);
40 #else
41 #define AFP_ASSERT(b)
42 #endif /* NDEBUG */
43
44 #ifndef MIN
45 #define MIN(a, b)  ((a) < (b) ? (a) : (b))
46 #endif
47
48 #ifndef MAX
49 #define MAX(a, b)  ((a) > (b) ? (a) : (b))
50 #endif
51
52 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
53 #define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
54 #define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
55
56 #ifdef WITH_SENDFILE
57 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
58 #endif
59
60 extern const int _diacasemap[], _dialowermap[];
61
62 extern char **getifacelist(void);
63 extern void freeifacelist(char **);
64
65 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
66 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
67 extern void bprint        (char *, int);
68 extern int strdiacasecmp  (const char *, const char *);
69 extern int strndiacasecmp (const char *, const char *, size_t);
70 extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
71 extern void fault_setup   (void (*fn)(void *));
72 extern void netatalk_panic(const char *why);
73 #define server_unlock(x)  (unlink(x))
74
75 #ifndef HAVE_DLFCN_H
76 extern void *mod_open    (const char *);
77 extern void *mod_symbol  (void *, const char *);
78 extern void mod_close    (void *);
79 #define mod_error()      ""
80 #else /* ! HAVE_DLFCN_H */
81 #include <dlfcn.h>
82
83 #ifndef RTLD_NOW
84 #define RTLD_NOW 1
85 #endif /* ! RTLD_NOW */
86
87 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
88  * OpenBSD currently does not use the second arg for dlopen(). For
89  * future compatibility we define DL_LAZY */
90 #ifdef __NetBSD__
91 #define mod_open(a)      dlopen(a, RTLD_LAZY)
92 #elif defined(__OpenBSD__)
93 #define mod_open(a)      dlopen(a, DL_LAZY)
94 #else /* ! __NetBSD__ && ! __OpenBSD__ */
95 #define mod_open(a)      dlopen(a, RTLD_NOW)
96 #endif /* __NetBSD__ */
97
98 #ifndef DLSYM_PREPEND_UNDERSCORE
99 #define mod_symbol(a, b) dlsym(a, b)
100 #else /* ! DLSYM_PREPEND_UNDERSCORE */
101 extern void *mod_symbol  (void *, const char *);
102 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
103 #define mod_error()      dlerror()
104 #define mod_close(a)     dlclose(a)
105 #endif /* ! HAVE_DLFCN_H */
106
107 /******************************************************************
108  * locking.c
109  ******************************************************************/
110
111 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
112 #define read_lock(fd, offset, whence, len) \
113     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
114 #define write_lock(fd, offset, whence, len) \
115     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
116 #define unlock(fd, offset, whence, len) \
117     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
118
119 /******************************************************************
120  * socket.c
121  ******************************************************************/
122
123 extern int setnonblock(int fd, int cmd);
124 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
125 extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
126 extern const char *getip_string(const struct sockaddr *sa);
127 extern unsigned int getip_port(const struct sockaddr *sa);
128 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
129 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
130
131 /* Structures and functions dealing with dynamic pollfd arrays */
132 enum fdtype {IPC_FD, LISTEN_FD};
133 struct polldata {
134     enum fdtype fdtype; /* IPC fd or listening socket fd                 */
135     void *data;         /* pointer to AFPconfig for listening socket and *
136                          * pointer to afp_child_t for IPC fd             */
137 };
138
139 extern void fdset_add_fd(struct pollfd **fdsetp,
140                          struct polldata **polldatap,
141                          int *fdset_usedp,
142                          int *fdset_sizep,
143                          int fd,
144                          enum fdtype fdtype,
145                          void *data);
146 extern void fdset_del_fd(struct pollfd **fdsetp,
147                          struct polldata **polldatap,
148                          int *fdset_usedp,
149                          int *fdset_sizep,
150                          int fd);
151 extern int send_fd(int socket, int fd);
152 extern int recv_fd(int fd, int nonblocking);
153
154 /******************************************************************
155  * unix.c
156  *****************************************************************/
157
158 extern const char *abspath(const char *name);
159 extern const char *getcwdpath(void);
160 extern char *stripped_slashes_basename(char *p);
161 extern int lchdir(const char *dir);
162 extern void randombytes(void *buf, int n);
163 #endif  /* _ATALK_UTIL_H */