如何在C中修复此开关案例菜单错误?

时间:2015-06-14 06:10:51

标签: c menu switch-statement

首先让我告诉你代码......

#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;
}

好吧我在输出

时遇到了这个问题
"_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)

我猜测它与switch case menu有关。此菜单的重点是让用户与选项进行交互(显然)。

我想知道如何将开关案例菜单修改到您可以轻松输入数字选项的位置,然后让代码输出您选择的任何选项。

例如:

如果我选择option 1,它会向IP输出SYN泛洪。

这就是切换案例菜单的重点。感谢

1 个答案:

答案 0 :(得分:2)

您发布的错误是链接器错误,这是由函数flood()dos()scan()的声明引起的,没有任何定义。如果您需要测试菜单,我建议为每个执行类似printf语句的内容定义“占位符”函数(或者您执行的操作,将printf语句放在案例中,但是不管怎么说,最后会改变它,直到你开始运行实际的功能。