summaryrefslogtreecommitdiff
path: root/ot_livesync.c
diff options
context:
space:
mode:
Diffstat (limited to 'ot_livesync.c')
-rw-r--r--ot_livesync.c21
1 files changed, 13 insertions, 8 deletions
diff --git a/ot_livesync.c b/ot_livesync.c
index f620592..b876ef8 100644
--- a/ot_livesync.c
+++ b/ot_livesync.c
@@ -4,15 +4,14 @@
4 $id$ */ 4 $id$ */
5 5
6/* System */ 6/* System */
7#include <arpa/inet.h>
7#include <pthread.h> 8#include <pthread.h>
8#include <stdlib.h> 9#include <stdlib.h>
9#include <string.h> 10#include <string.h>
10#include <sys/types.h> 11#include <sys/types.h>
11#include <sys/uio.h>
12#include <unistd.h> 12#include <unistd.h>
13 13
14/* Libowfat */ 14/* Libowfat */
15#include "byte.h"
16#include "ip6.h" 15#include "ip6.h"
17#include "ndelay.h" 16#include "ndelay.h"
18#include "socket.h" 17#include "socket.h"
@@ -57,7 +56,9 @@ static sync_buffer g_v6_buf;
57static sync_buffer g_v4_buf; 56static sync_buffer g_v4_buf;
58 57
59static pthread_t thread_id; 58static pthread_t thread_id;
60void livesync_init() { 59void livesync_init(void) {
60 uint32_t packet_type6 = htonl(OT_SYNC_PEER6);
61 uint32_t packet_type4 = htonl(OT_SYNC_PEER4);
61 62
62 if (g_socket_in == -1) 63 if (g_socket_in == -1)
63 exerr("No socket address for live sync specified."); 64 exerr("No socket address for live sync specified.");
@@ -66,8 +67,8 @@ void livesync_init() {
66 memcpy(g_v6_buf.data, &g_tracker_id, sizeof(g_tracker_id)); 67 memcpy(g_v6_buf.data, &g_tracker_id, sizeof(g_tracker_id));
67 memcpy(g_v4_buf.data, &g_tracker_id, sizeof(g_tracker_id)); 68 memcpy(g_v4_buf.data, &g_tracker_id, sizeof(g_tracker_id));
68 69
69 uint32_pack_big((char *)g_v6_buf.data + sizeof(g_tracker_id), OT_SYNC_PEER6); 70 memcpy(g_v6_buf.data + sizeof(g_tracker_id), &packet_type6, sizeof(packet_type6));
70 uint32_pack_big((char *)g_v4_buf.data + sizeof(g_tracker_id), OT_SYNC_PEER4); 71 memcpy(g_v4_buf.data + sizeof(g_tracker_id), &packet_type4, sizeof(packet_type4));
71 72
72 g_v6_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t); 73 g_v6_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t);
73 g_v4_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t); 74 g_v4_buf.fill = sizeof(g_tracker_id) + sizeof(uint32_t);
@@ -78,7 +79,7 @@ void livesync_init() {
78 pthread_create(&thread_id, NULL, livesync_worker, NULL); 79 pthread_create(&thread_id, NULL, livesync_worker, NULL);
79} 80}
80 81
81void livesync_deinit() { 82void livesync_deinit(void) {
82 if (g_socket_in != -1) 83 if (g_socket_in != -1)
83 close(g_socket_in); 84 close(g_socket_in);
84 if (g_socket_out != -1) 85 if (g_socket_out != -1)
@@ -167,7 +168,7 @@ static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size)
167/* Tickle the live sync module from time to time, so no events get 168/* Tickle the live sync module from time to time, so no events get
168 stuck when there's not enough traffic to fill udp packets fast 169 stuck when there's not enough traffic to fill udp packets fast
169 enough */ 170 enough */
170void livesync_ticker() { 171void livesync_ticker(void) {
171 /* livesync_issue_peersync sets g_next_packet_time */ 172 /* livesync_issue_peersync sets g_next_packet_time */
172 pthread_mutex_lock(&g_outbuf_mutex); 173 pthread_mutex_lock(&g_outbuf_mutex);
173 if (g_now_seconds > g_v6_buf.next_packet_time && g_v6_buf.fill > sizeof(g_tracker_id) + sizeof(uint32_t)) 174 if (g_now_seconds > g_v6_buf.next_packet_time && g_v6_buf.fill > sizeof(g_tracker_id) + sizeof(uint32_t))
@@ -216,6 +217,8 @@ static void *livesync_worker(void *args) {
216 memcpy(in_ip, V4mappedprefix, sizeof(V4mappedprefix)); 217 memcpy(in_ip, V4mappedprefix, sizeof(V4mappedprefix));
217 218
218 while (1) { 219 while (1) {
220 uint32_t packet_type;
221
219 ws.request_size = socket_recv4(g_socket_in, (char *)ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); 222 ws.request_size = socket_recv4(g_socket_in, (char *)ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port);
220 223
221 /* Expect at least tracker id and packet type */ 224 /* Expect at least tracker id and packet type */
@@ -228,7 +231,9 @@ static void *livesync_worker(void *args) {
228 continue; 231 continue;
229 } 232 }
230 233
231 switch (uint32_read_big(sizeof(g_tracker_id) + (char *)ws.inbuf)) { 234 memcpy(&packet_type, ws.inbuf + sizeof(g_tracker_id), sizeof(packet_type));
235
236 switch (ntohl(packet_type)) {
232 case OT_SYNC_PEER6: 237 case OT_SYNC_PEER6:
233 livesync_handle_peersync(&ws, OT_PEER_SIZE6); 238 livesync_handle_peersync(&ws, OT_PEER_SIZE6);
234 break; 239 break;