Fixed regex validation

This commit is contained in:
2017-09-05 20:27:07 -05:00
parent 07957aff00
commit 2f4f8c1a02
2 changed files with 18 additions and 19 deletions

View File

@@ -69,10 +69,9 @@ int main (int argc, char* argv[])
SOCKET master , new_socket , client_socket[30] , s;
struct sockaddr_in server, address;
int max_clients = 30 , activity, addrlen, i, valread;
char *message = "ECHO Daemon v1.0 \r\n";
//size of our receive buffer, this is string length.
int MAXRECV = 1024;
int MAXRECV = 16;
//set of socket descriptors
fd_set readfds;
//1 extra for null character, string termination
@@ -163,13 +162,13 @@ int main (int argc, char* argv[])
//inform user of socket number - used in send and receive commands
printf("New connection , socket fd is %d , ip is : %s , port : %d \n" , new_socket , inet_ntoa(address.sin_addr) , ntohs(address.sin_port));
//send new connection greeting message
/*//send new connection greeting message
if( send(new_socket, message, strlen(message), 0) != strlen(message) )
{
perror("send failed");
}
puts("Welcome message sent successfully");
puts("Welcome message sent successfully");*/
//add new socket to array of sockets
for (i = 0; i < max_clients; i++)
@@ -229,33 +228,33 @@ int main (int argc, char* argv[])
{
//add null character, if you want to use with printf/puts or other string handling functions
buffer[valread] = '\0';
printf("%s:%d - %s \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port), buffer);
// the 'buffer' is the string to use to edit our card0.txt file
// the 'buffer' is the string to use to edit our card0.txt file
std::regex cardRegex("[e,E]004[(0-9)(a-f)(A-F)]{12}"); // 16 digit hexadecimal starting with e004
if(std::regex_match(buffer, cardRegex))
{
printf("Recieved by: %s:%d contains: %s \n" , inet_ntoa(address.sin_addr) , ntohs(address.sin_port), buffer);
std::cout << "Data recieved is a valid game card" << std::endl;
}
else // don't do anything if not valid
{
std::cout << "Data recieved is not a valid game card" << std::endl;
}
// check if the check file exists
//if(std::ifstream(check))
//{
// check file exists, rewrite
// regex to validate buffer is a e004 hex number
std::regex cardRegex("[eE]004[0-9a-fA-F]{12}");
if(std::regex_match(buffer, cardRegex))
{
std::cout << "Data recieved is a valid game card" << std::endl;
}
else
{
std::cout << "Data recieved is not a valid game card" << std::endl;
}
//}
//else
//{
// no check file
// std::cout << "No check file! Will not re-write: " << file << std::endl;
//}
send( s , buffer , valread , 0 );
//send( s , "recieved" , valread , 0 );
}
}
}

Binary file not shown.