url::url(const char* URL, bool resolve):found_host(false) { parse_url(URL); if(resolve){ resolve_addr(); } }
int resolve_proto(RES_CONTEXT *context, VSTREAM *stream) { int flags; if (attr_scan(stream, ATTR_FLAG_STRICT, ATTR_TYPE_STR, MAIL_ATTR_ADDR, query, ATTR_TYPE_END) != 1) return (-1); resolve_addr(context, STR(query), channel, nexthop, nextrcpt, &flags); if (msg_verbose) msg_info("%s -> (`%s' `%s' `%s' `%d')", STR(query), STR(channel), STR(nexthop), STR(nextrcpt), flags); attr_print(stream, ATTR_FLAG_NONE, ATTR_TYPE_STR, MAIL_ATTR_TRANSPORT, STR(channel), ATTR_TYPE_STR, MAIL_ATTR_NEXTHOP, STR(nexthop), ATTR_TYPE_STR, MAIL_ATTR_RECIP, STR(nextrcpt), ATTR_TYPE_NUM, MAIL_ATTR_FLAGS, flags, ATTR_TYPE_END); if (vstream_fflush(stream) != 0) { msg_warn("write resolver reply: %m"); return (-1); } return (0); }
/* Like resolve_addr but exit on error */ void resolve(struct sockaddr *addr, char *hostname) { if (resolve_addr(addr, hostname) == -1) { fprintf(stderr, "Unable to resolve '%s'\n", hostname); exit(1); } }
/* Connects to a random DHT server listed in the DHTservers file */ int init_connection(Messenger *m) { FILE *fp = NULL; if (DHT_isconnected(m->dht)) return 0; fp = fopen(SRVLIST_FILE, "r"); if (!fp) return 1; char servers[MAXSERVERS][MAXLINE]; char line[MAXLINE]; int linecnt = 0; while (fgets(line, sizeof(line), fp) && linecnt < MAXSERVERS) { if (strlen(line) > MINLINE) strcpy(servers[linecnt++], line); } if (linecnt < 1) { fclose(fp); return 2; } fclose(fp); char *server = servers[rand() % linecnt]; char *ip = strtok(server, " "); char *port = strtok(NULL, " "); char *key = strtok(NULL, " "); if (!ip || !port || !key) return 3; IP_Port dht; dht.port = htons(atoi(port)); uint32_t resolved_address = resolve_addr(ip); if (resolved_address == 0) return 0; dht.ip.i = resolved_address; unsigned char *binary_string = hex_string_to_bin(key); DHT_bootstrap(m->dht, dht, binary_string); free(binary_string); return 0; }
int tun_config(struct tundev *tun, const char *ifconf, int fd, int mtu, int prot, int global, int v, const char *server, int port, const char *dns) { struct sockaddr addr; struct tundev_context *ctx = tun->contexts; if (resolve_addr(server, port, &addr)) { logger_stderr("Invalid server address"); return 1; } char *cidr = strchr(ifconf, '/'); if(!cidr) { logger_stderr("ifconf syntax error: %s", ifconf); exit(0); } uint8_t ipaddr[16] = {0}; memcpy(ipaddr, ifconf, (uint32_t) (cidr - ifconf)); in_addr_t netmask = 0xffffffff; netmask = netmask << (32 - atoi(++cidr)); tun->netmask = netmask; tun->network = inet_addr((const char *) ipaddr) & htonl(netmask); verbose = v; protocol = prot; struct sockaddr_in dns_server; uv_ip4_addr(dns, DNS_PORT, &dns_server); tun->dns_server = *((struct sockaddr *) &dns_server); tun->mtu = mtu; tun->addr = addr; tun->global = global; ctx->tunfd = fd; uv_async_init(uv_default_loop(), &ctx->async_handle, tun_close); uv_unref((uv_handle_t *) &ctx->async_handle); return 0; }
void cmd_connect(ToxWindow *self, Messenger *m, char **args) { IP_Port dht; char *ip = args[1]; char *port = args[2]; char *key = args[3]; if (atoi(port) == 0) { wprintw(self->window, "Invalid syntax.\n"); return; } dht.port = htons(atoi(port)); uint32_t resolved_address = resolve_addr(ip); if (resolved_address == 0) { return; } dht.ip.i = resolved_address; unsigned char *binary_string = hex_string_to_bin(key); DHT_bootstrap(dht, binary_string); free(binary_string); }
int server_generic_bind(server_generic_t *server, const char *saddr, unsigned int port) { int ret; ret = resolve_addr(server, saddr, port); if ( ret < 0 ) return ret; #if ! ((defined _WIN32 || defined __WIN32__) && !defined __CYGWIN__) if ( server->sa->sa_family == AF_UNIX ) ret = unix_server_start(server); else #endif ret = inet_server_start(server, server->sa, server->slen); if ( ret < 0 ) { free(server->sa); server->sa = NULL; return ret; } return sg_bind_common(server, port); }
static void execute(ToxWindow* self, char* cmd) { // quit/exit: Exit program. if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit")) { endwin(); exit(0); } else if(!strncmp(cmd, "connect ", strlen("connect "))) { char* ip; char* port; char* key; IP_Port dht; ip = strchr(cmd, ' '); if(ip == NULL) { return; } ip++; port = strchr(ip, ' '); if(port == NULL) { return; } port[0] = 0; port++; key = strchr(port, ' '); if(key == NULL) { return; } key[0] = 0; key++; if(atoi(port) == 0) { return; } wprintw(self->window, "ip=%s, port=%s, key=%s\n", ip, port, key); dht.port = htons(atoi(port)); int resolved_address = resolve_addr(ip); if (resolved_address == -1) { return; } dht.ip.i = resolved_address; DHT_bootstrap(dht, hex_string_to_bin(key)); } else if(!strncmp(cmd, "add ", strlen("add "))) { char* id; char* msg; int num; id = strchr(cmd, ' '); if(id == NULL) { return; } id++; msg = strchr(id, ' '); if(msg == NULL) { return; } msg[0] = 0; msg++; num = m_addfriend((uint8_t*) id, (uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Friend added as %d.\n", num); } else if(!strncmp(cmd, "status ", strlen("status "))) { char* msg; msg = strchr(cmd, ' '); if(msg == NULL) { return; } msg++; m_set_userstatus((uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Status set to: %s.\n", msg); } else if(!strncmp(cmd, "nick ", strlen("nick "))) { char* nick; nick = strchr(cmd, ' '); if(nick == NULL) { return; } nick++; setname((uint8_t*) nick, strlen(nick)+1); wprintw(self->window, "Nickname set to: %s.\n", nick); } else if(!strcmp(cmd, "myid")) { // XXX: Clean this up char idstring0[200]; char idstring1[32][5]; char idstring2[32][5]; uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) strcpy(idstring1[i], "0"); else strcpy(idstring1[i], ""); sprintf(idstring2[i], "%hhX", self_public_key[i]); } for (i=0; i<32; i++) { strcat(idstring0, idstring1[i]); strcat(idstring0, idstring2[i]); } wprintw(self->window, "%s\n", idstring0); } else if(!strncmp(cmd, "accept ", strlen("accept "))) { char* id; int num; id = strchr(cmd, ' '); if(id == NULL) { return; } id++; num = atoi(id); if(num >= num_requests) { return; } num = m_addfriend_norequest(pending_requests[num]); wprintw(self->window, "Friend accepted as: %d.\n", num); } else if(!strncmp(cmd, "msg ", strlen("msg "))) { char* id; char* msg; id = strchr(cmd, ' '); if(id == NULL) { return; } id++; msg = strchr(id, ' '); if(msg == NULL) { return; } msg[0] = 0; msg++; if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) != 1) { wprintw(self->window, "Error occurred while sending message.\n"); } else { wprintw(self->window, "Message successfully sent.\n"); } } }
int main(int argc, char *argv[]) { if (argc < 4) { printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); exit(0); } int c; int on = 0; initMessenger(); //if keyfiles exist if(argc > 4){ if(strncmp(argv[4], "nokey", 6) < 0){ //load_key(); } } else { load_key(); } m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); m_callback_namechange(print_nickchange); m_callback_userstatus(print_statuschange); char idstring0[200]; char idstring1[32][5]; char idstring2[32][5]; uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) { strcpy(idstring1[i],"0"); } else { strcpy(idstring1[i], ""); } sprintf(idstring2[i], "%hhX",self_public_key[i]); } strcpy(idstring0,"[i] your ID: "); for(i=0; i<32; i++) { strcat(idstring0,idstring1[i]); strcat(idstring0,idstring2[i]); } initscr(); noecho(); raw(); getmaxyx(stdscr,y,x); new_lines(idstring0); new_lines("[i] commands: /f ID (to add friend), /m friendnumber message (to send message), /s status (to change status), /n nick (to change nickname), /q (to quit)"); strcpy(line, ""); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); if (resolved_address != -1) { bootstrap_ip_port.ip.i = resolved_address; } else { exit(1); } DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); nodelay(stdscr, TRUE); while(true) { if (on == 0 && DHT_isconnected()) { new_lines("[i] connected to DHT\n[i] define username with /n"); on = 1; } doMessenger(); c_sleep(1); do_refresh(); c = getch(); if (c == ERR || c == 27) continue; getmaxyx(stdscr, y, x); if (c == '\n') { line_eval(lines, line); strcpy(line, ""); } else if (c == 127) { line[strlen(line) - 1] = '\0'; } else if (isalnum(c) || ispunct(c) || c == ' ') { strcpy(line, appender(line, (char) c)); } } endwin(); return 0; }
int main(int argc, char *argv[]) { if (argc < 4) { printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); exit(0); } if (initMessenger() == -1) { printf("initMessenger failed"); exit(0); } if (argc > 4) { if(strncmp(argv[4], "nokey", 6) < 0) { } } else { load_key(); } int nameloaded = 0; int statusloaded = 0; FILE* name_file = NULL; name_file = fopen("namefile.txt", "r"); if(name_file) { uint8_t name[MAX_NAME_LENGTH]; while (fgets(line, MAX_NAME_LENGTH, name_file) != NULL) { sscanf(line, "%s", (char*)name); } setname(name, strlen((char*)name)+1); nameloaded = 1; printf("%s\n", name); } fclose(name_file); FILE* status_file = NULL; status_file = fopen("statusfile.txt", "r"); if(status_file) { uint8_t status[MAX_USERSTATUS_LENGTH]; while (fgets(line, MAX_USERSTATUS_LENGTH, status_file) != NULL) { sscanf(line, "%s", (char*)status); } m_set_userstatus(status, strlen((char*)status)+1); statusloaded = 1; printf("%s\n", status); } fclose(status_file); m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); m_callback_namechange(print_nickchange); m_callback_userstatus(print_statuschange); char idstring1[PUB_KEY_BYTES][5]; char idstring2[PUB_KEY_BYTES][5]; int i; for(i = 0; i < PUB_KEY_BYTES; i++) { if(self_public_key[i] < (PUB_KEY_BYTES/2)) strcpy(idstring1[i],"0"); else strcpy(idstring1[i], ""); sprintf(idstring2[i], "%hhX",self_public_key[i]); } strcpy(users_id,"[i] your ID: "); int j; for (j = 0; j < PUB_KEY_BYTES; j++) { strcat(users_id,idstring1[j]); strcat(users_id,idstring2[j]); } do_header(); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); if (resolved_address != 0) bootstrap_ip_port.ip.i = resolved_address; else exit(1); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); int c; int on = 0; _beginthread(get_input, 0, NULL); if (nameloaded == 1) { printf("\nNickname automatically loaded"); printf("\n---------------------------------"); } if (statusloaded == 1) { printf("\nStatus automatically loaded"); printf("\n---------------------------------"); } while(1) { if (on == 1 && DHT_isconnected() == -1) { printf("\n---------------------------------"); printf("\n[i] Disconnected from the DHT"); printf("\n---------------------------------\n\n"); on = 0; } if (on == 0 && DHT_isconnected()) { printf("\n[i] Connected to DHT"); printf("\n---------------------------------\n\n"); on = 1; } doMessenger(); Sleep(1); } return 0; }
int main(int argc, char *argv[]) { if (argc < 4) { printf("[!] Usage: %s [IP] [port] [public_key] <nokey>\n", argv[0]); exit(0); } if (initMessenger() == -1) { printf("initMessenger failed"); exit(0); } if (argc > 4) { if(strncmp(argv[4], "nokey", 6) < 0) { //nothing } else { load_key(); } } m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); m_callback_namechange(print_nickchange); m_callback_userstatus(print_statuschange); system("cls"); char idstring0[200]; char idstring1[32][5]; char idstring2[32][5]; uint32_t i; for(i = 0; i < 32; i++) { if(self_public_key[i] < 16) strcpy(idstring1[i],"0"); else strcpy(idstring1[i], ""); sprintf(idstring2[i], "%hhX",self_public_key[i]); } strcpy(idstring0,"\n[i] your ID: "); for (i=0; i<32; i++) { strcat(idstring0,idstring1[i]); strcat(idstring0,idstring2[i]); } printf(idstring0); do_header(); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); if (resolved_address != -1) bootstrap_ip_port.ip.i = resolved_address; else exit(1); DHT_bootstrap(bootstrap_ip_port, hex_string_to_bin(argv[3])); int c; int on = 0; _beginthread(get_input, 0, NULL); while(1) { if (on == 0 && DHT_isconnected()) { printf("\n[i] connected to DHT\n\n"); on = 1; } doMessenger(); } return 0; }
static struct connection * connection_new(int iscsi_fd, const struct iscsi_daemon_request *request) { struct connection *conn; struct iscsi_session_limits *isl; struct addrinfo *from_ai, *to_ai; const char *from_addr, *to_addr; #ifdef ICL_KERNEL_PROXY struct iscsi_daemon_connect idc; #endif int error, sockbuf; conn = calloc(1, sizeof(*conn)); if (conn == NULL) log_err(1, "calloc"); /* * Default values, from RFC 3720, section 12. */ conn->conn_header_digest = CONN_DIGEST_NONE; conn->conn_data_digest = CONN_DIGEST_NONE; conn->conn_initial_r2t = true; conn->conn_immediate_data = true; conn->conn_max_recv_data_segment_length = 8192; conn->conn_max_send_data_segment_length = 8192; conn->conn_max_burst_length = 262144; conn->conn_first_burst_length = 65536; conn->conn_iscsi_fd = iscsi_fd; conn->conn_session_id = request->idr_session_id; memcpy(&conn->conn_conf, &request->idr_conf, sizeof(conn->conn_conf)); memcpy(&conn->conn_isid, &request->idr_isid, sizeof(conn->conn_isid)); conn->conn_tsih = request->idr_tsih; /* * Read the driver limits and provide reasonable defaults for the ones * the driver doesn't care about. If a max_snd_dsl is not explicitly * provided by the driver then we'll make sure both conn->max_snd_dsl * and isl->max_snd_dsl are set to the rcv_dsl. This preserves historic * behavior. */ isl = &conn->conn_limits; memcpy(isl, &request->idr_limits, sizeof(*isl)); if (isl->isl_max_recv_data_segment_length == 0) isl->isl_max_recv_data_segment_length = (1 << 24) - 1; if (isl->isl_max_send_data_segment_length == 0) isl->isl_max_send_data_segment_length = isl->isl_max_recv_data_segment_length; if (isl->isl_max_burst_length == 0) isl->isl_max_burst_length = (1 << 24) - 1; if (isl->isl_first_burst_length == 0) isl->isl_first_burst_length = (1 << 24) - 1; if (isl->isl_first_burst_length > isl->isl_max_burst_length) isl->isl_first_burst_length = isl->isl_max_burst_length; /* * Limit default send length in case it won't be negotiated. * We can't do it for other limits, since they may affect both * sender and receiver operation, and we must obey defaults. */ if (conn->conn_max_send_data_segment_length > isl->isl_max_send_data_segment_length) { conn->conn_max_send_data_segment_length = isl->isl_max_send_data_segment_length; } from_addr = conn->conn_conf.isc_initiator_addr; to_addr = conn->conn_conf.isc_target_addr; if (from_addr[0] != '\0') resolve_addr(conn, from_addr, &from_ai, true); else from_ai = NULL; resolve_addr(conn, to_addr, &to_ai, false); #ifdef ICL_KERNEL_PROXY if (conn->conn_conf.isc_iser) { memset(&idc, 0, sizeof(idc)); idc.idc_session_id = conn->conn_session_id; if (conn->conn_conf.isc_iser) idc.idc_iser = 1; idc.idc_domain = to_ai->ai_family; idc.idc_socktype = to_ai->ai_socktype; idc.idc_protocol = to_ai->ai_protocol; if (from_ai != NULL) { idc.idc_from_addr = from_ai->ai_addr; idc.idc_from_addrlen = from_ai->ai_addrlen; } idc.idc_to_addr = to_ai->ai_addr; idc.idc_to_addrlen = to_ai->ai_addrlen; log_debugx("connecting to %s using ICL kernel proxy", to_addr); error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc); if (error != 0) { fail(conn, strerror(errno)); log_err(1, "failed to connect to %s " "using ICL kernel proxy: ISCSIDCONNECT", to_addr); } return (conn); } #endif /* ICL_KERNEL_PROXY */ if (conn->conn_conf.isc_iser) { fail(conn, "iSER not supported"); log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY " "does not support iSER"); } conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype, to_ai->ai_protocol); if (conn->conn_socket < 0) { fail(conn, strerror(errno)); log_err(1, "failed to create socket for %s", from_addr); } sockbuf = SOCKBUF_SIZE; if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_RCVBUF, &sockbuf, sizeof(sockbuf)) == -1) log_warn("setsockopt(SO_RCVBUF) failed"); sockbuf = SOCKBUF_SIZE; if (setsockopt(conn->conn_socket, SOL_SOCKET, SO_SNDBUF, &sockbuf, sizeof(sockbuf)) == -1) log_warn("setsockopt(SO_SNDBUF) failed"); if (from_ai != NULL) { error = bind(conn->conn_socket, from_ai->ai_addr, from_ai->ai_addrlen); if (error != 0) { fail(conn, strerror(errno)); log_err(1, "failed to bind to %s", from_addr); } } log_debugx("connecting to %s", to_addr); error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen); if (error != 0) { fail(conn, strerror(errno)); log_err(1, "failed to connect to %s", to_addr); } return (conn); }
/* This reads the configuration file, and returns a struct server_conf_s with: *an error number: *-1 = file wasn't read, for whatever reason *-2 = no bootstrap servers found *the port *the location of the keys file *the location of the PID file *the list of bootstrap servers */ struct server_conf_s configure_server(char *cfg_file) { config_t cfg; config_setting_t *server_list; /* This one will be strcpy'd into the pid_file array in server_conf */ const char *pid_file_tmp; const char *keys_file_tmp; /* Remote bootstrap server variables */ int bs_port; const char *bs_ip; const char *bs_pk; /* The big struct */ static struct server_conf_s server_conf; /* Set both to their default values. If there's an error with opening/reading the config file, we return right away */ server_conf.port = DEFAULT_PORT; strcpy(server_conf.pid_file, DEFAULT_PID_FILE); strcpy(server_conf.keys_file, DEFAULT_KEYS_FILE); config_init(&cfg); /* Read the file. If there is an error, report it and exit. */ if (! config_read_file(&cfg, cfg_file)) { fprintf(stderr, "%s:%d - %s\n", config_error_file(&cfg), config_error_line(&cfg), config_error_text(&cfg)); config_destroy(&cfg); server_conf.err = -1; return server_conf; } /* Get the port to listen on */ if (config_lookup_int(&cfg, "port", &server_conf.port)) { //printf("Port: %d\n", port); } else { fprintf(stderr, "No 'port' setting in configuration file.\n"); } /* Get PID file location */ if (config_lookup_string(&cfg, "pid_file", &pid_file_tmp)) { //printf("PID file: %s\n", pid_file_tmp); strcpy(server_conf.pid_file, pid_file_tmp); } else { fprintf(stderr, "No 'pid_file' setting in configuration file.\n"); } /* Get keys file location */ if (config_lookup_string(&cfg, "keys_file", &keys_file_tmp)) { //printf("Keys file: %s\n", keys_file_tmp); strcpy(server_conf.keys_file, keys_file_tmp); } else { fprintf(stderr, "No 'keys_file' setting in configuration file.\n"); } /* Get all the servers in the list */ server_list = config_lookup(&cfg, "bootstrap_servers"); if (server_list != NULL) { int count = config_setting_length(server_list); int i; char tmp_ip[30]; /* IP */ char tmp_pk[64]; /* bs_pk */ for (i = 0; i < count; ++i) { config_setting_t *server = config_setting_get_elem(server_list, i); /* Get a pointer on the key aray */ uint8_t *bs_pk_p = server_conf.info[i].bs_pk; /* Only output the record if all of the expected fields are present. */ if (!(config_setting_lookup_string(server, "ip", &bs_ip) && config_setting_lookup_int(server, "port", &bs_port) && config_setting_lookup_string(server, "bs_pk", &bs_pk))) continue; /* Converting all that stuff into usable formats and storing it away in the server_info struct */ server_conf.info[i].valid = 1; if (resolve_addr(strcpy(tmp_ip, bs_ip)) == 0) { server_conf.info[i].valid = 0; printf("bootstrap_server %d: Invalid IP\n", i); } if (strlen(bs_pk) != 64) { server_conf.info[i].valid = 0; printf("bootstrap_server %d: Invalid public key\n", i); } if (!bs_port) { server_conf.info[i].valid = 0; printf("bootstrap_server %d: Invalid port\n", i); } server_conf.info[i].conn.ip.i = resolve_addr(strcpy(tmp_ip, bs_ip)); server_conf.info[i].conn.port = htons(bs_port); b16_to_key(strcpy(tmp_pk, bs_pk), bs_pk_p); } /* Check if at least one server entry is valid */ for (i = 0; i < 32; ++i) { if (server_conf.info[i].valid) break; else server_conf.err = -2; } } else { server_conf.err = -2; } config_destroy(&cfg); return server_conf; }
static void connect_to_server(void) { /*Check for loops. Won't work if Listen undefined */ if(info->server_control.address.sin_addr.s_addr == config.listen_address.sin_addr.s_addr && info->server_control.address.sin_port == config.listen_address.sin_port) die(ERROR, "Attempt to connect to self. " "Do you need to set DoNTP to yes?", 421, "Proxy tried to loop. Closing connection", 0); if(info->server_control.address.sin_addr.s_addr == 0) { if(!config.ntp) die(ERROR, "Frox unable to determine destination address. " "Do you need to set DoNTP to yes?", 501, "Unable to contact server", 0); else if(config.ntpdest.sin_addr.s_addr) die(ERROR, "Frox unable to determine destination address. " "Try commenting out NTPAddress", 501, "Unable to contact server", 0); else die(ERROR, "Frox unable to determine detination address. " "See FAQ for troubleshooting.", 501, "Unable to contact server", 0); } resolve_addr(&info->final_server_address.sin_addr, info->server_name); if(!config_connectionok(&info->client_control.address, &info->final_server_address, info->username ? sstr_buf(info-> username) : 0)) die(ATTACK, "Denied by ACLs.", 501, "Connection denied. Bye", 0); if(config.ftpproxy.sin_addr.s_addr) info->server_control.address = config.ftpproxy; write_log(VERBOSE, "Connecting to server..."); info->server_control.fd = connect_to_socket(&info->server_control.address, &config.tcpoutaddr, config.contports); if(info->server_control.fd == -1) die(ERROR, "Connection closed -- unable to contact server", 501, "Proxy unable to contact ftp server", 0); write_log(VERBOSE, " OK"); if(config.loglevel >= VERBOSE) { /*Save the overhead of DNS lookups */ write_log(VERBOSE, "Apparent address = %s", sstr_buf(addr2name (info->apparent_server_address.sin_addr))); write_log(VERBOSE, "Real address = %s", sstr_buf(addr2name (info->final_server_address.sin_addr))); write_log(VERBOSE, "Proxy address = %s", sstr_buf(addr2name (info->server_control.address.sin_addr))); } ssl_init(); }
void process_client(client_arg_t *args) { /* The thread responsible for accepting a client connection * and processing the request. * The QUERY cmd is processed entirely by this thread. * The MONITOR and CONNECT cmds are setup and then placed * in the conf->objs list to be handled by mux_io(). */ int sd; server_conf_t *conf; req_t *req; /* Free the tmp struct that was created by accept_client() * in order to pass multiple args to this thread. */ assert(args != NULL); sd = args->sd; conf = args->conf; free(args); DPRINTF((5, "Processing new client.\n")); x_pthread_detach(pthread_self()); req = create_req(); if (resolve_addr(conf, req, sd) < 0) goto err; if (recv_greeting(req) < 0) goto err; if (recv_req(req) < 0) goto err; if (query_consoles(conf, req) < 0) goto err; if (validate_req(req) < 0) goto err; /* send_rsp() needs to know if the reset command is supported. * Since it cannot check resetCmd in the server_conf struct, * we set a flag in the request struct instead. */ if (conf->resetCmd) req->enableReset = 1; switch(req->command) { case CONMAN_CMD_CONNECT: if (perform_connect_cmd(req, conf) < 0) goto err; break; case CONMAN_CMD_MONITOR: if (perform_monitor_cmd(req, conf) < 0) goto err; break; case CONMAN_CMD_QUERY: if (perform_query_cmd(req) < 0) goto err; break; default: log_msg(LOG_WARNING, "Received invalid command=%d from <%s@%s:%d>", req->command, req->user, req->fqdn, req->port); goto err; } return; err: destroy_req(req); return; }
int main(int argc, char *argv[]) { int on = 0; int c = 0; int i = 0; char *filename = "data"; char idstring[200] = {0}; if (argc < 4) { printf("[!] Usage: %s [IP] [port] [public_key] <keyfile>\n", argv[0]); exit(0); } for(i = 0; i < argc; i++) { if (argv[i] == NULL){ break; } else if(argv[i][0] == '-') { if(argv[i][1] == 'h') { print_help(); exit(0); } else if(argv[i][1] == 'f') { if(argv[i + 1] != NULL) filename = argv[i + 1]; else { fputs("[!] you passed '-f' without giving an argument!\n", stderr); } } } } initMessenger(); load_key(filename); m_callback_friendrequest(print_request); m_callback_friendmessage(print_message); m_callback_namechange(print_nickchange); m_callback_statusmessage(print_statuschange); initscr(); noecho(); raw(); getmaxyx(stdscr, y, x); new_lines("/h for list of commands"); get_id(idstring); new_lines(idstring); strcpy(line, ""); IP_Port bootstrap_ip_port; bootstrap_ip_port.port = htons(atoi(argv[2])); int resolved_address = resolve_addr(argv[1]); if (resolved_address != 0) bootstrap_ip_port.ip.i = resolved_address; else exit(1); unsigned char *binary_string = hex_string_to_bin(argv[3]); DHT_bootstrap(bootstrap_ip_port, binary_string); free(binary_string); nodelay(stdscr, TRUE); while(true) { if (on == 0 && DHT_isconnected()) { new_lines("[i] connected to DHT\n[i] define username with /n"); on = 1; } doMessenger(); c_sleep(1); do_refresh(); c = getch(); if (c == ERR || c == 27) continue; getmaxyx(stdscr, y, x); if (c == '\n') { line_eval(line); strcpy(line, ""); } else if (c == 8 || c == 127) { line[strlen(line)-1] = '\0'; } else if (isalnum(c) || ispunct(c) || c == ' ') { strcpy(line, appender(line, (char) c)); } } endwin(); return 0; }
static struct connection * connection_new(unsigned int session_id, const struct iscsi_session_conf *conf, int iscsi_fd) { struct connection *conn; struct addrinfo *from_ai, *to_ai; const char *from_addr, *to_addr; #ifdef ICL_KERNEL_PROXY struct iscsi_daemon_connect idc; #endif int error; conn = calloc(1, sizeof(*conn)); if (conn == NULL) log_err(1, "calloc"); /* * Default values, from RFC 3720, section 12. */ conn->conn_header_digest = CONN_DIGEST_NONE; conn->conn_data_digest = CONN_DIGEST_NONE; conn->conn_initial_r2t = true; conn->conn_immediate_data = true; conn->conn_max_data_segment_length = 8192; conn->conn_max_burst_length = 262144; conn->conn_first_burst_length = 65536; conn->conn_session_id = session_id; conn->conn_iscsi_fd = iscsi_fd; /* * XXX: Should we sanitize this somehow? */ memcpy(&conn->conn_conf, conf, sizeof(conn->conn_conf)); from_addr = conn->conn_conf.isc_initiator_addr; to_addr = conn->conn_conf.isc_target_addr; if (from_addr[0] != '\0') resolve_addr(conn, from_addr, &from_ai, true); else from_ai = NULL; resolve_addr(conn, to_addr, &to_ai, false); #ifdef ICL_KERNEL_PROXY memset(&idc, 0, sizeof(idc)); idc.idc_session_id = conn->conn_session_id; if (conn->conn_conf.isc_iser) idc.idc_iser = 1; idc.idc_domain = to_ai->ai_family; idc.idc_socktype = to_ai->ai_socktype; idc.idc_protocol = to_ai->ai_protocol; if (from_ai != NULL) { idc.idc_from_addr = from_ai->ai_addr; idc.idc_from_addrlen = from_ai->ai_addrlen; } idc.idc_to_addr = to_ai->ai_addr; idc.idc_to_addrlen = to_ai->ai_addrlen; log_debugx("connecting to %s using ICL kernel proxy", to_addr); error = ioctl(iscsi_fd, ISCSIDCONNECT, &idc); if (error != 0) { fail(conn, strerror(errno)); log_err(1, "failed to connect to %s using ICL kernel proxy", to_addr); } #else /* !ICL_KERNEL_PROXY */ if (conn->conn_conf.isc_iser) { fail(conn, "iSER not supported"); log_errx(1, "iscsid(8) compiled without ICL_KERNEL_PROXY " "does not support iSER"); } conn->conn_socket = socket(to_ai->ai_family, to_ai->ai_socktype, to_ai->ai_protocol); if (conn->conn_socket < 0) { fail(conn, strerror(errno)); log_err(1, "failed to create socket for %s", from_addr); } if (from_ai != NULL) { error = bind(conn->conn_socket, from_ai->ai_addr, from_ai->ai_addrlen); if (error != 0) { fail(conn, strerror(errno)); log_err(1, "failed to bind to %s", from_addr); } } log_debugx("connecting to %s", to_addr); error = connect(conn->conn_socket, to_ai->ai_addr, to_ai->ai_addrlen); if (error != 0) { fail(conn, strerror(errno)); log_err(1, "failed to connect to %s", to_addr); } #endif /* !ICL_KERNEL_PROXY */ return (conn); }
int main(int argc, char *argv[]) { int rc; uv_loop_t *loop; struct sockaddr bind_addr; parse_opts(argc, argv); if (xsignal) { return signal_process(xsignal, pidfile); } if (!tunnel_mode || !dest_addr || !password) { print_usage(argv[0]); return 1; } if (init()) { return 1; } if (daemon_mode) { if (daemonize()) { return 1; } if (already_running(pidfile)) { logger_stderr("xtunnel already running."); return 1; } } loop = uv_default_loop(); rc = resolve_addr(source_addr, &bind_addr); if (rc) { logger_stderr("invalid local address"); return 1; } rc = resolve_addr(dest_addr, &target_addr); if (rc) { logger_stderr("invalid target address"); return 1; } if (concurrency <= 1) { struct server_context ctx; uv_tcp_init(loop, &ctx.tcp); rc = uv_tcp_bind(&ctx.tcp, &bind_addr, 0); if (rc) { logger_stderr("bind error: %s", uv_strerror(rc)); return 1; } rc = uv_listen((uv_stream_t*)&ctx.tcp, SOMAXCONN, source_accept_cb); if (rc == 0) { logger_log(LOG_INFO, "listening on %s", source_addr); setup_signal(loop, signal_cb, &ctx); uv_run(loop, UV_RUN_DEFAULT); close_loop(loop); } else { logger_stderr("listen error: %s", uv_strerror(rc)); } } else { struct server_context *servers = calloc(concurrency, sizeof(servers[0])); for (int i = 0; i < concurrency; i++) { struct server_context *ctx = servers + i; ctx->index = i; ctx->tcp_fd = create_socket(SOCK_STREAM, 1); ctx->accept_cb = source_accept_cb; ctx->nameserver_num = -1; ctx->local_addr = &bind_addr; rc = uv_sem_init(&ctx->semaphore, 0); rc = uv_thread_create(&ctx->thread_id, consumer_start, ctx); } logger_log(LOG_INFO, "listening on %s", source_addr); setup_signal(loop, signal_cb, servers); uv_run(loop, UV_RUN_DEFAULT); close_loop(loop); for (int i = 0; i < concurrency; i++) { uv_sem_wait(&servers[i].semaphore); } free(servers); } if (daemon_mode) { delete_pidfile(pidfile); } return 0; }
int main(int argc, char *argv[]) { int rc; uv_loop_t *loop; parse_opts(argc, argv); #if !defined(_WIN32) if (xsignal) { return signal_process(xsignal, pidfile); } #endif if (!password || !server_addr_buf) { print_usage(argv[0]); return 1; } init(); #if !defined(_WIN32) if (daemon_mode) { if (daemonize()) { return 1; } if (already_running(pidfile)) { logger_stderr("xsocks already running."); return 1; } } #endif loop = uv_default_loop(); rc = resolve_addr(local_addr, &bind_addr); if (rc) { logger_stderr("invalid local address"); return 1; } rc = resolve_addr(server_addr_buf, &server_addr); if (rc) { logger_stderr("invalid server address"); return 1; } udprelay_init(); if (concurrency <= 1) { struct server_context ctx; ctx.udprelay = 1; ctx.udp_fd = create_socket(SOCK_DGRAM, 0); ctx.local_addr = &bind_addr; ctx.server_addr = &server_addr; uv_tcp_init(loop, &ctx.tcp); rc = uv_tcp_bind(&ctx.tcp, &bind_addr, 0); if (rc) { logger_stderr("bind error: %s", uv_strerror(rc)); return 1; } rc = uv_listen((uv_stream_t*)&ctx.tcp, 128, client_accept_cb); if (rc == 0) { logger_log(LOG_INFO, "listening on %s", local_addr); #if !defined(_WIN32) setup_signal(loop, signal_cb, &ctx); #endif udprelay_start(loop, &ctx); uv_run(loop, UV_RUN_DEFAULT); close_loop(loop); } else { logger_stderr("listen error: %s", uv_strerror(rc)); } } else { #if !defined(_WIN32) struct server_context *servers = calloc(concurrency, sizeof(servers[0])); for (int i = 0; i < concurrency; i++) { struct server_context *ctx = servers + i; ctx->index = i; ctx->tcp_fd = create_socket(SOCK_STREAM, 1); ctx->udp_fd = create_socket(SOCK_DGRAM, 1); ctx->udprelay = 1; ctx->accept_cb = client_accept_cb; ctx->local_addr = &bind_addr; ctx->server_addr = &server_addr; rc = uv_sem_init(&ctx->semaphore, 0); rc = uv_thread_create(&ctx->thread_id, consumer_start, ctx); } logger_log(LOG_INFO, "listening on %s", local_addr); setup_signal(loop, signal_cb, servers); uv_run(loop, UV_RUN_DEFAULT); close_loop(loop); for (int i = 0; i < concurrency; i++) { uv_sem_wait(&servers[i].semaphore); } free(servers); #else logger_stderr("don't support multithreading."); return 1; #endif } udprelay_destroy(); #if !defined(_WIN32) if (daemon_mode) { delete_pidfile(pidfile); } #endif logger_exit(); return 0; }
static void execute(ToxWindow* self, char* cmd) { if(!strcmp(cmd, "quit") || !strcmp(cmd, "exit") || !strcmp(cmd, "q")) { endwin(); exit(0); } else if(!strncmp(cmd, "connect ", strlen("connect "))) { char* ip; char* port; char* key; IP_Port dht; ip = strchr(cmd, ' '); if(ip == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } ip++; port = strchr(ip, ' '); if(port == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } port[0] = 0; port++; key = strchr(port, ' '); if(key == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } key[0] = 0; key++; if(atoi(port) == 0) { wprintw(self->window, "Invalid syntax.\n"); return; } dht.port = htons(atoi(port)); uint32_t resolved_address = resolve_addr(ip); if (resolved_address == 0) { return; } dht.ip.i = resolved_address; DHT_bootstrap(dht, hex_string_to_bin(key)); } else if(!strncmp(cmd, "add ", strlen("add "))) { uint8_t id_bin[32]; size_t i; char xx[3]; uint32_t x; char* id; char* msg; int num; id = strchr(cmd, ' '); if(id == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } id++; msg = strchr(id, ' '); if(msg != NULL) { msg[0] = 0; msg++; } else msg = ""; if(strlen(id) != 2*32) { wprintw(self->window, "Invalid ID length.\n"); return; } for(i=0; i<32; i++) { xx[0] = id[2*i]; xx[1] = id[2*i+1]; xx[2] = '\0'; if(sscanf(xx, "%02x", &x) != 1) { wprintw(self->window, "Invalid ID.\n"); return; } id_bin[i] = x; } num = m_addfriend(id_bin, (uint8_t*) msg, strlen(msg)+1); switch (num) { case -1: wprintw(self->window, "Message is too long.\n"); break; case -2: wprintw(self->window, "Please add a message to your request.\n"); break; case -3: wprintw(self->window, "That appears to be your own ID.\n"); break; case -4: wprintw(self->window, "Friend request already sent.\n"); break; case -5: wprintw(self->window, "[i] Undefined error when adding friend.\n"); break; default: wprintw(self->window, "Friend added as %d.\n", num); on_friendadded(num); break; } } else if(!strcmp(cmd, "help")) { print_usage(self); } else if(!strncmp(cmd, "status ", strlen("status "))) { char* msg; msg = strchr(cmd, ' '); if(msg == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } msg++; m_set_userstatus((uint8_t*) msg, strlen(msg)+1); wprintw(self->window, "Status set to: %s\n", msg); } else if(!strncmp(cmd, "nick ", strlen("nick "))) { char* nick; nick = strchr(cmd, ' '); if(nick == NULL) { return; } nick++; setname((uint8_t*) nick, strlen(nick)+1); wprintw(self->window, "Nickname set to: %s.\n", nick); } else if(!strcmp(cmd, "myid")) { char id[32*2 + 1] = {0}; size_t i; for(i=0; i<32; i++) { char xx[3]; snprintf(xx, sizeof(xx), "%02x", self_public_key[i] & 0xff); strcat(id, xx); } wprintw(self->window, "%s\n", id); } else if(!strncmp(cmd, "accept ", strlen("accept "))) { char* id; int num; id = strchr(cmd, ' '); if(id == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } id++; num = atoi(id); if(num >= num_requests) { wprintw(self->window, "Invalid syntax.\n"); return; } num = m_addfriend_norequest(pending_requests[num]); if(num == -1) { wprintw(self->window, "Failed to add friend.\n"); } else { wprintw(self->window, "Friend accepted as: %d.\n", num); on_friendadded(num); } } else if(!strncmp(cmd, "msg ", strlen("msg "))) { char* id; char* msg; id = strchr(cmd, ' '); if(id == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } id++; msg = strchr(id, ' '); if(msg == NULL) { wprintw(self->window, "Invalid syntax.\n"); return; } msg[0] = 0; msg++; if(m_sendmessage(atoi(id), (uint8_t*) msg, strlen(msg)+1) < 0) { wprintw(self->window, "Error occurred while sending message.\n"); } else { wprintw(self->window, "Message successfully sent.\n"); } } else { wprintw(self->window, "Invalid syntax.\n"); } }