-/*
- * $Id: util.h,v 1.12 2009-10-26 12:35:56 franklahm Exp $
+/*!
+ * @file
+ * Netatalk utility functions
+ *
+ * Utility functions for these areas: \n
+ * * sockets \n
+ * * locking \n
+ * * misc UNIX function wrappers, eg for getcwd
*/
#ifndef _ATALK_UTIL_H
#define _ATALK_UTIL_H 1
-#include <sys/cdefs.h>
#include <sys/types.h>
-#ifdef HAVE_UNISTD_H
+#include <sys/socket.h>
#include <unistd.h>
-#endif /* HAVE_UNISTD_H */
-#include <netatalk/at.h>
+#include <poll.h>
+
#include <atalk/unicode.h>
+#include <atalk/bstrlib.h>
+#include <atalk/cnid.h>
/* exit error codes */
#define EXITERR_CLNT 1 /* client related error */
#define EXITERR_CONF 2 /* error in config files/cmd line parameters */
#define EXITERR_SYS 3 /* local system error */
+/* Print a SBT and exit */
+#define AFP_PANIC(why) \
+ do { \
+ netatalk_panic(why); \
+ abort(); \
+ } while(0);
+
+/* LOG assert errors */
+#ifndef NDEBUG
+#define AFP_ASSERT(b) \
+ do { \
+ if (!(b)) { \
+ AFP_PANIC(#b); \
+ } \
+ } while(0);
+#else
+#define AFP_ASSERT(b)
+#endif /* NDEBUG */
+
+#ifndef MIN
+#define MIN(a, b) ((a) < (b) ? (a) : (b))
+#endif
+
+#ifndef MAX
+#define MAX(a, b) ((a) > (b) ? (a) : (b))
+#endif
+
+#define STRCMP(a,b,c) (strcmp(a,c) b 0)
+#define ZERO_STRUCT(a) memset(&(a), 0, sizeof(a))
+#define ZERO_STRUCTP(a) memset((a), 0, sizeof(a))
+#ifndef MAX
+#define MAX(a,b) ((a) > (b) ? a : b)
+#endif
+#ifndef MIN
+#define MIN(a,b) ((a) < (b) ? a : b)
+#endif
-extern int sys_ftruncate (int fd, off_t length);
+#ifdef WORDS_BIGENDIAN
+#define hton64(x) (x)
+#define ntoh64(x) (x)
+#else
+#define hton64(x) ((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
+ (uint64_t) ((htonl(x) & 0xffffffffLL) << 32))
+#define ntoh64(x) (hton64(x))
+#endif
#ifdef WITH_SENDFILE
extern ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset,size_t __count);
#define diatolower(x) _dialowermap[(unsigned char) (x)]
#define diatoupper(x) _diacasemap[(unsigned char) (x)]
-extern int atalk_aton (char *, struct at_addr *);
extern void bprint (char *, int);
extern int strdiacasecmp (const char *, const char *);
extern int strndiacasecmp (const char *, const char *, size_t);
-extern pid_t server_lock (char * /*program*/, char * /*file*/,
- int /*debug*/);
+extern pid_t server_lock (char * /*program*/, char * /*file*/, int /*debug*/);
+extern int check_lockfile (const char *program, const char *pidfile);
+extern int create_lockfile(const char *program, const char *pidfile);
extern void fault_setup (void (*fn)(void *));
+extern void netatalk_panic(const char *why);
#define server_unlock(x) (unlink(x))
-#ifndef HAVE_STRLCPY
-size_t strlcpy (char *, const char *, size_t);
-#endif
-
-#ifndef HAVE_STRLCAT
-size_t strlcat (char *, const char *, size_t);
-#endif
-
#ifndef HAVE_DLFCN_H
extern void *mod_open (const char *);
extern void *mod_symbol (void *, const char *);
* locking.c
******************************************************************/
-/*
- * Function: lock_reg
- *
- * Purpose: lock a file with fctnl
- *
- * Arguments:
- *
- * fd (r) File descriptor
- * cmd (r) cmd to fcntl, only F_SETLK is usable here
- * type (r) F_RDLCK, F_WRLCK, F_UNLCK
- * offset (r) byte offset relative to l_whence
- * whence (r) SEEK_SET, SEEK_CUR, SEEK_END
- * len (r) no. of bytes (0 means to EOF)
- *
- * Returns: 0 on success, -1 on failure
- * fcntl return value and errno
- *
- * Effects:
- *
- * Function called by macros to ease locking.
- */
extern int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len);
-
-/*
- * Macros: read_lock, write_lock, un_lock
- *
- * Purpose: lock and unlock files
- *
- * Arguments:
- *
- * fd (r) File descriptor
- * offset (r) byte offset relative to l_whence
- * whence (r) SEEK_SET, SEEK_CUR, SEEK_END
- * len (r) no. of bytes (0 means to EOF)
- *
- * Returns: 0 on success, -1 on failure
- * fcntl return value and errno
- *
- * Effects:
- *
- * Nice locking macros.
- */
-
#define read_lock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
#define write_lock(fd, offset, whence, len) \
#define unlock(fd, offset, whence, len) \
lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))
-#endif
-
/******************************************************************
* socket.c
******************************************************************/
-/*
- * Function: setnonblock
- *
- * Purpose: set or unset non-blocking IO on a fd
- *
- * Arguments:
- *
- * fd (r) File descriptor
- * cmd (r) 0: disable non-blocking IO, ie block
- * <>0: enable non-blocking IO
- *
- * Returns: 0 on success, -1 on failure
- *
- * Effects:
- *
- * fd's file flags.
- */
extern int setnonblock(int fd, int cmd);
+extern ssize_t readt(int socket, void *data, const size_t length, int setnonblocking, int timeout);
+extern ssize_t writet(int socket, void *data, const size_t length, int setnonblocking, int timeout);
+extern const char *getip_string(const struct sockaddr *sa);
+extern unsigned int getip_port(const struct sockaddr *sa);
+extern void apply_ip_mask(struct sockaddr *ai, int maskbits);
+extern int compare_ip(const struct sockaddr *sa1, const struct sockaddr *sa2);
+
+/* Structures and functions dealing with dynamic pollfd arrays */
+enum fdtype {IPC_FD, LISTEN_FD, DISASOCIATED_IPC_FD};
+struct polldata {
+ enum fdtype fdtype; /* IPC fd or listening socket fd */
+ void *data; /* pointer to AFPconfig for listening socket and *
+ * pointer to afp_child_t for IPC fd */
+};
+
+extern void fdset_add_fd(int maxconns,
+ struct pollfd **fdsetp,
+ struct polldata **polldatap,
+ int *fdset_usedp,
+ int *fdset_sizep,
+ int fd,
+ enum fdtype fdtype,
+ void *data);
+extern void fdset_del_fd(struct pollfd **fdsetp,
+ struct polldata **polldatap,
+ int *fdset_usedp,
+ int *fdset_sizep,
+ int fd);
+extern int send_fd(int socket, int fd);
+extern int recv_fd(int fd, int nonblocking);
+
+/******************************************************************
+ * unix.c
+ *****************************************************************/
+
+extern const char *getcwdpath(void);
+extern const char *fullpathname(const char *);
+extern char *stripped_slashes_basename(char *p);
+extern int lchdir(const char *dir);
+extern void randombytes(void *buf, int n);
+extern int daemonize(int nochdir, int noclose);
+extern int run_cmd(const char *cmd, char **cmd_argv);
+extern char *realpath_safe(const char *path);
+extern const char *basename_safe(const char *path);
+
+/******************************************************************
+ * cnid.c
+ *****************************************************************/
+
+extern bstring rel_path_in_vol(const char *path, const char *volpath);
+extern cnid_t cnid_for_path(struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did);
+
+/******************************************************************
+ * cnid.c
+ *****************************************************************/
+
+extern void initline (int, char *);
+extern int parseline (int, char *);
+
+#endif /* _ATALK_UTIL_H */