netatalk  4.4.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
util.h File Reference

Netatalk utility functions. More...

#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
 atalk socket event data More...
struct  asev
 atalk socket event More...

Macros

#define RLIM_MAX   65535
#define EXITERR_CLNT   1
#define EXITERR_CONF   2
#define EXITERR_SYS   3
#define EXITERR_CLOSED   4
#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

ssize_t sys_sendfile (int __out_fd, int __in_fd, off_t *__offset, size_t __count)
char ** getifacelist (void)
 Get interfaces from the kernel.
void freeifacelist (char **)
int atalk_aton (char *, struct at_addr *)
 Check whether "cp" is a valid ascii representation of an AppleTalk address and convert to a binary address.
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)
 non-blocking drop-in replacement for read with timeout using select
ssize_t writet (int socket, void *data, const size_t length, int setnonblocking, int timeout)
 non-blocking drop-in replacement for read with timeout using select
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)
 Tokenize IP(4/6) addresses with an optional port into address and port.
struct asevasev_init (int max)
bool asev_add_fd (struct asev *sev, int fd, enum asev_fdtype fdtype, void *private, int protocol)
 Add a fd to a dynamic pollfd array and associated data array.
bool asev_del_fd (struct asev *sev, int fd)
 Remove fd from asev.
int send_fd (int socket, int fd)
int recv_fd (int fd, int nonblocking)
 Receive a fd on a suitable socket.
const char * getcwdpath (void)
 get cwd in static buffer
const char * fullpathname (const char *)
 Request absolute path.
char * stripped_slashes_basename (char *p)
 Takes a buffer with a path, strips slashs, returns basename.
void randombytes (void *buf, int n)
int daemonize (void)
 Fork, exit parent, setsid(), chdir("/"), close all fds.
int run_cmd (const char *cmd, char **cmd_argv)
char * realpath_safe (const char *path)
const char * basename_safe (const char *path)
 safe basename() replacement
char * strtok_quote (char *s, const char *delim)
 extended strtok allows the quoted strings
const char * tmpdir (void)
 Find a suitable temporary directory for Netatalk.
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)
 ostat/fsstatat multiplexer
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)
 chmod() wrapper for symlink and ACL handling
bstring rel_path_in_vol (const char *path, const char *volpath)
 Build path relative to volume root.
cnid_t cnid_for_path (struct _cnid_db *cdb, const char *volpath, const char *path, cnid_t *did)
 Resolves CNID of a given path.
char * uuid_strip_dashes (const char *uuid)
void initline (int, char *)
int parseline (int, char *)
char * convert_utf8_to_mac (const struct vol *vol, const char *upath)
 Convert a UTF-8 filename to a mangled Mac filename.

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)

LOG assert errors

◆ AFP_PANIC

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

Print a SBT and exit

◆ BSTRING_STRIP_SLASH

#define BSTRING_STRIP_SLASH ( a)
Value:
do { \
while (bchar((a), blength(a) - 1) == '/') \
bdelete((a), blength(a) - 1, 1); \
} while (0);

strip slashes from end of a bstring

◆ 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
[in]fdFile descriptor
[in]offsetbyte offset relative to l_whence
[in]whenceSEEK_SET, SEEK_CUR, SEEK_END
[in]lenno. 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
[in]fdFile descriptor
[in]offsetbyte offset relative to l_whence
[in]whenceSEEK_SET, SEEK_CUR, SEEK_END
[in]lenno. 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
[in]fdFile descriptor
[in]offsetbyte offset relative to l_whence
[in]whenceSEEK_SET, SEEK_CUR, SEEK_END
[in]lenno. 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
[in,out]sapointer to an struct sockaddr
[in]masknumber 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

Check whether "cp" is a valid ascii representation of an AppleTalk address and convert to a binary address.

Examples of accepted forms are (in decimal, net of 4321, node of 65):

4321.65
0x10E1.41
16.225.65
0x10.E1.41

If hex is used, and the first digit is one of A-F, the leading 0x is redundant. Returns 1 if the address is valid, 0 if not.

Unlike Internet addresses, AppleTalk addresses can have leading 0's. This means that we can't support octal addressing.

◆ basename_safe()

const char * basename_safe ( const char * path)
extern

safe basename() replacement

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:

  1. relative: "dir/subdir" with cwd: "/afp_volume/topdir"
  2. 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
[in]cdbCNID db handle
[in]volpathUNIX path of volume
[in]pathpath, see above
[out]didparent 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
[in]sa1pointer to an struct sockaddr
[in]sa2pointer 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.

◆ convert_utf8_to_mac()

char * convert_utf8_to_mac ( const struct vol * vol,
const char * upath )
extern

Convert a UTF-8 filename to a mangled Mac filename.

Parameters
volvolume structure
upathinput path in UTF-8
Returns
pointer to static buffer containing mangled Mac filename
See also
utompath() in etc/afpd/desktop.c

◆ create_lockfile()

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

Check and create lockfile

◆ daemonize()

int daemonize ( void )
extern

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

setup our fault handlers

◆ freeifacelist()

void freeifacelist ( char ** ifacelist)
extern

go through and free the interface list

◆ 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

Get interfaces from the kernel.

Note
we keep an extra null entry to signify the end of the interface list.

◆ getip_port()

unsigned int getip_port ( const struct sockaddr * sa)
extern

return port number from struct sockaddr

Parameters
[in]sapointer 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
[in]sapointer 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
[in]fdFile descriptor
[in]cmdcmd to fcntl, only F_SETLK is usable here
[in]typeF_RDLCK, F_WRLCK, F_UNLCK
[in]offsetbyte offset relative to l_whence
[in]whenceSEEK_SET, SEEK_CUR, SEEK_END
[in]lenno. 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

Something really nasty happened - panic !

◆ 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
[in]pathpath
[in]moderequested mode
[in]ststat() of path or NULL
[in]optionsO_NOFOLLOW | O_NETATALK_ACL

Option descriptions:

  • 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

ostat/fsstatat multiplexer

ostatat mulitplexes ostat and fstatat.

Parameters
[in]dirfd-1 gives AT_FDCWD
[in]pathpathname
[in,out]stpointer to struct stat
[in]optionsfile options

◆ 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
[in]socketsocket, if in blocking mode, pass "setnonblocking" arg as 1
[in,out]databuffer for the read data
[in]lengthhow many bytes to read
[in]setnonblockingwhen non-zero this func will enable and disable non blocking io mode for the socket
[in]timeoutnumber 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

realpath() replacement that always allocates storage for returned path

◆ recv_fd()

int recv_fd ( int fd,
int nonblocking )
extern

Receive a fd on a suitable socket.

Parameters
[in]fdPF_UNIX socket to receive on
[in]nonblocking0: 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 relative to volume root.

path might be:

  1. relative: "dir/subdir" with cwd: "/afp_volume/topdir"
  2. absolute: "/afp_volume/dir/subdir"
Parameters
[in]pathpath relative to cwd() or absolute
[in]volpathvolume 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

Send a fd across a suitable socket

◆ server_lock()

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

this creates an open lock file which hangs around until the program dies. it returns the pid. due to problems w/ solaris, this has been changed to do the kill() thing.

◆ setnonblock()

int setnonblock ( int fd,
int cmd )
extern

set or unset non-blocking IO on a fd

Parameters
[in]fdFile descriptor
[in]cmd0: 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

◆ sys_sendfile()

ssize_t sys_sendfile ( int __out_fd,
int __in_fd,
off_t * __offset,
size_t __count )
extern

◆ tmpdir()

const char * tmpdir ( void )
extern

Find a suitable temporary directory for Netatalk.

Creates a subdirectory for current gid if it doesn't exist. The result should be copied immediately as it may be overwritten by a subsequent call.

◆ 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
[in]ipurlIP URL string
[out]addressIP address
[out]portIP 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.

◆ uuid_strip_dashes()

char * uuid_strip_dashes ( const char * uuid)
extern

Return allocated UUID string with dashes stripped

◆ 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
[in]socketsocket, if in blocking mode, pass "setnonblocking" arg as 1
[in,out]databuffer for the read data
[in]lengthhow many bytes to read
[in]setnonblockingwhen non-zero this func will enable and disable non blocking io mode for the socket
[in]timeoutnumber 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[]