static int handle_fakeid_announce(void *object, const uint8_t *source_pubkey, const uint8_t *data, uint16_t length) { Onion_Client *onion_c = object; if (length < FAKEID_DATA_MIN_LENGTH) return 1; if (length > FAKEID_DATA_MAX_LENGTH) return 1; int friend_num = onion_friend_num(onion_c, source_pubkey); if (friend_num == -1) return 1; uint64_t no_replay; memcpy(&no_replay, data + 1, sizeof(uint64_t)); net_to_host((uint8_t *) &no_replay, sizeof(no_replay)); if (no_replay <= onion_c->friends_list[friend_num].last_noreplay) return 1; onion_c->friends_list[friend_num].last_noreplay = no_replay; if (onion_c->friends_list[friend_num].dht_pk_callback) onion_c->friends_list[friend_num].dht_pk_callback(onion_c->friends_list[friend_num].dht_pk_callback_object, onion_c->friends_list[friend_num].dht_pk_callback_number, data + 1 + sizeof(uint64_t)); onion_set_friend_DHT_pubkey(onion_c, friend_num, data + 1 + sizeof(uint64_t)); onion_c->friends_list[friend_num].last_seen = unix_time(); uint16_t len_nodes = length - FAKEID_DATA_MIN_LENGTH; if (len_nodes != 0) { Node_format nodes[MAX_SENT_NODES]; int num_nodes = unpack_nodes(nodes, MAX_SENT_NODES, 0, data + 1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES, len_nodes, 1); if (num_nodes <= 0) return 1; int i; for (i = 0; i < num_nodes; ++i) { uint8_t family = nodes[i].ip_port.ip.family; if (family == AF_INET || family == AF_INET6) { DHT_getnodes(onion_c->dht, &nodes[i].ip_port, nodes[i].client_id, onion_c->friends_list[friend_num].fake_client_id); } else if (family == TCP_INET || family == TCP_INET6) { if (onion_c->friends_list[friend_num].tcp_relay_node_callback) { void *obj = onion_c->friends_list[friend_num].tcp_relay_node_callback_object; uint32_t number = onion_c->friends_list[friend_num].tcp_relay_node_callback_number; onion_c->friends_list[friend_num].tcp_relay_node_callback(obj, number, nodes[i].ip_port, nodes[i].client_id); } } } } return 0; }
int load_blocklist(char *path) { if (path == NULL) return -1; FILE *fp = fopen(path, "rb"); if (fp == NULL) return -1; off_t len = file_size(path); if (len == 0) { fclose(fp); return -1; } char data[len]; if (fread(data, len, 1, fp) != 1) { fclose(fp); return -1; } if (len % sizeof(BlockedFriend) != 0) { fclose(fp); return -1; } int num = len / sizeof(BlockedFriend); Blocked.max_idx = num; realloc_blocklist(num); int i; for (i = 0; i < num; ++i) { BlockedFriend tmp; memset(&tmp, 0, sizeof(BlockedFriend)); memset(&Blocked.list[i], 0, sizeof(BlockedFriend)); memcpy(&tmp, data + i * sizeof(BlockedFriend), sizeof(BlockedFriend)); Blocked.list[i].active = true; Blocked.list[i].num = i; Blocked.list[i].namelength = ntohs(tmp.namelength); memcpy(Blocked.list[i].name, tmp.name, Blocked.list[i].namelength + 1); memcpy(Blocked.list[i].pub_key, tmp.pub_key, TOX_PUBLIC_KEY_SIZE); uint8_t lastonline[sizeof(uint64_t)]; memcpy(lastonline, &tmp.last_on, sizeof(uint64_t)); net_to_host(lastonline, sizeof(uint64_t)); memcpy(&Blocked.list[i].last_on, lastonline, sizeof(uint64_t)); ++Blocked.num_blocked; } fclose(fp); sort_blocklist_index(); return 0; }
/************************************************************************* * the callback fnction for reading data from cc2420 * *************************************************************************/ void _MacRecvCallBack(int16_t timestamp) { VMAC_PPDU ppdu; vhal_data vd; mac_to_vhal(&ppdu, &vd); Radio_Disable_Interrupt(); //disable interrupt while reading data if( !Radio_Recv_Pack(&vd) ) { Radio_Enable_Interrupt(); //enable interrupt return; } Radio_Enable_Interrupt(); //enable interrupt vhal_to_mac(&vd, &ppdu); // Andreas - filter node ID here, even before allocating any new memory // if you're using sos/config/base you must comment this block out! if (net_to_host(ppdu.mpdu.daddr) != NODE_ADDR && net_to_host(ppdu.mpdu.daddr) != BCAST_ADDRESS) { ker_free(vd.payload); return; } Message *msg = msg_create(); if( msg == NULL ) { ker_free(vd.payload); return; } mac_to_sosmsg(&ppdu, msg); // Andreas - start debug #ifdef ENA_VMAC_UART_DEBUG uint8_t *payload; uint8_t msg_len; msg_len=msg->len; payload = msg->data; //post_uart(msg->sid, msg->did, msg->type, msg_len, payload, SOS_MSG_RELEASE, msg->daddr); // Swap daddr with saddr, because daddr is useless when debugging. // Of course, if sossrv says "dest addr: 15" that actually means the message SENDER was node 15 post_uart(msg->sid, msg->did, msg->type, msg_len, payload, SOS_MSG_RELEASE, msg->saddr); #endif //if (msg->daddr == NODE_ADDR || msg->daddr == BCAST_ADDRESS) handle_incoming_msg(msg, SOS_MSG_RADIO_IO); }
/************************************************************************* * change packet's format from MAC to SOS message * *************************************************************************/ void mac_to_sosmsg(VMAC_PPDU *ppdu, Message *msg) { msg->len = ppdu->len - (PRE_PAYLOAD_LEN + POST_PAYLOAD_LEN); msg->daddr = net_to_host(ppdu->mpdu.daddr); msg->saddr = net_to_host(ppdu->mpdu.saddr); msg->did = ppdu->mpdu.did; msg->sid = ppdu->mpdu.sid; msg->type = ppdu->mpdu.type; //msg->data = ppdu->mpdu.data; if(msg->len==0) msg->data = NULL; else msg->data = ppdu->mpdu.data; msg->flag |= SOS_MSG_RELEASE; }
static int handle_fakeid_announce(void *object, uint8_t *source_pubkey, uint8_t *data, uint32_t length) { Onion_Client *onion_c = object; if (length < FAKEID_DATA_MIN_LENGTH) return 1; if (length > FAKEID_DATA_MAX_LENGTH) return 1; if ((length - FAKEID_DATA_MIN_LENGTH) % sizeof(Node_format) != 0) return 1; int friend_num = onion_friend_num(onion_c, source_pubkey); if (friend_num == -1) return 1; uint64_t no_replay; net_to_host(data + 1, sizeof(no_replay)); memcpy(&no_replay, data + 1, sizeof(uint64_t)); if (no_replay <= onion_c->friends_list[friend_num].last_noreplay) return 1; onion_c->friends_list[friend_num].last_noreplay = no_replay; if (memcmp(data + 1 + sizeof(uint64_t), onion_c->friends_list[friend_num].fake_client_id, crypto_box_PUBLICKEYBYTES) != 0) { DHT_delfriend(onion_c->dht, onion_c->friends_list[friend_num].fake_client_id); onion_c->friends_list[friend_num].last_seen = unix_time(); if (DHT_addfriend(onion_c->dht, data + 1 + sizeof(uint64_t)) == 1) { return 1; } onion_c->friends_list[friend_num].is_fake_clientid = 1; memcpy(onion_c->friends_list[friend_num].fake_client_id, data + 1 + sizeof(uint64_t), crypto_box_PUBLICKEYBYTES); } uint16_t num_nodes = (length - FAKEID_DATA_MIN_LENGTH) / sizeof(Node_format); Node_format nodes[num_nodes]; memcpy(nodes, data + 1 + sizeof(uint64_t) + crypto_box_PUBLICKEYBYTES, sizeof(nodes)); uint32_t i; for (i = 0; i < num_nodes; ++i) { to_host_family(&nodes[i].ip_port.ip); DHT_getnodes(onion_c->dht, &nodes[i].ip_port, nodes[i].client_id, onion_c->friends_list[friend_num].fake_client_id); } return 0; }
int main(int argc, char* argv[]){ char chosen_server[500]; int lowest_stratum = 1000; int j; for (j = 4; j >= 0; j--) { struct timespec t1_time, t4_time; double sec, ns; header_t header; memset(&header, 0, sizeof(header_t)); times_t timestamps; char* node = server_list[j]; printf("------------\n"); printf("Testing server %s\n", server_list[j]); char* service = "123"; int status; struct addrinfo hints; struct addrinfo *serverinfo, recmsg; memset(&hints, 0, sizeof(hints)); hints.ai_family = AF_UNSPEC; hints.ai_socktype = SOCK_DGRAM; if ((status = getaddrinfo(node, service, &hints, &serverinfo)) != 0){ fprintf(stderr, "Error resolving host name or IP address\n"); exit(1); } int sockfd = socket(serverinfo->ai_family, serverinfo->ai_socktype, serverinfo->ai_protocol); // Start time if( clock_gettime( CLOCK_REALTIME, &t1_time) == -1 ) { perror( "clock gettime" ); exit( EXIT_FAILURE ); } /* Version set to 4 and Mode to 3 (Client), rest to 0 */ header.control[0] = 35; header.control[1] = 0; header.control[2] = 0; header.control[3] = 0; int i; printf("Control: "); for (i = 0; i < 4; i++) printf("%x|", header.control[i]); header.origin_t = time_to_NTP(t1_time); printf("\nSending header...\n"); int send_res; send_res = sendto(sockfd, &header, sizeof(header_t), 0, serverinfo->ai_addr, serverinfo->ai_addrlen); printf("Sent\n"); // Prepare receive timestamps.t1 = header.origin_t; printf("Waiting to receive header...\n"); //header_t rec_header; int bytes_rec; bytes_rec = recvfrom(sockfd, &header, sizeof(header_t), 0, serverinfo->ai_addr, &recmsg.ai_addrlen); printf("Received, result: %d\n", bytes_rec); // End time if( clock_gettime( CLOCK_REALTIME, &t4_time) == -1 ) { perror( "clock gettime" ); exit( EXIT_FAILURE ); } // Use netohost() to convert from the network byte order to host byte order! close(sockfd); timestamps.t2 = net_to_host(header.receive_t) - ((long )(2208988800) << 32); timestamps.t3 = net_to_host(header.transmit_t) - ((long )(2208988800) << 32); timestamps.t4 = time_to_NTP(t4_time); /* printf("Timestamps:\n"); printf("t1: %u %u\n", (int)(timestamps.t1 >> 32), (int)(timestamps.t1)); printf("t2: %u %u\n", (int)(timestamps.t2 >> 32), (int)(timestamps.t2)); printf("t3: %u %u\n", (int)(timestamps.t3 >> 32), (int)(timestamps.t3)); printf("t4: %u %u\n[all ns above were ns_frac]\n", (int)(timestamps.t4 >> 32), (int)(timestamps.t4)); */ long delay = NTP_to_ns(get_delay(timestamps)); long offset = NTP_to_ns(get_offset(timestamps)); printf("Delay: %u s %u ns\n", (int)(delay >> 32), (int)(delay)); printf("Offset: %d s %d ns\n", (int)(offset >> 32), (int)(offset)); printf("Stratum: %d\n", header.control[1]); if (header.control[1] < lowest_stratum) { lowest_stratum = header.control[1]; strcpy(chosen_server, server_list[j]); } printf("------------\n"); } printf("Chosen server with Stratum = %d: %s\n", lowest_stratum, chosen_server); return 0; }
int load_blocklist(char *path) { if (path == NULL) return -1; FILE *fp = fopen(path, "rb"); if (fp == NULL) return -1; off_t len = file_size(path); if (len == -1) { fclose(fp); return -1; } char *data = malloc(len); if (data == NULL) { fclose(fp); exit_toxic_err("Failed in load_blocklist", FATALERR_MEMORY); } if (fread(data, len, 1, fp) != 1) { fclose(fp); free(data); return -1; } if (len % sizeof(BlockedFriend) != 0) { fclose(fp); free(data); return -1; } int num = len / sizeof(BlockedFriend); Blocked.max_idx = num; realloc_blocklist(num); int i; for (i = 0; i < num; ++i) { memset(&Blocked.list[i], 0, sizeof(BlockedFriend)); BlockedFriend tmp; memcpy(&tmp, data + i * sizeof(BlockedFriend), sizeof(BlockedFriend)); Blocked.list[i].active = true; Blocked.list[i].num = i; Blocked.list[i].namelength = ntohs(tmp.namelength); memcpy(Blocked.list[i].name, tmp.name, Blocked.list[i].namelength + 1); memcpy(Blocked.list[i].pub_key, tmp.pub_key, TOX_CLIENT_ID_SIZE); uint8_t lastonline[sizeof(uint64_t)]; memcpy(lastonline, &tmp.last_on, sizeof(uint64_t)); net_to_host(lastonline, sizeof(uint64_t)); memcpy(&Blocked.list[i].last_on, lastonline, sizeof(uint64_t)); ++Blocked.num_blocked; } free(data); fclose(fp); sort_blocklist_index(); return 0; }