int handle_uevents(fd_set *rset) { struct client *citer; if(lsock == -1) { return -1; } if(FD_ISSET(lsock, rset)) { /* got an incoming connection */ int s; if((s = accept(lsock, 0, 0)) == -1) { perror("error while accepting connection on the UNIX socket"); } else { if(!add_client(CLIENT_UNIX, &s)) { perror("failed to add client"); } } } /* all the UNIX socket clients */ citer = first_client(); while(citer) { struct client *c = citer; citer = next_client(); if(get_client_type(c) == CLIENT_UNIX) { int s = get_client_socket(c); if(FD_ISSET(s, rset)) { int rdbytes; float sens; /* got a request from a client, decode and execute it */ /* XXX currently only sensitivity comes from clients */ while((rdbytes = read(s, &sens, sizeof sens)) <= 0 && errno == EINTR); if(rdbytes <= 0) { /* something went wrong... disconnect client */ close(get_client_socket(c)); remove_client(c); continue; } set_client_sensitivity(c, sens); } } } return 0; }
void send_uevent(spnav_event *ev, struct client *c) { int i, data[8] = {0}; float motion_mul; if(!lsock) return; switch(ev->type) { case EVENT_MOTION: data[0] = UEV_TYPE_MOTION; motion_mul = get_client_sensitivity(c); for(i=0; i<6; i++) { float val = (float)ev->motion.data[i] * motion_mul; data[i + 1] = (int)val; } data[7] = ev->motion.period; break; case EVENT_BUTTON: data[0] = ev->button.press ? UEV_TYPE_PRESS : UEV_TYPE_RELEASE; data[1] = ev->button.bnum; break; default: break; } while(write(get_client_socket(c), data, sizeof data) == -1 && errno == EINTR); }
int main() { int fd_in; char server[15]; char port[15]; pthread_t read_thread, write_thread; if ((fd_in = open(configfile, O_RDONLY)) < 0){ panic_with_system_message("Error no se ha podido abrir el fichero de configuración"); } LeerParametroHasta(fd_in,'\n',server); LeerParametroHasta(fd_in,'\n',port); LeerParametroHasta(fd_in,'\n',user); int sock = get_client_socket(server, port); if (sock < 0) { panic_with_user_message("Fallo en get_client_socket() ", "No se puede conectar"); } printbanner();//Bienvenida si no han habido errores de arranque pthread_create(&read_thread, NULL,(void *) t_read, (void *) (uintptr_t)sock); pthread_create(&write_thread, NULL,(void *) t_write, (void *)(uintptr_t) sock); pthread_join(read_thread, NULL); pthread_join(write_thread, NULL); close(sock); exit(0); }
int handle_request_tunnel_frame(protocol_frame *rcvd_frame) { char *hostname = NULL; tunnel *tun; int port = -1; int sockfd = 0; uint16_t tunnel_id; if(client_mode) { log_printf(L_WARNING, "Got tunnel request frame from friend #%d when in client mode\n", rcvd_frame->friendnumber); return -1; } port = rcvd_frame->connid; hostname = calloc(1, rcvd_frame->data_length + 1); if(!hostname) { log_printf(L_ERROR, "Could not allocate memory for tunnel request hostname\n"); return -1; } strncpy(hostname, rcvd_frame->data, rcvd_frame->data_length); hostname[rcvd_frame->data_length] = '\0'; log_printf(L_INFO, "Got a request to forward data from %s:%d\n", hostname, port); tunnel_id = get_random_tunnel_id(); log_printf(L_DEBUG, "Tunnel ID: %d\n", tunnel_id); sockfd = get_client_socket(hostname, port); if(sockfd >= 0) { tun = tunnel_create(sockfd, tunnel_id, rcvd_frame->friendnumber); if(tun) { FD_SET(sockfd, &master_server_fds); update_select_nfds(sockfd); log_printf(L_DEBUG, "Created tunnel, yay!\n"); send_tunnel_ack_frame(tun); } else { log_printf(L_ERROR, "Couldn't allocate memory for tunnel\n"); close(sockfd); } } else { log_printf(L_WARNING, "Could not connect to %s:%d\n", hostname, port); /* TODO send reject */ } free(hostname); return 0; }
int curses_ui() { int err; err = ui_init(); if (err == -1) { return (-1); } file_list.dir_name = malloc(MAXPATHLEN + 1); if (!file_list.dir_name) { mvwprintw(main_win, 0, 1, "ERROR: Can't initialize dir_name."); wrefresh(main_win); return (-1); } err = first_run_file_list(main_win); if (err == -1) { mvwprintw(main_win, 0, 1, "ERROR: Can't initialize file list."); wrefresh(main_win); return (-1); } sock_fd = get_client_socket(); err = pthread_create(&receiver_thread, rcv_attr, ui_socket_receiver, rcv_arg); if (err != 0) { mvwprintw(main_win, 0, 1, "ERROR: receiver thread"); wrefresh(main_win); return (-1); } show_files(main_win); curses_loop(); // wait for receiver_thread if alive if (pthread_kill(receiver_thread, 0) == 0) { printw("UI: waiting for receiver_thread.."); refresh(); pthread_join(receiver_thread, NULL); } free_dir_list(); free(file_list.dir_name); ui_cleanup(); return (0); }
int main(int argc, char *argv[]) { char buf[NET_BUF]; int s, len, port; char *server_name; if(argc != 3) { printf("usage: ./myftp <server> <port>\n"); return 0; } server_name = argv[1]; port = atoi(argv[2]); s = get_client_socket(argv[1], port); char line[BUF_SIZE]; help(); while(1) { printf("myftp>>> "); read_line(line); if (strcmp(line, "DWLD") == 0) { dwld(s); } else if (strcmp(line, "UPLD") == 0) { upld(s); } else if (strcmp(line, "LIST") == 0) { list(s); } else if (strcmp(line, "MDIR") == 0) { mdir(s); } else if (strcmp(line, "RDIR") == 0) { rdir(s); } else if (strcmp(line, "CDIR") == 0) { cdir(s); } else if (strcmp(line, "DELF") == 0) { delf(s); } else if (strcmp(line, "HELP") == 0) { help(); } else if (strcmp(line, "QUIT") == 0) { printf("goodbye\n"); quit(s); close(s); break; } else { printf("Unrecognized command: %s\n", line); } } return 0; }
int main(int argc, char **argv) { int i, ret, become_daemon = 1; for(i=1; i<argc; i++) { if(argv[i][0] == '-' && argv[i][2] == 0) { switch(argv[i][1]) { case 'd': become_daemon = !become_daemon; break; case 'v': verbose = 1; break; case 'h': printf("usage: %s [options]\n", argv[0]); printf("options:\n"); printf(" -d\tdo not daemonize\n"); printf(" -v\tverbose output\n"); printf(" -h\tprint this usage information\n"); return 0; default: fprintf(stderr, "unrecognized argument: %s\n", argv[i]); return 1; } } else { fprintf(stderr, "unexpected argument: %s\n", argv[i]); return 1; } } if(become_daemon) { daemonize(); } write_pid_file(); puts("Spacenav daemon " VERSION); read_cfg("/etc/spnavrc", &cfg); if(init_clients() == -1) { return 1; } signal(SIGINT, sig_handler); signal(SIGTERM, sig_handler); signal(SIGSEGV, sig_handler); signal(SIGHUP, sig_handler); signal(SIGUSR1, sig_handler); signal(SIGUSR2, sig_handler); if(init_dev() == -1) { init_hotplug(); } init_unix(); #ifdef USE_X11 init_x11(); #endif atexit(cleanup); for(;;) { fd_set rset; int fd, max_fd = 0; struct client *c; FD_ZERO(&rset); /* set the device fd if it's open, otherwise set the hotplug fd */ if((fd = get_dev_fd()) != -1 || (fd = get_hotplug_fd()) != -1) { FD_SET(fd, &rset); if(fd > max_fd) max_fd = fd; } /* the UNIX domain socket listening for connections */ if((fd = get_unix_socket()) != -1) { FD_SET(fd, &rset); if(fd > max_fd) max_fd = fd; } /* all the UNIX socket clients */ c = first_client(); while(c) { if(get_client_type(c) == CLIENT_UNIX) { int s = get_client_socket(c); assert(s >= 0); FD_SET(s, &rset); if(s > max_fd) max_fd = s; } c = next_client(); } /* and the X server socket */ #ifdef USE_X11 if((fd = get_x11_socket()) != -1) { FD_SET(fd, &rset); if(fd > max_fd) max_fd = fd; } #endif do { ret = select(max_fd + 1, &rset, 0, 0, 0); } while(ret == -1 && errno == EINTR); if(ret > 0) { handle_events(&rset); } } return 0; /* unreachable */ }
int handle_request_tunnel_frame(protocol_frame *rcvd_frame) { char *hostname = NULL; tunnel *tun; int port = -1; int sockfd = 0; uint16_t tunnel_id; if(client_mode) { log_printf(L_WARNING, "Got tunnel request frame from friend #%d when in client mode\n", rcvd_frame->friendnumber); return -1; } port = rcvd_frame->connid; hostname = calloc(1, rcvd_frame->data_length + 1); if(!hostname) { log_printf(L_ERROR, "Could not allocate memory for tunnel request hostname\n"); return -1; } strncpy(hostname, (char *)rcvd_frame->data, rcvd_frame->data_length); hostname[rcvd_frame->data_length] = '\0'; log_printf(L_INFO, "Got a request to forward data from %s:%d\n", hostname, port); // check rules if (rules_policy == VALIDATE && nrules > 0 ) { rule temp_rule, *found = NULL; temp_rule.host = hostname; temp_rule.port = port; LL_SEARCH(rules, found, &temp_rule, rule_cmp); if(!found) { log_printf(L_WARNING, "Rejected, request not in rules\n"); if(hostname) { free(hostname); } return -1; } } else if (rules_policy != NONE) { log_printf(L_WARNING, "Filter option active but no allowed host/port. All requests will be dropped.\n"); if(hostname) { free(hostname); } return -1; } tunnel_id = get_random_tunnel_id(); log_printf(L_DEBUG, "Tunnel ID: %d\n", tunnel_id); sockfd = get_client_socket(hostname, port); if(sockfd >= 0) { tun = tunnel_create(sockfd, tunnel_id, rcvd_frame->friendnumber); if(tun) { FD_SET(sockfd, &master_server_fds); update_select_nfds(sockfd); log_printf(L_DEBUG, "Created tunnel, yay!\n"); send_tunnel_ack_frame(tun); } else { log_printf(L_ERROR, "Couldn't allocate memory for tunnel\n"); close(sockfd); } } else { log_printf(L_WARNING, "Could not connect to %s:%d\n", hostname, port); /* TODO send reject */ } free(hostname); return 0; }