/* * Validate the obtained NN host and ports */ static void validate_result(NNHAConf *conf) { for (int i = 0; i < conf->numn; i++) { validate_string(conf->nodes[i], "HA Namenode host number %d is NULL value", i + 1); validate_port(conf->rpcports[i], "HA Namenode RPC port number %d is NULL value", i + 1); validate_port(conf->restports[i], "HA Namenode REST port number %d is NULL value", i + 1); } }
static struct server_data * server_ref(int32_t opt_port) { struct server_data *idata, *sdata = NULL; uint16_t i, port; port = validate_port(opt_port); if (!servers) { servers = calloc(1, sizeof(struct sol_ptr_vector)); SOL_NULL_CHECK(servers, NULL); sol_ptr_vector_init(servers); } SOL_PTR_VECTOR_FOREACH_IDX (servers, idata, i) { if (idata->port == port) { sdata = idata; break; } } if (!sdata) { int r; sdata = calloc(1, sizeof(struct server_data)); SOL_NULL_CHECK_GOTO(sdata, err_sdata); r = sol_ptr_vector_append(servers, sdata); SOL_INT_CHECK_GOTO(r, < 0, err_vec); sdata->server = sol_http_server_new(port); SOL_NULL_CHECK_GOTO(sdata->server, err_server); sdata->port = port; }
/** * Validate a full IP address, including a port number (optional) * @param ip The IP address to check * @return True if all parts are valid, false otherwise */ bool validate_ip(char *ip) { char *ip_copy = (char *) calloc(strlen(ip), sizeof(char)); strcpy(ip_copy, ip); char *ip_addr = strtok(ip_copy, ":"); char *port = strtok(NULL, ":"); bool valid = validate_ip_addr(ip_addr) && validate_port(port); free(ip_copy); return valid; }
uint16_t get_port_from_args() { //returns the port, which is currently args[0] uint16_t port = 0; char const *portstring = args[1]; if (0 == (port = validate_port(portstring, port))) { fprintf(stderr, "port not set correctly, input was: %s", portstring); exit(EXIT_FAILURE); } return port; }
int main(int argc, char *argv[]){ /* initialize options */ init_options(); /* parse command line args */ int c; while((c = getopt(argc, argv, ":hve:p:lku")) != -1){ switch(c){ case 'h': help(argv); exit(EXIT_SUCCESS); case 'v': o.verbose = 1; break; case 'e': o.execcommand = 1; o.command = optarg; break; case 'p': o.port = validate_port(optarg); break; case 'l': o.listen = 1; break; case 'k': o.keepopen = 1; break; case 'u': o.udp = 1; break; case ':': bye("-%c requires argument\n", optopt); /* case ? */ default: bye("unknown option -%c\n", optopt); }/* switch */ }/* while */ /* do some checking */ /* -k specified without -l */ if(o.keepopen && !o.listen){ bye("cannot specify -k without -l\n"); } /* -p not specified with -l */ if(o.listen && !o.port){ bye("must specify -p with -l\n"); } /* get IP-address and port in case of client mode */ if(!o.listen && optind < argc){ o.target = argv[optind++]; if(optind > argc){ bye("No port specified\n"); } o.port = validate_port(argv[optind]); } else if(!o.listen){ help(argv); exit(EXIT_FAILURE); } if(o.listen) start_server(); else start_client(); return 0; }/* main() */
/** * start up a server that listens for a single client. */ void run_server(int port) { struct pkg_conn *client; int netfd; char portname[MAX_DIGITS + 1] = {0}; /* int pkg_result = 0; */ char *buffer, *msgbuffer; long bytes = 0; FILE *fp; /** our server callbacks for each message type */ struct pkg_switch callbacks[] = { {MSG_HELO, server_helo, "HELO", NULL}, {MSG_DATA, server_data, "DATA", NULL}, {MSG_CIAO, server_ciao, "CIAO", NULL}, {0, 0, (char *)0, (void*)0} }; validate_port(port); /* start up the server on the given port */ snprintf(portname, MAX_DIGITS, "%d", port); netfd = pkg_permserver(portname, "tcp", 0, 0); if (netfd < 0) { bu_bomb("Unable to start the server"); } /* listen for a good client indefinitely. this is a simple * handshake that waits for a HELO message from the client. if it * doesn't get one, the server continues to wait. */ do { client = pkg_getclient(netfd, callbacks, NULL, 0); if (client == PKC_NULL) { bu_log("Connection seems to be busy, waiting...\n"); sleep(10); continue; } else if (client == PKC_ERROR) { bu_log("Fatal error accepting client connection.\n"); pkg_close(client); client = PKC_NULL; continue; } /* got a connection, process it */ msgbuffer = pkg_bwaitfor (MSG_HELO, client); if (msgbuffer == NULL) { bu_log("Failed to process the client connection, still waiting\n"); pkg_close(client); client = PKC_NULL; } else { bu_log("msgbuffer: %s\n", msgbuffer); /* validate magic header that client should have sent */ if (!BU_STR_EQUAL(msgbuffer, MAGIC_ID)) { bu_log("Bizarre corruption, received a HELO without at matching MAGIC ID!\n"); pkg_close(client); client = PKC_NULL; } } } while (client == PKC_NULL); /* have client, will send file */ fp = fopen("lempar.c", "rb"); buffer = (char *)bu_calloc(2048, 1, "buffer allocation"); if (fp == NULL) { bu_log("Unable to open lempar.c\n"); bu_bomb("Unable to read file\n"); } /* send the file data to the server */ while (!feof(fp) && !ferror(fp)) { bytes = fread(buffer, 1, 2048, fp); bu_log("Read %ld bytes from lempar.c\n", bytes); if (bytes > 0) { bytes = pkg_send(MSG_DATA, buffer, (size_t)bytes, client); if (bytes < 0) { pkg_close(client); bu_log("Unable to successfully send data"); bu_free(buffer, "buffer release"); return; } } } /* Tell the client we're done */ bytes = pkg_send(MSG_CIAO, "DONE", 5, client); if (bytes < 0) { bu_log("Connection to client seems faulty.\n"); } /* Confirm the client is done */ buffer = pkg_bwaitfor (MSG_CIAO , client); bu_log("buffer: %s\n", buffer); /* shut down the server, one-time use */ pkg_close(client); }
int main(int argc, char **argv) { s_options _opts; opts = &_opts; /* Set global pointer */ memset(opts,'\0',sizeof(s_options)); opts->fcheck = 1; // Check files are available before setting up socket int opt = getopt( argc, argv, "Cvc:p:i:" ); while( opt != -1 ) { switch( opt ) { case 'v': opts->verbose = 1; break; case 'c': opts->count = *optarg; break; case 'C': /* Don't check if files exist before listening */ opts->fcheck = 0; break; case 'p': opts->port = validate_port(optarg); break; case 'i': opts->ifname = (char *)validate_if(optarg); if (NULL == opts->ifname) die("Copying interface name failed - %s\n",strerror(errno)); break; default: die(USAGE_MSG, argv[0]); }; opt = getopt( argc, argv, "Cvc:p:i:" ); } opts->files = argv + optind; opts->files_num = argc - optind; // Validate arguments if (! opts->files[0] || strlen(*(opts->files))==0) die("No files to send.\n" USAGE_MSG, argv[0]); if (opts->files_num > 10) die("Too many files to offer. Maximum is 10 files, got %d\n", opts->files_num); int i; size_t a = 0; size_t file_args_length = 0; for(i=0;i<opts->files_num;i++) { PRINTV("checking element %s\n",opts->files[i]); a = strlen(opts->files[i]); if ( a > FILENAME_MAXLEN) die("File name number %d is too long.\n", i); /* Check if file exists */ if ((opts->fcheck) && (! check_file(opts->files[i])) ) die( "Failed accessing %s - %s\n", opts->files[i], strerror(errno) ); file_args_length += a; // used later in http replies to calculate length }; int htsock; conn_hdls.htsock_ptr = &htsock; if ((htsock = net_init()) == FAILURE) die("Failed initializing network connection\n"); FILE *fp = NULL; // fp_ptr = &fp; conn_hdls.fp_ptr = &fp; int conn; // conn_ptr = &conn; conn_hdls.conn_ptr = &conn; /* Added to line breaks at the end of the reply, adjusted length accordingly */ size_t reply_err_len = 254 + 140 + 2*file_args_length + 27*(opts->files_num); // unsigned int reply_err_len = 254 + 138 + 2*file_args_length + 27*(opts->files_num); char reply_error[ reply_err_len ]; prep_reply_text(reply_error, reply_err_len); if (signal(SIGINT,sigint_handler) == SIG_ERR) die("Failed installing signal handler (%s)", strerror(errno)); while((conn = get_connection(htsock))) { if ((fp = fdopen(conn, "w+")) == NULL) { /* Failed open a stream for this connection, close * connection and listen for another one */ close_conn(NULL,&conn); continue; } if (FAILURE == handle_connection(fp, reply_error)) { close_conn(&fp,&conn); continue; } else { /* Success */ close_conn(&fp,&conn); break; } } //while() if (close(htsock)) fprintf(stderr, "Failed closing socket - %s", strerror(errno)); free_am(); /* Free general purpose allocated memory */ return(EXIT_SUCCESS); } // main()
int main(int argc, char **argv) { int status; char ip_addr[INET_ADDRSTRLEN]; char *service; int server_sock = -1; int exit_flag = 0; int ftp_sock = 9999; fd_set temp; /*Init. Logger*/ #ifndef MY_PC cse4589_init_log(argv[2]); #endif int sock; if(argc<3) { printf("No args\n"); return 1; } if(!validate_port(argv[2])) return 0; FD_ZERO(&wait_fd); FD_ZERO(&temp); FD_SET(0, &wait_fd); tv.tv_sec = 15; tv.tv_usec = 0; if(!strcmp(argv[1], "s")) { is_server = true; } else if(strcmp(argv[1], "c")) { printf("Usage ./sock [s/c]\n"); return 0; } set_listening_port(argv[2]); server_sock = server_start(argv[2]); if(server_sock <=0)return 0; sock = server_sock; add_fd(sock); int i =2; while(!exit_flag) { temp = wait_fd; int ret = select(active_sockets, &temp, NULL, NULL, NULL); if(ret) { if(FD_ISSET(STDIN, &temp)) { exit_flag = parse_shell(); } else if(FD_ISSET(server_sock, &temp)) { // server socket is active, check for new connections if(is_server) { int new_socket = server_accept(server_sock); add_fd(new_socket); } else { int new_socket = server_accept(server_sock); //add ftp socket add_fd(new_socket); ftp_sock = new_socket; } } else if(FD_ISSET(ftp_sock, &temp)) { client_receive_file(&ftp_sock); } else { for(int fd = 3; fd<=active_sockets; fd++) { if(FD_ISSET(fd, &temp)) { if(is_server) server_receive(fd); else client_receive(fd); } } } //End of else } //end of select handling } //end loop clear_fd(sock); close(sock); #ifndef MY_PC fclose(fopen(LOGFILE, "w")); #endif return 0; }
int parse_shell() { char shell_input[MAX_LENGTH]; char message[MAX_LENGTH]; static bool is_client_connected = false; int ret = 0; static int server_sock; /* * Get the input */ fgets(shell_input, MAX_LENGTH, stdin); if(strlen(shell_input) <= 1) return 0; shell_input[strlen(shell_input)-1] = '\0'; strcpy(message, shell_input); /* * Tokenize the strings */ char *arg; int argc = 0; char argv[4][256]; char *temp; char *command = strtok_r(shell_input, " ", &temp); //TODO reorder all commands!! // ----------------Commands common for server/client ------------ if(!strcmp("AUTHOR", command)) { print_success(1, command); LOG("I, g8, have read and understood the course academic integrity policy.\n"); } else if(!strcmp("EXIT", command)) { return 1; } // -------------------Common commands --------------------------- else if(!is_server) { char arg_copy[MAX_LENGTH]; if(temp!=NULL) { strcpy(arg_copy, temp); for(arg = strtok_r(NULL, " ", &temp); arg && argc<4; arg = strtok_r(NULL, " ", &temp)) { strcpy(argv[argc], arg); argc++; } } if(!strcmp("LOGIN", command)) { if(!is_server && !(is_client_connected || argc!=2) && validate_ip(argv[0]) && validate_port(argv[1])) { int newfd = client_connect(argv[0], argv[1]); if(newfd<=1) { print_success(0, command); } else { //TODO client connects to unknown port? is_client_connected = true; add_fd(newfd); client_identify(newfd); server_sock = newfd; print_success(1, command); } } else print_success(0, command); } else if(is_client_connected) { if(!strcmp("PORT", command)) { print_success(1, command); LOG("PORT:%d\n", get_listening_port()); } else if(!strcmp("IP", command)) { print_success(1, command); get_ip(); } else if(!strcmp("SEND", command)) { if(!is_client_connected) print_success(0,command); else print_success(client_send_msg(server_sock, arg_copy), command); } else if(!strcmp("SENDFILE", command)) { if(argc == 2 && validate_ip(argv[0])) { print_success(1, command); client_send_file(argv[0], argv[1]); } else print_success(0, command); } else if(!strcmp("LOGOUT", command)) { if(is_client_connected) { //close(server_sock); //clear_fd(server_sock); client_send(server_sock, command); is_client_connected = false; //server_sock = -1; print_success(1, command); } else print_success(0, command); } else if(!strcmp("LIST", command)) { print_success(1, command); print_client_list(); } else if(!strcmp("REFRESH", command)) { client_send(server_sock, command); print_success(1, command); } else if(!strcmp("BROADCAST", command)) { if(argc) { client_send(server_sock, message); print_success(1, command); } else print_success(0, command); } else if(!strcmp("BLOCK", command)) { if(argc == 1 && verify_ip(argv[0]) && !is_client_blocked(argv[0])) { //TODO CHECK IP in list!! done! print_success(1, command); add_to_block_list(argv[0]); client_send(server_sock, message); } else { print_success(0, command); } } else if(!strcmp("UNBLOCK", command)) { if(argc == 1 && verify_ip(argv[0]) && is_client_blocked(argv[0])) { print_success(1, command); remove_from_block_list(argv[0]); client_send(server_sock, message); } else print_success(0, command); } else print_success(0, command); } else print_success(0, command); } else { if(!strcmp("STATISTICS", command)) { print_success(1, command); print_stats(); } else if(!strcmp("BLOCKED", command)) { if(temp == NULL) print_success(0, command); else { char *ip = strtok_r(NULL, " ", &temp); if(validate_ip(ip) && find_client_by_ip(ip) !=NULL) { print_success(1, command); print_blocked_clients(ip); } else print_success(0, command); } } else if(!strcmp("LIST", command)) { print_success(1, command); print_connected_client_list(); } else if(!strcmp("PORT", command)) { print_success(1, command); LOG("PORT:%d\n", get_listening_port()); } else if(!strcmp("IP", command)) { print_success(1, command); get_ip(); } else print_success(0, command); } LOG("[%s:END]\n", command); return 0; }