netatalk  4.4.0
Free and Open Source Apple Filing Protocol (AFP) Server
Loading...
Searching...
No Matches
socket.c File Reference
#include <arpa/inet.h>
#include <errno.h>
#include <fcntl.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <string.h>
#include <sys/ioctl.h>
#include <sys/socket.h>
#include <sys/time.h>
#include <sys/types.h>
#include <sys/uio.h>
#include <time.h>
#include <unistd.h>
#include <atalk/errchk.h>
#include <atalk/logger.h>
#include <atalk/util.h>

Macros

#define CMSG_SPACE(len)

Functions

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 *sa, int mask)
 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 *asev, 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 *asev, int fd)
 Remove fd from asev.
int recv_fd (int fd, int nonblocking)
 Receive a fd on a suitable socket.
int send_fd (int socket, int fd)

Variables

static char ipv4mapprefix [] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}

Detailed Description

Netatalk utility functions: socket

Macro Definition Documentation

◆ CMSG_SPACE

#define CMSG_SPACE ( len)
Value:
(__CMSG_ALIGN(sizeof(struct cmsghdr)) + __CMSG_ALIGN(len))
#define __CMSG_ALIGN(p)
Definition usockfd.h:26

Length of the space taken up by a padded control message of length len

Function Documentation

◆ apply_ip_mask()

void apply_ip_mask ( struct sockaddr * sa,
int mask )

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 )

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 )

Remove fd from asev.

Returns
true if the fd was deleted, otherwise false

◆ asev_init()

struct asev * asev_init ( int max)

Allocate and initialize atalk socket event struct

◆ compare_ip()

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

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.

◆ getip_port()

unsigned int getip_port ( const struct sockaddr * sa)

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)

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.

◆ readt()

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

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

◆ recv_fd()

int recv_fd ( int fd,
int nonblocking )

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

◆ send_fd()

int send_fd ( int socket,
int fd )

Send a fd across a suitable socket

◆ setnonblock()

int setnonblock ( int fd,
int cmd )

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

◆ tokenize_ip_port()

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

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.

◆ writet()

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

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

◆ ipv4mapprefix

char ipv4mapprefix[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff}
static