bproxy/src/args.c

122 lines
3.4 KiB
C

// This is a personal academic project. Dear PVS-Studio, please check it.
// PVS-Studio Static Code Analyzer for C, C++ and C#: http://www.viva64.com
#include <stdio.h>
#include <stdlib.h>
#include <limits.h>
#include <string.h>
#include <errno.h>
#include <getopt.h>
#include "args.h"
static unsigned short port(const char *s) {
char *end = 0;
const long sl = strtol(s, &end, 10);
if (end == s || '\0' != *end || ((LONG_MIN == sl || LONG_MAX == sl) && ERANGE == errno) || sl < 0 || sl > USHRT_MAX) {
fprintf(stderr, "Port should be in the range of 1-65536: %s\n", s);
exit(1);
return 1;
}
return (unsigned short) sl;
}
static void user(char *s, struct users *user) {
char *p = strchr(s, ':');
if (p == NULL) {
fprintf(stderr, "Password not found\n");
exit(1);
} else {
*p = 0;
p++;
user->name = s;
user->pass = p;
}
}
static void version(void) {
fprintf(stderr, "BProxy v0.0\nITBA Protocolos de Comunicación 2021/1 -- Grupo 7\nVer LICENSE.md\n");
}
static void usage(const char *progname) {
fprintf(stderr,
"Usage: %s [OPTION]\n\n"
" -h Imprime la ayuda y termina.\n"
" -l <SOCKS addr> Dirección donde servirá el proxy SOCKS.\n"
" -L <conf addr> Dirección donde servirá el servicio de management.\n"
" -p <SOCKS port> Puerto entrante conexiones SOCKS.\n"
" -P <conf port> Puerto entrante conexiones configuracion.\n"
" -u <name>:<pass> Usuario y contraseña de usuario que puede usar el proxy. Hasta 500.\n"
" -v Imprime información sobre la versión y termina.\n\n",
progname);
exit(1);
}
void parse_args(const int argc, char **argv, struct socks5args *args) {
memset(args, 0, sizeof(*args));
args->socks_addr = "0.0.0.0";
args->socks_port = 1080;
args->mng_addr = "127.0.0.1";
args->mng_port = 8080;
args->disectors_enabled = true;
int c;
int nusers = 0;
while (true) {
c = getopt(argc, argv, "hl:L:Np:P:u:v");
if (c == -1)
break;
switch (c) {
case 'h':
usage(argv[0]);
break;
case 'l':
args->socks_addr = optarg;
break;
case 'L':
args->mng_addr = optarg;
break;
case 'N':
args->disectors_enabled = false;
break;
case 'p':
args->socks_port = port(optarg);
break;
case 'P':
args->mng_port = port(optarg);
break;
case 'u':
if (nusers >= MAX_USERS) {
fprintf(stderr, "Maximun number of command line users reached: %d.\n", MAX_USERS);
exit(1);
} else {
user(optarg, args->users + nusers);
nusers++;
}
break;
case 'v':
version();
exit(0);
break;
default:
fprintf(stderr, "unknown argument %d.\n", c);
exit(1);
}
}
if (optind < argc) {
fprintf(stderr, "Argument not accepted: ");
while (optind < argc) {
fprintf(stderr, "%s ", argv[optind++]);
}
fprintf(stderr, "\n");
exit(1);
}
}