diff options
Diffstat (limited to 'ot_livesync.c')
| -rw-r--r-- | ot_livesync.c | 42 |
1 files changed, 10 insertions, 32 deletions
diff --git a/ot_livesync.c b/ot_livesync.c index f6bb28b..5a63f3c 100644 --- a/ot_livesync.c +++ b/ot_livesync.c | |||
| @@ -5,19 +5,16 @@ | |||
| 5 | 5 | ||
| 6 | /* System */ | 6 | /* System */ |
| 7 | #include <arpa/inet.h> | 7 | #include <arpa/inet.h> |
| 8 | #include <fcntl.h> | ||
| 9 | #include <pthread.h> | 8 | #include <pthread.h> |
| 10 | #include <stdlib.h> | 9 | #include <stdlib.h> |
| 11 | #include <string.h> | 10 | #include <string.h> |
| 12 | #include <sys/types.h> | 11 | #include <sys/types.h> |
| 13 | #include <unistd.h> | 12 | #include <unistd.h> |
| 14 | 13 | ||
| 15 | /* Libowfat */ | ||
| 16 | #include "socket.h" | ||
| 17 | |||
| 18 | /* Opentracker */ | 14 | /* Opentracker */ |
| 19 | #include "ot_accesslist.h" | 15 | #include "ot_accesslist.h" |
| 20 | #include "ot_ip.h" | 16 | #include "ot_ip.h" |
| 17 | #include "ot_socket.h" | ||
| 21 | #include "ot_livesync.h" | 18 | #include "ot_livesync.h" |
| 22 | #include "ot_mutex.h" | 19 | #include "ot_mutex.h" |
| 23 | #include "ot_stats.h" | 20 | #include "ot_stats.h" |
| @@ -25,7 +22,7 @@ | |||
| 25 | 22 | ||
| 26 | #ifdef WANT_SYNC_LIVE | 23 | #ifdef WANT_SYNC_LIVE |
| 27 | 24 | ||
| 28 | char groupip_1[4] = {224, 0, 23, 5}; | 25 | ot_ip6 groupip_1 = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0xff, 0xff, 224, 0, 23, 5}; |
| 29 | 26 | ||
| 30 | #define LIVESYNC_INCOMING_BUFFSIZE (256 * 256) | 27 | #define LIVESYNC_INCOMING_BUFFSIZE (256 * 256) |
| 31 | 28 | ||
| @@ -39,11 +36,11 @@ enum { OT_SYNC_PEER4, OT_SYNC_PEER6 }; | |||
| 39 | /* Forward declaration */ | 36 | /* Forward declaration */ |
| 40 | static void *livesync_worker(void *args); | 37 | static void *livesync_worker(void *args); |
| 41 | 38 | ||
| 42 | /* For outgoing packets */ | ||
| 43 | static int64 g_socket_in = -1; | ||
| 44 | |||
| 45 | /* For incoming packets */ | 39 | /* For incoming packets */ |
| 46 | static int64 g_socket_out = -1; | 40 | static int g_socket_in = -1; |
| 41 | |||
| 42 | /* For outgoing packets */ | ||
| 43 | static int g_socket_out = -1; | ||
| 47 | 44 | ||
| 48 | static pthread_mutex_t g_outbuf_mutex = PTHREAD_MUTEX_INITIALIZER; | 45 | static pthread_mutex_t g_outbuf_mutex = PTHREAD_MUTEX_INITIALIZER; |
| 49 | typedef struct { | 46 | typedef struct { |
| @@ -89,34 +86,17 @@ void livesync_deinit(void) { | |||
| 89 | } | 86 | } |
| 90 | 87 | ||
| 91 | void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { | 88 | void livesync_bind_mcast(ot_ip6 ip, uint16_t port) { |
| 92 | char tmpip[4] = {0, 0, 0, 0}; | ||
| 93 | char *v4ip; | ||
| 94 | |||
| 95 | if (!ip_is_v4mapped(ip)) | 89 | if (!ip_is_v4mapped(ip)) |
| 96 | exerr("v6 mcast support not yet available."); | 90 | exerr("v6 mcast support not yet available."); |
| 97 | v4ip = ip + 12; | ||
| 98 | 91 | ||
| 99 | if (g_socket_in != -1) | 92 | if (g_socket_in != -1) |
| 100 | exerr("Error: Livesync listen ip specified twice."); | 93 | exerr("Error: Livesync listen ip specified twice."); |
| 101 | 94 | ||
| 102 | if ((g_socket_in = socket_udp4()) < 0) | 95 | if ((g_socket_in = socket_mcast_join(groupip_1, ip, port)) == -1) |
| 103 | exerr("Error: Cant create live sync incoming socket."); | ||
| 104 | |||
| 105 | fcntl(g_socket_in, F_SETFL, fcntl(g_socket_in, F_GETFL, 0) & ~O_NONBLOCK); | ||
| 106 | |||
| 107 | if (socket_bind4_reuse(g_socket_in, tmpip, port) == -1) | ||
| 108 | exerr("Error: Cant bind live sync incoming socket."); | 96 | exerr("Error: Cant bind live sync incoming socket."); |
| 109 | 97 | ||
| 110 | if (socket_mcjoin4(g_socket_in, groupip_1, v4ip)) | 98 | if ((g_socket_out = socket_mcast_bind(ip, port, 0)) == -1) |
| 111 | exerr("Error: Cant make live sync incoming socket join mcast group."); | ||
| 112 | |||
| 113 | if ((g_socket_out = socket_udp4()) < 0) | ||
| 114 | exerr("Error: Cant create live sync outgoing socket."); | ||
| 115 | if (socket_bind4_reuse(g_socket_out, v4ip, port) == -1) | ||
| 116 | exerr("Error: Cant bind live sync outgoing socket."); | 99 | exerr("Error: Cant bind live sync outgoing socket."); |
| 117 | |||
| 118 | socket_mcttl4(g_socket_out, 1); | ||
| 119 | socket_mcloop4(g_socket_out, 0); | ||
| 120 | } | 100 | } |
| 121 | 101 | ||
| 122 | /* Caller MUST hold g_outbuf_mutex. Returns with g_outbuf_mutex unlocked */ | 102 | /* Caller MUST hold g_outbuf_mutex. Returns with g_outbuf_mutex unlocked */ |
| @@ -132,7 +112,7 @@ static void livesync_issue_peersync(sync_buffer *buf) { | |||
| 132 | has modified the protected element */ | 112 | has modified the protected element */ |
| 133 | pthread_mutex_unlock(&g_outbuf_mutex); | 113 | pthread_mutex_unlock(&g_outbuf_mutex); |
| 134 | 114 | ||
| 135 | socket_send4(g_socket_out, mycopy, fill, groupip_1, LIVESYNC_PORT); | 115 | socket_mcast_send(g_socket_out, mycopy, fill, groupip_1, LIVESYNC_PORT); |
| 136 | } | 116 | } |
| 137 | 117 | ||
| 138 | static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size) { | 118 | static void livesync_handle_peersync(struct ot_workstruct *ws, size_t peer_size) { |
| @@ -215,12 +195,10 @@ static void *livesync_worker(void *args) { | |||
| 215 | ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); | 195 | ws.inbuf = ws.request = malloc(LIVESYNC_INCOMING_BUFFSIZE); |
| 216 | ws.outbuf = ws.reply = 0; | 196 | ws.outbuf = ws.reply = 0; |
| 217 | 197 | ||
| 218 | memcpy(in_ip, ip_v4mapped_prefix, sizeof(ip_v4mapped_prefix)); | ||
| 219 | |||
| 220 | while (1) { | 198 | while (1) { |
| 221 | uint32_t packet_type; | 199 | uint32_t packet_type; |
| 222 | 200 | ||
| 223 | ws.request_size = socket_recv4(g_socket_in, (char *)ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, 12 + (char *)in_ip, &in_port); | 201 | ws.request_size = socket_recv(g_socket_in, ws.inbuf, LIVESYNC_INCOMING_BUFFSIZE, in_ip, &in_port); |
| 224 | 202 | ||
| 225 | /* Expect at least tracker id and packet type */ | 203 | /* Expect at least tracker id and packet type */ |
| 226 | if (ws.request_size <= (ssize_t)(sizeof(g_tracker_id) + sizeof(uint32_t))) | 204 | if (ws.request_size <= (ssize_t)(sizeof(g_tracker_id) + sizeof(uint32_t))) |
