tor_multipath_voip/src/tor_ctl.c
2019-02-11 16:23:20 +01:00

80 lines
2.1 KiB
C

#include "tor_ctl.h"
int tor_ctl_connect(struct tor_ctl* ctx, char* addr, char* service) {
int sock = create_tcp_client (addr, service);
int sock2 = dup(sock);
ctx->rsock = NULL;
ctx->wsock = NULL;
ctx->rsock = fdopen(sock, "r");
if (ctx->rsock == NULL) {
return -1;
}
setbuf(ctx->rsock, NULL);
ctx->wsock = fdopen(sock2, "w");
if (ctx->wsock == NULL) {
return -1;
}
setbuf(ctx->wsock, NULL);
fprintf (ctx->wsock, "authenticate \"\"\n");
int error_code = 0;
fscanf (ctx->rsock, "%d", &error_code);
if (error_code != 250) {
tor_ctl_close (ctx);
return -1;
}
fscanf(ctx->rsock," OK");
return 0;
}
int tor_ctl_add_onion(struct tor_ctl* ctx, struct tor_os_str* tos, uint16_t* port) {
int err = 0;
char buffer1[1024] = {0};
char buffer2[1024] = {0};
int to_create = tos->size - tos->filled;
/* Add onion services loaded from file */
for (int i = 0; i < tos->filled; i++) {
fprintf(ctx->wsock, "add_onion %s Port=%d\n", tos->keys[i].priv, port[i]);
fscanf(ctx->rsock, "%d", &err);
if (err != 250) {
printf("err: %d\n", err);
return -1;
}
fscanf(ctx->rsock, "-ServiceID=%s\n", buffer1);
printf("Added onion service %s.onion from file\n", buffer1);
fscanf(ctx->rsock, "250 OK");
}
/* Complete by creating new onion services */
for (int i = tos->filled; i < tos->size; i++) {
fprintf(ctx->wsock, "add_onion NEW:ED25519-V3 Port=%d\n", port[i]);
fscanf(ctx->rsock, "%d", &err);
if (err != 250) return -2;
err = fscanf(ctx->rsock, "-ServiceID=%s\n", buffer1);
if (err <= 0) return -3;
printf("Created onion service %s.onion\n", buffer1);
err = fscanf(ctx->rsock, "250-PrivateKey=%s\n", buffer2);
if (err <= 0) return -4;
//printf("Onion service private key: %s\n", buffer);
if (tor_os_append(tos, buffer1, buffer2) != 0) return -5;
err = fscanf(ctx->rsock, "250 OK");
if (err < 0) return -6;
}
if (to_create > 0) {
tor_os_persist(tos);
}
return 0;
}
void tor_ctl_close(struct tor_ctl* ctx) {
fclose(ctx->rsock);
fclose(ctx->wsock);
}