44 #define CONNECT_RETRY_TIMEOUT \
45 GNUNET_TIME_relative_multiply (GNUNET_TIME_UNIT_SECONDS, 5)
48 #define LOG_STRERROR(kind, syscall) \
49 GNUNET_log_from_strerror (kind, "util-connection", syscall)
314 connection->
sock = osSocket;
338 struct sockaddr_in *v4;
339 struct sockaddr_in6 *v6;
348 #if HAVE_GETPEEREID || defined(SO_PEERCRED) || HAVE_GETPEERUCRED
355 addrlen =
sizeof(addr);
364 if ((addrlen >
sizeof(addr)) || (addrlen <
sizeof(sa_family_t)))
371 sa = (
struct sockaddr *) addr;
372 v6 = (
struct sockaddr_in6 *) addr;
373 if ((AF_INET6 == sa->sa_family) && (IN6_IS_ADDR_V4MAPPED (&v6->sin6_addr)))
377 memset (v4, 0,
sizeof(
struct sockaddr_in));
378 v4->sin_family = AF_INET;
379 #if HAVE_SOCKADDR_IN_SIN_LEN
380 v4->sin_len = (u_char)
sizeof(
struct sockaddr_in);
383 &((
char *) &v6->sin6_addr)[
sizeof(
struct in6_addr)
384 -
sizeof(
struct in_addr)],
385 sizeof(
struct in_addr));
386 v4->sin_port = v6->sin6_port;
388 addrlen =
sizeof(
struct sockaddr_in);
396 if (AF_UNIX == sa->sa_family)
411 (olen ==
sizeof(
uc)))
418 #if HAVE_GETPEERUCRED
425 gc.
uid = ucred_geteuid (
uc);
426 gc.
gid = ucred_getegid (
uc);
435 if ((NULL != access_cb) &&
436 (
GNUNET_YES != (aret = access_cb (access_cb_cls, gcp, uaddr, addrlen))))
440 _ (
"Access denied to `%s'\n"),
451 connection->
addr = uaddr;
453 connection->
sock = sock;
455 _ (
"Accepting connection from `%s': %p\n"),
475 if ((NULL == connection->
addr) || (0 == connection->
addrlen))
479 *addrlen = connection->
addrlen;
496 "Receive encounters error (%s), connection closed (%p)\n",
521 "Connection signals timeout to receiver (%p)!\n",
541 "Transmission encountered error (%s), connection closed (%p)\n",
544 if (NULL != connection->
sock)
548 connection->
sock = NULL;
577 "Failed to establish TCP connection to `%s:%u', no further addresses to try.\n",
637 "Connection to `%s' succeeded! (%p)\n",
644 "Connection succeeded, starting with receiving data (%p)\n",
657 "Connection succeeded, starting with sending data (%p)\n",
740 const struct sockaddr *
addr,
750 if ((NULL == connection->
ap_head) && (NULL == connection->
sock) &&
755 if (NULL != connection->
sock)
760 "Trying to connect using address `%s:%u/%s:%u'\n",
766 ap->
addr = (
const struct sockaddr *) &ap[1];
771 switch (ap->
addr->sa_family)
774 ((
struct sockaddr_in *) ap->
addr)->sin_port = htons (connection->
port);
778 ((
struct sockaddr_in6 *) ap->
addr)->sin6_port = htons (connection->
port);
787 if (NULL == ap->
sock)
793 "Trying to connect to `%s' (%p)\n",
798 (EINPROGRESS != errno))
869 const char *unixpath)
873 struct sockaddr_un *un;
877 un->sun_family = AF_UNIX;
885 "USE_ABSTRACT_SOCKETS");
887 un->sun_path[0] =
'\0';
890 #if HAVE_SOCKADDR_UN_SUN_LEN
891 un->sun_len = (u_char)
sizeof(
struct sockaddr_un);
897 connection->
port = 0;
899 connection->
addr = (
struct sockaddr *) un;
900 connection->
addrlen =
sizeof(
struct sockaddr_un);
902 if (NULL == connection->
sock)
912 (EINPROGRESS != errno))
916 connection->
sock = NULL;
939 const struct sockaddr *serv_addr,
945 (EINPROGRESS != errno))
950 "Attempt to connect to `%s' failed\n",
960 "Trying to connect to `%s' (%p)\n",
980 const struct sockaddr *serv_addr,
1074 (ENOTCONN != errno) && (ECONNRESET != errno))
1109 char buffer[connection->
max];
1118 "Receive from `%s' encounters error: timeout (%s, %p)\n",
1128 if (NULL == connection->
sock)
1145 "receive_ready read %lu/%lu bytes from `%s' (%p)!\n",
1146 (
unsigned long)
ret,
1147 (
unsigned long) connection->
max,
1190 if (NULL != connection->
sock)
1204 receiver (receiver_cls, NULL, 0, NULL, 0, ETIMEDOUT);
1303 "Transmit to `%s:%u/%s' fails, time out reached (%p).\n",
1330 "Transmission request of size %lu fails (%s/%u), connection failed (%p).\n",
1364 "Transmit to `%s' fails, time out reached (%p).\n",
1379 goto SCHEDULE_WRITE;
1386 goto SCHEDULE_WRITE;
1426 "Connection transmitted %lu/%lu bytes to `%s' (%p)\n",
1427 (
unsigned long)
ret,
1428 (
unsigned long) have,
1444 "Re-scheduling transmit_ready (more to do) (%p).\n",
1513 "Scheduling transmission (%p).\n",
1525 "Need to wait to schedule transmission for connection, adding timeout task (%p).\n",
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
static int ret
Return value of the commandline.
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
static uint16_t port
Port number.
static struct GNUNET_PEERINFO_NotifyContext * notify
Handle to the peerinfo notify service (NULL until we've connected to it).
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
static struct GNUNET_TIME_Relative delay
When should dkg communication start?
static char * hostname
Our hostname; we give this to all the peers we start.
static struct GNUNET_FS_UnindexContext * uc
Functions related to doing DNS lookups.
#define GNUNET_MIN_MESSAGE_SIZE
Smallest supported message.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message,...
enum GNUNET_GenericReturnValue GNUNET_CONFIGURATION_get_value_yesno(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *section, const char *option)
Get a configuration value that should be in a set of "YES" or "NO".
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#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).
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_DEBUG
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
#define GNUNET_new(type)
Allocate a struct or union of the given type.
#define GNUNET_malloc(size)
Wrapper around malloc.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
#define GNUNET_free(ptr)
Wrapper around free.
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.
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
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).
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.
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.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
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_socket_shutdown(struct GNUNET_NETWORK_Handle *desc, int how)
Shut down socket operations.
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_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
struct GNUNET_RESOLVER_RequestHandle * GNUNET_RESOLVER_ip_get(const char *hostname, int af, struct GNUNET_TIME_Relative timeout, GNUNET_RESOLVER_AddressCallback callback, void *callback_cls)
Convert a string to one or more IP addresses.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_now(GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run as soon as possible.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_write_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *wfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_read_net(struct GNUNET_TIME_Relative delay, struct GNUNET_NETWORK_Handle *rfd, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay or when the specified file descriptor is ready f...
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
struct GNUNET_SCHEDULER_Task * GNUNET_SCHEDULER_add_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
@ GNUNET_SCHEDULER_REASON_TIMEOUT
The specified timeout has expired.
@ GNUNET_SCHEDULER_REASON_WRITE_READY
The writing socket is ready.
size_t GNUNET_strlcpy(char *dst, const char *src, size_t n)
Like strlcpy but portable.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_min(struct GNUNET_TIME_Relative t1, struct GNUNET_TIME_Relative t2)
Return the minimum of two relative time values.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_duration(struct GNUNET_TIME_Absolute whence)
Get the duration of an operation as the difference of the current time and the given start time "henc...
struct GNUNET_TIME_Relative GNUNET_TIME_absolute_get_remaining(struct GNUNET_TIME_Absolute future)
Given a timestamp in the future, how much time remains until then?
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
static unsigned int size
Size of the "table".
size_t(* GNUNET_CONNECTION_TransmitReadyNotify)(void *cls, size_t size, void *buf)
Function called to notify a client about the connection begin ready to queue more data.
void(* GNUNET_CONNECTION_Receiver)(void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode)
Callback function for data received from the network.
int(* GNUNET_CONNECTION_AccessCheck)(void *cls, const struct GNUNET_CONNECTION_Credentials *ucred, const struct sockaddr *addr, socklen_t addrlen)
Function to call for access control checks.
#define RETRY
Return code we give on 'send' if we failed to send right now but it makes sense to retry later.
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
void receiver(void *cls, const void *buf, size_t available, const struct sockaddr *addr, socklen_t addrlen, int errCode)
Callback to read from the SOCKS5 proxy.
During connect, we try multiple possible IP addresses to find out which one might work.
socklen_t addrlen
Length of addr.
struct AddressProbe * prev
This is a doubly-linked list.
struct GNUNET_CONNECTION_Handle * connection
Connection for which we are probing.
struct GNUNET_SCHEDULER_Task * task
Task waiting for the connection to finish connecting.
struct GNUNET_NETWORK_Handle * sock
Underlying OS's socket.
const struct sockaddr * addr
The address; do not free (allocated at the end of this struct).
struct AddressProbe * next
This is a linked list.
Credentials for UNIX domain sockets.
uid_t uid
UID of the other end of the connection.
gid_t gid
GID of the other end of the connection.
handle for a network connection
int8_t persist
When shutdown, do not ever actually close the socket, but free resources.
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
int8_t destroy_later
Usually 0.
size_t write_buffer_pos
Current read-offset in write_buffer (how many bytes have already been sent).
char * hostname
Pointer to the hostname if connection was created using DNS lookup, otherwise NULL.
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
size_t max
Maximum number of bytes to read (for receiving).
struct sockaddr * addr
Network address of the other end-point, may be NULL.
socklen_t addrlen
Length of addr.
GNUNET_CONNECTION_Receiver receiver
Function to call on data received, NULL if no receive is pending.
uint16_t port
Port to connect to.
size_t write_buffer_off
Current write-offset in write_buffer (where would we write next).
char * write_buffer
Pointer to our write buffer.
struct AddressProbe * ap_head
Linked list of sockets we are currently trying out (during connect).
struct GNUNET_CONNECTION_TransmitHandle nth
The handle we return for GNUNET_CONNECTION_notify_transmit_ready().
size_t write_buffer_size
Current size of our write_buffer.
struct AddressProbe * ap_tail
Linked list of sockets we are currently trying out (during connect).
struct GNUNET_SCHEDULER_Task * read_task
Read task that we may need to wait for.
struct GNUNET_NETWORK_Handle * sock
Underlying OS's socket, set to NULL after fatal errors.
void * receiver_cls
Closure for receiver.
struct GNUNET_CONNECTION_Handle * proxy_handshake
Handle to subsequent connection after proxy handshake completes,.
struct GNUNET_SCHEDULER_Task * write_task
Write task that we may need to wait for.
struct GNUNET_TIME_Absolute receive_timeout
Timeout for receiving (in absolute time).
struct GNUNET_CONNECTION_Handle * connection
Our connection handle.
struct GNUNET_SCHEDULER_Task * timeout_task
Task called on timeout.
size_t notify_size
At what number of bytes available in the write buffer should the notify method be called?
struct GNUNET_TIME_Absolute transmit_timeout
Timeout for receiving (in absolute time).
GNUNET_CONNECTION_TransmitReadyNotify notify_ready
Function to call if the send buffer has notify_size bytes available.
void * notify_ready_cls
Closure for notify_ready.
socklen_t addrlen
Number of bytes in addr.
Handle to a request given to the resolver.
Context information passed to each scheduler task.
const struct GNUNET_NETWORK_FDSet * read_ready
Set of file descriptors ready for reading; note that additional bits may be set that were not in the ...
enum GNUNET_SCHEDULER_Reason reason
Reason why the task is run now.
const struct GNUNET_NETWORK_FDSet * write_ready
Set of file descriptors ready for writing; note that additional bits may be set that were not in the ...
Entry in list of pending tasks.
Time for absolute times used by GNUnet, in microseconds.
Time for relative time used by GNUnet, in microseconds.
static void try_connect_using_address(void *cls, const struct sockaddr *addr, socklen_t addrlen)
Try to establish a connection given the specified address.
static void signal_receive_timeout(struct GNUNET_CONNECTION_Handle *connection)
Tell the receiver callback that a timeout was reached.
void * GNUNET_CONNECTION_receive_cancel(struct GNUNET_CONNECTION_Handle *connection)
Cancel receive job on the given connection.
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_connect_to_unixpath(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *unixpath)
Create a connection handle by connecting to a UNIX domain service.
static void transmit_timeout(void *cls)
Task invoked by the scheduler when a call to transmit is timing out (we never got enough buffer space...
struct GNUNET_CONNECTION_TransmitHandle * GNUNET_CONNECTION_notify_transmit_ready(struct GNUNET_CONNECTION_Handle *connection, size_t size, struct GNUNET_TIME_Relative timeout, GNUNET_CONNECTION_TransmitReadyNotify notify, void *notify_cls)
Ask the connection to call us once the specified number of bytes are free in the transmission buffer.
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_connect_socket(struct GNUNET_NETWORK_Handle *s, const struct sockaddr *serv_addr, socklen_t addrlen)
Create a connection handle by (asynchronously) connecting to a host.
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_proxied_from_handshake(struct GNUNET_CONNECTION_Handle *cph)
Create a connection to be proxied using a given connection.
int GNUNET_CONNECTION_disable_corking(struct GNUNET_CONNECTION_Handle *connection)
Disable the "CORK" feature for communication with the given connection, forcing the OS to immediately...
static void transmit_ready(void *cls)
We are ready to transmit (or got a timeout).
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_existing(struct GNUNET_NETWORK_Handle *osSocket)
Create a connection handle by boxing an existing OS socket.
static void connect_probe_continuation(void *cls)
Scheduler let us know that we're either ready to write on the socket OR connect timed out.
static void connect_success_continuation(struct GNUNET_CONNECTION_Handle *connection)
We've succeeded in establishing a connection.
static int process_notify(struct GNUNET_CONNECTION_Handle *connection)
Try to call the transmit notify method (check if we do have enough space available first)!
int GNUNET_CONNECTION_get_address(struct GNUNET_CONNECTION_Handle *connection, void **addr, size_t *addrlen)
Obtain the network address of the other party.
int GNUNET_CONNECTION_check(struct GNUNET_CONNECTION_Handle *connection)
Check if connection is valid (no fatal errors have happened so far).
int GNUNET_CONNECTION_receive(struct GNUNET_CONNECTION_Handle *connection, size_t max, struct GNUNET_TIME_Relative timeout, GNUNET_CONNECTION_Receiver receiver, void *receiver_cls)
Receive data from the given connection.
static void receive_ready(void *cls)
This function is called once we either timeout or have data ready to read.
void GNUNET_CONNECTION_destroy(struct GNUNET_CONNECTION_Handle *connection)
Close the connection and free associated resources.
void GNUNET_CONNECTION_acivate_proxied(struct GNUNET_CONNECTION_Handle *proxied)
Activate proxied connection and destroy initial proxy handshake connection.
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_accept(GNUNET_CONNECTION_AccessCheck access_cb, void *access_cb_cls, struct GNUNET_NETWORK_Handle *lsock)
Create a connection handle by accepting on a listen socket.
static void connect_error(void *cls)
Task invoked by the scheduler when we failed to connect at the time of being asked to transmit.
void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *connection)
Set the persist option on this connection handle.
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_sockaddr(int af_family, const struct sockaddr *serv_addr, socklen_t addrlen)
Create a connection handle by creating a socket and (asynchronously) connecting to a host.
#define LOG_STRERROR(kind, syscall)
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const char *hostname, uint16_t port)
Create a connection handle by (asynchronously) connecting to a host.
static void signal_receive_error(struct GNUNET_CONNECTION_Handle *connection, int errcode)
Tell the receiver callback that we had an IO error.
void GNUNET_CONNECTION_notify_transmit_ready_cancel(struct GNUNET_CONNECTION_TransmitHandle *th)
Cancel the specified transmission-ready notification.
#define CONNECT_RETRY_TIMEOUT
Timeout we use on TCP connect before trying another result from the DNS resolver.
static void connect_fail_continuation(struct GNUNET_CONNECTION_Handle *connection)
We've failed for good to establish a connection (timeout or no more addresses to try).
static void signal_transmit_error(struct GNUNET_CONNECTION_Handle *connection, int ecode)
We failed to transmit data to the service, signal the error.