gdr / tuntox (public) (License: GPLv3) (since 2017-01-24) (hash sha1)
Tunnel TCP connections over the Tox protocol

/util.c (653a009bf54217c9ff82699f9f97677b68bb491c) (2707 bytes) (mode 100644) (type blob)

#include "util.h"
#include <string.h>
#include <tox/tox.h>
#include <stdio.h>

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 */
void to_hex(char_t *a, const char_t *p, int size)
{
    char_t b, c;
    const char_t *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);
}

/* From utox/util.c */
int string_to_id(char_t *w, char_t *a)
{
    char_t *end = w + TOX_FRIEND_ADDRESS_SIZE;
    while(w != end) {
        char_t c, v;

        c = *a++;
        if(c >= '0' && c <= '9') {
            v = (c - '0') << 4;
        } else if(c >= 'A' && c <= 'F') {
            v = (c - 'A' + 10) << 4;
        } else if(c >= 'a' && c <= 'f') {
            v = (c - 'a' + 10) << 4;
        } else {
            return 0;
        }

        c = *a++;
        if(c >= '0' && c <= '9') {
            v |= (c - '0');
        } else if(c >= 'A' && c <= 'F') {
            v |= (c - 'A' + 10);
        } else if(c >= 'a' && c <= 'f') {
            v |= (c - 'a' + 10);
        } else {
            return 0;
        }

        *w++ = v;
    }

    return 1;
}

/* 0 = success */
int parse_local_port_forward(char *string, int *local_port, char **hostname, int *remote_port)
{
    char *lport;
    char *host;
    char *rport;
    
    lport = strtok(string, ":");
    host = strtok(NULL, ":");
    rport = strtok(NULL, ":");

    if(!lport || !host || !rport)
    {
        return -1;
    }

    *local_port = atoi(lport);
    *hostname = host;
    *remote_port = atoi(rport);

    return 0;
}

void* file_raw(char *path, uint32_t *size)
{
    FILE *file;
    char *data;
    int len;

    file = fopen(path, "rb");
    if(!file) {
        fprintf(stderr, "File not found (%s)\n", path);
        return NULL;
    }

    fseek(file, 0, SEEK_END);
    len = ftell(file);
    data = malloc(len);
    if(!data) {
        fclose(file);
        return NULL;
    }

    fseek(file, 0, SEEK_SET);

    if(fread(data, len, 1, file) != 1) {
        fprintf(stderr, "Read error (%s)\n", path);
        fclose(file);
        free(data);
        return NULL;
    }

    fclose(file);

    fprintf(stderr, "Read %u bytes (%s)\n", len, path);

    if(size) {
        *size = len;
    }
    return data;
}


Mode Type Size Ref File
100644 blob 495 7617e079b35d71d06f4429fef404cf87991e8476 Makefile
100644 blob 12050 75d4b0dffb30516fc89b6c63bc8fe3394801348f client.c
100644 blob 646 7d5e50d558094ed74875659f6ea3039b1ddc3e10 client.h
100644 blob 23719 4b7a3c54d258fdd21c8a138795fe8043e6d0df4b main.c
100644 blob 2364 ba0a6d0b41fa91492cbb98e1d6df668417f9bef4 main.h
100644 blob 3876 119a2d25778f9f213d8c44aaf5c6047a311810e8 tox_bootstrap.h
100644 blob 12536 75e9dc5ed9399120416e8da5f24d1ccde41cf901 utarray.h
100644 blob 61492 7205c67efa27c66884c8d4d1c8a105d4854a0548 uthash.h
100644 blob 2707 653a009bf54217c9ff82699f9f97677b68bb491c util.c
100644 blob 380 30d24a59885fa184228e8fd29c87efee48fd5ac8 util.h
100644 blob 55882 b5f3f04c104785a57d8280c37c1b19b36068e56e utlist.h
100644 blob 11555 867442c843dbe6bf096a488e3ce9ec6323809f7f utstring.h
Hints:
Before first commit, do not forget to setup your git environment:
git config --global user.name "your_name_here"
git config --global user.email "your@email_here"

Clone this repository using HTTP(S):
git clone https://rocketgit.com/user/gdr/tuntox

Clone this repository using ssh (do not forget to upload a key first):
git clone ssh://rocketgit@ssh.rocketgit.com/user/gdr/tuntox

Clone this repository using git:
git clone git://git.rocketgit.com/user/gdr/tuntox

You are allowed to anonymously push to this repository.
This means that your pushed commits will automatically be transformed into a merge request:
... clone the repository ...
... make some changes and some commits ...
git push origin main