static void get_source_host_ip(void) { const char *hostname; IPADDR ip4, ip6; if (source_host_ok) return; /* FIXME: This will block! */ hostname = settings_get_str("hostname"); source_host_ok = *hostname != '\0' && net_gethostbyname(hostname, &ip4, &ip6) == 0; if (source_host_ok) save_ips(&ip4, &ip6, &source_host_ip4, &source_host_ip6); else { g_free_and_null(source_host_ip4); g_free_and_null(source_host_ip6); } }
/* ============= NET_StringToAdr localhost idnewt idnewt:28000 192.246.40.70 192.246.40.70:28000 ============= */ qboolean NET_StringToSockaddr (char *s, struct sockaddr *sadr) { struct hostent *h; char *colon; char copy[128]; memset (sadr, 0, sizeof(*sadr)); ((struct sockaddr_in *)sadr)->sin_family = AF_INET; ((struct sockaddr_in *)sadr)->sin_port = 0; strcpy (copy, s); // strip off a trailing :port if present for (colon = copy ; *colon ; colon++) if (*colon == ':') { *colon = 0; ((struct sockaddr_in *)sadr)->sin_port = htons((short)atoi(colon+1)); } if (copy[0] >= '0' && copy[0] <= '9') { *(int *)&((struct sockaddr_in *)sadr)->sin_addr = inet_addr(copy); } else { // >>> FIX: For Nintendo Wii using devkitPPC / libogc // Switching to the equivalent function in the platform: //if (! (h = gethostbyname(copy)) ) if (! (h = net_gethostbyname(copy)) ) // <<< FIX return 0; // >>> FIX: For Nintendo Wii using devkitPPC / libogc // Adjusting for previous fixes: //*(int *)&((struct sockaddr_in *)sadr)->sin_addr = *(int *)h->h_addr_list[0]; ((struct sockaddr_in *)sadr)->sin_addr.s_addr = *(int *)h->h_addr_list[0]; // <<< FIX } return true; }
static void cmd_zlibconnect(int argc ATTR_UNUSED, char *argv[]) { struct client client; struct ip_addr *ips; unsigned int ips_count; in_port_t port = 143; int fd, ret; if (argv[1] == NULL || (argv[2] != NULL && net_str2port(argv[2], &port) < 0)) help(&doveadm_cmd_zlibconnect); ret = net_gethostbyname(argv[1], &ips, &ips_count); if (ret != 0) { i_fatal("Host %s lookup failed: %s", argv[1], net_gethosterror(ret)); } if ((fd = net_connect_ip(&ips[0], port, NULL)) == -1) i_fatal("connect(%s, %u) failed: %m", argv[1], port); i_info("Connected to %s port %u. Ctrl-D starts compression", net_ip2addr(&ips[0]), port); memset(&client, 0, sizeof(client)); client.fd = fd; client.input = i_stream_create_fd(fd, (size_t)-1); client.output = o_stream_create_fd(fd, 0); o_stream_set_no_error_handling(client.output, TRUE); client.io_client = io_add(STDIN_FILENO, IO_READ, client_input, &client); client.io_server = io_add(fd, IO_READ, server_input, &client); master_service_run(master_service, NULL); io_remove(&client.io_client); io_remove(&client.io_server); i_stream_unref(&client.input); o_stream_unref(&client.output); if (close(fd) < 0) i_fatal("close() failed: %m"); }
/***************************** * Fill in sockaddr_in 'addr' variable */ std::string FillAddress(std::string address, uint16_t port, uint16_t http_version, sockaddr_in &addr) { memset(&addr, 0, sizeof(addr)); std::string header; std::string path = "/"; std::string::size_type cut_at = address.find_first_of("/"); if ( cut_at != address.npos ) { path = address.substr(cut_at); address = address.erase(cut_at); } cut_at = address.find_first_of(":"); if ( cut_at != address.npos ) { uint16_t port_t = elix::string::ToIntU16( address.substr(cut_at+1) ); if ( port_t > 80 ) port = port_t; address = address.erase(cut_at); } header = "GET " + path + " HTTP/1.0\r\nAccept: */*\r\nHost: " + address + "\r\nUser-Agent:Elix-httpdl "ELIX_VERSION_STRING"\r\n\r\n"; addr.sin_family = AF_INET; #if defined (__GAMECUBE__) || defined (__WII__) hostent * host = net_gethostbyname( address.c_str() ); #else hostent * host = gethostbyname( address.c_str() ); #endif if ( host ) addr.sin_addr.s_addr = *((uint32_t *) host->h_addr_list[0]); else addr.sin_addr.s_addr = 0; addr.sin_port = htons(port); return header; }
static void conn_set_ip(IRC_SERVER_CONNECT_REC *conn, IPADDR **own_ip, const char *own_host) { IPADDR ip; if (*own_ip != NULL) { /* use already resolved IP */ if (conn->own_ip == NULL) conn->own_ip = g_new(IPADDR, 1); memcpy(conn->own_ip, *own_ip, sizeof(IPADDR)); return; } /* resolve the IP and use it */ if (net_gethostbyname(own_host, &ip) == 0) { if (conn->own_ip == NULL) conn->own_ip = g_new(IPADDR, 1); memcpy(conn->own_ip, &ip, sizeof(IPADDR)); *own_ip = g_new(IPADDR, 1); memcpy(*own_ip, &ip, sizeof(IPADDR)); } }
int main() { struct sigaction sa = {0}; sa.sa_handler = &timer_handler; sigaction(SIGALRM, &sa, NULL); struct itimerval timer = {0}; timer.it_value.tv_sec = 1; timer.it_interval.tv_sec = 1; setitimer(ITIMER_REAL, &timer, NULL); struct sockaddr_storage listen_addr; net_gethostbyname(&listen_addr, "::", 1234); int fd = net_bind_udp(&listen_addr); char buf[MTU_SIZE]; while (1) { int r = read(fd, buf, MTU_SIZE); if (r == 0) { return 0; } if (r < 0) { if (errno == EINTR) { continue; } PFATAL("recv()"); } else { packets += 1; bytes += r; } } close(fd); return 0; }
/* nonblocking gethostbyname(), ip (IPADDR) + error (int, 0 = not error) is written to pipe when found PID of the resolver child is returned */ int net_gethostbyname_nonblock(const char *addr, GIOChannel *pipe, int reverse_lookup) { RESOLVED_IP_REC rec; const char *errorstr; int pid; int len; g_return_val_if_fail(addr != NULL, FALSE); pid = fork(); if (pid > 0) { /* parent */ pidwait_add(pid); return pid; } if (pid != 0) { /* failed! */ g_warning("net_connect_thread(): fork() failed! " "Using blocking resolving"); } /* child */ srand(time(NULL)); memset(&rec, 0, sizeof(rec)); rec.error = net_gethostbyname(addr, &rec.ip4, &rec.ip6); if (rec.error == 0) { errorstr = NULL; if (reverse_lookup) { /* reverse lookup the IP, ignore any error */ if (rec.ip4.family != 0) net_gethostbyaddr(&rec.ip4, &rec.host4); if (rec.ip6.family != 0) net_gethostbyaddr(&rec.ip6, &rec.host6); } } else { errorstr = net_gethosterror(rec.error); rec.errlen = errorstr == NULL ? 0 : strlen(errorstr)+1; } g_io_channel_write_block(pipe, &rec, sizeof(rec)); if (rec.errlen != 0) g_io_channel_write_block(pipe, (void *) errorstr, rec.errlen); else { if (rec.host4) { len = strlen(rec.host4) + 1; g_io_channel_write_block(pipe, (void *) &len, sizeof(int)); g_io_channel_write_block(pipe, (void *) rec.host4, len); } if (rec.host6) { len = strlen(rec.host6) + 1; g_io_channel_write_block(pipe, (void *) &len, sizeof(int)); g_io_channel_write_block(pipe, (void *) rec.host6, len); } } if (pid == 0) _exit(99); /* we used blocking lookup */ return 0; }
static SERVER_CONNECT_REC *get_server_connect(const char *data, int *plus_addr, char **rawlog_file) { CHAT_PROTOCOL_REC *proto; SERVER_CONNECT_REC *conn; GHashTable *optlist; char *addr, *portstr, *password, *nick, *chatnet, *host, *tmp; void *free_arg; g_return_val_if_fail(data != NULL, NULL); if (!cmd_get_params(data, &free_arg, 4 | PARAM_FLAG_OPTIONS, "connect", &optlist, &addr, &portstr, &password, &nick)) return NULL; if (plus_addr != NULL) *plus_addr = *addr == '+'; if (*addr == '+') addr++; if (*addr == '\0') { signal_emit("error command", 1, GINT_TO_POINTER(CMDERR_NOT_ENOUGH_PARAMS)); cmd_params_free(free_arg); return NULL; } if (strcmp(password, "-") == 0) *password = '******'; /* check if -<chatnet> option is used to specify chat protocol */ proto = chat_protocol_find_net(optlist); /* connect to server */ chatnet = proto == NULL ? NULL : g_hash_table_lookup(optlist, proto->chatnet); if (chatnet == NULL) chatnet = g_hash_table_lookup(optlist, "network"); conn = server_create_conn(proto != NULL ? proto->id : -1, addr, atoi(portstr), chatnet, password, nick); if (proto == NULL) proto = chat_protocol_find_id(conn->chat_type); if (proto->not_initialized) { /* trying to use protocol that isn't yet initialized */ signal_emit("chat protocol unknown", 1, proto->name); server_connect_unref(conn); cmd_params_free(free_arg); return NULL; } if (strchr(addr, '/') != NULL) conn->unix_socket = TRUE; if (g_hash_table_lookup(optlist, "6") != NULL) conn->family = AF_INET6; else if (g_hash_table_lookup(optlist, "4") != NULL) conn->family = AF_INET; if (g_hash_table_lookup(optlist, "ssl") != NULL) conn->use_ssl = TRUE; if ((tmp = g_hash_table_lookup(optlist, "ssl_cert")) != NULL) conn->ssl_cert = g_strdup(tmp); if ((tmp = g_hash_table_lookup(optlist, "ssl_pkey")) != NULL) conn->ssl_pkey = g_strdup(tmp); if ((tmp = g_hash_table_lookup(optlist, "ssl_pass")) != NULL) conn->ssl_pass = g_strdup(tmp); if (g_hash_table_lookup(optlist, "ssl_verify") != NULL) conn->ssl_verify = TRUE; if ((tmp = g_hash_table_lookup(optlist, "ssl_cafile")) != NULL) conn->ssl_cafile = g_strdup(tmp); if ((tmp = g_hash_table_lookup(optlist, "ssl_capath")) != NULL) conn->ssl_capath = g_strdup(tmp); if ((conn->ssl_capath != NULL && conn->ssl_capath[0] != '\0') || (conn->ssl_cafile != NULL && conn->ssl_cafile[0] != '\0')) conn->ssl_verify = TRUE; if ((conn->ssl_cert != NULL && conn->ssl_cert[0] != '\0') || conn->ssl_verify) conn->use_ssl = TRUE; if (g_hash_table_lookup(optlist, "!") != NULL) conn->no_autojoin_channels = TRUE; if (g_hash_table_lookup(optlist, "noautosendcmd") != NULL) conn->no_autosendcmd = TRUE; if (g_hash_table_lookup(optlist, "noproxy") != NULL) g_free_and_null(conn->proxy); *rawlog_file = g_strdup(g_hash_table_lookup(optlist, "rawlog")); host = g_hash_table_lookup(optlist, "host"); if (host != NULL && *host != '\0') { IPADDR ip4, ip6; if (net_gethostbyname(host, &ip4, &ip6) == 0) server_connect_own_ip_save(conn, &ip4, &ip6); } cmd_params_free(free_arg); return conn; }
int main() { struct sigaction sa = {0}; sa.sa_handler = &timer_handler; sigaction(SIGALRM, &sa, NULL); struct itimerval timer = {0}; timer.it_value.tv_sec = 1; timer.it_interval.tv_sec = 1; setitimer(ITIMER_REAL, &timer, NULL); struct sockaddr_storage listen_addr; net_gethostbyname(&listen_addr, "::", 1234); int fd = net_bind_udp(&listen_addr); int busy_poll = 50; // ms int r = setsockopt(fd, SOL_SOCKET, SO_BUSY_POLL, &busy_poll, sizeof(busy_poll)); if (r != 0) { PFATAL("setsockopt(SO_BUSY_POLL)"); } struct mmsghdr messages[MAX_MSG] = {0}; char buffers[MAX_MSG][MTU_SIZE]; struct iovec iovecs[MAX_MSG] = {0}; /* Setup recvmmsg data structures. */ int i; for (i = 0; i < MAX_MSG; i++) { char *buf = &buffers[i][0]; struct iovec *iovec = &iovecs[i]; struct mmsghdr *msg = &messages[i]; msg->msg_hdr.msg_iov = iovec; msg->msg_hdr.msg_iovlen = 1; iovec->iov_base = &buf[0]; iovec->iov_len = MTU_SIZE; } while (1) { int r = recvmmsg(fd, messages, MAX_MSG, MSG_WAITFORONE, NULL); if (r == 0) { return 0; } if (r < 0) { if (errno == EINTR) { continue; } PFATAL("recv()"); } else { for (i = 0; i < MAX_MSG; i++) { struct mmsghdr *msg = &messages[i]; bytes += msg->msg_len; msg->msg_len = 0; } packets += r; } } close(fd); return 0; }