int udp_output2_(struct socket *so, struct mbuf *m, const SockAddress* saddr, const SockAddress* daddr, int iptos) { register struct udpiphdr *ui; uint32_t saddr_ip = sock_address_get_ip(saddr); uint32_t daddr_ip = sock_address_get_ip(daddr); int saddr_port = sock_address_get_port(saddr); int daddr_port = sock_address_get_port(daddr); int error = 0; DEBUG_CALL("udp_output"); DEBUG_ARG("so = %lx", (long)so); DEBUG_ARG("m = %lx", (long)m); DEBUG_ARG("saddr = %lx", (long) saddr_ip); DEBUG_ARG("daddr = %lx", (long) daddr_ip); /* * Adjust for header */ m->m_data -= sizeof(struct udpiphdr); m->m_len += sizeof(struct udpiphdr); /* * Fill in mbuf with extended UDP header * and addresses and length put into network format. */ ui = mtod(m, struct udpiphdr *); memset(&ui->ui_i.ih_mbuf, 0 , sizeof(struct mbuf_ptr)); ui->ui_x1 = 0; ui->ui_pr = IPPROTO_UDP; ui->ui_len = htons(m->m_len - sizeof(struct ip)); /* + sizeof (struct udphdr)); */ /* XXXXX Check for from-one-location sockets, or from-any-location sockets */ ui->ui_src = ip_seth(saddr_ip); ui->ui_dst = ip_seth(daddr_ip); ui->ui_sport = port_seth(saddr_port); ui->ui_dport = port_seth(daddr_port); ui->ui_ulen = ui->ui_len; /* * Stuff checksum and output datagram. */ ui->ui_sum = 0; if (UDPCKSUM) { if ((ui->ui_sum = cksum(m, /* sizeof (struct udpiphdr) + */ m->m_len)) == 0) ui->ui_sum = 0xffff; } ((struct ip *)ui)->ip_len = m->m_len; ((struct ip *)ui)->ip_ttl = IPDEFTTL; ((struct ip *)ui)->ip_tos = iptos; STAT(udpstat.udps_opackets++); error = ip_output(so, m); return (error); }
static ProxyConnection* http_service_connect( HttpService* service, SocketType sock_type, SockAddress* address ) { /* the HTTP proxy can only handle TCP connections */ if (sock_type != SOCKET_STREAM) return NULL; /* if the client tries to directly connect to the proxy, let it do so */ if (sock_address_equal( address, &service->server_addr )) return NULL; PROXY_LOG("%s: trying to connect to %s", __FUNCTION__, sock_address_to_string(address)); if (sock_address_get_port(address) == 80) { /* use the rewriter for HTTP */ PROXY_LOG("%s: using HTTP rewriter", __FUNCTION__); return http_rewriter_connect(service, address); } else { PROXY_LOG("%s: using HTTP rewriter", __FUNCTION__); return http_connector_connect(service, address); } }
/* * XXX This should really be tcp_listen */ struct socket * solisten(u_int port, u_int32_t laddr, u_int lport, int flags) { SockAddress addr; uint32_t addr_ip; struct socket *so; int s; DEBUG_CALL("solisten"); DEBUG_ARG("port = %d", port); DEBUG_ARG("laddr = %x", laddr); DEBUG_ARG("lport = %d", lport); DEBUG_ARG("flags = %x", flags); if ((so = socreate()) == NULL) { /* free(so); Not sofree() ??? free(NULL) == NOP */ return NULL; } /* Don't tcp_attach... we don't need so_snd nor so_rcv */ if ((so->so_tcpcb = tcp_newtcpcb(so)) == NULL) { free(so); return NULL; } insque(so,&tcb); /* * SS_FACCEPTONCE sockets must time out. */ if (flags & SS_FACCEPTONCE) so->so_tcpcb->t_timer[TCPT_KEEP] = TCPTV_KEEP_INIT*2; so->so_state = (SS_FACCEPTCONN|flags); so->so_laddr_port = lport; /* Kept in host format */ so->so_laddr_ip = laddr; /* Ditto */ so->so_haddr_port = port; s = socket_loopback_server( port, SOCKET_STREAM ); if (s < 0) return NULL; socket_get_address(s, &addr); so->so_faddr_port = sock_address_get_port(&addr); addr_ip = (uint32_t) sock_address_get_ip(&addr); if (addr_ip == 0 || addr_ip == loopback_addr_ip) so->so_faddr_ip = alias_addr_ip; else so->so_faddr_ip = addr_ip; so->s = s; return so; }
static void coreconsole_init(CoreConsole* cc, const SockAddress* address, Looper* looper) { int fd = socket_create_inet(SOCKET_STREAM); AsyncStatus status; cc->port = sock_address_get_port(address); cc->ok = 0; loopIo_init(cc->io, looper, fd, coreconsole_io_func, cc); if (fd >= 0) { status = asyncConsoleConnector_connect(cc->connector, address, cc->io); if (status == ASYNC_ERROR) { cc->ok = 0; } } }
int udp_output_(struct socket *so, struct mbuf *m, SockAddress* from) { SockAddress saddr, daddr; uint32_t saddr_ip; uint16_t saddr_port; saddr_ip = sock_address_get_ip(from); saddr_port = sock_address_get_port(from); if ((so->so_faddr_ip & 0xffffff00) == special_addr_ip) { saddr_ip = so->so_faddr_ip; if ((so->so_faddr_ip & 0x000000ff) == 0xff) saddr_ip = alias_addr_ip; } sock_address_init_inet( &saddr, saddr_ip, saddr_port ); sock_address_init_inet( &daddr, so->so_laddr_ip, so->so_laddr_port ); return udp_output2_(so, m, &saddr, &daddr, so->so_iptos); }
struct socket * udp_listen(u_int port, u_int32_t laddr, u_int lport, int flags) { struct socket *so; SockAddress addr; uint32_t addr_ip; if ((so = socreate()) == NULL) { free(so); return NULL; } so->s = socket_anyaddr_server( port, SOCKET_DGRAM ); so->so_expire = curtime + SO_EXPIRE; so->so_haddr_port = port; insque(so,&udb); if (so->s < 0) { udp_detach(so); return NULL; } socket_get_address(so->s, &addr); so->so_faddr_port = sock_address_get_port(&addr); addr_ip = sock_address_get_ip(&addr); if (addr_ip == 0 || addr_ip == loopback_addr_ip) so->so_faddr_ip = alias_addr_ip; else so->so_faddr_ip = addr_ip; so->so_laddr_port = lport; so->so_laddr_ip = laddr; if (flags != SS_FACCEPTONCE) so->so_expire = 0; so->so_state = SS_ISFCONNECTED; return so; }
static void udp_emu(struct socket *so, struct mbuf *m) { SockAddress sockaddr; struct cu_header { uint16_t d_family; // destination family uint16_t d_port; // destination port uint32_t d_addr; // destination address uint16_t s_family; // source family uint16_t s_port; // source port uint32_t so_addr; // source address uint32_t seqn; // sequence number uint16_t message; // message uint16_t data_type; // data type uint16_t pkt_len; // packet length } *cu_head; switch(so->so_emu) { case EMU_CUSEEME: /* * Cu-SeeMe emulation. * Hopefully the packet is more that 16 bytes long. We don't * do any other tests, just replace the address and port * fields. */ if (m->m_len >= sizeof (*cu_head)) { if (socket_get_address(so->s, &sockaddr) < 0) return; cu_head = mtod(m, struct cu_header *); cu_head->s_port = htons( sock_address_get_port(&sockaddr)); cu_head->so_addr = htonl( sock_address_get_ip(&sockaddr)); } return; } }
int inet_listen_opts(QemuOpts *opts, int port_offset) { SockAddress** list; SockAddress* e; unsigned flags = SOCKET_LIST_PASSIVE; const char *addr; char port[33]; char uaddr[256+1]; char uport[33]; int slisten,to,try_next,nn; #ifdef CONFIG_ANDROID const char* socket_fd = qemu_opt_get(opts, "socket"); if (socket_fd) { return atoi(socket_fd); } #endif if ((qemu_opt_get(opts, "host") == NULL) || (qemu_opt_get(opts, "port") == NULL)) { fprintf(stderr, "%s: host and/or port not specified\n", __FUNCTION__); return -1; } pstrcpy(port, sizeof(port), qemu_opt_get(opts, "port")); addr = qemu_opt_get(opts, "host"); to = qemu_opt_get_number(opts, "to", 0); if (qemu_opt_get_bool(opts, "ipv4", 0)) flags |= SOCKET_LIST_FORCE_INET; if (qemu_opt_get_bool(opts, "ipv6", 0)) flags |= SOCKET_LIST_FORCE_IN6; /* lookup */ if (port_offset) snprintf(port, sizeof(port), "%d", atoi(port) + port_offset); list = sock_address_list_create( strlen(addr) ? addr : NULL, port, flags ); if (list == NULL) { fprintf(stderr,"%s: getaddrinfo(%s,%s): %s\n", __FUNCTION__, addr, port, errno_str); return -1; } /* create socket + bind */ for (nn = 0; list[nn] != NULL; nn++) { SocketFamily family; e = list[nn]; family = sock_address_get_family(e); sock_address_get_numeric_info(e, uaddr, sizeof uaddr, uport, sizeof uport); slisten = socket_create(family, SOCKET_STREAM); if (slisten < 0) { fprintf(stderr,"%s: socket(%s): %s\n", __FUNCTION__, sock_address_strfamily(e), errno_str); continue; } socket_set_xreuseaddr(slisten); #ifdef IPV6_V6ONLY /* listen on both ipv4 and ipv6 */ if (family == SOCKET_IN6) { socket_set_ipv6only(slisten); } #endif for (;;) { if (socket_bind(slisten, e) == 0) { if (sockets_debug) fprintf(stderr,"%s: bind(%s,%s,%d): OK\n", __FUNCTION__, sock_address_strfamily(e), uaddr, sock_address_get_port(e)); goto listen; } socket_close(slisten); try_next = to && (sock_address_get_port(e) <= to + port_offset); if (!try_next || sockets_debug) fprintf(stderr,"%s: bind(%s,%s,%d): %s\n", __FUNCTION__, sock_address_strfamily(e), uaddr, sock_address_get_port(e), strerror(errno)); if (try_next) { sock_address_set_port(e, sock_address_get_port(e) + 1); continue; } break; } } sock_address_list_free(list); fprintf(stderr, "%s: FAILED\n", __FUNCTION__); return -1; listen: if (socket_listen(slisten,1) != 0) { perror("listen"); socket_close(slisten); return -1; } snprintf(uport, sizeof(uport), "%d", sock_address_get_port(e) - port_offset); qemu_opt_set(opts, "host", uaddr); qemu_opt_set(opts, "port", uport); qemu_opt_set(opts, "ipv6", (e->family == SOCKET_IN6) ? "on" : "off"); qemu_opt_set(opts, "ipv4", (e->family != SOCKET_IN6) ? "on" : "off"); sock_address_list_free(list); return slisten; }
/* Attaches starting UI to a running core process. * This routine is called from main() when -attach-core parameter is set, * indicating that this UI instance should attach to a running core, rather than * start a new core process. * Param: * opts Android options containing non-NULL attach_core. * Return: * 0 on success, or -1 on failure. */ static int attach_to_core(AndroidOptions* opts) { int iter; SockAddress console_socket; SockAddress** sockaddr_list; QEmulator* emulator; // Parse attach_core param extracting the host name, and the port name. char* console_address = strdup(opts->attach_core); char* host_name = console_address; char* port_num = strchr(console_address, ':'); if (port_num == NULL) { // The host name is ommited, indicating the localhost host_name = "localhost"; port_num = console_address; } else if (port_num == console_address) { // Invalid. derror("Invalid value %s for -attach-core parameter\n", opts->attach_core); return -1; } else { *port_num = '\0'; port_num++; if (*port_num == '\0') { // Invalid. derror("Invalid value %s for -attach-core parameter\n", opts->attach_core); return -1; } } /* Create socket address list for the given address, and pull appropriate * address to use for connection. Note that we're fine copying that address * out of the list, since INET and IN6 will entirely fit into SockAddress * structure. */ sockaddr_list = sock_address_list_create(host_name, port_num, SOCKET_LIST_FORCE_INET); free(console_address); if (sockaddr_list == NULL) { derror("Unable to resolve address %s: %s\n", opts->attach_core, errno_str); return -1; } for (iter = 0; sockaddr_list[iter] != NULL; iter++) { if (sock_address_get_family(sockaddr_list[iter]) == SOCKET_INET || sock_address_get_family(sockaddr_list[iter]) == SOCKET_IN6) { memcpy(&console_socket, sockaddr_list[iter], sizeof(SockAddress)); break; } } if (sockaddr_list[iter] == NULL) { derror("Unable to resolve address %s. Note that 'port' parameter passed to -attach-core\n" "must be resolvable into an IP address.\n", opts->attach_core); sock_address_list_free(sockaddr_list); return -1; } sock_address_list_free(sockaddr_list); if (attachUiImpl_create(&console_socket)) { return -1; } // Save core's port, and set the title. android_base_port = sock_address_get_port(&console_socket); emulator = qemulator_get(); qemulator_set_title(emulator); return 0; }