Hi,
I'm writing a client-server socket program. the client will be an instance of the well-known telnet application. i want to implement a simple authentication between the server and the client.
- the client should send this message (after the connection established): my password "anypassword"
and the server will check, if it's not the same password that's hardcoded in the server program, the server close connection.
so the client will be like that:
1.telnet server
2. got connected
3. provide password, if it's not coreect server close connection
here is my server application:
Code:
#include <unistd.h>
#include <stdio.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/wait.h>
#include <signal.h>
#include <ctype.h>
#include <time.h>
#define SERVERPORT 111 /* port used for the connection */
#define QUEUE 5 /* max # of queued connects */
int main(void)
{
fd_set master; // master file descriptor list
fd_set read_fds; // temp file descriptor list for select()
int fdmax; // maximum file descriptor number
int i , j;
int nbytes;
int s_listen,d_newconn;
int yes=1;
struct sockaddr_in serveraddr; /* server address */
struct sockaddr_in clientaddr; /* client address */
struct sigaction sig;
socklen_t sin_size; /* Socket address length type */
string buffer[256];
FD_ZERO(&master); // clear the master and temp sets
FD_ZERO(&read_fds);
if ((s_listen = socket(AF_INET, SOCK_STREAM, 0)) == -1) { /* create listen socket */
perror("socket");
exit(1);
}
/* set the options for the socket to allow reuse of a local port/address combination. */
if (setsockopt(s_listen,SOL_SOCKET,SO_REUSEADDR,&yes,sizeof(int)) == -1) {
perror("setsockopt");
exit(1);
}
memset(&(serveraddr.sin_zero), '\0', 8); /* clear our address */
serveraddr.sin_family = AF_INET; /* Address Family */
serveraddr.sin_port = htons(SERVERPORT); /* Port number */
/* a wild IP number to allow the system to pick the route to the remote service */
serveraddr.sin_addr.s_addr = INADDR_ANY;
/* bind address to socket */
if (bind(s_listen, (struct sockaddr *)&serveraddr, sizeof(struct sockaddr)) == -1) {
perror("bind");
exit(1);
}
/*listen*/
if (listen(s_listen, QUEUE) == -1) {
perror("listen");
exit(1);
}
// add the listener to the master set
FD_SET(s_listen, &master);
// keep track of the biggest file descriptor
fdmax = s_listen; // so far, it's this one
// main loop
for(;;) {
read_fds = master; // copy it
if (select(fdmax+1, &read_fds, NULL, NULL, NULL) == -1) {
perror("select");
exit(1);
}
// run through the existing connections looking for data to read
for(i = 0; i <= fdmax; i++) {
if (FD_ISSET(i, &read_fds)) { // we got one!!
if (i == s_listen) {
// handle new connections
sin_size = sizeof(clientaddr);
if ((d_newconn = accept(s_listen, (struct sockaddr *)&clientaddr,
&sin_size)) == -1) {
perror("accept");
} else {
FD_SET(d_newconn, &master); // add to master set
if (d_newconn > fdmax) { // keep track of the maximum
fdmax = d_newconn;
}
printf("EDMTS SERVER: new connection from %s on "
"socket %d\n", inet_ntoa(clientaddr.sin_addr), d_newconn);
}
} else {
// handle data from a client
if ((nbytes = recv(i, buffer, sizeof(buffer), 0)) <= 0) {
// got error or connection closed by client
if (nbytes == 0) {
// connection closed
printf("SERVER: socket %d hung up\n", i);
} else {
perror("recv");
}
close(i); // bye!
FD_CLR(i, &master); // remove from master set
} else {
/*
{{{{{{{{{{{{{{{{{{{{{{ NOT COMPLETED }}}}}}}}}}}}}}}}}
{{{{HERE I SHOULD COMPARE THE CLIENT MESSAGE PASSOWRD IF IT'S THE SAME }}}}}}}}}} */
}
} // it's SO UGLY!
}
}
}
return 0;
}