]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
Merge branch-2-1
[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/cdefs.h>
15 #include <sys/types.h>
16 #ifdef HAVE_UNISTD_H
17 #include <unistd.h>
18 #endif /* HAVE_UNISTD_H */
19 #include <netatalk/at.h>
20 #include <atalk/unicode.h>
21
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 */
26
27 /* Print a SBT and exit */
28 #define AFP_PANIC(why) \
29     do {                                            \
30         netatalk_panic(why);                        \
31         abort();                                    \
32     } while(0);
33
34 /* LOG assert errors */
35 #ifndef NDEBUG
36 #define AFP_ASSERT(b) \
37     do {                                                                \
38         if (!(b)) {                                                     \
39             AFP_PANIC(#b);                                              \
40         } \
41     } while(0);
42 #else
43 #define AFP_ASSERT(b)
44 #endif /* NDEBUG */
45
46 #define STRCMP(a,b,c) (strcmp(a,c) b 0)
47
48 #ifdef WITH_SENDFILE
49 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
50 #endif
51
52 extern const int _diacasemap[], _dialowermap[];
53
54 extern char **getifacelist(void);
55 extern void freeifacelist(char **);
56
57 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
58 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
59 extern int atalk_aton     (char *, struct at_addr *);
60 extern void bprint        (char *, int);
61 extern int strdiacasecmp  (const char *, const char *);
62 extern int strndiacasecmp (const char *, const char *, size_t);
63 extern pid_t server_lock  (char * /*program*/, char * /*file*/, int /*debug*/);
64 extern void fault_setup   (void (*fn)(void *));
65 extern void netatalk_panic(const char *why);
66 #define server_unlock(x)  (unlink(x))
67
68 /* strlcpy and strlcat are used by pam modules */
69 #ifndef UAM_MODULE_EXPORT
70 #define UAM_MODULE_EXPORT 
71 #endif
72
73 #ifndef HAVE_STRLCPY
74 UAM_MODULE_EXPORT size_t strlcpy (char *, const char *, size_t);
75 #endif
76  
77 #ifndef HAVE_STRLCAT
78 UAM_MODULE_EXPORT size_t strlcat (char *, const char *, size_t);
79 #endif
80
81 #ifndef HAVE_DLFCN_H
82 extern void *mod_open    (const char *);
83 extern void *mod_symbol  (void *, const char *);
84 extern void mod_close    (void *);
85 #define mod_error()      ""
86 #else /* ! HAVE_DLFCN_H */
87 #include <dlfcn.h>
88
89 #ifndef RTLD_NOW
90 #define RTLD_NOW 1
91 #endif /* ! RTLD_NOW */
92
93 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
94  * OpenBSD currently does not use the second arg for dlopen(). For
95  * future compatibility we define DL_LAZY */
96 #ifdef __NetBSD__
97 #define mod_open(a)      dlopen(a, RTLD_LAZY)
98 #elif defined(__OpenBSD__)
99 #define mod_open(a)      dlopen(a, DL_LAZY)
100 #else /* ! __NetBSD__ && ! __OpenBSD__ */
101 #define mod_open(a)      dlopen(a, RTLD_NOW)
102 #endif /* __NetBSD__ */
103
104 #ifndef DLSYM_PREPEND_UNDERSCORE
105 #define mod_symbol(a, b) dlsym(a, b)
106 #else /* ! DLSYM_PREPEND_UNDERSCORE */
107 extern void *mod_symbol  (void *, const char *);
108 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
109 #define mod_error()      dlerror()
110 #define mod_close(a)     dlclose(a)
111 #endif /* ! HAVE_DLFCN_H */
112
113 /******************************************************************
114  * locking.c
115  ******************************************************************/
116
117 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
118 #define read_lock(fd, offset, whence, len) \
119     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
120 #define write_lock(fd, offset, whence, len) \
121     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
122 #define unlock(fd, offset, whence, len) \
123     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
124
125 /******************************************************************
126  * socket.c
127  ******************************************************************/
128
129 extern int setnonblock(int fd, int cmd);
130 extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
131 extern const char *getip_string(const struct sockaddr *sa);
132 extern unsigned int getip_port(const struct sockaddr *sa);
133 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
134 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
135
136 /******************************************************************
137  * unix.c
138  *****************************************************************/
139
140 extern const char *getcwdpath(void);
141 extern char *stripped_slashes_basename(char *p);
142 extern int lchdir(const char *dir);
143
144 #endif  /* _ATALK_UTIL_H */