static gboolean local_validate (UmAccountDialog *self) { gboolean valid_login; gboolean valid_name; GtkWidget *entry; const gchar *name; gchar *tip; name = gtk_combo_box_text_get_active_text (GTK_COMBO_BOX_TEXT (self->local_username)); valid_login = is_valid_username (name, &tip); entry = gtk_bin_get_child (GTK_BIN (self->local_username)); if (tip) { set_entry_validation_error (GTK_ENTRY (entry), tip); g_free (tip); } else { clear_entry_validation_error (GTK_ENTRY (entry)); } name = gtk_entry_get_text (GTK_ENTRY (self->local_name)); valid_name = is_valid_name (name); return valid_name && valid_login; }
int respond_to_command(protocol *current_protocol, char *command, char *return_value) { char *save, *token; token = strtok_r(command, " ", &save); if(current_protocol->state == UPDATE) { strcpy(return_value, "+OK POP3 server ready\r\n"); current_protocol->state = AUTHORIZATION; } else if(token) { if ( strcmp(token,"USER") == 0 && current_protocol->state == AUTHORIZATION) { token = strtok_r(NULL, " ", &save); if(is_valid_username(&(current_protocol->current_user), token) == SUCCEED) strcpy(return_value, "+OK Nice username. Give PASS your password\r\n"); else strcpy(return_value, "-ERR I don't know you\r\n"); } else if ( strcmp(token,"PASS") == 0 && current_protocol->state == AUTHORIZATION) { token = strtok_r(NULL, " ", &save); if(is_correct_password(&(current_protocol->current_user), token) == SUCCEED) { strcpy(return_value, "+OK WOW. Correct password. Let's go to transaction\r\n"); current_protocol->state = TRANSACTION; } else strcpy(return_value, "-ERR Oops wrong password. Try again\r\n"); } else if ( strcmp(token,"QUIT") == 0) { strcpy(return_value, "+OK You want to leave. Bye Bye. Go to update.\r\n"); current_protocol->state = UPDATE; return QUIT; } else if ( strcmp(token,"STAT") == 0 && current_protocol->state == TRANSACTION) strcpy(return_value, "+OK STAT not implemented\r\n"); else if (strcmp(token,"DELE") == 0 && current_protocol->state == TRANSACTION) strcpy(return_value, "+OK DELE not implemented\r\n"); else if (strcmp(token,"LIST") == 0 && current_protocol->state == TRANSACTION) strcpy(return_value, "+OK LIST not implemented\r\n"); else if (strcmp(token,"RETR") == 0 && current_protocol->state == TRANSACTION ) strcpy(return_value, "+OK LIST not implemented\r\n"); else strcpy(return_value, "-ERR I don't understand you\r\n"); } return SUCCEED; }
void startClient() { char *logfile = "/sdcard/client.out"; FILE *fp; if((fp=freopen("/sdcard/freeciv_out_client.log", "w" ,stdout))==NULL) { printf("Cannot open file.\n"); exit(1); } if((fp=freopen("/sdcard/freeciv_err_client.log", "w" ,stderr))==NULL) { printf("Cannot open file.\n"); exit(1); } setenv ("HOME", "/sdcard/FreeCiv", 0); setenv ("USER", "Sparky", 0); LOGI("Hello JNI"); log_init(logfile, LOG_DEBUG , NULL); int i, loglevel; int ui_options = 0; bool ui_separator = FALSE; char *option=NULL; bool user_tileset = FALSE; i_am_client(); /* Tell to libfreeciv that we are client */ /* Ensure that all AIs are initialized to unused state */ ai_type_iterate(ai) { init_ai(ai); } ai_type_iterate_end; init_our_capability(); (void)user_username(default_user_name, MAX_LEN_NAME); if (!is_valid_username(default_user_name)) { char buf[sizeof(default_user_name)]; my_snprintf(buf, sizeof(buf), "_%s", default_user_name); if (is_valid_username(buf)) { sz_strlcpy(default_user_name, buf); } else { my_snprintf(default_user_name, sizeof(default_user_name), "player%d", myrand(10000)); } } game.all_connections = conn_list_new(); game.est_connections = conn_list_new(); ui_init(); fc_init_network(); init_our_capability(); chatline_common_init(); init_player_dlg_common(); init_themes(); options_init(); strcpy(default_sound_plugin_name,"none"); server_port = 9999; sz_strlcpy(server_host, "localhost"); options_load(); mysrand(time(NULL)); helpdata_init(); boot_help_texts(NULL); LOGI("Reading tilespec"); tilespec_try_read("amplio2", TRUE); LOGI("Done reading tilespec"); overview_size_changed(); audio_real_init("stdsounds", "none"); //audio_play_music("music_start", NULL); auto_connect = TRUE; init_mapcanvas_and_overview(); /* run gui-specific client */ ui_main(0, NULL); /* termination */ client_exit(); return; }
int main(int argc, const char* argv[]) { if (argc != 2) { printf("<port>\n"); exit(EXIT_FAILURE); } uint16_t PORT; if (!str_to_uint16(argv[1], &PORT)) { printf("invalid <port>\n"); exit(EXIT_FAILURE); } char* users[MAX_USERS]; // set up the user name trie ascii_trie* user_names = ascii_trie_init(); for (int i = 0; i < MAX_USERS; i++) { users[i] = NULL; } char buffer[BUFFER_SIZE]; int sockfd = socket(PF_INET, SOCK_STREAM, STANDARD_PROTOCOL); // socket() creates an endpoint for communication and returns a descriptor. struct sockaddr_in serv_addr; memset(&serv_addr, '0', sizeof(serv_addr)); serv_addr.sin_family = AF_INET; serv_addr.sin_addr.s_addr = htonl(INADDR_ANY); serv_addr.sin_port = htons(PORT); // INADDR_ANY allows program to work without knowing the IP address of the machine it was running on, // or, in the case of a machine with multiple network interfaces, it allows the server // to receive packets destined to any of the interfaces. bind(sockfd, (struct sockaddr*)&serv_addr, sizeof(serv_addr)); // bind() assigns a name to an unnamed socket. When a socket is created with // socket(2) it exists in a name space (address family) but has no name // assigned. bind() requests that address be assigned to the socket. listen(sockfd, BACKLOG); // Creation of socket-based connections requires several operations. First, a // socket is created with socket(2). Next, a willingness to accept incoming // connections and a queue limit for incoming connections are specified with // listen(). Finally, the connections are accepted with accept(2). The // listen() call applies only to sockets of type SOCK_STREAM. // // The backlog parameter defines the maximum length for the queue of pending // connections. If a connection request arrives with the queue full, the // client may receive an error with an indication of ECONNREFUSED. Alterna- // tively, if the underlying protocol supports retransmission, the request may // be ignored so that retries may succeed. fd_set active_fd_set, read_fd_set; struct sockaddr_in clientname; FD_ZERO (&active_fd_set); FD_SET (sockfd, &active_fd_set); while (1) { memset(buffer, 0, BUFFER_SIZE); read_fd_set = active_fd_set; if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) { perror ("select"); exit (EXIT_FAILURE); } for (int fd = 0; fd < FD_SETSIZE; ++fd) { if (FD_ISSET(fd, &read_fd_set)) { if (fd == sockfd) { // connection on server socket // we want to get the address of the client connecting // and add its fd to the set of connections printf("new client connectint...\n"); socklen_t size = sizeof (clientname); int new_connfd = accept(sockfd, (struct sockaddr*) &clientname, &size); // The argument socket is a socket that has been created with socket(2), bound // to an address with bind(2), and is listening for connections after a // listen(2). accept() extracts the first connection request on the queue of // pending connections, creates a new socket with the same properties of // socket, and allocates a new file descriptor for the socket. If no pending // connections are present on the queue, and the socket is not marked as non- // blocking, accept() blocks the caller until a connection is present. If the // socket is marked non-blocking and no pending connections are present on the // queue, accept() returns an error as described below. The accepted socket // may not be used to accept more connections. The original socket socket, // remains open. if (new_connfd < 0) { perror ("accept"); exit (EXIT_FAILURE); } assert(ntohs(clientname.sin_port) > 0); fprintf (stderr,"Server: connect from host %s, port %d.\n", inet_ntoa(clientname.sin_addr), ntohs(clientname.sin_port)); char* uname = "PLEASE PROVIDE A USER NAME...\n"; send(new_connfd, uname, strlen(uname), 0); FD_SET (new_connfd, &active_fd_set); } else { ssize_t nbytes; // return length if ((nbytes = read_from_connected_client(fd, buffer)) < 1) { memset(buffer, 0, BUFFER_SIZE); printf("user: %s left\n", users[fd]); ascii_trie_delete(users[fd], user_names); free(users[fd]); users[fd] = NULL; close(fd); FD_CLR(fd, &active_fd_set); } else { if (users[fd] == NULL) { // new user is not registered yet.. we assume that the buffer // contains the user name to use followed by a '\n' char name[nbytes]; memset(name, 0, nbytes); strncpy(name, buffer, nbytes-1); if (!is_valid_username(name)) { char* badname = "INVALID USERNAME...\n"; send(fd, badname, strlen(badname), 0); } else { int insert = ascii_trie_insert(name, fd, user_names); if (insert == 0) { char* exists = "USER NAME TAKEN...\n"; send(fd, exists, strlen(exists), 0); } else if (insert == 1) { users[fd] = malloc(sizeof(char)*nbytes); memset(users[fd], 0, nbytes); strncpy(users[fd], buffer, nbytes-1); } else { perror("trie insert"); } } } else { printf("user: %s sent a message\n", users[fd]); if (buffer[0] == '@') { char toname[10]; int i = 0; while ((i < 10) && (buffer[i+1] != 0x00)) { if (!isspace(buffer[i+1])) { toname[i] = buffer[i+1]; i++; } else { break; } } toname[i] = 0; int to_fd; ascii_trie* node = ascii_trie_lookup(toname, &to_fd, user_names); if (node != NULL) { send(to_fd, buffer, BUFFER_SIZE, 0); } } } memset(buffer, 0, BUFFER_SIZE); } } } } } return 0; }
int auth_user(char *authbuf, const char *peername, int *is_reg, int *reconnect_id) { json_error_t err; json_t *obj, *cmd, *name, *pass, *email, *reconn; const char *namestr, *passstr, *emailstr; int userid = 0; obj = json_loads(authbuf, 0, &err); if (!obj) return 0; /* try 1: is it an auth command? */ *is_reg = 0; cmd = json_object_get(obj, "auth"); if (!cmd) { /* try 2: register? */ *is_reg = 1; cmd = json_object_get(obj, "register"); } if (!cmd) /* not a recognized command */ goto err; name = json_object_get(cmd, "username"); pass = json_object_get(cmd, "password"); email = json_object_get(cmd, "email"); /* is null for auth */ reconn = json_object_get(cmd, "reconnect"); if (!name || !pass) goto err; namestr = json_string_value(name); passstr = json_string_value(pass); if (!namestr || !passstr || strlen(namestr) < 3 || strlen(passstr) < 3 || !is_valid_username(namestr)) goto err; *reconnect_id = 0; if (!*is_reg) { if (reconn && json_is_integer(reconn)) *reconnect_id = json_integer_value(reconn); /* authenticate against a user database */ userid = db_auth_user(namestr, passstr); if (userid > 0) log_msg("%s has logged in as \"%s\" (userid %d)", peername, namestr, userid); else if (userid < 0) log_msg("%s has failed to log in as \"%s\" (userid %d)", peername, namestr, -userid); } else { /* register a new user */ emailstr = email ? json_string_value(email) : ""; if (strlen(emailstr) > 100) goto err; userid = db_register_user(namestr, passstr, emailstr); if (userid) { char savedir[1024]; snprintf(savedir, 1024, "%s/save/%s", settings.workdir, namestr); mkdir(savedir, 0700); log_msg("%s has registered as \"%s\" (userid: %d)", peername, namestr, userid); } } json_decref(obj); return userid; err: json_decref(obj); return 0; }
int main(int argc, char **argv) { char *config_dir; char *config_file; int c; char *name = NULL; program_name = argv[0]; config_dir = DEFAULT_CONFIG_DIR; while ((c = getopt_long(argc, argv, short_opts, long_opts, NULL)) != EOF) { switch (c) { case 'h': usage (1); break; case 'c': config_dir = strdup(optarg); break; case 'n': name = strdup (optarg); break; case 'H': host_str = strdup (optarg); break; case 'P': port_str = strdup (optarg); break; default: usage(1); } } config_dir = ccnet_expand_path (config_dir); /* printf("[conf_dir=%s\n]", config_dir); */ SSLeay_add_all_algorithms(); if (RAND_status() != 1) { /* it should be seeded automatically */ fprintf(stderr, "PRNG is not seeded\n"); exit (1); } if (bits == 0) bits = DEFAULT_BITS; /* create peer key */ if (!name) { usage(-ERR_NAME_NULL); } if (strlen(name) < 2 || strlen (name) > 16 || !is_valid_username(name)) { fprintf (stderr, "The user name should be more than 2 bytes and less than 16 bytes, only digits, alphabetes and '-', '_' are allowed"); exit(-ERR_NAME_INVALID); } user_name = name; peer_name = g_strdup (name); create_peerkey (); peer_id = id_from_pubkey (peer_pubkey); identity_file_peer = g_build_filename (config_dir, PEER_KEYFILE, NULL); /* create dir */ if (ccnet_mkdir(config_dir, 0700) < 0) { fprintf (stderr, "Make dir %s error: %s\n", config_dir, strerror(errno)); exit(-ERR_PERMISSION); } /* save key */ save_privkey (peer_privkey, identity_file_peer); /* make configure file */ config_file = g_build_filename (config_dir, CONFIG_FILE_NAME, NULL); make_configure_file (config_file); printf ("Successly create configuration dir %s.\n", config_dir); exit(0); }