简体   繁体   中英

How can I fix this switch case menu error in C?

Well first let me show you the code...

#include <stdio.h>
#include <stdlib.h>
#include <string.h>    
#include <errno.h> 
#include <sys/socket.h>    
#include <netinet/in.h> 
#include <arpa/inet.h> 
#include <unistd.h>    


void load_menu(void);
void flood(void);
void dos(void);
void scan(void);


int main(int argc, char** argv)
{

    // Ip values
    const char* google_dns_server = "192.168.1.1";
    int dns_port = 53;

    struct sockaddr_in serv;

    int sock = socket ( AF_INET, SOCK_DGRAM, 0);

    //Socket could not be created
    if(sock < 0)
    {
        perror("Socket error");
    }

    printf ("                                                      \n");
    printf ("                 __     __   ____  ____               \n");
    printf ("                (  )   (  ) (  , |(  , |              \n");
    printf ("                 )(__  )__(  ) _/  ) _/               \n");
    printf ("                (____)(_)(_)(_)   (_)                 \n");  
    printf ("                                                      \n");     
    printf ("                   Version [0.1.0]                    \n");
    printf ("            https://github.com/Abrupt/Lapp            \n");
    printf ("                                                      \n");
    printf ("                                  Written by @Abrrupt \n");

    memset( &serv, 0, sizeof(serv) );
    serv.sin_family = AF_INET;
    serv.sin_addr.s_addr = inet_addr( google_dns_server );
    serv.sin_port = htons( dns_port );

    int err = connect( sock , (const struct sockaddr*) &serv , sizeof(serv) );

    struct sockaddr_in name;
    socklen_t namelen = sizeof(name);
    err = getsockname(sock, (struct sockaddr*) &name, &namelen);

    char buffer[100];
    const char* p = inet_ntop(AF_INET, &name.sin_addr, buffer, 100);

    if(p != NULL) {
        printf("Local ip: %s \n" , buffer);
    }
    else{
        printf ("Error number: %d . Error message: %s \n" , errno , strerror(errno));
    }

    close(sock);

    load_menu();
    return 0;
}

void load_menu(void)
{
    int choice;
    int num;
    int ch;

    do
    {
    printf("+------------------------------------------------------------------+\n");
    printf("|            Commands           |          Description             |\n");
    printf("+------------------------------------------------------------------+\n");

    printf("+------------------------------------------------------------------+\n");      
    printf("|(1) SYN Flood Attack           | Create a new flood;              |\n");
    printf("|(2) DoS Attack                 | Start a DoS attack;              |\n");
    printf("|(3) Port Scan                  | Run TCP port scan                |\n");
    printf("|(4) Delete_All                 | Delete all exisitng history;     |\n");
    printf("+------------------------------------------------------------------+\n");
    scanf("%d",&choice);

        switch(choice)
        {
            case 1: flood();
                break;
            case 2: dos();
                break;
            case 3: scan();
                break;
            case 4: printf("Quitting program!\n");
                exit( 0 );
                break;
            default: printf("Invalid choice!\n");
                break;
        }

    } while (choice != 3); 

    printf("use > ");
    scanf("%d",&num);

    /* Flushes input buffer from the newline from scanf() */
    while ( (ch = getchar()) != '\n' && ch != EOF) ;

    printf("\n\nPress ENTER to continue.");
    while ( (ch = getchar()) != '\n' && ch != EOF);

    return;
}

Ok well I'm having this issue where its outputting

"_dos", referenced from:
      _load_menu in menu-20c7bc.o
  "_flood", referenced from:
      _load_menu in menu-20c7bc.o
  "_scan", referenced from:
      _load_menu in menu-20c7bc.o
ld: symbol(s) not found for architecture x86_64
clang: error: linker command failed with exit code 1 (use -v to see invocation)

I'm guessing its something to do with the switch case menu . The whole point of this menu is for users to interact with the options (obviously).

I was wondering how I can fix the switch case menu to where you can easy type the number option and then have the code output whatever option you chose.

So for example:

if I chose option 1 it would output a SYN flood to an IP.

That's kinda the whole point of the switch case menu. Thanks

The errors you posted are linker errors, caused by the declarations of the functions flood() , dos() , and scan() not having any definitions. If you need to test out the menu, I'd recommend defining "placeholder" functions for each one that do something like a printf statement (or what you did, which is put the printf statements in the cases, but you'll end up changing it later on anyway) until you get the actual functions up and running.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM