简体   繁体   English

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

[英]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 .我猜它与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.我想知道如何将 switch case 菜单修复到您可以轻松输入数字选项的位置,然后让代码输出您选择的任何选项。

So for example:例如:

if I chose option 1 it would output a SYN flood to an IP.如果我选择option 1 ,它将向 IP 输出 SYN 洪水。

That's kinda the whole point of the switch case menu.这就是 switch case 菜单的重点。 Thanks谢谢

The errors you posted are linker errors, caused by the declarations of the functions flood() , dos() , and scan() not having any definitions.您发布的错误是链接器错误,由函数flood()dos()scan()的声明引起的没有任何定义。 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.如果您需要测试菜单,我建议为每个执行类似printf语句的操作(或您所做的,将 printf 语句放在案例中,但您最终会无论如何稍后更改它),直到您启动并运行实际的功能。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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