如何为我的 FTP 服务器检查 <CRLF>

How to check for a <CRLF> for my FTP server

我创建了一个小型 FTP 服务器并且有一些可用的命令。

以下是我如何检查用户的输入以查看它是否与我的链接列表中的命令之一匹配:

int check_cmd(char *buff, char *cmd)
{
  int end;

  if (strstr(buff, cmd) != buff)
    return (-1);
  end = strlen(cmd);
  if (buff[end] != '[=12=]' && buff[end] != ' ' && buff[end] != '\n')
    return (-1);
  return (0);
}

void read_command(t_client *client, t_cmd *lexer)
{
  t_cmd *current;

  bzero(client->buff, MAX_READ + 1);
  server_read(client);
  current = lexer;
  while (current != NULL) // Go through the linked list, checking if it matches
    {
      if (check_cmd(client->buff, current->cmd) == 0) // It matches a command !
        {
          current->ptr(client); // Calls the appropriate function
          return ;
        }
      current = current->next;
    }
  server_write(client, "Invalid command.\n");
}

但使用 netcat-C 选项将在每个命令中默认发送 \r\n,但我没有检查它。

如何检查 <CRLF> 是否通过命令行传递?

我首先看到的是结尾实际上应该是:

end = strlen(cmd) - 1;

因为 C 中的数组是通过以 0 而不是 1 开头的索引访问的。

要检查字符串末尾的 a,确定 end - 1 是否为“\r”且 end 是否为“\n”:

if(buff[end-1] == '\r' && buff[end] == '\n')
{
  // do something
}