Witam.
Zwracam się z prośbą o ocenę poniższego kodu, którego zadaniem jest wspomagać postfixa.
Kod celowo nie jest pisany obiektowo a serwer jest blokujący jednowątkowy...
#include <string>
#include <iostream>
#include <fstream>
#include <ctime>
//-------------------------------
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <arpa/inet.h>
//-------------------------------
#define LEN 10240
#define PORT 2121
#define IPNUM "127.0.0.1"
#define CONN 5
//-------------------------------
#define DUNNO "action=dunno\n\n"
#define DEFER "action=defer_if_permit Service temporarily unavailable\n\n"
#define REJECT "action=reject Incorrect email address\n\n"
//--- greylis delay sec
#define DELAY 120
int main()
{
struct sockaddr_in serwer =
{
.sin_family = AF_INET,
.sin_port = htons(PORT)
};
if(inet_pton(AF_INET, IPNUM, & serwer.sin_addr ) <= 0)
{
perror("inet_pton() ERROR");
exit(1);
}
const int socket_ = socket(AF_INET, SOCK_STREAM, 0);
if(socket_ < 0)
{
perror("socket() ERROR");
exit(2);
}
socklen_t len = sizeof(serwer);
if(bind(socket_,(struct sockaddr *) & serwer, len) < 0)
{
perror("bind() ERROR");
exit(3);
}
if(listen(socket_, CONN) < 0)
{
perror("listen() ERROR");
exit(4);
}
while(1)
{
struct sockaddr_in client = {};
const int clientSocket = accept(socket_,(struct sockaddr *) & client, & len);
if(clientSocket < 0)
{
perror("accept() ERROR");
continue;
}
char buffer[LEN] = {};
if(recv(clientSocket, buffer, sizeof(buffer), 0) <= 0)
{
perror("recv() ERROR");
exit(5);
}
//--------------------------------------------------------------------------
std::string buff; //--- cpp buff
buff = buffer; //--- copy c buffer to cpp buff
//std::cout << buff.length();
std::string line = "";
std::string sender = "";
std::string sasluname = "";
while (buff.find("\n"))
{
line = buff.substr(0, buff.find("\n"));
buff.erase (0, buff.find("\n")+1);
if (line.find("sender=") == 0)
{
sender = line.substr(7, line.length());
}
else if (line.find("sasl_username=") == 0)
{
sasluname = line.substr(14, line.length());
}
}
// std::cout << "|||" << sender << "|||" << sasluname << "|||\n";
//--- sender --- sasluname ----------------------------------------
if (sasluname != "" && sasluname == sender) //--- gdy user lokalny prawidłowy email
{
strcpy( buffer, DUNNO );
std::cout << "OK local sender" << "\n";
}
else if (sasluname != "" && sasluname != sender) //--- podszywanie się
{
strcpy( buffer, REJECT );
std::cout << "REJECT sasl|sender - " << sasluname << "|" << sender << "\n";
}
else { //--- remote user
std::cout << "remote user" << "\n";
std::time_t t = std::time(nullptr);
std::ifstream file ("grey/"+sender);
if ( file.is_open () )
{
while ( getline (file,line) )
{
if (std::stoi(line) + DELAY <= t)
{
strcpy( buffer, DUNNO );
std::cout << "GREY dunno\n";
}
else
{
strcpy( buffer, DEFER );
std::cout << "GREY defer\n";
}
}
file.close();
}
else {
std::ofstream newfile;
newfile.open ("grey/"+sender);
newfile << t;
newfile.close();
strcpy( buffer, DEFER );
std::cout << "GREY defer\n";
}
}
// strcpy( buffer, DUNNO );
if(send(clientSocket, buffer, strlen(buffer), 0) <= 0)
{
perror("send() ERROR");
exit(6);
}
shutdown(clientSocket, SHUT_RDWR);
}
shutdown(socket_, SHUT_RDWR);
}