From 72abb9e2756d5c19f43c7f3d83f3d8eade510ede Mon Sep 17 00:00:00 2001 From: jjungo Date: Fri, 18 Dec 2015 12:43:47 +0100 Subject: [PATCH] Fix #12 Segmentation fault Problem: bad parsing argument Solution: * check the number of arguments * first argument is "-s" or "-c" This commit doesn't check if IPv4 is valid. --- icmptunnel.c | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/icmptunnel.c b/icmptunnel.c index e6c7d0e..526c044 100644 --- a/icmptunnel.c +++ b/icmptunnel.c @@ -8,14 +8,34 @@ #include #include +#define ARG_SERVER_MODE "-s" +#define ARG_CLIENT_MODE "-c" + +void usage() +{ + printf("Wrong argument\n"); + fprintf(stdout, "usage: icmptunnel [-s serverip] | [-c clientip]\n"); +} + int main(int argc, char *argv[]) { char ip_addr[100] = {0,}; - if ((strlen(argv[2]) + 1) > sizeof(ip_addr)){ - perror("Bad ip argument\n"); - exit(EXIT_FAILURE); + if ((argc < 3) || ((strlen(argv[2]) + 1) > sizeof(ip_addr))) { + usage(); + exit(EXIT_FAILURE); } memcpy(ip_addr, argv[2], strlen(argv[2]) + 1); - run_tunnel(ip_addr, !strcmp(argv[1], "-s")); + + if (strncmp(argv[1], ARG_SERVER_MODE, strlen(argv[1])) == 0) { + run_tunnel(ip_addr, 1); + } + else if (strncmp(argv[1], ARG_CLIENT_MODE, strlen(argv[1])) == 0) { + run_tunnel(ip_addr, 0); + } + else { + usage(); + exit(EXIT_FAILURE); + } + return EXIT_SUCCESS; }