netatalk  4.4.0dev
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
util.h File Reference
#include <poll.h>
#include <stdbool.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/types.h>
#include <unistd.h>
#include <bstrlib.h>
#include <netatalk/at.h>
#include <atalk/cnid.h>
#include <atalk/unicode.h>

Go to the source code of this file.

Data Structures

struct  asev_data
struct  asev

Macros

#define RLIM_MAX   65535
#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 */
#define EXITERR_CLOSED   4 /* connection was immediately closed after TCP handshake */
#define AFP_PANIC(why)
#define AFP_ASSERT(b)
#define MIN(a, b)
#define MAX(a, b)
#define STRCMP(a, b, c)
#define ZERO_STRUCT(a)
#define ZERO_STRUCTP(a)
#define hton64(x)
#define ntoh64(x)
#define SAFE_FREE(x)
#define diatolower(x)
#define diatoupper(x)
#define server_unlock(x)
#define mod_error()
#define strequal(a, b)
#define cfrombstr(b)
#define BSTRING_STRIP_SLASH(a)
#define read_lock(fd, offset, whence, len)
 place read lock on file
#define write_lock(fd, offset, whence, len)
 place write lock on file
#define unlock(fd, offset, whence, len)
 unlock a file

Enumerations

enum  asev_fdtype { IPC_FD , LISTEN_FD }

Functions

char ** getifacelist (void)
void freeifacelist (char **)
int atalk_aton (char *, struct at_addr *)
void bprint (char *, int)
int strdiacasecmp (const char *, const char *)
int strndiacasecmp (const char *, const char *, size_t)
pid_t server_lock (char *, char *, int)
int check_lockfile (const char *program, const char *pidfile)
int create_lockfile (const char *program, const char *pidfile)
void fault_setup (void(*fn)(void *))
void netatalk_panic (const char *why)
void * mod_open (const char *)
void * mod_symbol (void *, const char *)
void mod_close (void *)
int lock_reg (int fd, int cmd, int type, off_t offest, int whence, off_t len)
 lock a file with fctnl
int setnonblock (int fd, int cmd)
 set or unset non-blocking IO on a fd
ssize_t readt (int socket, void *data, const size_t length, int setnonblocking, int timeout)
ssize_t writet (int socket, void *data, const size_t length, int setnonblocking, int timeout)
const char * getip_string (const struct sockaddr *sa)
 convert an IPv4 or IPv6 address to a static string using inet_ntop
unsigned int getip_port (const struct sockaddr *sa)
 return port number from struct sockaddr
void apply_ip_mask (struct sockaddr *ai, int maskbits)
 apply netmask to IP (v4 or v6)
int compare_ip (const struct sockaddr *sa1, const struct sockaddr *sa2)
 compare IP addresses for equality
int tokenize_ip_port (const char *ipurl, char **address, char **port)
struct asevasev_init (int max)
bool asev_add_fd (struct asev *sev, int fd, enum asev_fdtype fdtype, void *private, int protocol)
bool asev_del_fd (struct asev *sev, int fd)
int send_fd (int socket, int fd)
int recv_fd (int fd, int nonblocking)
const char * getcwdpath (void)
 get cwd in static buffer
const char * fullpathname (const char *)
 Request absolute path.
char * stripped_slashes_basename (char *p)
void randombytes (void *buf, int n)
int daemonize (void)
int run_cmd (const char *cmd, char **cmd_argv)
char * realpath_safe (const char *path)
const char * basename_safe (const char *path)
char * strtok_quote (char *s, const char *delim)
const char * tmpdir (void)
int ochdir (const char *dir, int options)
 symlink safe chdir replacement
int ostat (const char *path, struct stat *buf, int options)
int ostatat (int dirfd, const char *path, struct stat *st, int options)
int ochown (const char *path, uid_t owner, gid_t group, int options)
int ochmod (char *path, mode_t mode, const struct stat *st, int options)
bstring rel_path_in_vol (const char *path, const char *volpath)
cnid_t cnid_for_path (struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did)
void initline (int, char *)
int parseline (int, char *)

Variables

const int _diacasemap []
const int _dialowermap []

Detailed Description

Netatalk utility functions

Utility functions for these areas:

  • sockets
  • locking
  • misc UNIX function wrappers, e.g. for getcwd

Macro Definition Documentation

◆ AFP_ASSERT

#define AFP_ASSERT ( b)
Value:
do { \
if (!(b)) { \
AFP_PANIC(#b); \
} \
} while(0)

◆ AFP_PANIC

#define AFP_PANIC ( why)
Value:
do { \
netatalk_panic(why); \
abort(); \
} while(0);

◆ BSTRING_STRIP_SLASH

#define BSTRING_STRIP_SLASH ( a)
Value:
do { \
while (bchar((a), blength(a) - 1) == '/') \
bdelete((a), blength(a) - 1, 1); \
} while (0);
#define blength(b)
Definition bstrlib.h:1427
#define bchar(b, p)
Definition bstrlib.h:1481

◆ cfrombstr

#define cfrombstr ( b)
Value:
((char *)((b)->data))
#define data
Definition hash.c:37

◆ diatolower

#define diatolower ( x)
Value:
_dialowermap[(unsigned char) (x)]
const int _dialowermap[]
Definition include/atalk/util.h:91

◆ diatoupper

#define diatoupper ( x)
Value:
_diacasemap[(unsigned char) (x)]
const int _diacasemap[]
Definition strdicasecmp.c:7

◆ EXITERR_CLNT

#define EXITERR_CLNT   1 /* client related error */

◆ EXITERR_CLOSED

#define EXITERR_CLOSED   4 /* connection was immediately closed after TCP handshake */

◆ EXITERR_CONF

#define EXITERR_CONF   2 /* error in config files/cmd line parameters */

◆ EXITERR_SYS

#define EXITERR_SYS   3 /* local system error */

◆ hton64

#define hton64 ( x)
Value:
((uint64_t) (htonl(((x) >> 32) & 0xffffffffLL)) | \
(uint64_t) ((htonl(x) & 0xffffffffLL) << 32))

◆ MAX

#define MAX ( a,
b )
Value:
((a) > (b) ? (a) : (b))

◆ MIN

#define MIN ( a,
b )
Value:
((a) < (b) ? (a) : (b))

◆ mod_error

#define mod_error ( )
Value:
""

◆ ntoh64

#define ntoh64 ( x)
Value:
(hton64(x))
#define hton64(x)
Definition include/atalk/util.h:77

◆ read_lock

#define read_lock ( fd,
offset,
whence,
len )
Value:
lock_reg((fd), F_SETLK, F_RDLCK, (offset), (whence), (len))
int lock_reg(int fd, int cmd, int type, off_t offest, int whence, off_t len)
lock a file with fctnl
Definition locking.c:86

place read lock on file

Parameters
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 with fcntl return value and errno

◆ RLIM_MAX

#define RLIM_MAX   65535

◆ SAFE_FREE

#define SAFE_FREE ( x)
Value:
do { if ((x) != NULL) {free(x); x=NULL;} } while(0)

◆ server_unlock

#define server_unlock ( x)
Value:
(unlink(x))

◆ STRCMP

#define STRCMP ( a,
b,
c )
Value:
(strcmp(a,c) b 0)

◆ strequal

#define strequal ( a,
b )
Value:
(strcmp((a),(b)) == 0)

◆ unlock

#define unlock ( fd,
offset,
whence,
len )
Value:
lock_reg((fd), F_SETLK, F_UNLCK, (offset), (whence), (len))

unlock a file

Parameters
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 with fcntl return value and errno

◆ write_lock

#define write_lock ( fd,
offset,
whence,
len )
Value:
lock_reg((fd), F_SETLK, F_WRLCK, (offset), (whence), (len))

place write lock on file

Parameters
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 with fcntl return value and errno

◆ ZERO_STRUCT

#define ZERO_STRUCT ( a)
Value:
memset(&(a), 0, sizeof(a))

◆ ZERO_STRUCTP

#define ZERO_STRUCTP ( a)
Value:
memset((a), 0, sizeof(a))

Enumeration Type Documentation

◆ asev_fdtype

Enumerator
IPC_FD 
LISTEN_FD 

Function Documentation

◆ apply_ip_mask()

void apply_ip_mask ( struct sockaddr * sa,
int mask )
extern

apply netmask to IP (v4 or v6)

Modifies IP address in sa->sin[6]_addr-s[6]_addr. The caller is responsible for passing a value for mask that is sensible to the passed address, e.g. 0 <= mask <= 32 for IPv4 or 0<= mask <= 128 for IPv6. mask > 32 for IPv4 is treated as mask = 32, mask > 128 is set to 128 for IPv6.

Parameters
sa(rw) pointer to an struct sockaddr
mask(r) number of maskbits

◆ asev_add_fd()

bool asev_add_fd ( struct asev * asev,
int fd,
enum asev_fdtype fdtype,
void * private,
int protocol )
extern

Add a fd to a dynamic pollfd array and associated data array

This uses an additional array of struct polldata which stores type information (enum fdtype) and a pointer to anciliary user data.

◆ asev_del_fd()

bool asev_del_fd ( struct asev * asev,
int fd )
extern

Remove fd from asev

Returns
true if the fd was deleted, otherwise false

◆ asev_init()

struct asev * asev_init ( int max)
extern

Allocate and initialize atalk socket event struct

◆ atalk_aton()

int atalk_aton ( char * cp,
struct at_addr * addr )
extern

◆ basename_safe()

const char * basename_safe ( const char * path)
extern

Returns pointer to static buffer with basename of path

◆ bprint()

void bprint ( char * data,
int len )
extern

◆ check_lockfile()

int check_lockfile ( const char * program,
const char * pidfile )
extern

Check lockfile

◆ cnid_for_path()

cnid_t cnid_for_path ( struct _cnid_db * cdb,
const char * volpath,
const char * path,
cnid_t * did )
extern

Resolves CNID of a given path

path might be: (a) relative: "dir/subdir" with cwd: "/afp_volume/topdir" (b) absolute: "/afp_volume/dir/subdir"

path MUST be pointing inside vol, this is usually the case as vol has been build from path using loadvolinfo and friends.

Parameters
cdb(r) CNID db handle
volpath(r) UNIX path of volume
path(r) path, see above
did(w) parent CNID of returned CNID
Returns
CNID of path

◆ compare_ip()

int compare_ip ( const struct sockaddr * sa1,
const struct sockaddr * sa2 )
extern

compare IP addresses for equality

Parameters
sa1(r) pointer to an struct sockaddr
sa2(r) pointer to an struct sockaddr
Returns
Addresses are converted to strings and compared with strcmp and the result of strcmp is returned.
Note
IPv6 mapped IPv4 addresses are treated as IPv4 addresses.

◆ create_lockfile()

int create_lockfile ( const char * program,
const char * pidfile )
extern

Check and create lockfile

◆ daemonize()

int daemonize ( void )
extern

Daemonize

Fork, exit parent, setsid(), chdir("/"), close all fds

returns -1 on failure, but you can't do much except exit in that case since we may already have forked

◆ fault_setup()

void fault_setup ( void(* fn )(void *))
extern

◆ freeifacelist()

void freeifacelist ( char ** ifacelist)
extern

◆ fullpathname()

const char * fullpathname ( const char * name)
extern

Request absolute path.

Returns
Absolute filesystem path to object

◆ getcwdpath()

const char * getcwdpath ( void )
extern

get cwd in static buffer

Returns
pointer to path or pointer to error messages on error

◆ getifacelist()

char ** getifacelist ( void )
extern

◆ getip_port()

unsigned int getip_port ( const struct sockaddr * sa)
extern

return port number from struct sockaddr

Parameters
sa(r) pointer to an struct sockaddr
Returns
port as unsigned int

◆ getip_string()

const char * getip_string ( const struct sockaddr * sa)
extern

convert an IPv4 or IPv6 address to a static string using inet_ntop

IPv6 mapped IPv4 addresses are returned as IPv4 addreses e.g. ::ffff:10.0.0.0 is returned as "10.0.0.0".

Parameters
sa(r) pointer to an struct sockaddr
Returns
pointer to a static string cotaining the converted address as string.
On error pointers to "0.0.0.0" or "::0" are returned.

◆ initline()

void initline ( int len,
char * line )
extern

◆ lock_reg()

int lock_reg ( int fd,
int cmd,
int type,
off_t offset,
int whence,
off_t len )
extern

lock a file with fctnl

This function is called via the macros: read_lock, write_lock, un_lock

Parameters
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 with fcntl return value and errno
See also
read_lock, write_lock, unlock

◆ mod_close()

void mod_close ( void * )
extern

◆ mod_open()

void * mod_open ( const char * )
extern

◆ mod_symbol()

void * mod_symbol ( void * ,
const char *  )
extern

◆ netatalk_panic()

void netatalk_panic ( const char * why)
extern

◆ ochdir()

int ochdir ( const char * dir,
int options )
extern

symlink safe chdir replacement

Only chdirs to dir if it doesn't contain symlinks or if symlink checking is disabled

Returns
1 if a path element is a symlink, 0 otherwise, -1 on syserror

◆ ochmod()

int ochmod ( char * path,
mode_t mode,
const struct stat * st,
int options )
extern

chmod() wrapper for symlink and ACL handling

Parameters
path(r) path
mode(r) requested mode
st(r) stat() of path or NULL
options(r) O_NOFOLLOW | O_NETATALK_ACL

Options description: O_NOFOLLOW: don't chmod() symlinks, do nothing, return 0 O_NETATALK_ACL: call chmod_acl() instead of chmod() O_IGNORE: ignore chmod() request, directly return 0

◆ ochown()

int ochown ( const char * path,
uid_t owner,
gid_t group,
int options )
extern

◆ ostat()

int ostat ( const char * path,
struct stat * buf,
int options )
extern

◆ ostatat()

int ostatat ( int dirfd,
const char * path,
struct stat * st,
int options )
extern

◆ parseline()

int parseline ( int len,
char * token )
extern

◆ randombytes()

void randombytes ( void * buf,
int n )
extern

Store n random bytes an buf

◆ readt()

ssize_t readt ( int socket,
void * data,
const size_t length,
int setnonblocking,
int timeout )
extern

non-blocking drop-in replacement for read with timeout using select

Parameters
socket(r) socket, if in blocking mode, pass "setnonblocking" arg as 1
data(rw) buffer for the read data
length(r) how many bytes to read
setnonblocking(r) when non-zero this func will enable and disable non blocking io mode for the socket
timeout(r) number of seconds to try reading, 0 means no timeout
Returns
number of bytes actually read or -1 on timeout or error

◆ realpath_safe()

char * realpath_safe ( const char * path)
extern

◆ recv_fd()

int recv_fd ( int fd,
int nonblocking )
extern

Receive a fd on a suitable socket

Parameters
fd(r) PF_UNIX socket to receive on
nonblocking(r) 0: fd is in blocking mode - 1: fd is nonblocking, poll for 1 sec
Returns
fd on success, -1 on error

◆ rel_path_in_vol()

bstring rel_path_in_vol ( const char * path,
const char * volpath )
extern

Build path relativ to volume root

path might be: (a) relative: "dir/subdir" with cwd: "/afp_volume/topdir" (b) absolute: "/afp_volume/dir/subdir"

Parameters
path(r) path relative to cwd() or absolute
volpath(r) volume path that path is a subdir of (has been computed in volinfo funcs)
Returns
relative path in new bstring, caller must bdestroy it

◆ run_cmd()

int run_cmd ( const char * cmd,
char ** cmd_argv )
extern

Run command in a child and wait for it to finish

◆ send_fd()

int send_fd ( int socket,
int fd )
extern

◆ server_lock()

pid_t server_lock ( char * program,
char * pidfile,
int debug )
extern

◆ setnonblock()

int setnonblock ( int fd,
int cmd )
extern

set or unset non-blocking IO on a fd

Parameters
fd(r) File descriptor
cmd(r) 0: disable non-blocking IO, i.e. block
<>0: enable non-blocking IO
Returns
0 on success, -1 on failure

◆ strdiacasecmp()

int strdiacasecmp ( const char * s1,
const char * s2 )
extern

◆ stripped_slashes_basename()

char * stripped_slashes_basename ( char * p)
extern

Takes a buffer with a path, strips slashs, returns basename

Parameters
p(rw) path path may be "[/][dir/[...]]file" or "[/][dir/[...]]dir/[/]" Result is "file" or "dir"
Returns
pointer to basename in path buffer, buffer is possibly modified

◆ strndiacasecmp()

int strndiacasecmp ( const char * s1,
const char * s2,
size_t n )
extern

◆ strtok_quote()

char * strtok_quote ( char * s,
const char * delim )
extern

extended strtok allows the quoted strings modified strtok.c in glibc 2.0.6

◆ tmpdir()

const char * tmpdir ( void )
extern

◆ tokenize_ip_port()

int tokenize_ip_port ( const char * ipurl,
char ** address,
char ** port )
extern

Tokenize IP(4/6) addresses with an optional port into address and port

Parameters
ipurl(r) IP URL string
address(w) IP address
port(w) IP port
Returns
0 on success, -1 on failure

Tokenize IPv4, IPv4:port, IPv6, [IPv6] or [IPv6:port] URL into address and port and return two allocated strings with the address and the port.

If the function returns 0, then address point to a newly allocated valid address string, port may either be NULL or point to a newly allocated port number.

If the function returns -1, then the contents of address and port are undefined.

◆ writet()

ssize_t writet ( int socket,
void * data,
const size_t length,
int setnonblocking,
int timeout )
extern

non-blocking drop-in replacement for read with timeout using select

Parameters
socket(r) socket, if in blocking mode, pass "setnonblocking" arg as 1
data(rw) buffer for the read data
length(r) how many bytes to read
setnonblocking(r) when non-zero this func will enable and disable non blocking io mode for the socket
timeout(r) number of seconds to try reading
Returns
number of bytes actually read or -1 on fatal error

Variable Documentation

◆ _diacasemap

const int _diacasemap[]
extern

◆ _dialowermap

const int _dialowermap[]