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 encounterd 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",
705 if ((NULL == connection->
ap_head) &&
719 while (NULL != (pos = connection->
ap_head))
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,
1063 while (NULL != (pos = connection->
ap_head))
1074 (ENOTCONN != errno) && (ECONNRESET != errno))
1077 if (NULL != connection->
sock)
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,
1189 connection->
max = 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",
1498 if ((NULL == connection->
sock) && (NULL == connection->
ap_head) &&
1505 return &connection->
nth;
1508 return &connection->
nth;
1509 if (NULL != connection->
sock)
1513 "Scheduling transmission (%p).\n",
1521 return &connection->
nth;
1525 "Need to wait to schedule transmission for connection, adding timeout task (%p).\n",
1529 return &connection->
nth;
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
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.
const struct GNUNET_SCHEDULER_TaskContext * GNUNET_SCHEDULER_get_task_context(void)
Obtain the reasoning why the current task was started.
int GNUNET_NETWORK_get_fd(const struct GNUNET_NETWORK_Handle *desc)
Return file descriptor for this network handle.
struct GNUNET_NETWORK_Handle * sock
Underlying OS's socket, set to NULL after fatal errors.
void GNUNET_CONNECTION_persist_(struct GNUNET_CONNECTION_Handle *connection)
Set the persist option on this connection handle.
const struct sockaddr * addr
The address; do not free (allocated at the end of this struct).
void GNUNET_RESOLVER_request_cancel(struct GNUNET_RESOLVER_RequestHandle *rh)
Cancel a request that is still pending with the resolver.
static const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration we are using.
int8_t destroy_later
Usually 0.
struct AddressProbe * prev
This is a doubly-linked list.
#define RETRY
Return code we give on 'send' if we failed to send right now but it makes sense to retry later...
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
ssize_t GNUNET_NETWORK_socket_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
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...
struct AddressProbe * ap_head
Linked list of sockets we are currently trying out (during connect).
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.
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)! ...
struct GNUNET_CONNECTION_Handle * proxy_handshake
Handle to subsequent connection after proxy handshake completes,.
static void connect_error(void *cls)
Task invoked by the scheduler when we failed to connect at the time of being asked to transmit...
Context information passed to each scheduler task.
void GNUNET_CONNECTION_acivate_proxied(struct GNUNET_CONNECTION_Handle *proxied)
Activate proxied connection and destroy initial proxy handshake connection.
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).
struct GNUNET_TIME_Absolute GNUNET_TIME_relative_to_absolute(struct GNUNET_TIME_Relative rel)
Convert relative time to an absolute time in the future.
#define GNUNET_assert(cond)
Use this for fatal errors that cannot be handled.
static struct GNUNET_SCHEDULER_TaskContext tc
Task context of the current task.
uid_t uid
UID of the other end of the connection.
int GNUNET_NETWORK_socket_getsockopt(const struct GNUNET_NETWORK_Handle *desc, int level, int optname, void *optval, socklen_t *optlen)
Get socket options.
int 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...
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 ...
static struct GNUNET_FS_UnindexContext * uc
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...
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
static int ret
Return value of the commandline.
static void signal_receive_error(struct GNUNET_CONNECTION_Handle *connection, int errcode)
Tell the receiver callback that we had an IO error.
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.
void * GNUNET_CONNECTION_receive_cancel(struct GNUNET_CONNECTION_Handle *connection)
Cancel receive job on the given connection.
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 ...
#define GNUNET_new(type)
Allocate a struct or union of the given type.
struct AddressProbe * ap_tail
Linked list of sockets we are currently trying out (during connect).
void GNUNET_CONNECTION_notify_transmit_ready_cancel(struct GNUNET_CONNECTION_TransmitHandle *th)
Cancel the specified transmission-ready notification.
struct GNUNET_SCHEDULER_Task * task
Task waiting for the connection to finish connecting.
struct GNUNET_CONNECTION_Handle * GNUNET_CONNECTION_create_from_existing(struct GNUNET_NETWORK_Handle *osSocket)
Create a connection handle by boxing an existing OS socket.
int GNUNET_NETWORK_socket_shutdown(struct GNUNET_NETWORK_Handle *desc, int how)
Shut down socket operations.
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.
#define GNUNET_strdup(a)
Wrapper around GNUNET_xstrdup_.
uint16_t port
Port to connect to.
enum GNUNET_SCHEDULER_Reason reason
Reason why the task is run now.
size_t write_buffer_size
Current size of our write_buffer.
#define GNUNET_break(cond)
Use this for internal assertion violations that are not fatal (can be handled) but should not occur...
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.
void * notify_ready_cls
Closure for notify_ready.
gid_t gid
GID of the other end of the connection.
void GNUNET_NETWORK_socket_free_memory_only_(struct GNUNET_NETWORK_Handle *desc)
Only free memory of a socket, keep the file descriptor untouched.
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).
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.
char * hostname
Pointer to the hostname if connection was created using DNS lookup, otherwise NULL.
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...
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.
int 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.
static struct GNUNET_TIME_Relative timeout
Desired timeout for the lookup (default is no timeout).
struct GNUNET_SCHEDULER_Task * read_task
Read task that we may need to wait for.
struct sockaddr * addr
Network address of the other end-point, may be NULL.
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.
#define GNUNET_realloc(ptr, size)
Wrapper around realloc.
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.
static struct GNUNET_PEERINFO_NotifyContext * notify
Handle to the peerinfo notify service (NULL until we've connected to it).
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
#define GNUNET_MIN_MESSAGE_SIZE
Smallest supported message.
struct GNUNET_RESOLVER_RequestHandle * dns_active
Handle to a pending DNS lookup request.
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.
int GNUNET_CONNECTION_check(struct GNUNET_CONNECTION_Handle *connection)
Check if connection is valid (no fatal errors have happened so far).
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
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...
const struct GNUNET_CONFIGURATION_Handle * cfg
Configuration to use.
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)...
socklen_t addrlen
Lenth of addr.
struct GNUNET_TIME_Absolute receive_timeout
Timeout for receiving (in absolute time).
size_t write_buffer_off
Current write-offset in write_buffer (where would we write next).
struct GNUNET_NETWORK_Handle * sock
Underlying OS's socket.
int8_t persist
When shutdown, do not ever actually close the socket, but free resources.
static void transmit_ready(void *cls)
We are ready to transmit (or got a timeout).
size_t write_buffer_pos
Current read-offset in write_buffer (how many bytes have already been sent).
The writing socket is ready.
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.
struct GNUNET_CONNECTION_Handle * connection
Our connection handle.
static unsigned int size
Size of the "table".
Handle to a request given to the resolver.
The specified timeout has expired.
int GNUNET_CONNECTION_get_address(struct GNUNET_CONNECTION_Handle *connection, void **addr, size_t *addrlen)
Obtain the network address of the other party.
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.
struct GNUNET_CONNECTION_TransmitHandle nth
The handle we return for GNUNET_CONNECTION_notify_transmit_ready().
void GNUNET_CONNECTION_destroy(struct GNUNET_CONNECTION_Handle *connection)
Close the connection and free associated resources.
GNUNET_CONNECTION_Receiver receiver
Function to call on data received, NULL if no receive is pending.
static void connect_success_continuation(struct GNUNET_CONNECTION_Handle *connection)
We've succeeded in establishing a connection.
#define GNUNET_MAX_MESSAGE_SIZE
Largest supported message (to be precise, one byte more than the largest possible message...
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.
struct AddressProbe * next
This is a linked list.
static char * hostname
Our hostname; we give this to all the peers we start.
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 uint16_t port
Port number.
static void signal_receive_timeout(struct GNUNET_CONNECTION_Handle *connection)
Tell the receiver callback that a timeout was reached.
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_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...
size_t max
Maximum number of bytes to read (for receiving).
size_t notify_size
At what number of bytes available in the write buffer should the notify method be called...
Entry in list of pending tasks.
Credentials for UNIX domain sockets.
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".
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.
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_SCHEDULER_Task * write_task
Write task that we may need to wait for.
Time for absolute times used by GNUnet, in microseconds.
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...
struct GNUNET_TIME_Absolute transmit_timeout
Timeout for receiving (in absolute time).
socklen_t addrlen
Length of addr.
char * write_buffer
Pointer to our write buffer.
GNUNET_CONNECTION_TransmitReadyNotify notify_ready
Function to call if the send buffer has notify_size bytes available.
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.
handle for a network connection
void * receiver_cls
Closure for receiver.
#define LOG_STRERROR(kind, syscall)
size_t GNUNET_strlcpy(char *dst, const char *src, size_t n)
Like strlcpy but portable.
#define CONNECT_RETRY_TIMEOUT
Timeout we use on TCP connect before trying another result from the DNS resolver. ...
int GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
#define GNUNET_malloc(size)
Wrapper around malloc.
During connect, we try multiple possible IP addresses to find out which one might work...
static void receive_ready(void *cls)
This function is called once we either timeout or have data ready to read.
static void signal_transmit_error(struct GNUNET_CONNECTION_Handle *connection, int ecode)
We failed to transmit data to the service, signal the error.
#define GNUNET_free(ptr)
Wrapper around free.
Time for relative time used by GNUnet, in microseconds.
uint16_t len
length of data (which is always a uint32_t, but presumably this can be used to specify that fewer byt...
struct GNUNET_CONNECTION_Handle * connection
Connection for which we are probing.
struct GNUNET_NETWORK_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
struct GNUNET_SCHEDULER_Task * timeout_task
Task called on timeout.
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.