67{
69
72
73 return burst_sync;
74}
75
76
87void
92{
96
101 "other sync ready %u, other rtt %lu and rtt %lu rel1 %lu rel2 %lu\n",
103 (unsigned long) other_rtt.rel_value_us,
105 (unsigned long) rel1.rel_value_us,
106 (unsigned long) rel2.rel_value_us);
109 rel1.rel_value_us <
RTT_DIFF.rel_value_us &&
110 rel2.rel_value_us <
RTT_DIFF.rel_value_us)
111 {
113 "other sync ready 1\n");
115 {
117 "other sync ready 2\n");
119 2);
120 }
121 else
122 {
124 "other sync ready 3\n");
126 4);
127 }
129 task (task_cls);
130 }
131 else
132 {
134 "other sync ready 6\n");
136 }
137}
138
139
140void
142
143
149static void
151{
153 struct sockaddr_storage sa;
154 socklen_t salen = sizeof(sa);
155 char buf[UINT16_MAX];
156 ssize_t rcvd;
157
159
161 "Reading from socket\n");
162 while (1)
163 {
165 buf,
166 sizeof(buf),
167 (struct sockaddr *) &sa,
168 &salen);
169 if (-1 == rcvd)
170 {
171 struct sockaddr *addr = (struct sockaddr*) &sa;
172
173 if (EAGAIN == errno)
174 break;
176 "Failed to recv from %s family %d failed sock %p\n",
178 sizeof (*addr)),
179 addr->sa_family,
182 return;
183 }
185 "Read %llu bytes\n",
186 (unsigned long long) rcvd);
187 if (0 == rcvd)
188 {
191 "Read 0 bytes from UDP socket\n");
192 return;
193 }
194
196 {
198
200 "Received a burst message from remote port %u to local port %u!\n",
204 sock_info->
nus (sock_info);
206 return;
207 }
208 else
210 "Received a non burst message on local port %u %lu!\n",
213 }
214}
215
216
224static struct sockaddr *
226{
227 struct sockaddr *in;
230 char *colon;
231 char *cp;
232
234 colon = strrchr (cp, ':');
235 if (NULL != colon)
236 {
237
238 *colon = '\0';
239 colon++;
240 if (1 == sscanf (colon,
"%u%1s", &
port,
dummy))
241 {
242
243 if (
port > UINT16_MAX)
244 {
246 "BINDTO specification `%s' invalid: value too large for port\n",
247 bindto);
249 return NULL;
250 }
251 }
252 else
253 {
256 "BINDTO specification `%s' invalid: last ':' not followed by number\n",
257 bindto);
259 return NULL;
260 }
261 }
262 else
263 {
264
266 }
267 {
268
269 struct sockaddr_in v4;
270
271 memset (&v4, 0, sizeof(v4));
272 if (1 == inet_pton (AF_INET, cp, &v4.sin_addr))
273 {
274 v4.sin_family = AF_INET;
275 v4.sin_port = htons ((uint16_t)
port);
276#if HAVE_SOCKADDR_IN_SIN_LEN
277 v4.sin_len = sizeof(struct sockaddr_in);
278#endif
280 *sock_len = sizeof(struct sockaddr_in);
282 return in;
283 }
284 }
285 {
286
287 struct sockaddr_in6 v6;
289
290 memset (&v6, 0, sizeof(v6));
292 if (('[' == *cp) && (']' == cp[strlen (cp) - 1]))
293 {
295 cp[strlen (cp) - 1] = '\0';
296 }
297 if (1 == inet_pton (AF_INET6,
start, &v6.sin6_addr))
298 {
299 v6.sin6_family = AF_INET6;
300 v6.sin6_port = htons ((uint16_t)
port);
301#if HAVE_SOCKADDR_IN_SIN_LEN
302 v6.sin6_len = sizeof(struct sockaddr_in6);
303#endif
305 *sock_len = sizeof(v6);
307 return in;
308 }
309 }
310
312 return NULL;
313}
314
315
316static void
318{
320
322 "timeout task\n");
328 "timeout nr_open_sockets %u\n",
333 sock_info);
335 "freeing sock_info %p\n",
336 sock_info);
338}
339
340
341static void
343{
348 struct sockaddr *in;
349 socklen_t in_len;
352 struct sockaddr *bind_in;
353 socklen_t bind_in_len;
354 char *bind_address;
356 rtt,
357 4);
358
364 {
366 "Trying again in %s",
370 sock_info);
371 }
372
374 "%s:%u",
378 "3 sock %p addr %s addr %s %u\n",
379 sock_info,
384 if (NULL == in)
385 {
387 "Failed to setup UDP socket address with path `%s'\n",
390 }
394 "%s:%u",
398 "4 sock addr %s addr %s\n",
400 bind_address);
402 if (NULL == bind_in)
403 {
405 "Failed to setup UDP socket bind address with path `%s'\n",
406 bind_address);
408 }
410 bind_address = NULL;
413 SOCK_DGRAM,
414 IPPROTO_UDP);
416 {
419 "Failed to create socket for %s family %d\n",
421 bind_in_len),
422 in->sa_family);
424 if (EMFILE == errno)
425 {
427 "Trying again in %s, because of EMFILE\n",
431 sock_info);
435 return;
436 }
438 goto next_port;
439 }
442 bind_in,
443 bind_in_len))
444 {
447 "Failed to bind socket for %s family %d sock %p\n",
449 bind_in_len),
450 bind_in->sa_family,
455 goto next_port;
456 }
465 sock_info);
467 "Timeout in %s\n",
469 ;
472 sock_info);
473
475
476 memcpy (dgram, &bm, sizeof(bm));
478 dgram,
479 sizeof(dgram),
480 in,
481 in_len))
482 {
484 "Sending burst to %s family %d failed sock %p\n",
486 in_len),
487 in->sa_family,
490 }
491
492next_port:
496
498 return;
500
503 si);
504}
505
506
510{
515 struct sockaddr *in;
516 socklen_t in_len;
517
519 "%s:%u",
523 "2 sock addr %s addr %s rtt %s %u\n",
527 false),
531 memcpy (dgram, &bm, sizeof(bm));
533 dgram,
534 sizeof(dgram),
535 in,
536 in_len))
537 {
539 "Sending burst to %s family %d failed sock %p\n",
541 in_len),
542 in->sa_family,
544 }
545
549 sock_info->
nus = nus;
550
552
555 si);
559}
560
561
562void
564{
567
569 "stopping burst\n");
571 {
574 }
576 while (NULL != pos)
577 {
578 sock_info = pos;
579 pos = sock_info->
next;
582 sock_info);
587 if (do_not_touch != sock_info->
udp_sock)
588 {
590 if (NULL != sock_info->
address)
593 "freeing sock_info %p\n",
594 sock_info);
596 }
597 }
598}
static int start
Set if we are to start default services (including ARM).
static uint16_t port
Port number.
static struct GNUNET_NETWORK_Handle * udp_sock
Our socket.
static char * address
GNS address for this phone.
static struct in_addr dummy
Target "dummy" address of the packet we pretend to respond to.
#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_log(kind,...)
#define GNUNET_memcpy(dst, src, n)
Call memcpy() but check for n being 0 first.
#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.
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...
@ 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_free(ptr)
Wrapper around free.
#define GNUNET_memdup(buf, size)
Allocate and initialize a block of memory.
void GNUNET_is_burst_ready(struct GNUNET_TIME_Relative rtt_average, struct GNUNET_BurstSync *burst_sync, GNUNET_SCHEDULER_TaskCallback task, struct GNUNET_StartBurstCls *task_cls)
Checks if we are ready and starts burst when we and the other peer is ready.
struct GNUNET_SCHEDULER_Task * GNUNET_get_udp_socket(struct GNUNET_UdpSocketInfo *sock_info, GNUNET_NotifyUdpSocket nus)
Method to get a UDP socket for a peer that is natted.
void GNUNET_stop_burst(struct GNUNET_NETWORK_Handle *do_not_touch)
Method to stop all sockets we established to the other peer.
void(* GNUNET_NotifyUdpSocket)(struct GNUNET_UdpSocketInfo *sock_info)
enum GNUNET_GenericReturnValue GNUNET_NETWORK_socket_close(struct GNUNET_NETWORK_Handle *desc)
Close a socket.
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_Handle * GNUNET_NETWORK_socket_create(int domain, int type, int protocol)
Create a new socket.
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.
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).
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...
void * GNUNET_SCHEDULER_cancel(struct GNUNET_SCHEDULER_Task *task)
Cancel the task with the specified identifier.
void(* GNUNET_SCHEDULER_TaskCallback)(void *cls)
Signature of the main function of a task.
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.
const char * GNUNET_TIME_relative2s(struct GNUNET_TIME_Relative delta, bool do_round)
Give relative time in human-readable fancy format.
#define GNUNET_TIME_UNIT_FOREVER_REL
Constant used to specify "forever".
struct GNUNET_TIME_Relative GNUNET_TIME_relative_ntoh(struct GNUNET_TIME_RelativeNBO a)
Convert relative time from network byte order.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_saturating_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Saturating multiply relative time by a given factor.
const char * GNUNET_STRINGS_relative_time_to_string(struct GNUNET_TIME_Relative delta, int do_round)
Give relative time in human-readable fancy format.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_subtract(struct GNUNET_TIME_Relative a1, struct GNUNET_TIME_Relative a2)
Subtract relative timestamp from the other.
struct GNUNET_TIME_Relative GNUNET_TIME_relative_multiply(struct GNUNET_TIME_Relative rel, unsigned long long factor)
Multiply relative time by a given factor.
struct GNUNET_TIME_RelativeNBO GNUNET_TIME_relative_hton(struct GNUNET_TIME_Relative a)
Convert relative time to network byte order.
static void timeout_task_cb(void *cls)
#define RTT_DIFF
Difference of the average RTT for the DistanceVector calculate by us and the target we are willing to...
static void read_send(void *cls)
static void sock_read(void *cls)
Socket read task.
static struct GNUNET_SCHEDULER_Task * read_send_task
static struct sockaddr * udp_address_to_sockaddr(const char *bindto, socklen_t *sock_len)
Convert UDP bind specification to a struct sockaddr *
static struct GNUNET_UdpSocketInfo * sock_infos_tail
static struct GNUNET_UdpSocketInfo * sock_infos_head
unsigned int nr_open_sockets
Maximum of open sockets.
Message send during burst mode.
unsigned int local_port
The local port the message was send from.
Wrapper struct with the average RTT of message to some peer and if this peer und us is ready to sync.
enum GNUNET_GenericReturnValue sync_ready
Is this peer already ready to sync.
struct GNUNET_TIME_RelativeNBO rtt_average
The average RTT for the peer to communicate with.
Entry in list of pending tasks.
Struct wrapping information we use for starting the burst.
struct GNUNET_TIME_Relative delay
The delay - calculate from the RTT and which peer was ready to sync first, after we will start the bu...
unsigned int sync_ready
We are ready to start the burst.
Time for relative time used by GNUnet, in microseconds.
uint64_t rel_value_us
The actual value.
Struct with the socket we like to use to send messages to another peer.
const char * bind_address
Our address without port.
enum GNUNET_GenericReturnValue has_port
Flag indicating, if the address is without port information.
struct GNUNET_TIME_Relative rtt
The actual RTT between the peers.
GNUNET_NotifyUdpSocket nus
The notify function to call if burst mode was successful.
char * address
The address of the other peer without port.
unsigned int port
The port we are bound to.
struct GNUNET_UdpSocketInfo * next
This is a linked list.
unsigned int std_port
Default local port we are bound to.
struct sockaddr * actual_address
The address of the other peer we received a burst message from.
struct GNUNET_SCHEDULER_Task * timeout_task
Timeout task for this socket.
struct GNUNET_NETWORK_Handle * udp_sock
struct GNUNET_SCHEDULER_Task * read_task
The read task for retrieving a burst message for this socket.