From 3b42ed1ca1be6d1c8f14606befee8b8deae64ac8 Mon Sep 17 00:00:00 2001 From: GDR! Date: Mon, 10 Nov 2014 19:05:14 +0100 Subject: builds well --- Makefile | 20 ++-- main.c | 289 ++++++++++++++++++++++++++++++++++++++++++++++++++++++++ tox_bootstrap.h | 122 ++++++++++++++++++++++++ 3 files changed, 424 insertions(+), 7 deletions(-) create mode 100644 main.c create mode 100644 tox_bootstrap.h diff --git a/Makefile b/Makefile index ef572e6..7617e07 100644 --- a/Makefile +++ b/Makefile @@ -1,14 +1,20 @@ -SOURCES = main.c -DEPS= +SOURCES = $(wildcard *.c) +DEPS=libtoxcore CC=gcc -CFLAGS=-g +CFLAGS=-g #-std=c99 +CFLAGS += $(shell pkg-config --cflags $(DEPS)) +LDFLAGS=-g -pthread -lm -static +LDFLAGS += $(shell pkg-config --libs $(DEPS)) OBJECTS=$(SOURCES:.c=.o) +INCLUDES = $(wildcard *.h) -.c.o: +.c.o: $(INCLUDES) $(CC) $(CFLAGS) $< -c -o $@ -tuntox: $(OBJECTS) - $(CC) --static -o $@ -ltoxcore $^ $(CFLAGS) /usr/local/lib/libsodium.a +tuntox: $(OBJECTS) $(INCLUDES) + $(CC) -o $@ $(OBJECTS) -ltoxcore -lpthread $(LDFLAGS) /usr/local/lib/libsodium.a /usr/local/lib/libtoxcore.a -all: tuntox +cscope.out: + cscope -bv ./*.[ch] +all: cscope.out tuntox diff --git a/main.c b/main.c new file mode 100644 index 0000000..2d09e2e --- /dev/null +++ b/main.c @@ -0,0 +1,289 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#include "main.h" +#include "tox_bootstrap.h" + +static Tox_Options tox_options; +static Tox *tox; +int client_socket = 0; + +static void writechecksum(uint8_t *address) +{ + uint8_t *checksum = address + 36; + uint32_t i; + + for (i = 0; i < 36; ++i) + checksum[i % 2] ^= address[i]; +} + +/* From utox/util.c */ +static void to_hex(char_t *a, const char_t *p, int size) +{ + char_t b, c, *end = p + size; + + while(p != end) { + b = *p++; + + c = (b & 0xF); + b = (b >> 4); + + if(b < 10) { + *a++ = b + '0'; + } else { + *a++ = b - 10 + 'A'; + } + + if(c < 10) { + *a++ = c + '0'; + } else { + *a++ = c - 10 + 'A'; + } + } +} + +/* From utox/util.c */ +void id_to_string(char_t *dest, const char_t *src) +{ + to_hex(dest, src, TOX_FRIEND_ADDRESS_SIZE); +} + +/* bootstrap to dht with bootstrap_nodes */ +/* From uTox/tox.c */ +static void do_bootstrap(Tox *tox) +{ + static unsigned int j = 0; + + if (j == 0) + j = rand(); + + int i = 0; + while(i < 4) { + struct bootstrap_node *d = &bootstrap_nodes[j % countof(bootstrap_nodes)]; + tox_bootstrap_from_address(tox, d->address, d->port, d->key); + i++; + j++; + } +} + +/* Set username to the machine's FQDN */ +void set_tox_username(Tox *tox) +{ + unsigned char hostname[1024]; + struct addrinfo hints, *info, *p; + int gai_result; + + gethostname(hostname, 1024); + hostname[1023] = '\0'; +# if 0 + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; /*either IPV4 or IPV6*/ + hints.ai_socktype = SOCK_STREAM; + hints.ai_flags = AI_CANONNAME; + + if ((gai_result = getaddrinfo(hostname, "ftp", &hints, &info)) != 0) + { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(gai_result)); + exit(1); + } + + for(p = info; p != NULL; p = p->ai_next) + { + printf("hostname: %s\n", p->ai_canonname); + } +# endif + + tox_set_name(tox, hostname, strlen(hostname)); + +// freeaddrinfo(info); +} + +void accept_friend_request(Tox *tox, const uint8_t *public_key, const uint8_t *data, uint16_t length, void *userdata) +{ + unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; + + printf("Got friend request\n"); + tox_add_friend_norequest(tox, public_key); + id_to_string(tox_printable_id, public_key); + printf("Accepted friend request from %s\n", tox_printable_id); +} + +void cleanup(int status, void *tmp) +{ + printf("kthxbye\n"); + fflush(stdout); + tox_kill(tox); + if(client_socket) + { + close(client_socket); + } +} + +// get sockaddr, IPv4 or IPv6: +/* From Beej */ +void *get_in_addr(struct sockaddr *sa) +{ + if (sa->sa_family == AF_INET) { + return &(((struct sockaddr_in*)sa)->sin_addr); + } + + return &(((struct sockaddr_in6*)sa)->sin6_addr); +} + +/* From Beej */ +int get_client_socket() +{ + int sockfd, numbytes; + char buf[READ_BUFFER_SIZE]; + struct addrinfo hints, *servinfo, *p; + int rv; + char s[INET6_ADDRSTRLEN]; + int port = 22; + char hostname[4096] = "127.0.0.1"; + char port_str[6]; + + snprintf(port_str, 6, "%d", port); + + memset(&hints, 0, sizeof hints); + hints.ai_family = AF_UNSPEC; + hints.ai_socktype = SOCK_STREAM; + + if ((rv = getaddrinfo(hostname, port_str, &hints, &servinfo)) != 0) { + fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); + exit(1); + } + + // loop through all the results and connect to the first we can + for(p = servinfo; p != NULL; p = p->ai_next) { + if ((sockfd = socket(p->ai_family, p->ai_socktype, + p->ai_protocol)) == -1) { + perror("client: socket"); + continue; + } + + if (connect(sockfd, p->ai_addr, p->ai_addrlen) == -1) { + close(sockfd); + perror("client: connect"); + continue; + } + + break; + } + + if (p == NULL) { + fprintf(stderr, "failed to connect to %s:%d\n", hostname, port); + exit(1); + } + + inet_ntop(p->ai_family, get_in_addr((struct sockaddr *)p->ai_addr), + s, sizeof s); + fprintf(stderr, "connecting to %s\n", s); + + freeaddrinfo(servinfo); // all done with this structure + + fprintf(stderr, "Connected to %s:%d\n", hostname, port); + + return sockfd; +} + +unsigned int create_packet(unsigned char *dst, unsigned char *data, int data_len, int sockfd) +{ +// assert data_len < 65536 + dst[0] = 0xa2; + dst[1] = 0x6a; + dst[2] = sockfd >> 8; + dst[3] = sockfd & 0xff; + dst[4] = (data_len >> 8) & 0xff; + dst[5] = data_len & 0xff; + memcpy(dst+PROTOCOL_BUFFER_OFFSET, data, data_len); + return data_len + PROTOCOL_BUFFER_OFFSET; +} + +int do_loop() +{ + struct timeval tv; + fd_set fds; + fd_set master; + unsigned char read_buf[READ_BUFFER_SIZE+1]; + unsigned char tox_packet_buf[PROTOCOL_MAX_PACKET_SIZE]; + + tv.tv_sec = 0; + tv.tv_usec = 20000; + + FD_ZERO(&fds); + FD_SET(client_socket, &fds); + + master = fds; + + while(1) + { + /* Let tox do its stuff */ + tox_do(tox); + + /* Poll for data from our client connection */ + select(client_socket+1, &fds, NULL, NULL, &tv); + if(FD_ISSET(client_socket, &fds)) + { + int nbytes = recv(client_socket, read_buf, READ_BUFFER_SIZE, 0); + + /* Check if connection closed */ + if(nbytes == 0) + { + printf("conn closed!\n"); + } + else + { + unsigned int tox_packet_length = 0; + read_buf[nbytes] = '\0'; + tox_packet_length = create_packet(tox_packet_buf, read_buf, nbytes, client_socket); + tox_send_lossless_packet(tox, 0, tox_packet_buf, tox_packet_length); + printf("READ: %s\n", read_buf); + } + } + + fds = master; + } +} + +int main(int argc, char *argv[]) +{ + unsigned char tox_id[TOX_FRIEND_ADDRESS_SIZE]; + unsigned char tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2 + 1]; + + on_exit(cleanup, NULL); + + /* Bootstrap tox */ + tox_options.ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; + tox_options.udp_disabled = 0; + tox_options.proxy_enabled = 0; + + tox = tox_new(&tox_options); + + tox_callback_friend_request(tox, accept_friend_request, NULL); + + set_tox_username(tox); + + tox_get_address(tox, tox_id); + id_to_string(tox_printable_id, tox_id); + tox_printable_id[TOX_FRIEND_ADDRESS_SIZE * 2] = '\0'; + printf("Generated Tox ID: %s\n", tox_printable_id); + + do_bootstrap(tox); + + /* Connect to the forwarded service */ + client_socket = get_client_socket(); + + do_loop(); + + return 0; +} diff --git a/tox_bootstrap.h b/tox_bootstrap.h new file mode 100644 index 0000000..119a2d2 --- /dev/null +++ b/tox_bootstrap.h @@ -0,0 +1,122 @@ +struct bootstrap_node { + char *address; + uint16_t port; + uint8_t key[32]; +} bootstrap_nodes[] = { + { + "37.187.46.132", + 33445, + { + 0x5E, 0xB6, 0x7C, 0x51, 0xD3, 0xFF, 0x5A, 0x9D, 0x52, 0x8D, 0x24, 0x2B, 0x66, 0x90, 0x36, 0xED, + 0x2A, 0x30, 0xF8, 0xA6, 0x0E, 0x67, 0x4C, 0x45, 0xE7, 0xD4, 0x30, 0x10, 0xCB, 0x2E, 0x13, 0x31 + } + }, + + { + "144.76.60.215", + 33445, + { + 0x04, 0x11, 0x9E, 0x83, 0x5D, 0xF3, 0xE7, 0x8B, 0xAC, 0xF0, 0xF8, 0x42, 0x35, 0xB3, 0x00, 0x54, + 0x6A, 0xF8, 0xB9, 0x36, 0xF0, 0x35, 0x18, 0x5E, 0x2A, 0x8E, 0x9E, 0x0A, 0x67, 0xC8, 0x92, 0x4F + } + }, + + { + "23.226.230.47", + 33445, + { + 0xA0, 0x91, 0x62, 0xD6, 0x86, 0x18, 0xE7, 0x42, 0xFF, 0xBC, 0xA1, 0xC2, 0xC7, 0x03, 0x85, 0xE6, + 0x67, 0x96, 0x04, 0xB2, 0xD8, 0x0E, 0xA6, 0xE8, 0x4A, 0xD0, 0x99, 0x6A, 0x1A, 0xC8, 0xA0, 0x74 + } + }, + + { + "54.199.139.199", + 33445, + { + 0x7F, 0x9C, 0x31, 0xFE, 0x85, 0x0E, 0x97, 0xCE, 0xFD, 0x4C, 0x45, 0x91, 0xDF, 0x93, 0xFC, 0x75, + 0x7C, 0x7C, 0x12, 0x54, 0x9D, 0xDD, 0x55, 0xF8, 0xEE, 0xAE, 0xCC, 0x34, 0xFE, 0x76, 0xC0, 0x29 + } + }, + + { + "192.210.149.121", + 33445, + { + 0xF4, 0x04, 0xAB, 0xAA, 0x1C, 0x99, 0xA9, 0xD3, 0x7D, 0x61, 0xAB, 0x54, 0x89, 0x8F, 0x56, 0x79, + 0x3E, 0x1D, 0xEF, 0x8B, 0xD4, 0x6B, 0x10, 0x38, 0xB9, 0xD8, 0x22, 0xE8, 0x46, 0x0F, 0xAB, 0x67 + } + }, + + { + "192.254.75.98", + 33445, + { + 0x95, 0x1C, 0x88, 0xB7, 0xE7, 0x5C, 0x86, 0x74, 0x18, 0xAC, 0xDB, 0x5D, 0x27, 0x38, 0x21, 0x37, + 0x2B, 0xB5, 0xBD, 0x65, 0x27, 0x40, 0xBC, 0xDF, 0x62, 0x3A, 0x4F, 0xA2, 0x93, 0xE7, 0x5D, 0x2F + } + }, + + { + "31.7.57.236", + 443, + { + 0x2A, 0x4B, 0x50, 0xD1, 0xD5, 0x25, 0xDA, 0x2E, 0x66, 0x95, 0x92, 0xA2, 0x0C, 0x32, 0x7B, 0x5F, + 0xAD, 0x6C, 0x7E, 0x59, 0x62, 0xDC, 0x69, 0x29, 0x6F, 0x9F, 0xEC, 0x77, 0xC4, 0x43, 0x6E, 0x4E + } + }, + + { + "107.161.17.51", + 33445, + { + 0x7B, 0xE3, 0x95, 0x1B, 0x97, 0xCA, 0x4B, 0x9E, 0xCD, 0xDA, 0x76, 0x8E, 0x8C, 0x52, 0xBA, 0x19, + 0xE9, 0xE2, 0x69, 0x0A, 0xB5, 0x84, 0x78, 0x7B, 0xF4, 0xC9, 0x0E, 0x04, 0xDB, 0xB7, 0x51, 0x11 + } + }, + + { + "37.59.102.176", + 33445, + { + 0xB9, 0x8A, 0x2C, 0xEA, 0xA6, 0xC6, 0xA2, 0xFA, 0xDC, 0x2C, 0x36, 0x32, 0xD2, 0x84, 0x31, 0x8B, + 0x60, 0xFE, 0x53, 0x75, 0xCC, 0xB4, 0x1E, 0xFA, 0x08, 0x1A, 0xB6, 0x7F, 0x50, 0x0C, 0x1B, 0x0B + } + }, + + { + "178.21.112.187", + 33445, + { + 0x4B, 0x2C, 0x19, 0xE9, 0x24, 0x97, 0x2C, 0xB9, 0xB5, 0x77, 0x32, 0xFB, 0x17, 0x2F, 0x8A, 0x86, + 0x04, 0xDE, 0x13, 0xEE, 0xDA, 0x2A, 0x62, 0x34, 0xE3, 0x48, 0x98, 0x33, 0x44, 0xB2, 0x30, 0x57 + } + }, + + { + "63.165.243.15", + 443, + { + 0x8C, 0xD0, 0x87, 0xE3, 0x1C, 0x67, 0x56, 0x81, 0x03, 0xE8, 0xC2, 0xA2, 0x86, 0x53, 0x33, 0x7E, + 0x90, 0xE6, 0xB8, 0xED, 0xA0, 0xD7, 0x65, 0xD5, 0x7C, 0x6B, 0x51, 0x72, 0xB4, 0xF1, 0xF0, 0x4C + } + }, + + { + "195.154.119.113", + 33445, + { + 0xE3, 0x98, 0xA6, 0x96, 0x46, 0xB8, 0xCE, 0xAC, 0xA9, 0xF0, 0xB8, 0x4F, 0x55, 0x37, 0x26, 0xC1, + 0xC4, 0x92, 0x70, 0x55, 0x8C, 0x57, 0xDF, 0x5F, 0x3C, 0x36, 0x8F, 0x05, 0xA7, 0xD7, 0x13, 0x54 + } + }, + + { + "176.31.28.63", + 465, + { + 0x0B, 0x6C, 0x7A, 0x93, 0xA6, 0xD8, 0xC0, 0xEB, 0x44, 0x96, 0x5C, 0x4A, 0x85, 0xCB, 0x88, 0xBA, + 0x75, 0x71, 0x17, 0x0F, 0xE2, 0xDB, 0x3D, 0xEA, 0x10, 0x71, 0x3E, 0x48, 0x55, 0x9A, 0x55, 0x4D + } + }, +}; -- cgit v1.2.3