55#define LOG(kind, ...) GNUNET_log_from (kind, "core-underlay-dummy", __VA_ARGS__ \
58#define SOCK_NAME_BASE "/tmp/gnunet-core-underlay-dummy-socket"
59#define SOCK_EXTENSION ".sock"
321 return "C_INITIALIZING";
324 return "C_INITIALIZING_SEND";
327 return "C_INITIALIZING_RECV";
396 if (NULL != connection->
sock)
433 for (
unsigned int i = 0; NULL !=
handlers[i].cb; i++)
474 "no messages in the queue - receive more from socket\n");
507 h->notify_connect (
h->cls,
519 connection->
cls_mq = cls_mq;
552 "Going to cancel all initializing connections to %s\n",
556 for (
struct Connection *c_iter =
h->connections_head;
558 c_iter = c_iter_next)
560 c_iter_next = c_iter->
next;
565 if ((c_iter != connection) &&
566 (NULL != c_iter->peer_addr) &&
567 (0 == strcmp (c_iter->peer_addr,
peer_addr)) &&
574 " -> Cancelling connection - MQ_destroy()\n");
597 "do_read(): receiving bytes from %s :%s\n",
615 "Other peer closed connection - MQ_destroy()\n");
633 while (0 < ret_remain)
645 struct GNUNET_UNDERLAY_DUMMY_Message
655 struct GNUNET_UNDERLAY_DUMMY_Message *msg_dbg =
656 (
struct GNUNET_UNDERLAY_DUMMY_Message *) msg_iter;
659 ", batch: %" PRIu64
"\n",
669 buf_iter = buf_iter + ntohs (msg_iter->
size);
670 ret_remain = ret_remain - ntohs (msg_iter->
size);
676 buf_iter = buf + ntohs (
msg->
size);
679 while (0 < ret_remain)
691 buf_iter = buf_iter + ntohs (msg_iter->
size);
692 ret_remain = ret_remain - ntohs (msg_iter->
size);
697 if ((0 == ntohs (
msg->
type)) &&
700 char *peer_addr_sent = (
char *) &
msg[1];
704 "Init-message: %s\n",
707 (0 != strcmp (peer_addr_sent, connection->
peer_addr)))
710 "Peer sent us an Init-message with an address different from the one we already know. -> MQ_destroy()\n");
712 "sent address: %s\n",
721 switch (connection->
status)
735 "Connection to %s is now %s\n",
743 c_iter = c_iter->next)
749 if ((NULL != c_iter->peer_addr) &&
750 (0 == strcmp (peer_addr_sent, c_iter->peer_addr)) &&
752 (connection != c_iter))
757 "Init from other peer - already connected: %s - MQ_destroy()\n",
764 connection->
peer_addr = strdup (peer_addr_sent);
766 "Init-Message - notifying caller (%s :%s)\n",
772 "Finished initializing - going to cancel all still initializing connections to this address\n");
786 else if ((0 == ntohs (
msg->
type)) &&
844 "write_cb(): sending bytes to %s :%s\n",
851 struct GNUNET_UNDERLAY_DUMMY_Message
861 struct GNUNET_UNDERLAY_DUMMY_Message *msg_dbg =
905 switch (connection->
status)
911 "Finished initializing - going to cancel all still initializing connections to this address\n");
923 "Connection to %s is now %s\n",
935 uint32_t peer_addr_len;
939 "Sending init: %s\n",
970 struct GNUNET_UNDERLAY_DUMMY_Message
979 struct GNUNET_UNDERLAY_DUMMY_Message *msg_dbg =
980 (
struct GNUNET_UNDERLAY_DUMMY_Message *)
msg;
1043 "mq_error_handler_impl: %u\n",
1060 struct sockaddr_un addr_other;
1061 socklen_t addr_other_len =
sizeof(addr_other);
1062 memset (&addr_other, 0,
sizeof (addr_other));
1075 (
struct sockaddr *) &addr_other,
1086 "Failed setting socket of incoming connection to non-blocking\n");
1090 connection->
sock = sock;
1099 h->connections_tail,
1138 struct sockaddr_un addr_other;
1139 memset (&addr_other, 0,
sizeof (addr_other));
1148 if (NULL == connection->
sock)
1162 addr_other.sun_family = AF_UNIX;
1167 (
struct sockaddr *) &
1169 sizeof(addr_other)))
1172 "failed to connect to the socket: %u %s (closing socket)\n",
1173 errno, strerror (errno));
1195 h->connections_tail,
1219 const char *addresses[1] = {
h->sock_name};
1222 h->notify_address_change_task = NULL;
1223 h->notify_address_change (
h->cls, 1, addresses);
1232 if (0 == strcmp (
address,
h->sock_name))
1238 "Discovered another peer with address `%s'\n",
1247 for (
struct Connection *conn_iter =
h->connections_head;
1249 conn_iter = conn_iter->next)
1253 conn_iter->peer_addr,
1255 if ((NULL != conn_iter->peer_addr) &&
1256 (0 == strcmp (
address, conn_iter->peer_addr)) &&
1260 "Already connected to this peer - don't try to open another connection\n");
1266 pcc_iter = pcc_iter->next)
1269 pcc_iter->sock_name))
1280 peer_connect_cls->
h =
h;
1286 h->peer_connect_cls_tail,
1352 struct sockaddr_un *addr_un;
1353 socklen_t addr_un_len;
1361 addr_un->sun_family = AF_UNIX;
1363 GNUNET_memcpy (&addr_un->sun_path, sock_name, strlen (sock_name));
1364 addr_un_len =
sizeof (
struct sockaddr_un);
1367 (
struct sockaddr *) addr_un,
1373 "Failed binding to socket: %u %s (closing socket)\n",
1374 errno, strerror (errno));
1376 h->sock_listen = NULL;
1383 " -> something went wrong (address is probably in use)\n");
1385 h->sock_listen = NULL;
1391 " -> succeeded! (binding to socket)\n");
1411 uint64_t sock_name_ctr_start)
1415 uint64_t sock_name_ctr = sock_name_ctr_start;
1423 SOCK_NAME_BASE
"%" PRIu64
"" SOCK_EXTENSION
"\0",
1431 h->sock_name_index_start = sock_name_ctr_start;
1432 h->sock_name_index = sock_name_ctr;
1439 "Failed listening to socket (closing socket)\n");
1441 h->sock_listen = NULL;
1446 if (NULL !=
h->notify_address_change)
1450 h->notify_address_change_task =
1476 if (NULL !=
h->notify_address_change_task)
1480 h->notify_address_change_task = NULL;
1482 if (NULL !=
h->peer_discovery_task)
1486 h->peer_discovery_task = NULL;
1492 pcc_next = pcc->
next;
1496 h->peer_connect_cls_tail,
1501 if (NULL !=
h->listen_task)
1505 h->listen_task = NULL;
1507 if (NULL !=
h->sock_listen)
1511 h->sock_listen = NULL;
1513 for (
struct Connection *conn_iter =
h->connections_head;
1515 conn_iter = conn_next)
1520 conn_next = conn_iter->
next;
1555 h->notify_connect =
nc;
1556 h->notify_disconnect = nd;
1557 h->notify_address_change = na;
1586 if (NULL !=
handle->address_change_task)
1589 if (NULL !=
handle->handlers)
1618GNUNET_CORE_UNDERLAY_DUMMY_receive_continue (
1627 for (
struct Connection *conn_iter =
h->connections_head;
1629 conn_iter = conn_iter->next)
1631 if (
mq == conn_iter->mq)
1633 connection = conn_iter;
1657GNUNET_CORE_UNDERLAY_DUMMY_connect_to_peer (
1659 const char *peer_address,
1673GNUNET_CORE_UNDERLAY_DUMMY_change_address (
1678 if (0 ==
h->sock_name_index_start)
struct GNUNET_MQ_MessageHandlers handlers[]
struct GNUNET_MessageHeader * msg
struct GNUNET_MQ_Envelope * env
static size_t strnlen(const char *s, size_t n)
static struct GNUNET_ARM_Handle * h
Connection with ARM.
static int ret
Final status code.
static struct GNUNET_CONFIGURATION_Handle * cfg
Our configuration.
static char * address
GNS address for this phone.
static struct GNUNET_IDENTITY_Handle * id
Handle to IDENTITY.
static int status
The program status; 0 for success.
static struct GNUNET_NotificationContext * nc
Notification context for broadcasting to monitors.
static struct GNUNET_VPN_Handle * handle
Handle to vpn service.
API of the dummy core underlay that uses unix domain sockets.
API to schedule computations using continuation passing style.
struct GNUNET_MQ_MessageHandler * handlers
Array of message handlers given by the client.
const struct GNUNET_MessageHeader * message_to_send
Message about to be sent, given by the message queue, waiting for the socket to be ready.
static void do_close_listening_socket(void *cls)
struct GNUNET_NETWORK_Handle * sock
Socket for the connected peer.
void GNUNET_CORE_UNDERLAY_DUMMY_disconnect(struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *handle)
Disconnect from the core underlay dummy service.
struct QueuedMessage * prev
struct QueuedMessage * queued_recv_messages_tail
GNUNET_CORE_UNDERLAY_DUMMY_NotifyAddressChange notify_address_change
Callback (from/to client) to call when our address changes.
void * cls
Closure for handlers given by the client - connection-independant (notify_connect,...
GNUNET_CORE_UNDERLAY_DUMMY_NotifyConnect notify_connect
Callback (from/to client) to call when another peer connects.
struct GNUNET_SCHEDULER_Task * listen_task
Task that waits for incoming connections.
char * sock_name
Name of the listening socket.
struct Connection * prev
Linked list previous.
struct GNUNET_SCHEDULER_Task * write_task
Task waiting until the socket becomes ready to be written to.
static void do_read(void *cls)
Callback scheduled to run when there is something to read from the socket.
struct Connection * connections_head
Head of linked list with peer connect closures.
struct PeerConnectCls * peer_connect_cls_tail
Tail of linked list with peer connect closures.
struct PeerConnectCls * next
Linked list next.
struct GNUNET_CORE_UNDERLAY_DUMMY_Handle * GNUNET_CORE_UNDERLAY_DUMMY_connect(const struct GNUNET_CONFIGURATION_Handle *cfg, const struct GNUNET_MQ_MessageHandler *handlers, void *cls, GNUNET_CORE_UNDERLAY_DUMMY_NotifyConnect nc, GNUNET_CORE_UNDERLAY_DUMMY_NotifyDisconnect nd, GNUNET_CORE_UNDERLAY_DUMMY_NotifyAddressChange na)
Connect to the core underlay dummy service.
static void connection_destroy(struct Connection *connection)
Destroy a connection.
static void do_notify_address_change(void *cls)
Notify core about address change.
struct Connection * connections_tail
Tail of linked list with peer connect closures.
char * peer_addr
Address of the connected peer.
static void do_discover_peers(void *cls)
Discover sockets of other peers.
struct GNUNET_CORE_UNDERLAY_DUMMY_Handle * h
The handle for the service.
static void cancel_initiating_connections(struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h, struct Connection *connection)
void * cls_mq
Closure for the mq towards the client.
struct GNUNET_SCHEDULER_Task * address_change_task
struct GNUNET_SCHEDULER_Task * peer_discovery_task
Task to discover other peers.
static void write_cb(void *cls)
Callback scheduled to run once the socket is ready for writing.
struct GNUNET_SCHEDULER_Task * peer_connect_task
Task to connect to another peer.
struct QueuedMessage * queued_recv_messages_head
Queued received messages in a DLL TODO implement cleanup TODO replace with a performant queue.
struct PeerConnectCls * prev
Linked list previous.
static enum GNUNET_GenericReturnValue try_open_listening_socket(struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h, char *sock_name)
static void do_connect_to_peer(void *cls)
Connect to another peer.
struct GNUNET_SCHEDULER_Task * notify_address_change_task
Task to notify core about address changes.
static void check_for_messages(struct Connection *connection)
struct GNUNET_SCHEDULER_Task * handle_message_task
Task to pass the message to the client's handlers.
void(* GNUNET_CORE_UNDERLAY_DUMMY_NotifyAddressChange)(void *cls, uint32_t num_addresses, const char *addresses[static num_addresses])
Function called to notify core of the now available addresses.
static void mq_destroy_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
Callback to destroy the message queue.
struct Connection * next
Linked list next.
static const char * status2string(enum ConnectionStatus status)
struct GNUNET_NETWORK_Handle * sock_listen
Socket on which we listen for incoming connections.
static void try_notify_connect(struct Connection *connection)
enum GNUNET_GenericReturnValue notify_connect_called
Whether notify_connect was called already It is used to check whether to call notify_disconnect or no...
static void do_notify_connect(void *cls)
Notify the api caller about a new connection.
struct GNUNET_MessageHeader * msg
struct GNUNET_MQ_Handle * mq
Message queue towards the connected peer.
static void do_handle_message(void *cls)
static void mq_error_handler_impl(void *cls, enum GNUNET_MQ_Error error)
Handle mq errors.
static void do_open_listening_socket(void *cls, uint64_t sock_name_ctr_start)
Opens UNIX domain socket.
struct GNUNET_MQ_MessageHandler * handlers
Handlers for mq.
struct GNUNET_CORE_UNDERLAY_DUMMY_Handle * handle
Handle to the service.
enum ConnectionStatus status
Status of the connection: Initializing, Live, Destroy.
static void do_accept(void *cls)
Accept a connection on the dummy's socket.
static void mq_cancel_impl(struct GNUNET_MQ_Handle *mq, void *impl_state)
Callback to cancel sending a message.
GNUNET_CORE_UNDERLAY_DUMMY_NotifyDisconnect notify_disconnect
Callback (from/to client) to call when a peer disconnects.
uint64_t sock_name_index_start
struct PeerConnectCls * peer_connect_cls_head
Head of linked list with peer connect closures.
struct QueuedMessage * next
static void set_handlers_closure(struct GNUNET_MQ_MessageHandler *handlers, void *handlers_cls)
Set the closures for mq handlers.
enum GNUNET_GenericReturnValue client_ready_to_receive
Indicating whether the client is ready to receive new messages.
static enum GNUNET_GenericReturnValue discovered_socket_cb(void *cls, const char *filename)
Handle the discovery of a certain socket.
void *(* GNUNET_CORE_UNDERLAY_DUMMY_NotifyConnect)(void *cls, uint32_t num_addresses, const char *addresses[static num_addresses], struct GNUNET_MQ_Handle *mq, const struct GNUNET_PeerIdentity *peer_id)
Function called to notify core underlay dummy users that another peer connected to us.
void(* GNUNET_CORE_UNDERLAY_DUMMY_NotifyDisconnect)(void *cls, void *handler_cls)
Function called to notify core underlay dummy users that another peer disconnected from us.
static void send_init(struct Connection *connection)
struct GNUNET_SCHEDULER_Task * recv_task
Task waiting for incoming messages.
struct GNUNET_SCHEDULER_Task * notify_connect_task
Task to notify the client about an open connection.
char * sock_name
The file name to connect to.
static void try_connect(struct GNUNET_CORE_UNDERLAY_DUMMY_Handle *h, const char *address)
static void mq_send_impl(struct GNUNET_MQ_Handle *mq, const struct GNUNET_MessageHeader *msg, void *impl_state)
Callback called from the MQ to send a message over a socket.
@ CONNECTION_INITIALIZING
@ CONNECTION_INITIALIZING_RECV
@ CONNECTION_INITIALIZING_SEND
int GNUNET_DISK_glob(const char *glob_pattern, GNUNET_FileNameCallback callback, void *callback_cls)
Find all files matching a glob pattern.
#define GNUNET_CONTAINER_DLL_remove(head, tail, element)
Remove an element from a DLL.
#define GNUNET_CONTAINER_DLL_insert_tail(head, tail, element)
Insert an element at the tail of a DLL.
#define GNUNET_CONTAINER_DLL_insert(head, tail, element)
Insert an element at the head of a DLL.
uint16_t type
The type of the message (GNUNET_MESSAGE_TYPE_XXXX), in big-endian format.
uint64_t GNUNET_ntohll(uint64_t n)
Convert unsigned 64-bit integer to host byte order.
#define GNUNET_ALIGN
gcc-ism to force alignment; we use this to align char-arrays that may then be cast to 'struct's.
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
GNUNET_GenericReturnValue
Named constants for return values.
uint16_t size
The length of the struct (in bytes, including the length field itself), in big-endian format.
#define GNUNET_break_op(cond)
Use this for assertion violations caused by other peers (i.e.
#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.
@ GNUNET_ERROR_TYPE_WARNING
@ GNUNET_ERROR_TYPE_ERROR
@ GNUNET_ERROR_TYPE_DEBUG
int int GNUNET_asprintf(char **buf, const char *format,...) __attribute__((format(printf
Like asprintf, just portable.
#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_free(ptr)
Wrapper around free.
GNUNET_MQ_Error
Error codes for the queue.
struct GNUNET_MQ_Handle * GNUNET_MQ_queue_for_callbacks(GNUNET_MQ_SendImpl send, GNUNET_MQ_DestroyImpl destroy, GNUNET_MQ_CancelImpl cancel, void *impl_state, const struct GNUNET_MQ_MessageHandler *handlers, GNUNET_MQ_ErrorHandler error_handler, void *cls)
Create a message queue for the specified handlers.
void GNUNET_MQ_send(struct GNUNET_MQ_Handle *mq, struct GNUNET_MQ_Envelope *ev)
Send a message with the given message queue.
struct GNUNET_MQ_MessageHandler * GNUNET_MQ_copy_handlers(const struct GNUNET_MQ_MessageHandler *handlers)
Copy an array of handlers.
void GNUNET_MQ_impl_send_continue(struct GNUNET_MQ_Handle *mq)
Call the send implementation for the next queued message, if any.
GNUNET_MQ_PriorityPreferences
Per envelope preferences and priorities.
enum GNUNET_GenericReturnValue GNUNET_MQ_handle_message(const struct GNUNET_MQ_MessageHandler *handlers, const struct GNUNET_MessageHeader *mh)
Call the message message handler that was registered for the type of the given message in the given h...
#define GNUNET_MQ_msg_header_extra(mh, esize, type)
Allocate a GNUNET_MQ_Envelope, where the message only consists of a header and extra space.
void GNUNET_MQ_destroy(struct GNUNET_MQ_Handle *mq)
Destroy the message queue.
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_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
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_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_create(int domain, int type, int protocol)
Create a new socket.
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_send(const struct GNUNET_NETWORK_Handle *desc, const void *buffer, size_t length)
Send data (always non-blocking).
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.
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_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...
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
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_delayed(struct GNUNET_TIME_Relative delay, GNUNET_SCHEDULER_TaskCallback task, void *task_cls)
Schedule a new task to be run with a specified delay.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
#define GNUNET_TIME_UNIT_MILLISECONDS
One millisecond.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
static struct GNUNET_MQ_Handle * mq
Our connection to the resolver service, created on-demand, but then persists until error or shutdown.
Used to keep track of context of peer.
32-bit bandwidth used for network exchange by GNUnet, in bytes per second.
Opaque handle to the service.
Handle to a message queue.
Message handler for a specific message type.
Entry in list of pending tasks.
Closure used for the peer_connect_task.