I am trying to send data / read data from socket fds with
write() and read() functions.
The problem is that one of them are not working correctly, I explain:
write() sends data to "sockfd" -> "sockfd" inserts data to "answer" variable with read() function
the problem is that when I try to get the content of answer (with the usual printf("%s\n",answer) ), it is empty.
I don't know if the problem comes from read() or write(), if both of them fail, they return -1 so I added a simple check to see if one of them return < 0.
Below the code:
This one is the sender, problem comes with the last write() call [This: if (write(client_sockfd,denied,sizeof(denied)) < 0)]
if(login_required(client_ipaddr) == 1)
{
char getpasswd[256];
char auth[256]="auth-required";
char granted[9]="granted";
char denied[7]="denied";
//checks that password file exists!
//check_passwd_file("passfile",client_sockfd);
log_string("[CLIENT-AUTH]Authentication required! ...");
write(client_sockfd,auth,sizeof(auth));
read(client_sockfd,getpasswd,sizeof(getpasswd));
//log_string(getpasswd);
//check here
if(strcmp(getpasswd,"jasmtest") == 0)
{
log_string("[PWD][OK]Password accepted!\n");
log_string("[PWD][OK]Authorized!\n");
}
else if(strcmp(getpasswd,"jasmtest") != 0)
{
log_error("[PWD][DEN]Wrong password!\n");
log_error("[PWD][DEN]Closing connection...\n");
if (write(client_sockfd,denied,sizeof(denied)) < 0)
log_error("[core/ipc.c][start_server()][getpasswd][write()] ERROR while sending denied\n");
}
}
else
{
write(client_sockfd,not_required,sizeof(not_required));
log_string("[CLIENT-AUTH]Authentication NOT required!");
}
Here is the "client" I explain: until write(sockfd,get_my_pass,sizeof(get_my_pass)); all is working, when i call: read(sockfd,answer,sizeof(answer)) it is not working anymore as I described above.
read(sockfd,get_msg_from_server,255);
if(strcmp(get_msg_from_server, "auth-required") == 0)
{
printf("+-----------------------------------------------------------------------+\n");
printf("* Authentication is required before accessing JASM Command Line Interface\n");
printf("* Password: \n");
write(sockfd,get_my_pass,sizeof(get_my_pass));
if(read(sockfd,answer,sizeof(answer)) < 0)
printf("* Error on read\n");
if(strcmp(answer,"denied") == 0)
{
printf("* Non-authorized access!!\n");
printf("* Exiting...\n");
close(sockfd);
exit(3);
}
}
else if(strcmp(get_msg_from_server, "auth-not-required") == 0)
printf("* Authentication is not required for this session\n");
Thanks to all of u :)
errno
when the one write you check returns with an error? – Erikerrno
as an indication of why it failed. Theperror
function is a relatively simple way to get this information to begin with. – Erik