2 * $Id: util.h,v 1.17 2010-01-05 13:48:47 franklahm Exp $
6 #define _ATALK_UTIL_H 1
12 #endif /* HAVE_UNISTD_H */
13 #include <netatalk/at.h>
14 #include <atalk/unicode.h>
16 /* exit error codes */
17 #define EXITERR_CLNT 1 /* client related error */
18 #define EXITERR_CONF 2 /* error in config files/cmd line parameters */
19 #define EXITERR_SYS 3 /* local system error */
23 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
26 extern const int _diacasemap[], _dialowermap[];
28 extern char **getifacelist(void);
29 extern void freeifacelist(char **);
31 #define diatolower(x) _dialowermap[(unsigned char) (x)]
32 #define diatoupper(x) _diacasemap[(unsigned char) (x)]
33 extern int atalk_aton (char *, struct at_addr *);
34 extern void bprint (char *, int);
35 extern int strdiacasecmp (const char *, const char *);
36 extern int strndiacasecmp (const char *, const char *, size_t);
37 extern pid_t server_lock (char * /*program*/, char * /*file*/,
39 extern void fault_setup (void (*fn)(void *));
40 #define server_unlock(x) (unlink(x))
42 /* strlcpy and strlcat are used by pam modules */
43 #ifndef UAM_MODULE_EXPORT
44 #define UAM_MODULE_EXPORT
48 UAM_MODULE_EXPORT size_t strlcpy (char *, const char *, size_t);
52 UAM_MODULE_EXPORT size_t strlcat (char *, const char *, size_t);
56 extern void *mod_open (const char *);
57 extern void *mod_symbol (void *, const char *);
58 extern void mod_close (void *);
59 #define mod_error() ""
60 #else /* ! HAVE_DLFCN_H */
65 #endif /* ! RTLD_NOW */
67 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
68 * OpenBSD currently does not use the second arg for dlopen(). For
69 * future compatibility we define DL_LAZY */
71 #define mod_open(a) dlopen(a, RTLD_LAZY)
72 #elif defined(__OpenBSD__)
73 #define mod_open(a) dlopen(a, DL_LAZY)
74 #else /* ! __NetBSD__ && ! __OpenBSD__ */
75 #define mod_open(a) dlopen(a, RTLD_NOW)
76 #endif /* __NetBSD__ */
78 #ifndef DLSYM_PREPEND_UNDERSCORE
79 #define mod_symbol(a, b) dlsym(a, b)
80 #else /* ! DLSYM_PREPEND_UNDERSCORE */
81 extern void *mod_symbol (void *, const char *);
82 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
83 #define mod_error() dlerror()
84 #define mod_close(a) dlclose(a)
85 #endif /* ! HAVE_DLFCN_H */
87 /******************************************************************
89 ******************************************************************/
94 * Purpose: lock a file with fctnl
98 * fd (r) File descriptor
99 * cmd (r) cmd to fcntl, only F_SETLK is usable here
100 * type (r) F_RDLCK, F_WRLCK, F_UNLCK
101 * offset (r) byte offset relative to l_whence
102 * whence (r) SEEK_SET, SEEK_CUR, SEEK_END
103 * len (r) no. of bytes (0 means to EOF)
105 * Returns: 0 on success, -1 on failure
106 * fcntl return value and errno
110 * Function called by macros to ease locking.
112 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
115 * Macros: read_lock, write_lock, un_lock
117 * Purpose: lock and unlock files
121 * fd (r) File descriptor
122 * offset (r) byte offset relative to l_whence
123 * whence (r) SEEK_SET, SEEK_CUR, SEEK_END
124 * len (r) no. of bytes (0 means to EOF)
126 * Returns: 0 on success, -1 on failure
127 * fcntl return value and errno
131 * Nice locking macros.
134 #define read_lock(fd, offset, whence, len) \
135 lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
136 #define write_lock(fd, offset, whence, len) \
137 lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
138 #define unlock(fd, offset, whence, len) \
139 lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
143 /******************************************************************
145 ******************************************************************/
148 * Function: setnonblock
150 * Purpose: set or unset non-blocking IO on a fd
154 * fd (r) File descriptor
155 * cmd (r) 0: disable non-blocking IO, ie block
156 * <>0: enable non-blocking IO
158 * Returns: 0 on success, -1 on failure
164 extern int setnonblock(int fd, int cmd);
167 * Function: getip_string
169 * Purpose: convert an IPv4 or IPv6 address to a static string using inet_ntop
173 * sa (r) pointer to an struct sockaddr
175 * Returns: pointer to a static string cotaining the converted address as string.
176 * On error pointers to "0.0.0.0" or "::0" are returned.
180 * IPv6 mapped IPv4 addresses are returned as IPv4 addreses eg
181 * ::ffff:10.0.0.0 is returned as "10.0.0.0".
183 extern const char *getip_string(const struct sockaddr *sa);
186 * Function: getip_string
188 * Purpose: return port number from struct sockaddr
192 * sa (r) pointer to an struct sockaddr
194 * Returns: port as unsigned int
198 extern unsigned int getip_port(const struct sockaddr *sa);
201 * Function: apply_ip_mask
203 * Purpose: apply netmask to IP (v4 or v6)
207 * ai (rw) pointer to an struct sockaddr
208 * mask (r) number of maskbits
214 * Modifies IP address in sa->sin[6]_addr-s[6]_addr. The caller is responsible
215 * for passing a value for mask that is sensible to the passed address,
216 * eg 0 <= mask <= 32 for IPv4 or 0<= mask <= 128 for IPv6. mask > 32 for
217 * IPv4 is treated as mask = 32, mask > 128 is set to 128 for IPv6.
219 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
222 * Function: compare_ip
224 * Purpose: Compare IP addresses for equality
228 * sa1 (r) pointer to an struct sockaddr
229 * sa2 (r) pointer to an struct sockaddr
231 * Returns: Addresses are converted to strings and compared with strcmp and
232 * the result of strcmp is returned.
236 * IPv6 mapped IPv4 addresses are treated as IPv4 addresses.
238 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
240 /******************************************************************
242 *****************************************************************/
245 * @brief get cwd in static buffer
247 * @returns pointer to path or pointer to error messages on error
249 extern const char *getcwdpath(void);