Skip to content
Browse files

Version 1

  • Loading branch information...
1 parent ed25e05 commit 4336304d6b2fa7b752e0f0aa503658b0e3d7d27f Amaury Brisou committed
Showing with 0 additions and 618 deletions.
  1. +0 −362 Own_lib.c~
  2. 0 citations.txt~
  3. +0 −127 common.c~
  4. +0 −84 functions.h~
  5. +0 −1 save
  6. +0 −44 server.c~
View
362 Own_lib.c~
@@ -1,362 +0,0 @@
-#include "functions.h"
-
-
-void server(long int port){
-
- int listnum;
- struct timeval timeout; /* Timeout for select */
- int readsocks;/* Number of sockets ready for reading */
-
-
-
-
- sprintf(message, "Launching server using port : %ld", port); /*LOG_INFO to syslog */
- fatal(1, message);
- memset(message, 0, MAX);
-
- /* Obtain a file descriptor for our "listening" socket */
- sock = socket(AF_INET, SOCK_STREAM, 0);
- if (sock < 0) {
- fatal(0,"socket");
- exit(EXIT_FAILURE);
- }
-
-
- /* Set socket to non-blocking with our set_non_blocking routine */
- set_non_blocking(sock);
-
-
- sock = create_socket_uni_tcp_listen(port);
- if( sock == -1){
- fatal(0, "Error listening socket");
- }
- fatal(1,"Server bound");
-
- /* Since we start with only one socket, the listening socket,
- it is the highest socket so far. */
-
- highsock = sock;
- memset((char *) &connectlist, 0, sizeof(connectlist));
- curnb_user = 0; /* set current connected user to 0 */
-
- while (1) { /* Main server loop */
-
-
-
- t_cur = time(NULL);
-
-
- for( listnum = 0; listnum < curnb_user; listnum++){
- if( ((t_cur - (connectlist[listnum].t_con)) > MAX_TIMEOUT) && (connectlist[listnum].fd != 0)){ /* every 2mn, admit all users disconnected ( they'll have to tell us they're still alive "); */
-
- warning_disconnection(connectlist[listnum].fd, connectlist[listnum].name, connectlist[listnum].address);
- disconnect(&connectlist[listnum]);
-
- }
- }
-
- build_select_list();
-
-
-
- timeout.tv_sec = 5;
- timeout.tv_usec = 0;
-
-
- /* set server aware of incoming datas/connections */
-
- readsocks = select(highsock+1, &socks, (fd_set *) 0,
- (fd_set *) 0, &timeout);
-
-
- if (readsocks < 0) {
- fatal(0,"select");
-
- } else {
- read_socks();
- }
- } /* while(1) */
-}
-
-
-
-void handle_new_connection() {
- /* This function aims to be sure that Client has a username and test if it doesn't already exists before going on creating connectlist entry ( including : socket file descriptor, name, IP address and filename to log all activity */
-
- int listnum;
- int connection;
- int isHere = 0;
-
-
- /* accept temporary connection */
- connection = accept(sock, NULL, NULL);
-
- if (connection < 0) {
- fatal(0,"accept");
- }
-
- /* get client wanted name in temp_name ( we'll use many times this var later )*/
- memset(temp_name, 0, NAMESIZE);
-
- if ( read_from(connection,temp_name, NAMESIZE) <= 0 ) {
- fatal(0, "recv_from");
- }
-
- temp_name[NAMESIZE-1] = '\0';
- /* check if client wanted name doesn't exists yet in connectlist array of structure*/
- for (listnum = 0; (listnum < curnb_user) && (connection != -1); listnum ++) {
- if ( strcmp( temp_name, connectlist[listnum].name) == 0) {
-
- send_to(connection, "Username already exists or too long\n");
- isHere = 1;
- }
-
- }
-
-
-
-
-
-
- if( isHere == 0){
- /* set non-blocking this new socket in order to let the server handles other connections */
- set_non_blocking(connection);
- /* Finally, Create a complete entry in the conectlist array of user structure */
- for (listnum = 0; (listnum < NB_USER) && (connection != -1); listnum ++)
- if (connectlist[listnum].fd == 0 ) {
-
-
- sprintf(message, "Connection of %s accepted: FD=%d; Slot=%d", temp_name, connection, listnum); /* server side */
- fatal(1, message);
- memset(message, 0, MAX);
-
-
- curnb_user++; /* one more user; */
-
- connectlist[listnum].t_con = time(NULL); /* set time of connection */
-
-
-
- connectlist[listnum].fd = connection; /* set connectlist file descriptor in ordre to send data to this user later */
-
- if( get_time(&t_cur) < 0){
- fatal(0,"strftime");
- }
-
- strtok( temp_name, "\r\n"); /* supress odd CR CF if client is telnet for example. */
-
- strncpy( connectlist[listnum].name, temp_name,strlen( temp_name)); /* copy the username */
-
-
-
-
-
- strncpy(connectlist[listnum].address, getIP(connection), 20); /* copy the IP address */
-
-
- warning_connection(connectlist[listnum].name, connectlist[listnum].address); /* finally warn everybody that <temp_name> has just connect from the connection IP address */
-
-
- sleep(1);
- get_citation();
- send_to(connection, message); /* send proverbe */
- memset(message,0,MAX);
-
-
-
- connection = -1;
-
-
- }
- if (connection != -1) {
- /* No place left in the queue! */
- fatal(1,"No room left for new client");
- close(connection);
-
- }
- }
-
-} /* handle_new_connection */
-
-void warning_connection(char* name, char* address ) {
-
- /* This function aims to warn all connected users that someone connects, the message includes
- the username and his IP address, all of this being sent to all users defined in connectlist structure */
-
- int listnum;
-
- /* Create message; ex : Amaury Connects : IP : 127.0.0.1<LF> */
- sprintf(message,"%s Connects : IP : %s\n", name, address);
-
- /* this bloc browses users defined in connectlist structure with listnum index. Warns each of them about this new mate arrival !*/
- for (listnum = 0; listnum < NB_USER; listnum++) {
- if(connectlist[listnum].fd != 0){
-
- send_to(connectlist[listnum].fd, message);
-
- }
- }
- fatal(1,message);
-
- /* reset Buffer content for late use */
- memset(message,0,MAX);
-}
-
-void warning_disconnection(int fd, char* name, char* address) {
-
- /* Exactly the same function as warning_connection() one. It just has a reverse role. */
- int listnum;
-
- sprintf(message,"%s Disconnects : IP : %s\n", name, address);
- fatal(1, message);
-
-
- for (listnum = 0; listnum < NB_USER; listnum++) { /* warning all chatter that someone's just disconnect */
- if(connectlist[listnum].fd != 0 && connectlist[listnum].fd != fd){
-
- send_to(connectlist[listnum].fd, message);
-
- }
-
- }
- memset(message,0,MAX);
-}
-
-
-void set_non_blocking(int sock)
- /* This function is dedicated to set socket non-blocking */
-{
- int opts;
-
- opts = fcntl(sock,F_GETFL);
- if (opts < 0) {
- fatal(0,"fcntl(F_GETFL)");
- exit(EXIT_FAILURE);
- }
- opts = (opts | O_NONBLOCK);
- if (fcntl(sock,F_SETFL,opts) < 0) {
- fatal(0,"fcntl(F_SETFL)");
- exit(EXIT_FAILURE);
- }
- return;
-} /* Set_Non_BLocking */
-
-int disconnect( struct user* user){
-
-
-
-
- user->t_con = 0;
- strncpy(user->name, "\00",NAMESIZE);
- strncpy(user->address, "\00", 20);
-
- curnb_user--;
- close(user->fd);
- user->fd = 0;
- return(0);
-}
-void deal_with_data(int listnum ) { /* Current item in connectlist for for loops */
-
- int temp_fd;
-
- char buffer[MAX]; /* Buffer for socket reads */
- char buf[MAX+30];
-
- memset(buffer,0,MAX);
- memset(buf,0,MAX+30);
- memset( temp_name, 0, NAMESIZE);
-
- if (((read_from(connectlist[listnum].fd,buffer, MAX)) <= 0)) {
- /* Connection closed, close this end
- and free up entries in connectlist */
- /*warning_disconnection(connectlist[listnum].name, connectlist[listnum].address);*/
- disconnect(&connectlist[listnum]);
-
- } else {
- /* We got some data, lets send them and save them to files*/
-
-
- temp_fd = connectlist[listnum].fd;
-
-
-
-
-
-
- if( ((strlen(buffer) == 0))){
-
-
- connectlist[listnum].t_con = t_cur;
-
-
- } else {
- if( (strlen(buffer) == 1) && (strcmp(buffer, "w") == 0)){
-
- send_to(temp_fd, "[#] User List\n");
- for (listnum = 0; listnum < NB_USER; listnum++) {
- if(connectlist[listnum].fd != 0){
- memset(buf,0,MAX+30);
- sprintf(buf, "[%d]: %s IP : %s\n", listnum, connectlist[listnum].name, connectlist[listnum].address);
- sleep(1);
- send_to(temp_fd, buf);
-
- }
- }
-
- } else {
- get_time(&t_cur);
- strncpy(temp_name , connectlist[listnum].name, strlen(connectlist[listnum].name));
- sprintf(buf,"%s : %s : %s\n", cur_time, temp_name, buffer);
-
- for (listnum = 0; listnum < NB_USER; listnum++) {
- if(connectlist[listnum].fd != 0){
-
- send_to(connectlist[listnum].fd, buf);
- }
- }
-
- memset(buffer,0,MAX);
- memset(buf,0,MAX+30);
- }
- }
-
-
- }
-
-} /* deal_with_data */
-
-
-
-
-void read_socks() {
- int listnum;
-
- if (FD_ISSET(sock,&socks)){
- handle_new_connection();
- }
-
-
- for (listnum = 0; listnum < NB_USER; listnum++) {
- if (FD_ISSET(connectlist[listnum].fd,&socks)){
- deal_with_data(listnum);
- }
- }
-
-
-} /* Read_socks */
-
-
-
-
-
-void fatal(int i, char *message) {
-
- openlog( "chat_server", LOG_PID, LOG_USER);
- if( i == 0){
- syslog(LOG_ERR, "%s : %s", message, strerror(errno));
- } if( i == 1){
- syslog(LOG_INFO, "%s",message);
- }
-}
-
-
View
0 citations.txt~
No changes.
View
127 common.c~
@@ -1,127 +0,0 @@
-#include "functions.h"
-
-
-
-int create_socket_uni_tcp_listen (int port)
-{
-
- struct sockaddr_in host_addr;
- int socket_listen_fd, reuse_addr = 1;
-
- if((socket_listen_fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
- fatal(0, "socket");
-
- if(setsockopt(socket_listen_fd, SOL_SOCKET, SO_REUSEADDR, &reuse_addr, sizeof(reuse_addr)) < 0)
- fatal(0, "setsockopt");
-
-
-
-
- host_addr.sin_family = AF_INET;
- host_addr.sin_port = htons(port);
- host_addr.sin_addr.s_addr = INADDR_ANY;
- memset(&(host_addr.sin_zero), '\0', 8);
-
-
- if(bind( socket_listen_fd, (struct sockaddr *)&host_addr, sizeof(struct sockaddr)) == -1)
- fatal(0,"bind");
-
- if(listen(socket_listen_fd,NB_USER))
- fatal(0,"listen");
-
-
- return socket_listen_fd;
-
-}
-
-void build_select_list() {
- int listnum;
-
- FD_ZERO(&socks);
-
-
- FD_SET(sock,&socks);
-
-
-
- for (listnum = 0; listnum < NB_USER; listnum++) {
- if (connectlist[listnum].fd != 0) {
- FD_SET(connectlist[listnum].fd,&socks);
- if (connectlist[listnum].fd > highsock)
- highsock = connectlist[listnum].fd;
- }
- }
-}
-
-int get_citation(){
- int i = 0;
- int s = rand() % 177;
- FILE *fd;
-
-
- printf("%d",s);
- fd = fopen("citations.txt","r");
- while( (fread(message, MAX, 1, fd )) != 0 && i != s)
- {
- i++;
- memset(message,0,MAX);
- }
- fclose(fd);
- return(1);
-}
-
-int read_from( int fd, char* buffer, int len)
-{
-
- struct sockaddr_in *client_addr;
- socklen_t sin_size;
- int length;
-
-
- sin_size = sizeof(struct sockaddr_in);
-
- client_addr = (struct sockaddr_in *)malloc(sin_size);
-
- if((length = recvfrom(fd, buffer, len, 0, (struct sockaddr *)client_addr, &sin_size)) < 0){
- fatal(0,"recvfrom");
- return(-1);
- }
-
- buffer[MAX-1] = '\0'; /* ensure end string char is in our buffer */
-
- return(length);
-} /* Read_from */
-
-
-
-int get_time(time_t* t)
-{
-
- return strftime(cur_time, TIMESIZE, "%H:%M", localtime(t));
-}
-
-
-void send_to(int fd, char* buffer)
-{
-
-
- if(write(fd, buffer, strlen(buffer)) == 0)
- fatal(0, "write");
-
-} /* send_to*/
-
-
-char* getIP(int connection){
-
- struct sockaddr_in adresse;
- socklen_t longueur;
-
- longueur = sizeof( struct sockaddr_in);
- /* get Client Ip address */
- if((getpeername(connection, (struct sockaddr *)& adresse, & longueur)) < 0){
- fatal(0, "getsockname");
- }
-
- return inet_ntoa(adresse.sin_addr);
-
-}
View
84 functions.h~
@@ -1,84 +0,0 @@
-#define _GNU_SOURCE
-#include <stdio.h>
-#include <stdlib.h>
-#include <string.h>
-#include <sys/types.h>
-#include <sys/socket.h>
-#include <netinet/in.h>
-#include <arpa/inet.h>
-#include <unistd.h> /* close */
-#include <netdb.h> /* gethostbyname */
-#include <sys/time.h>
-#include <errno.h>
-#include <signal.h>
-#include <ctype.h>
-#include <sys/time.h>
-#include <fcntl.h>
-#include <sys/wait.h>
-#include <time.h>
-#include <stdarg.h>
-#include <syslog.h>
-#include <limits.h>
-#include <inttypes.h>
-
-
-
-#define MAX_TIMEOUT 120 /* timeout in s */
-
-#define MAX 1024
-#define NB_USER 100
-#define NAMESIZE 12
-#define TIMESIZE 24
-
-
-#define OPT "p:"
-
-char message[MAX];
-
-time_t t_cur;
-
-char cur_time[TIMESIZE];
-
-struct user{
- int fd;
- char name[NAMESIZE];
- char address[20];
-
- long t_con;
-}users;
-
-int sock;
-
-int curnb_user;
-
-
-
-
-struct user connectlist[NB_USER]; /* Array of connected sockets so we know who
- we are talking to */
-fd_set socks; /* Socket file descriptors we want to wake
- up for, using select() */
-int highsock;
-
-
-char temp_name[NAMESIZE];
-
-void get_citation();
-int disconnect( struct user * connectlist);
-void server(long int port);
-char* getIP(int connection);
-int get_time(time_t* t);
-void warning_disconnection(int fd, char* name, char* address);
-void warning_connection(char* name, char* address);
-void build_select_list();
-void deal_with_data(int listnum ) ;
-void set_non_blocking(int sock);
-void handle_new_connection();
-void fatal(int i, char *message);
-void read_socks();
-int read_from( int fd, char* buffer, int len);
-void send_to( int cli_fd, char* buffer);
-int create_socket_uni_tcp_listen (int port);
-int create_socket_uni_tcp(int port);
-
-
View
1 save
@@ -1 +0,0 @@
-Amaury Connects : IP : 192.168.1.254
View
44 server.c~
@@ -1,44 +0,0 @@
-#include "functions.h"
-
-
-int main (int argc, char* argv[]) { /* int argc, char** argv */
-
- int i,c;
- char* fin;
- long int port;
-
- signal(SIGPIPE, SIG_IGN);
-
-
- if (argc < 2) {
- printf("Usage: %s -p <port>\r\n",argv[0]);
- exit(0);
- }
- port = 9500;
- while((c = getopt(argc, argv, OPT)) != -1){
- switch(c){
- case 'p': /* get local listening port */
- port = strtol(optarg, &fin, 10);
- if( fin == optarg ){
- fatal(0,"strtol");
-
- }
- if( ((port == LONG_MAX) || (port == LONG_MIN)) && (errno == ERANGE) ) {
- fatal(0,"Débordement");
- }
- break;
- }
- }
-
- /* chdir("/"); */
- if( fork() != 0 )
- exit(0);
-
- for ( i = 0; i < 255; i++)
- close(i);
- server(port);
-
- return(0);
-
-} /* main */
-

0 comments on commit 4336304

Please sign in to comment.
Something went wrong with that request. Please try again.