static int module7_init(void) { int port= 13394; struct socket* serversocket; struct socket* newsocks; size_t recieving, lens; char * Buffer; Buffer = (char*)__get_free_page(GFP_KERNEL); lens=strlen(Buffer); serversocket= set_up_server_socket(port); do{ newsocks= server_accept_connection(serversocket); recieving = RecvBuffer(newsocks, Buffer, lens); if (recieving<=0) { printk(KERN_INFO "Server:error when receiving message"); } else { printk(KERN_INFO "Server received msg: %s",Buffer); } }while(1); return 0; }
/** * Handle a connection attempt from a player : * - check the crc * - check server/user password (TODO) * - check if the player is already connected (TODO) * - initialize the player, add it to the pool * - notify the player if he has been accepted (or not TODO) * - notify the other players (TODO) * - move the player to the default channel (TODO) * * @param data the connection packet * @param len the length of the connection packet * @param cli_addr the adress of the client * @param cli_len the length of cli_addr */ void handle_player_connect(char *data, unsigned int len, struct sockaddr_in *cli_addr, unsigned int cli_len, struct server *s) { struct player *pl, *tmp_pl; char password[30]; char login[30]; struct registration *r; size_t iter; /* Check crc */ if (!packet_check_crc(data, len, 16)) return; /* Check if the IP is banned */ if (get_ban_by_ip(s, cli_addr->sin_addr) != NULL) { server_refuse_connection_ban(cli_addr, cli_len, s); logger(LOG_INFO, "PLAYER BANNED TRIED TO CONNECT"); return; } /* If registered, check if player exists, else check server password */ bzero(password, 30 * sizeof(char)); strncpy(password, data + 121, MIN(29, data[120])); bzero(login, 30 * sizeof(char)); strncpy(login, data + 91, MIN(29, data[90])); pl = new_player_from_data(data, len, cli_addr, cli_len); if (data[90] == 0) { /* no login = anonymous mode */ /* check password against server password */ if (strcmp(password, s->password) != 0) { destroy_player(pl); return; /* wrong server password */ } pl->global_flags = GLOBAL_FLAG_UNREGISTERED; } else { r = get_registration(s, login, password); if (r == NULL) { logger(LOG_INFO, "Invalid credentials for a registered player"); destroy_player(pl); return; /* nobody found with those credentials */ } pl->global_flags |= r->global_flags; pl->global_flags |= GLOBAL_FLAG_REGISTERED; pl->reg = r; } /* Add player to the pool */ add_player(s, pl); /* Send a message to the client indicating he has been accepted */ /* Send server information to the player (0xf4be0400) */ server_accept_connection(pl); /* Send a message to all players saying that a new player arrived (0xf0be6400) */ s_notify_new_player(pl); /* Send the new player the list of all the Voice Requests */ ar_each(struct player *, tmp_pl, iter, s->players) if (pl->player_attributes & PL_ATTR_REQUEST_VOICE) s_notify_player_requested_voice(tmp_pl, pl); ar_end_each; }
static int module7_init(void) { int port= 13394; size_t recieving; struct netlink_kernel_cfg cfg = { .groups = 1, .input = netlinkmsg, }; nl_sk = netlink_kernel_create(&init_net, 3, &cfg); Buffer = (char*)__get_free_page(GFP_KERNEL); serversocket= set_up_server_socket(port); newsocks= server_accept_connection(serversocket); recieving = RecvBuffer(newsocks, Buffer, 1024); if (recieving<=0) { printk(KERN_INFO "Server:error when receiving message\n"); } else { printk(KERN_INFO "Server received msg: %s \n",Buffer); } file1 = debugfs_create_file("mmap_example1", 0644, NULL, NULL, &my_fops); if (file1 == NULL) { printk(KERN_ALERT "Error occurred\n"); } else { printk("The creation of the file should has occured\n"); printk("The pointer value of the create file %p \n", &file1); } return 0; } static void module7_exit(void) { sock_release(serversocket); sock_release(newsocks); debugfs_remove(file1); netlink_kernel_release(nl_sk); printk(KERN_ALERT "Server exiting \n"); return; }
static int module7_init(void) { int port= 13394; unsigned int IP_addr= 168430083; size_t sendings; char *sendbuff; struct socket* serversock; struct socket* newsocket; size_t recieve; size_t length; size_t lens; char *recvbuffer; recvbuffer = (char*)__get_free_page(GFP_KERNEL); sendbuff= "hi i am thinh"; //file1 = debugfs_create_file("mmap_example1", 0644, NULL, NULL, &my_fops); /* //setup_timer(&my_timer, my_timer_callback, 0 ); if (file1 == NULL) { printk(KERN_ALERT "Error occurred\n"); } else { printk("The creation of the file should has occured\n"); printk("The pointer value of the create file %p \n", &file1); } */ lens = strlen(recvbuffer); serversock=set_up_server_socket(port); do{ newsocket = server_accept_connection(serversock); recieve = RecvBuffer(newsocket, recvbuffer, lens); if (recieve <= 0) { printk("Client Error when recieving msg"); break; } else { printk("ClientMSG recieved: %s \n", recvbuffer); } }while(1); socketz=set_up_client_socket(IP_addr,port); length=strlen(sendbuff); sendings= SendBuffer(socketz, sendbuff, length); sock_release(newsocket); sock_release(socketz); return 0; }
int main(int argc, char *argv[]) { // if the kernel module is loaded, it will have created the "/dev/pg_stream" device file if(access("/dev/pg_stream", R_OK) != 0) { printf("ERROR driver needs to be loaded before starting the host\n"); return; } // load config global_config = default_global_config; storage_load_structure("global_config", global_config); pgdriver_send("config", &global_config, sizeof(global_config)); printf("starting. device name: %s\n", global_config.device_name); // create the folder, just in case mkdir("records"); // start the broadcast pthread_t thread_id; if(pthread_create(&(thread_id), NULL, &broadcast_thread_entry, NULL)) { printf("ERROR creating thread\n"); } // wait for connections, forever while(true) { broadcast_enabled = true; msleep(1000); // prevent cpu exhaust // wait for a connection server_accept_connection(); if(!server_is_connected) { continue; } // debug output printf("=============== new connection\n"); // we dont need to broadcast the IP address anymore broadcast_enabled = false; // handle each received command while(server_is_connected) { // read the command byte, which is always the first byte of every command data block char command; server_receive_bytes(1, &command); if(!server_is_connected) break; // debug output printf("cmd: %d\n", (int)command); if(command == 0xFF) // command: quit break; if(command == 1) // command: download records { // upload all files enumerate_records(&each_record_filename); // send the byte indicating that no more files are available command = 0; server_send_bytes(1, &command); printf("enumeration complete\n"); continue; } if(command == 2) // command: delete record (successfully downloaded) { char filename[32]; server_receive_bytes(32, filename); // receive file name to delete printf("deleting: %s\n", filename); sprintf(record_file_path, "./records/%s", filename); // delete if(unlink(record_file_path) < 0) { printf("delete failed. %s\n", record_file_path); } continue; } if(command == 3) // command: get config { server_send_bytes(sizeof(global_config), &global_config); continue; } if(command == 4) // command: set config { server_receive_bytes(sizeof(global_config), &global_config); printf("new config loaded\n"); printf("device_name = %s\n", global_config.device_name); printf("unit_name = %s\n", global_config.unit_name); printf("unit_significand = %d\n", global_config.unit_significand_a); printf("unit_exponent = %d\n", global_config.unit_exponent_a); printf("ADC_min_value = %04X\n", global_config.ADC_min_value); printf("ADC_max_value = %04X\n", global_config.ADC_max_value); printf("SPI_output = %08X\n", global_config.SPI_output); printf("SPI_clock_mode = %d\n", global_config.SPI_clock_mode); printf("bit_shift = %d\n", global_config.bit_shift); printf("digital_bit_shift = %d\n", global_config.digital_bit_shift); printf("\n"); printf("frequency = %d\n", global_config.frequency); printf("downsampling_rate = %d\n", global_config.downsampling_rate); printf("digital = %d\n", global_config.digital); printf("filename_counter = %d\n", global_config.filename_counter); printf("unit_range_choice = %d\n", global_config.unit_range_choice); printf("\n"); storage_save_structure("global_config", global_config); pgdriver_send("config", &global_config, sizeof(global_config)); continue; } if(command == 7) // start measurement { cat_start_next_id(); continue; } if(command == 8) // get status { // is the measurement currently running? char v = !!cat_enabled; server_send_bytes(1, &v); continue; } if(command == 9) // stop measurement { cat_stop(); continue; } if(command == 10) // start dry run. (measurement without saving data) { cat_start(-1); continue; } if(command == 11) // get raw SPI 32-bit block { int raw = pgdriver_get_small("config", 4); server_send_bytes(4, &raw); continue; } printf("unknown command\n"); } } printf("the end\n"); return 0; }
/***************************************************************************** Get and handle: - new connections, - input from connections, - input from server operator in stdin Returns: 0 if went past end-of-turn timeout 2 if force_end_of_sniff found to be set 1 otherwise (got and processed something?) *****************************************************************************/ int sniff_packets(void) { int i; int max_desc; fd_set readfs; struct timeval tv; static time_t time_at_turn_end; static int year; int rv; #if defined (commentout) tv.tv_sec=0; tv.tv_usec=0; tv.tv_sec=1; tv.tv_usec=0; #endif tv.tv_sec=0; tv.tv_usec=0; FD_ZERO(&readfs); #if defined (commentout) FD_SET(0, &readfs); #endif FD_SET(sock, &readfs); #if defined (WIN32) max_desc = 0; /* W32 ignores first argument to select() */ #else max_desc=sock; #endif for(i=0; i<MAX_CONNECTIONS; i++) { if(connections[i].used) { FD_SET(connections[i].sock, &readfs); } #if !defined (WIN32) max_desc=MAX(connections[i].sock, max_desc); #endif } rv = select(max_desc+1, &readfs, NULL, NULL, &tv); if(rv==0) { /* timeout */ } #if defined (WIN32) if (rv == SOCKET_ERROR) { printf ("Got a SOCKET_ERROR from select()!!!\n"); } #endif /* new players connects */ if(FD_ISSET(sock, &readfs)) { printf ("got new connection\n"); if(server_accept_connection(sock)==-1) printf("failed accepting connection\n"); } #if defined (commentout) /* input from server operator */ else if(FD_ISSET(0, &readfs)) { int didget; char buf[BUF_SIZE+1]; if((didget=read(0, buf, BUF_SIZE))==-1) { printf ("read from stdin failed\n"); exit(1); } *(buf+didget)='\0'; printf ("Prompt garbage\n"); } #endif /* input from a player */ else { for(i=0; i<MAX_CONNECTIONS; i++) { if(connections[i].used && FD_ISSET(connections[i].sock, &readfs)) { int rv; rv = net_recv_message (connections[i].sock, &connections[i].msgbuf); if (rv < 0) { printf ("Lost connection to player...\n"); close_connection(&connections[i]); break; } else if (rv > 0) { server_handle_message (&connections[i]); } } } } return 1; }