/*
* filename: telnet_server.c
* DESC: Imitate the telnet login the sever point.
* Author: linjian
* E-mail: lin.jian1986@gmail.com
* Date: 2008/10/27
* VERSION: 1.0
*/
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>
#include <errno.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include "../my_error.h"
#include "telnet.h"
/*
* @name: the user name from client point
* DESC: to find the name in the server point
*/
int find_user_name( const char *name )
{
assert( name != NULL );
for ( int i = 0; users[i].username[0] != ' '; ++i )
if ( strcmp(users[i].username, name) == 0 )
return i;
return FALSE;
}
int main( int argc, char *argv[] )
{
int sockfd, connfd;
int optval;
int recv_type = USERNAME;
int client_size;
int recv_bits;
int name_index;
char buffer[ BUF_SIZE ];
pid_t pid;
struct sockaddr_in client_addr, server_addr;
/* initialize some variables */
client_size = sizeof( struct sockaddr_in );
memset( buffer, 0, BUF_SIZE );
memset( &server_addr, 0, sizeof(server_addr) );
memset( &client_addr, 0, sizeof(server_addr) );
if ( (sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0 )
my_error( "socket failed" );
/* set the option and the sockfd can rebind points */
optval = 1;
if ( setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR,
(void *)&optval, sizeof(int)) < 0 )
my_error( "setsockopt failed" );
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons( PORT );
server_addr.sin_addr.s_addr = htonl( INADDR_ANY );
if ( bind(sockfd, (struct sockaddr *)&server_addr,
sizeof(struct sockaddr_in)) < 0 )
my_error( "bind failed" );
if ( listen(sockfd, MAX_LISTEN) < 0 )
my_error( "listen failed" );
while ( 1 ) {
if ( (connfd = accept(sockfd,
(struct sockaddr*)&client_addr, &client_size)) < 0 )
my_error( "accept failed" );
printf( "Server accept a client: ip = %s\n",
inet_ntoa(client_addr.sin_addr) );
/* the child process is used to deal with the connection */
if ( (pid = fork()) == 0 ) {
while ( 1 ) {
/* recieve the string from the client point */
if ( (recv_bits =
recv(connfd, buffer, BUF_SIZE, 0)) < 0 )
my_error( "recv failed" );
/* delete the last character of the buffer string */
buffer[recv_bits - 1] = '\0';
puts(buffer);
/* the buffer is user's name */
if ( recv_type == USERNAME ) {
name_index = find_user_name( buffer );
if ( name_index == FALSE ) {
if ( send(connfd, error_user,
strlen(error_user), 0) < 0 )
my_error( "send failed" );
break;
} else {
if ( send(connfd, correct_user,
strlen(correct_user), 0) < 0 )
my_error( "send failed" );
recv_type = PASSWORD;
}
}
/* the buffer is user's password */
else if ( recv_type == PASSWORD ) {
if ( strcmp(users[name_index].password, buffer)
!= 0 ) {
if ( send(connfd, error_password,
strlen(error_password), 0) < 0 )
my_error( "send failed" );
} else {
if ( send(connfd, success_login,
strlen(success_login), 0) < 0 )
my_error( "send failed" );
}
break;
}
}
close( sockfd );
close( connfd );
exit ( 0 );
}
else
close( connfd );
}
return 0;
}
|