]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
02a0cc6247be5ea49c4faa6338a906537772fa47
[netatalk.git] / include / atalk / util.h
1 /*
2  * $Id: util.h,v 1.16 2009-11-23 18:17:30 didg Exp $
3  */
4
5 #ifndef _ATALK_UTIL_H
6 #define _ATALK_UTIL_H 1
7
8 #include <sys/cdefs.h>
9 #include <sys/types.h>
10 #ifdef HAVE_UNISTD_H
11 #include <unistd.h>
12 #endif /* HAVE_UNISTD_H */
13 #include <netatalk/at.h>
14 #include <atalk/unicode.h>
15
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 */
20
21
22 #ifdef WITH_SENDFILE
23 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
24 #endif
25
26 extern const int _diacasemap[], _dialowermap[];
27
28 extern char **getifacelist(void);
29 extern void freeifacelist(char **);
30
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*/, 
38                                int /*debug*/);
39 extern void fault_setup   (void (*fn)(void *));
40 #define server_unlock(x)  (unlink(x))
41
42 /* strlcpy and strlcat are used by pam modules */
43 #ifndef UAM_MODULE_EXPORT
44 #define UAM_MODULE_EXPORT 
45 #endif
46
47 #ifndef HAVE_STRLCPY
48 UAM_MODULE_EXPORT size_t strlcpy (char *, const char *, size_t);
49 #endif
50  
51 #ifndef HAVE_STRLCAT
52 UAM_MODULE_EXPORT size_t strlcat (char *, const char *, size_t);
53 #endif
54
55 #ifndef HAVE_DLFCN_H
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 */
61 #include <dlfcn.h>
62
63 #ifndef RTLD_NOW
64 #define RTLD_NOW 1
65 #endif /* ! RTLD_NOW */
66
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 */
70 #ifdef __NetBSD__
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__ */
77
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 */
86
87 /******************************************************************
88  * locking.c
89  ******************************************************************/
90
91 /*
92  * Function: lock_reg
93  *
94  * Purpose: lock a file with fctnl
95  *
96  * Arguments:
97  *
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)
104  *
105  * Returns: 0 on success, -1 on failure
106  *          fcntl return value and errno
107  *
108  * Effects:
109  *
110  * Function called by macros to ease locking.
111  */
112 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
113
114 /*
115  * Macros: read_lock, write_lock, un_lock
116  *
117  * Purpose: lock and unlock files
118  *
119  * Arguments:
120  *
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)
125  *
126  * Returns: 0 on success, -1 on failure
127  *          fcntl return value and errno
128  *
129  * Effects:
130  *
131  * Nice locking macros.
132  */
133
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))
140
141 #endif
142
143 /******************************************************************
144  * socket.c
145  ******************************************************************/
146
147 /*
148  * Function: setnonblock
149  *
150  * Purpose: set or unset non-blocking IO on a fd
151  *
152  * Arguments:
153  *
154  *    fd         (r) File descriptor
155  *    cmd        (r) 0: disable non-blocking IO, ie block
156  *                   <>0: enable non-blocking IO
157  *
158  * Returns: 0 on success, -1 on failure
159  *
160  * Effects:
161  *
162  * fd's file flags.
163  */
164 extern int setnonblock(int fd, int cmd);
165
166 /* 
167  * Function: getip_string
168  *
169  * Purpose: convert an IPv4 or IPv6 address to a static string using inet_ntop
170  *
171  * Arguments:
172  *
173  *   sa        (r) pointer to an struct sockaddr
174  *
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.
177  *
178  * Effects:
179  *
180  * IPv6 mapped IPv4 addresses are returned as IPv4 addreses eg
181  * ::ffff:10.0.0.0 is returned as "10.0.0.0".
182  */
183 extern const char *getip_string(const struct sockaddr *sa);
184
185 /* 
186  * Function: getip_string
187  *
188  * Purpose: return port number from struct sockaddr
189  *
190  * Arguments:
191  *
192  *   sa        (r) pointer to an struct sockaddr
193  *
194  * Returns: port as unsigned int
195  *
196  * Effects: none.
197  */
198 extern unsigned int getip_port(const struct sockaddr *sa);
199
200 /* 
201  * Function: apply_ip_mask
202  *
203  * Purpose: apply netmask to IP (v4 or v6)
204  *
205  * Arguments:
206  *
207  *   ai        (rw) pointer to an struct sockaddr
208  *   mask      (r) number of maskbits
209  *
210  * Returns: void
211  *
212  * Effects: 
213  *
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.
218  */
219 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
220
221 /* 
222  * Function: compare_ip
223  *
224  * Purpose: Compare IP addresses for equality
225  *
226  * Arguments:
227  *
228  *   sa1       (r) pointer to an struct sockaddr
229  *   sa2       (r) pointer to an struct sockaddr
230  *
231  * Returns: Addresses are converted to strings and compared with strcmp and
232  *          the result of strcmp is returned.
233  *
234  * Effects: 
235  *
236  * IPv6 mapped IPv4 addresses are treated as IPv4 addresses.
237  */
238 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);