31#define LOG(kind, ...) GNUNET_log_from (kind, "util-network", __VA_ARGS__)
32#define LOG_STRERROR_FILE(kind, syscall, \
33 filename) GNUNET_log_from_strerror_file (kind, \
37#define LOG_STRERROR(kind, syscall) GNUNET_log_from_strerror (kind, \
41#define DEBUG_NETWORK GNUNET_EXTRA_LOGGING
45#define INVALID_SOCKET -1
81 static int cache_v4 = -1;
82 static int cache_v6 = -1;
83 static int cache_un = -1;
106 s = socket (pf, SOCK_STREAM, 0);
109 if (EAFNOSUPPORT != errno)
145 struct sockaddr_un
dummy;
152 upm =
sizeof(
dummy.sun_path);
153 slen = strlen (unixpath);
157 while (16 + strlen (unixpath) >= upm)
159 if (NULL == (
end = strrchr (unixpath,
'/')))
163 "Unable to shorten unix path `%s' while keeping name unique\n"),
172 strcat (unixpath, (
char *) ae.
encoding);
185 s = socket (AF_UNIX, SOCK_STREAM, 0);
189 "Failed to open AF_UNIX socket");
193 (
struct sockaddr *) un,
194 sizeof(
struct sockaddr_un));
199 if (ECONNREFUSED != eno)
202 if (0 != stat (un->sun_path,
205 if (S_IFSOCK != (S_IFMT & sbuf.st_mode))
209 "Removing left-over `%s' from previous execution\n",
211 if (0 != unlink (un->sun_path))
219#define FD_COPY(s, d) do { GNUNET_memcpy ((d), (s), sizeof(fd_set)); } while (0)
227 int flags = fcntl (fd->
fd, F_GETFL);
236 flags &= ~O_NONBLOCK;
240 if (0 != fcntl (fd->
fd,
264 i = fcntl (
h->fd, F_GETFD);
267 if (i == (i | FD_CLOEXEC))
270 if (fcntl (
h->fd, F_SETFD, i) < 0)
289 setsockopt (
h->fd, SOL_SOCKET, SO_NOSIGPIPE,
290 (
const void *) &abs_value,
351 if (
h->fd >= FD_SETSIZE)
361 "socket_set_inheritable");
381 if ((
type == SOCK_STREAM)
394 socklen_t *address_len)
402 struct sockaddr_storage
name;
403 socklen_t namelen =
sizeof(
name);
405 int gsn = getsockname (desc->
fd,
406 (
struct sockaddr *) &
name,
411 "Accepting connection on `%s'\n",
416 ret->fd = accept (desc->
fd,
440 const struct sockaddr *
address,
441 socklen_t address_len)
450 if (AF_INET6 == desc->
af)
451 if (setsockopt (desc->
fd,
461 if (AF_UNIX ==
address->sa_family)
467 if ( (SOCK_STREAM == desc->
type) &&
468 (0 != setsockopt (desc->
fd,
479 const struct sockaddr_un *un = (
const struct sockaddr_un *)
address;
480 int not_abstract = 0;
482 if ((AF_UNIX ==
address->sa_family)
483 && (
'\0' != un->sun_path[0]))
486 old_mask = umask (S_IWGRP | S_IRGRP | S_IXGRP | S_IWOTH | S_IROTH
489 ret = bind (desc->
fd,
494 (void) umask (old_mask);
511 const struct sockaddr_un *un;
513 ret = close (desc->
fd);
515 un = (
const struct sockaddr_un *) desc->
addr;
519 if ((AF_UNIX == desc->
af) &&
520 (NULL != desc->
addr) &&
521 (
'\0' != un->sun_path[0]))
524 sizeof(un->sun_path));
526 if (0 != unlink (dirname))
536 len = strlen (dirname);
540 if ((0 != len) && (0 != rmdir (dirname)))
584 if (fcntl (
fd, F_GETFD) < 0)
603 const struct sockaddr *
address,
604 socklen_t address_len)
608 ret = connect (desc->
fd,
635 ret = getsockopt (desc->
fd,
657 ret = listen (desc->
fd,
678 error = ioctl (desc->
fd,
691 struct sockaddr *src_addr,
697 flags |= MSG_DONTWAIT;
699 return recvfrom (desc->
fd,
727 flags |= MSG_DONTWAIT;
729 ret = recv (desc->
fd,
747 flags |= MSG_DONTWAIT;
750 flags |= MSG_NOSIGNAL;
752 ret = send (desc->
fd,
775 const struct sockaddr *dest_addr,
781 flags |= MSG_DONTWAIT;
784 flags |= MSG_NOSIGNAL;
786 return sendto (desc->
fd,
809 const void *option_value,
810 socklen_t option_len)
812 return (0 == setsockopt (
fd->fd,
840 fd = socket (domain,
type, protocol);
866 ret = shutdown (desc->
fd, how);
891 setsockopt (desc->
fd,
900 setsockopt (desc->
fd,
953 return FD_ISSET (desc->
fd,
970 for (nfds = src->
nsds; nfds >= 0; nfds--)
971 if (FD_ISSET (nfds, &src->
sds))
972 FD_SET (nfds, &dst->
sds);
1062 FD_SET (nfd, &to->
sds);
1130 return FD_ISSET (
h->fd,
1153 if ((FD_ISSET (nfds,
1205 char open_port_str[6];
1206 struct addrinfo hint;
1207 struct addrinfo *
ret;
1208 struct addrinfo *
ai;
1211 sizeof(open_port_str),
1213 (
unsigned int)
port);
1214 socktype = (IPPROTO_TCP == ipproto) ? SOCK_STREAM : SOCK_DGRAM;
1216 memset (&hint, 0,
sizeof(hint));
1217 hint.ai_family = AF_UNSPEC;
1218 hint.ai_socktype = socktype;
1219 hint.ai_protocol = ipproto;
1220 hint.ai_addrlen = 0;
1221 hint.ai_addr = NULL;
1222 hint.ai_canonname = NULL;
1223 hint.ai_next = NULL;
1283 "Fatal internal logic error, process hangs in `%s' (abort with CTRL-C)!\n"),
1290 tv.tv_sec = LONG_MAX;
1291 tv.tv_usec = 999999L;
1301 return select (nfds,
1302 (NULL != rfds) ? &rfds->
sds : NULL,
1303 (NULL != wfds) ? &wfds->
sds : NULL,
1304 (NULL != efds) ? &efds->
sds : NULL,
enum GNUNET_GenericReturnValue GNUNET_DISK_internal_file_handle_(const struct GNUNET_DISK_FileHandle *fh, int *dst)
Retrieve OS file handle.
Internal DISK related helper functions.
static struct GNUNET_ARM_Handle * h
Connection with ARM.
static int ret
Final status code.
static struct GNUNET_TIME_Relative timeout
User defined timestamp for completing operations.
static int end
Set if we are to shutdown all services (including ARM).
static uint16_t port
Port number.
static struct GNUNET_TRANSPORT_AddressIdentifier * ai
Handle to the operation that publishes our address.
static char * address
GNS address for this phone.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
static struct GNUNET_IDENTITY_Handle * sh
Handle to IDENTITY service.
static char * name
Name (label) of the records to list.
static char * value
Value of the record to add/remove.
static uint32_t type
Type string converted to DNS type value.
static unsigned int pending
The number of DNS queries that are outstanding.
commonly used definitions; globals in this file are exempt from the rule that the module name ("commo...
void GNUNET_CRYPTO_hash(const void *block, size_t size, struct GNUNET_HashCode *ret)
Compute hash of a given block.
void GNUNET_CRYPTO_hash_to_enc(const struct GNUNET_HashCode *block, struct GNUNET_CRYPTO_HashAsciiEncoded *result)
Convert hash to ASCII encoding.
#define GNUNET_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur.
const char * GNUNET_a2s(const struct sockaddr *addr, socklen_t addrlen)
Convert a "struct sockaddr*" (IPv4 or IPv6 address) to a string (for printing debug messages).
#define GNUNET_log_strerror(level, cmd)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
#define GNUNET_log_strerror_file(level, cmd, filename)
Log an error message at log-level 'level' that indicates a failure of the command 'cmd' with the mess...
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strndup(a, length)
Wrapper around GNUNET_xstrndup_.
int GNUNET_snprintf(char *buf, size_t size, const char *format,...) __attribute__((format(printf
Like snprintf, just aborts if the buffer is of insufficient size.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_free(ptr)
Wrapper around free.
struct sockaddr * GNUNET_NETWORK_get_addr(const struct GNUNET_NETWORK_Handle *desc)
Return sockaddr for this network handle.
void GNUNET_NETWORK_fdset_handle_set_first(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Add a file handle to the fd set.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_accept(const struct GNUNET_NETWORK_Handle *desc, struct sockaddr *address, socklen_t *address_len)
Accept a new connection on a socket.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname, void *optval, socklen_t *optlen)
Get socket options.
void GNUNET_NETWORK_fdset_zero(struct GNUNET_NETWORK_FDSet *fds)
Reset FD set.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
void GNUNET_NETWORK_fdset_destroy(struct GNUNET_NETWORK_FDSet *fds)
Releases the associated memory of an fd set.
socklen_t GNUNET_NETWORK_get_addrlen(const struct GNUNET_NETWORK_Handle *desc)
Return sockaddr length for this network handle.
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
void GNUNET_NETWORK_fdset_copy_native(struct GNUNET_NETWORK_FDSet *to, const fd_set *from, int nfds)
Copy a native fd set.
int GNUNET_NETWORK_test_port_free(int ipproto, uint16_t port)
Test if the given port is available.
int GNUNET_NETWORK_fdset_overlap(const struct GNUNET_NETWORK_FDSet *fds1, const struct GNUNET_NETWORK_FDSet *fds2)
Checks if two fd sets overlap.
ssize_t GNUNET_NETWORK_socket_recv(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length)
Read data from a connected socket (always non-blocking).
char * GNUNET_NETWORK_shorten_unixpath(char *unixpath)
Given a unixpath that is too long (larger than UNIX_PATH_MAX), shorten it to an acceptable length whi...
ssize_t GNUNET_NETWORK_socket_recvfrom(const struct GNUNET_NETWORK_Handle *desc, void *buffer, size_t length, struct sockaddr *src_addr, socklen_t *addrlen)
Read data from a socket (always non-blocking).
struct GNUNET_NETWORK_FDSet * GNUNET_NETWORK_fdset_create()
Creates an fd set.
void GNUNET_NETWORK_fdset_add(struct GNUNET_NETWORK_FDSet *dst, const struct GNUNET_NETWORK_FDSet *src)
Add one fd set to another.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_set_blocking(struct GNUNET_NETWORK_Handle *fd, int doBlock)
Set if a socket should use blocking or non-blocking IO.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_box_native(int fd)
Box a native socket (and check that it is a socket).
void GNUNET_NETWORK_fdset_set_native(struct GNUNET_NETWORK_FDSet *to, int nfd)
Set a native fd in a set.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
void GNUNET_NETWORK_fdset_copy(struct GNUNET_NETWORK_FDSet *to, const struct GNUNET_NETWORK_FDSet *from)
Copy one fd set to another.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_connect(const struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Connect a socket to some remote address.
ssize_t GNUNET_NETWORK_socket_recvfrom_amount(const struct GNUNET_NETWORK_Handle *desc)
How much data is available to be read on this descriptor?
int GNUNET_NETWORK_fdset_test_native(const struct GNUNET_NETWORK_FDSet *to, int nfd)
Test native fd in a set.
void GNUNET_NETWORK_fdset_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Add a socket to the FD set.
void GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
Only free memory of a socket, keep the file descriptor untouched.
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
enum GNUNET_GenericReturnValue GNUNET_NETWORK_test_pf(int pf)
Test if the given protocol family is supported by this system.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_bind(struct GNUNET_NETWORK_Handle *desc, const struct sockaddr *address, socklen_t address_len)
Bind a socket to a particular address.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_listen(const struct GNUNET_NETWORK_Handle *desc, int backlog)
Listen on a socket.
void GNUNET_NETWORK_unix_precheck(const struct sockaddr_un *un)
If services crash, they can leave a unix domain socket file on the disk.
int GNUNET_NETWORK_fdset_handle_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Check if a file handle is part of an fd set.
int GNUNET_NETWORK_socket_select(struct GNUNET_NETWORK_FDSet *rfds, struct GNUNET_NETWORK_FDSet *wfds, struct GNUNET_NETWORK_FDSet *efds, const struct GNUNET_TIME_Relative timeout)
Check if sockets or pipes meet certain conditions.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc, int how)
Shut down socket operations.
int GNUNET_NETWORK_socket_setsockopt(struct GNUNET_NETWORK_Handle *fd, int level, int option_name, const void *option_value, socklen_t option_len)
Set socket option.
ssize_t GNUNET_NETWORK_socket_sendto(const struct GNUNET_NETWORK_Handle *desc, const void *message, size_t length, const struct sockaddr *dest_addr, socklen_t dest_len)
Send data to a particular destination (always non-blocking).
int GNUNET_NETWORK_fdset_isset(const struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_NETWORK_Handle *desc)
Check whether a socket is part of the fd set.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_disable_corking(struct GNUNET_NETWORK_Handle *desc)
Disable the "CORK" feature for communication with the given socket, forcing the OS to immediately flu...
void GNUNET_NETWORK_fdset_handle_set(struct GNUNET_NETWORK_FDSet *fds, const struct GNUNET_DISK_FileHandle *h)
Add a file handle to the fd set.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_UNIT_SECONDS
One second.
static int initialize_network_handle(struct GNUNET_NETWORK_Handle *h, int af, int type)
Perform proper canonical initialization for a network handle.
static void socket_set_nodelay(const struct GNUNET_NETWORK_Handle *h)
Disable delays when sending data via the socket.
#define LOG_STRERROR_FILE(kind, syscall, filename)
#define LOG_STRERROR(kind, syscall)
static int socket_set_inheritable(const struct GNUNET_NETWORK_Handle *h)
Make a socket non-inheritable to child processes.
0-terminated ASCII encoding of a struct GNUNET_HashCode.
unsigned char encoding[104]
Handle used to access files (and pipes).
collection of IO descriptors
int nsds
Maximum number of any socket descriptor in the set (plus one)
fd_set sds
Bitset with the descriptors.
struct sockaddr * addr
Address we were bound to, or NULL.
int type
Type of the socket.
socklen_t addrlen
Number of bytes in addr.
int af
Address family / domain.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.