void *thread_connection(void *args) { int connection_socket = ((struct thread_arguments *) args ) -> connection_socket; struct sockaddr_in client_address = ((struct thread_arguments *) args ) -> client_address; struct client_data data = get_client_address(client_address); char buffer[BUFSIZE]; bzero(buffer, BUFSIZE); int status = recv(connection_socket, buffer, BUFSIZE, MSG_DONTWAIT); if (WHITELIST != NULL && check_whitelist(data.ip_address) == NULL) { display_info(data, NULL, "Rejected connection from unknown user."); save_log(NULL, data.ip_address, data.hostname); if (write(connection_socket, "You are not whitelisted!\n", 26) < 0) printf("Error writing on stream socket\n"); close(connection_socket); pthread_exit(NULL); } if (BANLIST != NULL && check_banlist(data.ip_address) != NULL) { display_info(data, NULL, "Rejected connection from banned user."); save_log(NULL, data.ip_address, data.hostname); if (write(connection_socket, "You are banned!\n", 17) < 0) printf("Error writing on stream socket\n"); close(connection_socket); pthread_exit(NULL); } if (check_protocol(buffer) == 1) status = -1; if (status != -1) { char slug[SLUG_SIZE+8]; generate_url(buffer, slug, SLUG_SIZE+8, data); save_log(slug, data.ip_address, data.hostname); char response[strlen(slug) + strlen(DOMAIN) + 2]; snprintf(response, sizeof response, "%s%s\n", DOMAIN, slug); if (write(connection_socket, response, strlen(response)) < 0) printf("Error writing on stream socket\n"); } else { display_info(data, NULL, "Invalid connection."); save_log(NULL, data.ip_address, data.hostname); if (write(connection_socket, "Use netcat.\n", 12) < 0) printf("Error writing on stream socket\n"); } close(connection_socket); pthread_exit(NULL); }
static void listen_ready_read (int fd) { int s; struct SOCKADDR_IN saddr; int siz = sizeof(saddr); #ifdef CONFIG_IPV6 char buf[HOSTLEN+1]; #else char buf[16]; #endif struct htlc_conn *htlc; s = accept(fd, (struct SOCKADDR *)&saddr, &siz); if (s < 0) { hxd_log("htls: accept: %s", strerror(errno)); return; } if (s >= hxd_open_max) { hxd_log("%s:%d: %d >= hxd_open_max (%d)", __FILE__, __LINE__, s, hxd_open_max); close(s); return; } fd_closeonexec(s, 1); fd_blocking(s, 0); #ifdef CONFIG_IPV6 inet_ntop(AFINET, (char *)&saddr.SIN_ADDR, buf, sizeof(buf)); #else inet_ntoa_r(saddr.SIN_ADDR, buf, sizeof(buf)); #endif hxd_log("%s:%u -- htlc connection accepted", buf, ntohs(saddr.SIN_PORT)); htlc = xmalloc(sizeof(struct htlc_conn)); memset(htlc, 0, sizeof(struct htlc_conn)); htlc->sockaddr = saddr; hxd_files[s].ready_read = htlc_read; hxd_files[s].ready_write = htlc_write; hxd_files[s].conn.htlc = htlc; htlc->fd = s; htlc->rcv = rcv_magic; htlc->trans = 1; htlc->chattrans = 1; htlc->put_limit = hxd_cfg.limits.individual_uploads > HTXF_PUT_MAX ? HTXF_PUT_MAX : hxd_cfg.limits.individual_uploads; htlc->get_limit = hxd_cfg.limits.individual_downloads > HTXF_GET_MAX ? HTXF_GET_MAX : hxd_cfg.limits.individual_downloads; htlc->limit_out_Bps = hxd_cfg.limits.out_Bps; INITLOCK_HTXF(htlc); if (high_fd < s) high_fd = s; htlc->flags.visible = 1; htlc->identfd = -1; if (check_banlist(htlc)) return; htlc->access_extra.can_login = 1; timer_add_secs(14, login_timeout, htlc); if (hxd_cfg.options.ident) { start_ident(htlc); } else { qbuf_set(&htlc->in, 0, HTLC_MAGIC_LEN); FD_SET(s, &hxd_rfds); } }