Пример #1
0
/* Like tox_bootstrap_from_address but for TCP relays only.
 *
 * return 0 on failure.
 * return 1 on success.
 */
int tox_add_tcp_relay(Tox *tox, const char *address, uint16_t port, const uint8_t *public_key)
{
    Messenger *m = tox;
    IP_Port ip_port, ip_port_v4;

    if (!addr_parse_ip(address, &ip_port.ip)) {
        if (m->options.udp_disabled) /* Disable DNS when udp is disabled. */
            return 0;

        IP *ip_extra = NULL;
        ip_init(&ip_port.ip, m->options.ipv6enabled);

        if (m->options.ipv6enabled) {
            /* setup for getting BOTH: an IPv6 AND an IPv4 address */
            ip_port.ip.family = AF_UNSPEC;
            ip_reset(&ip_port_v4.ip);
            ip_extra = &ip_port_v4.ip;
        }

        if (!addr_resolve(address, &ip_port.ip, ip_extra))
            return 0;
    }

    ip_port.port = htons(port);
    add_tcp_relay(m->net_crypto, ip_port, public_key);
    onion_add_path_node(m->onion_c, ip_port, public_key); //TODO: move this
    return 1;
}
Пример #2
0
/*
 * addr_resolve_or_parse_ip
 *  resolves string into an IP address
 *
 *  address: a hostname (or something parseable to an IP address)
 *  to: to.family MUST be initialized, either set to a specific IP version
 *     (AF_INET/AF_INET6) or to the unspecified AF_UNSPEC (= 0), if both
 *     IP versions are acceptable
 *  extra can be NULL and is only set in special circumstances, see returns
 *
 *  returns in *tro a matching address (IPv6 or IPv4)
 *  returns in *extra, if not NULL, an IPv4 address, if to->family was AF_UNSPEC
 *  returns 1 on success
 *  returns 0 on failure
 */
int addr_resolve_or_parse_ip(const char *address, IP *to, IP *extra)
{
    if (!addr_resolve(address, to, extra))
        if (!addr_parse_ip(address, to))
            return 0;

    return 1;
};
Пример #3
0
int main(int argc, char **argv) {
	char *port = "443";
	char *hostname;
	struct addrinfo *ai0, *ai;

	if(argc < 2) {
		fprintf(stderr, "Usage: %s <host> [port (= %s)] [output file]\n", argv[0], port);
		return 0;
	}

	hostname = argv[1];
	if(argc == 3)
		port = argv[2];

	if(argc == 4) {
		if(freopen(argv[3], "w", stdout) == NULL)
			return -1;
	}
	#if OPT
	/* hack for xargs */
	else {
		if(freopen(argv[1], "w", stdout) == NULL)
			return -1;
	}
	#endif

	setlocale(LC_ALL, "");
	openlog(APPNAME, LOG_PERROR, LOG_USER);
	signal(SIGPIPE, SIG_IGN);

	if((ai0 = addr_resolve(hostname, port)) == NULL)
		return 0;

	if(!strcmp(hostname, addr_ai2ip(ai0)))
		hostname = NULL;

	printf("[\n");
	for(ai = ai0; ai; ai = ai->ai_next) probe_server(ai, hostname);
	printf("]\n");

	return 0;
}
int main(int argc, char *argv[])
{
    /* let user override default by cmdline */
    uint8_t ipv6enabled = TOX_ENABLE_IPV6_DEFAULT; /* x */
    int argvoffset = cmdline_parsefor_ipv46(argc, argv, &ipv6enabled);

    if (argvoffset < 0)
        exit(1);

    if (argc < argvoffset + 4) {
        printf("Usage: %s [--ipv4|--ipv6] ip port filename\n", argv[0]);
        exit(0);
    }

    uint8_t buffer[512];
    int read;

    FILE *file = fopen(argv[argvoffset + 3], "rb");

    if (file == NULL) {
        printf("Failed to open file \"%s\".\n", argv[argvoffset + 3]);
        return 1;
    }


    /* initialize networking */
    /* bind to ip 0.0.0.0:PORT */
    IP ip;
    ip_init(&ip, ipv6enabled);

    Lossless_UDP *ludp = new_lossless_udp(new_networking(ip, PORT));
    perror("Initialization");

    IP_Port serverip;
    ip_init(&serverip.ip, ipv6enabled);

    if (!addr_resolve(argv[argvoffset + 1], &serverip.ip, NULL)) {
        printf("Failed to convert \"%s\" into an IP address.\n", argv[argvoffset + 1]);
        return 1;
    }

    serverip.port = htons(atoi(argv[argvoffset + 2]));
    printip(serverip);

    int connection = new_connection(ludp, serverip);
    uint64_t timer = current_time();

    while (1) {
        /* printconnection(connection); */
        networking_poll(ludp->net);
        do_lossless_udp(ludp);

        if (is_connected(ludp, connection) == 3) {
            printf("Connecting took: %llu us\n", (unsigned long long)(current_time() - timer));
            break;
        }

        if (is_connected(ludp, connection) == 0) {
            printf("Connection timeout after: %llu us\n", (unsigned long long)(current_time() - timer));
            return 1;
        }

        c_sleep(1);
    }

    timer = current_time();


    /*read first part of file */
    read = fread(buffer, 1, 512, file);

    while (1) {
        /* printconnection(connection); */
        networking_poll(ludp->net);
        do_lossless_udp(ludp);

        if (is_connected(ludp, connection) == 3) {

            if (write_packet(ludp, connection, buffer, read)) {
                /* printf("Wrote data.\n"); */
                read = fread(buffer, 1, 512, file);

            }

            /* printf("%u\n", sendqueue(connection)); */
            if (sendqueue(ludp, connection) == 0) {
                if (read == 0) {
                    printf("Sent file successfully in: %llu us\n", (unsigned long long)(current_time() - timer));
                    break;
                }
            }
        } else {
            printf("Connecting Lost after: %llu us\n", (unsigned long long)(current_time() - timer));
            return 0;
        }

        /* c_sleep(1); */
    }

    return 0;
}