]> arthur.barton.de Git - netatalk.git/blob - include/atalk/util.h
better layout for struct vol
[netatalk.git] / include / atalk / util.h
1 /*
2  * $Id: util.h,v 1.14 2009-11-08 22:08:04 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 extern int     sys_ftruncate (int fd, off_t length);
23
24 #ifdef WITH_SENDFILE
25 extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
26 #endif
27
28 extern const int _diacasemap[], _dialowermap[];
29
30 extern char **getifacelist(void);
31 extern void freeifacelist(char **);
32
33 #define diatolower(x)     _dialowermap[(unsigned char) (x)]
34 #define diatoupper(x)     _diacasemap[(unsigned char) (x)]
35 extern int atalk_aton     (char *, struct at_addr *);
36 extern void bprint        (char *, int);
37 extern int strdiacasecmp  (const char *, const char *);
38 extern int strndiacasecmp (const char *, const char *, size_t);
39 extern pid_t server_lock  (char * /*program*/, char * /*file*/, 
40                                int /*debug*/);
41 extern void fault_setup   (void (*fn)(void *));
42 #define server_unlock(x)  (unlink(x))
43
44 #ifndef HAVE_STRLCPY
45 __attribute__ ((visibility("default"))) size_t strlcpy (char *, const char *, size_t);
46 #endif
47  
48 #ifndef HAVE_STRLCAT
49 size_t strlcat (char *, const char *, size_t);
50 #endif
51
52 #ifndef HAVE_DLFCN_H
53 extern void *mod_open    (const char *);
54 extern void *mod_symbol  (void *, const char *);
55 extern void mod_close    (void *);
56 #define mod_error()      ""
57 #else /* ! HAVE_DLFCN_H */
58 #include <dlfcn.h>
59
60 #ifndef RTLD_NOW
61 #define RTLD_NOW 1
62 #endif /* ! RTLD_NOW */
63
64 /* NetBSD doesn't like RTLD_NOW for dlopen (it fails). Use RTLD_LAZY.
65  * OpenBSD currently does not use the second arg for dlopen(). For
66  * future compatibility we define DL_LAZY */
67 #ifdef __NetBSD__
68 #define mod_open(a)      dlopen(a, RTLD_LAZY)
69 #elif defined(__OpenBSD__)
70 #define mod_open(a)      dlopen(a, DL_LAZY)
71 #else /* ! __NetBSD__ && ! __OpenBSD__ */
72 #define mod_open(a)      dlopen(a, RTLD_NOW)
73 #endif /* __NetBSD__ */
74
75 #ifndef DLSYM_PREPEND_UNDERSCORE
76 #define mod_symbol(a, b) dlsym(a, b)
77 #else /* ! DLSYM_PREPEND_UNDERSCORE */
78 extern void *mod_symbol  (void *, const char *);
79 #endif /* ! DLSYM_PREPEND_UNDERSCORE */
80 #define mod_error()      dlerror()
81 #define mod_close(a)     dlclose(a)
82 #endif /* ! HAVE_DLFCN_H */
83
84 /******************************************************************
85  * locking.c
86  ******************************************************************/
87
88 /*
89  * Function: lock_reg
90  *
91  * Purpose: lock a file with fctnl
92  *
93  * Arguments:
94  *
95  *    fd         (r) File descriptor
96  *    cmd        (r) cmd to fcntl, only F_SETLK is usable here
97  *    type       (r) F_RDLCK, F_WRLCK, F_UNLCK
98  *    offset     (r) byte offset relative to l_whence
99  *    whence     (r) SEEK_SET, SEEK_CUR, SEEK_END
100  *    len        (r) no. of bytes (0 means to EOF)
101  *
102  * Returns: 0 on success, -1 on failure
103  *          fcntl return value and errno
104  *
105  * Effects:
106  *
107  * Function called by macros to ease locking.
108  */
109 extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
110
111 /*
112  * Macros: read_lock, write_lock, un_lock
113  *
114  * Purpose: lock and unlock files
115  *
116  * Arguments:
117  *
118  *    fd         (r) File descriptor
119  *    offset     (r) byte offset relative to l_whence
120  *    whence     (r) SEEK_SET, SEEK_CUR, SEEK_END
121  *    len        (r) no. of bytes (0 means to EOF)
122  *
123  * Returns: 0 on success, -1 on failure
124  *          fcntl return value and errno
125  *
126  * Effects:
127  *
128  * Nice locking macros.
129  */
130
131 #define read_lock(fd, offset, whence, len) \
132     lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
133 #define write_lock(fd, offset, whence, len) \
134     lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))
135 #define unlock(fd, offset, whence, len) \
136     lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
137
138 #endif
139
140 /******************************************************************
141  * socket.c
142  ******************************************************************/
143
144 /*
145  * Function: setnonblock
146  *
147  * Purpose: set or unset non-blocking IO on a fd
148  *
149  * Arguments:
150  *
151  *    fd         (r) File descriptor
152  *    cmd        (r) 0: disable non-blocking IO, ie block
153  *                   <>0: enable non-blocking IO
154  *
155  * Returns: 0 on success, -1 on failure
156  *
157  * Effects:
158  *
159  * fd's file flags.
160  */
161 extern int setnonblock(int fd, int cmd);
162
163 /* 
164  * Function: getip_string
165  *
166  * Purpose: convert an IPv4 or IPv6 address to a static string using inet_ntop
167  *
168  * Arguments:
169  *
170  *   sa        (r) pointer to an struct sockaddr
171  *
172  * Returns: pointer to a static string cotaining the converted address as string.
173  *          On error pointers to "0.0.0.0" or "::0" are returned.
174  *
175  * Effects:
176  *
177  * IPv6 mapped IPv4 addresses are returned as IPv4 addreses eg
178  * ::ffff:10.0.0.0 is returned as "10.0.0.0".
179  */
180 extern const char *getip_string(const struct sockaddr *sa);
181
182 /* 
183  * Function: getip_string
184  *
185  * Purpose: return port number from struct sockaddr
186  *
187  * Arguments:
188  *
189  *   sa        (r) pointer to an struct sockaddr
190  *
191  * Returns: port as unsigned int
192  *
193  * Effects: none.
194  */
195 extern unsigned int getip_port(const struct sockaddr *sa);
196
197 /* 
198  * Function: apply_ip_mask
199  *
200  * Purpose: apply netmask to IP (v4 or v6)
201  *
202  * Arguments:
203  *
204  *   ai        (rw) pointer to an struct sockaddr
205  *   mask      (r) number of maskbits
206  *
207  * Returns: void
208  *
209  * Effects: 
210  *
211  * Modifies IP address in sa->sin[6]_addr-s[6]_addr. The caller is responsible
212  * for passing a value for mask that is sensible to the passed address,
213  * eg 0 <= mask <= 32 for IPv4 or 0<= mask <= 128 for IPv6. mask > 32 for
214  * IPv4 is treated as mask = 32, mask > 128 is set to 128 for IPv6.
215  */
216 extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
217
218 /* 
219  * Function: compare_ip
220  *
221  * Purpose: Compare IP addresses for equality
222  *
223  * Arguments:
224  *
225  *   sa1       (r) pointer to an struct sockaddr
226  *   sa2       (r) pointer to an struct sockaddr
227  *
228  * Returns: Addresses are converted to strings and compared with strcmp and
229  *          the result of strcmp is returned.
230  *
231  * Effects: 
232  *
233  * IPv6 mapped IPv4 addresses are treated as IPv4 addresses.
234  */
235 extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);