void http_callback_auth(httpd * webserver, request * r) { t_client *client; httpVar *token; char *mac; httpVar *logout = httpdGetVariableByName(r, "logout"); const s_config *config = config_get_config(); if ((token = httpdGetVariableByName(r, "token"))) { /* They supplied variable "token" */ if (!(mac = arp_get(r->clientAddr))) { /* We could not get their MAC address */ debug(LOG_ERR, "Failed to retrieve MAC address for ip %s", r->clientAddr); send_http_page(r, "WiFiDog Error", "Failed to retrieve your MAC address"); } else { t_redir_node *node; int index = 0; LOCK_REDIR(); node = redir_list_find(mac); if (node) { index = node->wlindex; } UNLOCK_REDIR(); /* We have their MAC address */ LOCK_CLIENT_LIST(); if ((client = client_list_find(r->clientAddr, mac)) == NULL) { debug(LOG_DEBUG, "New client for %s", r->clientAddr); client = client_list_add(r->clientAddr, mac, token->value); client->fw_connection_state = FW_MARK_REDIR; client->counters.active_duration = config->sessiontimeout[index-1]; } else if (logout) { logout_client(client); } else { debug(LOG_DEBUG, "Client for %s is already in the client list", client->ip); } UNLOCK_CLIENT_LIST(); if (!logout) { /* applies for case 1 and 3 from above if */ authenticate_client(r); } free(mac); } } else { /* They did not supply variable "token" */ send_http_page(r, "WiFiDog error", "Invalid token"); } }
void create_client(int sockfd, struct sockaddr_in *addr, socklen_t *addrlen) { char addrstr[INET6_ADDRSTRLEN]; client_t *client; if(sockfd > fd_client_index_limit) { fprintf(stderr, "cannot create any more clients (sockfd > _SO_OPEN_MAX)!\n"); return; } if(fd_client_index[sockfd]) { fprintf(stderr, "FD/client index inconsistent (already set)!\n"); return; } client = calloc(1, sizeof(client_t)); if(client == NULL) { perror("calloc()"); close(sockfd); return; } if(inet_ntop(AF_INET, &(addr->sin_addr), addrstr, INET6_ADDRSTRLEN) == NULL) { perror("inet_ntop()"); kill_client(client); return; } fprintf(stderr, "client connected from %s:%d\n", addrstr, ntohs(addr->sin_port)); client->sockfd = sockfd; client->socklen = addrlen; if((client->addr = calloc(1, *client->socklen)) == NULL) { perror("calloc()"); kill_client(client); return; } memcpy(client->addr, addr, *addrlen); client->is_sender = 1; client->message = first_message; client->last_message = NULL; client_list_add(client); fd_client_index[sockfd] = client; }
void http_callback_auth(httpd * webserver, request * r) { t_client *client; httpVar *token; char *mac; httpVar *logout = httpdGetVariableByName(r, "logout"); if ((token = httpdGetVariableByName(r, "token"))) { /* They supplied variable "token" */ if (!(mac = arp_get(r->clientAddr))) { /* We could not get their MAC address */ debug(LOG_ERR, "Failed to retrieve MAC address for ip %s", r->clientAddr); send_http_page(r, "WiFiDog Error", "Failed to retrieve your MAC address"); } else { /* We have their MAC address */ LOCK_CLIENT_LIST(); if ((client = client_list_find(r->clientAddr, mac)) == NULL) { debug(LOG_DEBUG, "New client for %s", r->clientAddr); client_list_add(r->clientAddr, mac, token->value); } else if (logout) { logout_client(client); } else { debug(LOG_DEBUG, "Client for %s is already in the client list", client->ip); } UNLOCK_CLIENT_LIST(); if (!logout) { /* applies for case 1 and 3 from above if */ authenticate_client(r); } free(mac); } } else { /* They did not supply variable "token" */ send_http_page(r, "WiFiDog error", "Invalid token"); } }
int main() { lineserver ls; struct pollfd ufds[max_clients+1]; client_list cl; pollset_handle h; signal(SIGPIPE, SIG_IGN); signal(SIGTERM, sighandler_term); lineserver_construct(&ls, ufds, max_clients+1, sendbuf_len, recvbuf_len); client_list_construct(&cl); if(lineserver_listen(&ls, 7584)==-1) { perror("lineserver bind() and listen()"); return 0; } while(!done) { if(poll(ufds, max_clients+1, 100)==-1) { perror("lineserver poll()"); break; } lineserver_operate(&ls); handle_packets(&cl, &ls); //close_finished_clients(&cl, &ls); if((h=lineserver_accept(&ls))!=-1) { printf("Added new connection %d.\n", h); client_list_add(&cl, h); } } client_list_destruct(&cl); lineserver_destruct(&ls); return 0; }
static int server_thread(SceSize args, void *argp) { int ret; UNUSED(ret); SceNetSockaddrIn serveraddr; DEBUG("Server thread started!\n"); /* Create server socket */ server_sockfd = sceNetSocket("FTPVita_server_sock", PSP2_NET_AF_INET, PSP2_NET_SOCK_STREAM, 0); DEBUG("Server socket fd: %d\n", server_sockfd); /* Fill the server's address */ serveraddr.sin_family = PSP2_NET_AF_INET; serveraddr.sin_addr.s_addr = sceNetHtonl(PSP2_NET_INADDR_ANY); serveraddr.sin_port = sceNetHtons(FTP_PORT); /* Bind the server's address to the socket */ ret = sceNetBind(server_sockfd, (SceNetSockaddr *)&serveraddr, sizeof(serveraddr)); DEBUG("sceNetBind(): 0x%08X\n", ret); /* Start listening */ ret = sceNetListen(server_sockfd, 128); DEBUG("sceNetListen(): 0x%08X\n", ret); while (1) { /* Accept clients */ SceNetSockaddrIn clientaddr; int client_sockfd; unsigned int addrlen = sizeof(clientaddr); DEBUG("Waiting for incoming connections...\n"); client_sockfd = sceNetAccept(server_sockfd, (SceNetSockaddr *)&clientaddr, &addrlen); if (client_sockfd >= 0) { DEBUG("New connection, client fd: 0x%08X\n", client_sockfd); /* Get the client's IP address */ char remote_ip[16]; sceNetInetNtop(PSP2_NET_AF_INET, &clientaddr.sin_addr.s_addr, remote_ip, sizeof(remote_ip)); INFO("Client %i connected, IP: %s port: %i\n", number_clients, remote_ip, clientaddr.sin_port); /* Create a new thread for the client */ char client_thread_name[64]; sprintf(client_thread_name, "FTPVita_client_%i_thread", number_clients); SceUID client_thid = sceKernelCreateThread( client_thread_name, client_thread, 0x10000100, 0x10000, 0, 0, NULL); DEBUG("Client %i thread UID: 0x%08X\n", number_clients, client_thid); /* Allocate the ClientInfo struct for the new client */ ClientInfo *client = malloc(sizeof(*client)); client->num = number_clients; client->thid = client_thid; client->ctrl_sockfd = client_sockfd; client->data_con_type = FTP_DATA_CONNECTION_NONE; sprintf(client->cur_path, "%s%s", FTP_DEFAULT_PREFIX, FTP_DEFAULT_PATH); memcpy(&client->addr, &clientaddr, sizeof(client->addr)); /* Add the new client to the client list */ client_list_add(client); /* Start the client thread */ sceKernelStartThread(client_thid, sizeof(*client), client); number_clients++; } else { /* if sceNetAccept returns < 0, it means that the listening * socket has been closed, this means that we want to * finish the server thread */ DEBUG("Server socket closed, 0x%08X\n", client_sockfd); break; } } DEBUG("Server thread exiting!\n"); sceKernelExitDeleteThread(0); return 0; }
void client_new(struct player_control *player_control, int fd, const struct sockaddr *sa, size_t sa_length, int uid) { static unsigned int next_client_num; struct client *client; char *remote; assert(player_control != NULL); assert(fd >= 0); #ifdef HAVE_LIBWRAP if (sa->sa_family != AF_UNIX) { char *hostaddr = sockaddr_to_string(sa, sa_length, NULL); const char *progname = g_get_prgname(); struct request_info req; request_init(&req, RQ_FILE, fd, RQ_DAEMON, progname, 0); fromhost(&req); if (!hosts_access(&req)) { /* tcp wrappers says no */ g_log(G_LOG_DOMAIN, LOG_LEVEL_SECURE, "libwrap refused connection (libwrap=%s) from %s", progname, hostaddr); g_free(hostaddr); close_socket(fd); return; } g_free(hostaddr); } #endif /* HAVE_WRAP */ if (client_list_is_full()) { g_warning("Max Connections Reached!"); close_socket(fd); return; } client = g_new0(struct client, 1); client->player_control = player_control; client->channel = g_io_channel_new_socket(fd); /* GLib is responsible for closing the file descriptor */ g_io_channel_set_close_on_unref(client->channel, true); /* NULL encoding means the stream is binary safe; the MPD protocol is UTF-8 only, but we are doing this call anyway to prevent GLib from messing around with the stream */ g_io_channel_set_encoding(client->channel, NULL, NULL); /* we prefer to do buffering */ g_io_channel_set_buffered(client->channel, false); client->source_id = g_io_add_watch(client->channel, G_IO_IN|G_IO_ERR|G_IO_HUP, client_in_event, client); client->input = fifo_buffer_new(4096); client->permission = getDefaultPermissions(); client->uid = uid; client->last_activity = g_timer_new(); client->cmd_list = NULL; client->cmd_list_OK = -1; client->cmd_list_size = 0; client->deferred_send = g_queue_new(); client->deferred_bytes = 0; client->num = next_client_num++; client->send_buf_used = 0; client->subscriptions = NULL; client->messages = NULL; client->num_messages = 0; (void)send(fd, GREETING, sizeof(GREETING) - 1, 0); client_list_add(client); remote = sockaddr_to_string(sa, sa_length, NULL); g_log(G_LOG_DOMAIN, LOG_LEVEL_SECURE, "[%u] opened from %s", client->num, remote); g_free(remote); }