int buffer_message(t_client_info *client, t_client_info *list, t_channel **channels) { char buff[512]; bzero(buff, 512); if (read(client->sockfd, buff, 512) < 0) return (-1); if (strlen(client->command_buffer) + strlen(buff) > 8192) return (-1); strcpy(client->command_buffer + strlen(client->command_buffer), buff); if (client->command_buffer[strlen(client->command_buffer) - 1] == '\n') return (handle_client_connection(client, list, client->command_buffer, channels)); return (0); }
static int process_event(prelude_client_profile_t *cp, int server_sock, prelude_io_t *fd, gnutls_x509_privkey_t key, gnutls_x509_crt_t cacrt, gnutls_x509_crt_t crt) { char buf[512]; void *inaddr; socklen_t len; int ret, csock; union { struct sockaddr sa; #ifndef HAVE_IPV6 struct sockaddr_in addr; # define ADDR_PORT(x) (x).sin_port #else struct sockaddr_in6 addr; # define ADDR_PORT(x) (x).sin6_port #endif } addr; len = sizeof(addr.addr); csock = accept(server_sock, &addr.sa, &len); if ( csock < 0 ) { fprintf(stderr, "accept returned an error: %s.\n", strerror(errno)); return -1; } inaddr = prelude_sockaddr_get_inaddr(&addr.sa); if ( ! inaddr ) return -1; inet_ntop(addr.sa.sa_family, inaddr, buf, sizeof(buf)); snprintf(buf + strlen(buf), sizeof(buf) - strlen(buf), ":%u", ntohs(ADDR_PORT(addr.addr))); prelude_io_set_sys_io(fd, csock); fprintf(stderr, "\nConnection from %s...\n", buf); ret = handle_client_connection("", cp, fd, key, cacrt, crt); if ( ret == 0 ) fprintf(stderr, "%s successfully registered.\n", buf); prelude_io_close(fd); return ret; }
int main(int argc, char *argv[]) { // Clear all invalid data memset((char*) &serverhost, 0, sizeof(serverhost)); // Get host information for 127.0.0.1 host_entity = gethostbyname(HOST_NAME); if (host_entity == NULL) { printf("Unable to resolve hostname %s\n", HOST_NAME); terminate(1); return 1; } // Copy host information to serverhost memcpy((char*) &serverhost.sin_addr, host_entity->h_addr_list[0], host_entity->h_length); serverhost.sin_port = htons((short) PORT); serverhost.sin_family = host_entity->h_addrtype; // Open socket for listening sockfd = socket(AF_INET, SOCK_STREAM, 0); if (sockfd < 0) { printf("Unable to open socket\n"); terminate(1); return 1; } // Try to bind to socket if (bind(sockfd, (struct sockaddr*) &serverhost, sizeof(serverhost)) < 0) { printf("Unable to bind to socket\n"); terminate(1); return 1; } // Try to listen to socket if (listen(sockfd, MAX_CONNECTIONS) < 0) { printf("Unable to listen on socket\n"); terminate(1); return 1; } while(TRUE) { // Clear all invalid data memset(&clienthost, 0, sizeof(clienthost)); // Try to accept connection with client client_sockfd = accept(sockfd, (struct sockaddr*) &clienthost, (socklen_t*) &addr_len); if (client_sockfd < 0) { printf("Unable to accept connection\n"); terminate(1); return 1; } // Timestamp the connection accept time(&seconds); timestamp = localtime(&seconds); memset(timestamp_str, 0, MAX_TIMESTAMP_LENGTH); strftime(timestamp_str, MAX_TIMESTAMP_LENGTH, "%r %A %d %B, %Y", timestamp); // Get client host client_entity = gethostbyaddr((char*) &(clienthost.sin_addr), sizeof(clienthost.sin_addr), AF_INET); if (client_entity > 0) { printf("[%s] Connection accepted from %s (%s)\n", timestamp_str, client_entity->h_name, client_entity->h_addr_list[0]); } else { printf("[%s] Connection accepted from unresolvable host\n", timestamp_str); } // Handle the new connection with a child process child = fork(); if (child == 0) { // Continuously perform GET responses until we no longer wish to keep alive while (keep_alive) { content_length = -1; cookie = FALSE; header_err_flag = FALSE; if_modified_since = NULL; time_is_valid = TRUE; content = NULL; not_eng = FALSE; acceptable_text = TRUE; acceptable_charset = TRUE; acceptable_encoding = TRUE; handle_client_connection(); if (content != NULL) free(content); } exit(0); } } return 0; }
int main(int argc, char ** argv) { int sockfd, new_fd; struct addrinfo hints, *servinfo, *p ; struct sockaddr_storage their_addr; socklen_t sin_size ; struct sigaction sa; int yes =1; //char buf[MAXDATASIZE] ; int bytes_rcv = 0 ; int bytes_snd = 0 ; char s[INET6_ADDRSTRLEN]; int rv; int wait_for_message = -1 ; //initializing variables for use in select function fd_set fdset; int client_connections[MAXCLIENTS] = {0}; int max_fd ; if(argc != 2) { printf("<usage>: ./server 5000\n"); exit(0 ); } memset(&hints,0 ,sizeof hints); //setting up the information about the type of the address to query hints.ai_family = AF_INET; hints.ai_socktype = SOCK_STREAM; hints.ai_flags = AI_PASSIVE; if((rv = getaddrinfo(NULL,argv[1],&hints,&servinfo)) != 0) { fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv)); return 1; } sockfd = socket(servinfo->ai_family,servinfo->ai_socktype,servinfo->ai_protocol); if (sockfd == -1) { printf("could not create socket\n"); return 1; } if (setsockopt(sockfd,SOL_SOCKET,SO_REUSEADDR, &yes,sizeof (int)) == -1) { printf("setsockopt\n"); exit(1); } if (bind(sockfd,servinfo->ai_addr, servinfo->ai_addrlen) == -1) { close(sockfd); printf("socket:bind\n"); exit(1); } freeaddrinfo(servinfo); //done with the job of this structure if(listen(sockfd,BACKLOG) == -1) { printf("listen\n"); exit(1); } // initializeFD(fdset,sockfd,client_connections); sin_size = sizeof their_addr ; // max_fd = sockfd ; int i = 0 ; //counter variable int sd ; // temporary holdder int clients_to_read = 0 ; printf("Starting select loop, fdmax %d\n",sockfd) ; while(1) { /* adding logic for using select function*/ FD_ZERO(&fdset) ; FD_SET(sockfd,&fdset) ; max_fd = sockfd ; for(i = 0; i <MAXCLIENTS ;i ++) { sd = client_connections[i]; if(sd >0) { FD_SET(sd,&fdset); } if (max_fd < sd) { max_fd = sd; } } clients_to_read = select (max_fd +1 ,&fdset,NULL,NULL,NULL) ; // wait till request arrives //if (clients_to_read > 0) if (FD_ISSET(sockfd, &fdset) != 0) { new_fd = accept(sockfd, (struct sockaddr *)&their_addr, &sin_size); if (new_fd == -1) { printf("accept error\n"); continue ; } inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s); printf("server: got connection from %s and socket %d\n", s,new_fd); // adding new connection to clients array for (i = 0 ; i<MAXCLIENTS; i++) { if (client_connections[i] == 0) { client_connections[i] = new_fd ; break ; } } } for(i = 0 ; i< MAXCLIENTS ; i++) { if (FD_ISSET(client_connections[i], &fdset) !=0) { printf("In select loop: socket %d is ready to read\n", client_connections[i]) ; handle_client_connection(client_connections[i],client_connections,i) ; } } } return 0 ; }