int zmq::udp_address_t::resolve (const char *name_) { // Find the ':' at end that separates address from the port number. const char *delimiter = strrchr (name_, ':'); if (!delimiter) { errno = EINVAL; return -1; } // Separate the address/port. std::string addr_str (name_, delimiter - name_); std::string port_str (delimiter + 1); // Remove square brackets around the address, if any, as used in IPv6 if (addr_str.size () >= 2 && addr_str [0] == '[' && addr_str [addr_str.size () - 1] == ']') addr_str = addr_str.substr (1, addr_str.size () - 2); // Parse the port number (0 is not a valid port). uint16_t port = (uint16_t) atoi (port_str.c_str ()); if (port == 0) { errno = EINVAL; return -1; } dest_address.sin_family = AF_INET; dest_address.sin_port = htons (port); dest_address.sin_addr.s_addr = inet_addr (addr_str.c_str ()); if (dest_address.sin_addr.s_addr == INADDR_NONE) { errno = EINVAL; return -1; } // we will check only first byte of IP // and if it from 224 to 239, then it can // represent multicast IP. int i = dest_address.sin_addr.s_addr & 0xFF; if(i >= 224 && i <= 239) { multicast = dest_address.sin_addr; is_mutlicast = true; } else is_mutlicast = false; interface.s_addr = htons (INADDR_ANY); if (interface.s_addr == INADDR_NONE) { errno = EINVAL; return -1; } bind_address.sin_family = AF_INET; bind_address.sin_port = htons (port); bind_address.sin_addr.s_addr = htons (INADDR_ANY); address = name_; return 0; }
/** * Logs information about a connection to the console. */ void print_conn_desc(const struct ble_gap_conn_desc *desc) { BSNCENT_LOG(DEBUG, "handle=%d our_ota_addr_type=%d our_ota_addr=%s ", desc->conn_handle, desc->our_ota_addr.type, addr_str(desc->our_ota_addr.val)); BSNCENT_LOG(DEBUG, "our_id_addr_type=%d our_id_addr=%s ", desc->our_id_addr.type, addr_str(desc->our_id_addr.val)); BSNCENT_LOG(DEBUG, "peer_ota_addr_type=%d peer_ota_addr=%s ", desc->peer_ota_addr.type, addr_str(desc->peer_ota_addr.val)); BSNCENT_LOG(DEBUG, "peer_id_addr_type=%d peer_id_addr=%s ", desc->peer_id_addr.type, addr_str(desc->peer_id_addr.val)); BSNCENT_LOG(DEBUG, "conn_itvl=%d conn_latency=%d supervision_timeout=%d " "encrypted=%d authenticated=%d bonded=%d", desc->conn_itvl, desc->conn_latency, desc->supervision_timeout, desc->sec_state.encrypted, desc->sec_state.authenticated, desc->sec_state.bonded); }
int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv4only_) { // Find the ':' at end that separates address from the port number. const char *delimiter = strrchr (name_, ':'); if (!delimiter) { errno = EINVAL; return -1; } // Separate the address/port. std::string addr_str (name_, delimiter - name_); std::string port_str (delimiter + 1); // Remove square brackets around the address, if any. if (addr_str.size () >= 2 && addr_str [0] == '[' && addr_str [addr_str.size () - 1] == ']') addr_str = addr_str.substr (1, addr_str.size () - 2); uint16_t port; // Allow 0 specifically, to detect invalid port error in atoi if not if (port_str == "*" || port_str == "0") // Resolve wildcard to 0 to allow autoselection of port port = 0; else { // Parse the port number (0 is not a valid port). port = (uint16_t) atoi (port_str.c_str ()); if (port == 0) { errno = EINVAL; return -1; } } // Resolve the IP address. int rc; if (local_) rc = resolve_interface (addr_str.c_str (), ipv4only_); else rc = resolve_hostname (addr_str.c_str (), ipv4only_); if (rc != 0) return -1; // Set the port into the address structure. if (address.generic.sa_family == AF_INET6) address.ipv6.sin6_port = htons (port); else address.ipv4.sin_port = htons (port); return 0; }
int TAO_AV_SCTP_SEQ_Base_Acceptor::acceptor_open (TAO_AV_SCTP_SEQ_Acceptor *acceptor, ACE_Reactor *reactor, const ACE_INET_Addr &local_addr, TAO_FlowSpec_Entry *entry) { ORBSVCS_DEBUG ((LM_DEBUG, "In base acceptor open")); this->acceptor_ = acceptor; this->reactor_ = reactor; this->entry_ = entry; ACE_Auto_Array_Ptr<ACE_UINT32> local_ip_addr (new ACE_UINT32[entry->num_local_sec_addrs ()]); ACE_INET_Addr ip_addr; char** addrs = entry->get_local_sec_addr (); for (int i = 0; i < entry->num_local_sec_addrs (); i++) { ACE_CString addr_str (addrs[i]); addr_str += ":"; ip_addr.set (addr_str.c_str ()); local_ip_addr[i] = ip_addr.get_ip_address (); } ACE_Multihomed_INET_Addr multi_addr; multi_addr.set (local_addr.get_port_number (), local_addr.get_ip_address (), 1, local_ip_addr.get(), entry->num_local_sec_addrs ()); char buf[BUFSIZ]; multi_addr.addr_to_string (buf, BUFSIZ); if (TAO_debug_level > 0) ORBSVCS_DEBUG ((LM_DEBUG, "TAO_AV_SCTP_SEQ_Base_Acceptor::open: %s", buf)); if (this->open (multi_addr,reactor) < 0) ORBSVCS_ERROR_RETURN ((LM_ERROR,"TAO_AV_SCTP_SEQ_Base_Acceptor::open failed\n"),-1); return 0; }
int zmq::udp_engine_t::resolve_raw_address (char *name_, size_t length_) { memset (&raw_address, 0, sizeof raw_address); const char *delimiter = NULL; // Find delimiter, cannot use memrchr as it is not supported on windows if (length_ != 0) { int chars_left = (int) length_; char *current_char = name_ + length_; do { if (*(--current_char) == ':') { delimiter = current_char; break; } } while (--chars_left != 0); } if (!delimiter) { errno = EINVAL; return -1; } std::string addr_str (name_, delimiter - name_); std::string port_str (delimiter + 1, name_ + length_ - delimiter - 1); // Parse the port number (0 is not a valid port). uint16_t port = (uint16_t) atoi (port_str.c_str ()); if (port == 0) { errno = EINVAL; return -1; } raw_address.sin_family = AF_INET; raw_address.sin_port = htons (port); raw_address.sin_addr.s_addr = inet_addr (addr_str.c_str ()); if (raw_address.sin_addr.s_addr == INADDR_NONE) { errno = EINVAL; return -1; } return 0; }
int TAO_AV_SCTP_SEQ_Connector::connect (TAO_FlowSpec_Entry *entry, TAO_AV_Transport *&transport, TAO_AV_Core::Flow_Component flow_comp) { this->entry_ = entry; if (flow_comp == TAO_AV_Core::TAO_AV_CONTROL) this->flowname_ = TAO_AV_Core::get_control_flowname (entry->flowname ()); else this->flowname_ = entry->flowname (); ACE_Addr *remote_addr = entry->address (); ACE_INET_Addr *inet_addr = dynamic_cast<ACE_INET_Addr *> (remote_addr); TAO_AV_SCTP_SEQ_Flow_Handler *handler = 0; ACE_Multihomed_INET_Addr remote_multi_addr; remote_multi_addr.set (inet_addr->get_port_number (), inet_addr->get_ip_address (), 1, 0, 0); ACE_Multihomed_INET_Addr local_addr; //This can be a multihomed address ACE_INET_Addr *addr; if (entry->get_peer_addr () != 0) { addr = dynamic_cast<ACE_INET_Addr *> (entry->get_peer_addr ()); } else { ACE_NEW_RETURN (addr, ACE_INET_Addr ("0"), -1); } ACE_Auto_Array_Ptr<ACE_UINT32> local_ip_addr (new ACE_UINT32[entry->num_peer_sec_addrs ()]); ACE_INET_Addr ip_addr; char** addrs = entry->get_peer_sec_addr (); for (int i = 0; i < entry->num_peer_sec_addrs (); i++) { ACE_CString addr_str (addrs[i]); addr_str += ":"; ip_addr.set (addr_str.c_str ()); local_ip_addr[i] = ip_addr.get_ip_address (); } if (entry->num_peer_sec_addrs () != 0) local_addr.set (addr->get_port_number (), addr->get_ip_address (), 1, local_ip_addr.get(), entry->num_peer_sec_addrs ()); else local_addr.set (addr->get_port_number (), addr->get_ip_address (), 1, 0, entry->num_peer_sec_addrs ()); int result = this->connector_.connector_connect (handler, remote_multi_addr, local_addr); if (result < 0) ORBSVCS_ERROR_RETURN ((LM_ERROR,"TAO_AV_SCTP_SEQ_connector::connect failed\n"),-1); entry->handler (handler); transport = handler->transport (); if (TAO_debug_level > 0) { ORBSVCS_DEBUG ((LM_DEBUG, "Local Addrs\n")); char buf [BUFSIZ]; size_t size = BUFSIZ; ACE_INET_Addr *peer_addrs = 0; ACE_NEW_RETURN (peer_addrs,ACE_INET_Addr [size], -1); handler->peer ().get_local_addrs (peer_addrs, size); for (unsigned int i=0; i < size; i++) { peer_addrs [i].addr_to_string (buf, BUFSIZ); ORBSVCS_DEBUG ((LM_DEBUG, "%s %d\n", buf, size)); } ORBSVCS_DEBUG ((LM_DEBUG, "Remote Addrs\n")); size = BUFSIZ; handler->peer ().get_remote_addrs (peer_addrs, size); for (unsigned int i=0; i < size; i++) { peer_addrs [i].addr_to_string (buf, BUFSIZ); ORBSVCS_DEBUG ((LM_DEBUG, "%s %d\n", buf, size)); } //delete peer_addrs; } return 0; }
int zmq::tcp_address_t::resolve (const char *name_, bool local_, bool ipv6_, bool is_src_) { if (!is_src_) { // Test the ';' to know if we have a source address in name_ const char *src_delimiter = strrchr (name_, ';'); if (src_delimiter) { std::string src_name (name_, src_delimiter - name_); const int rc = resolve (src_name.c_str (), local_, ipv6_, true); if (rc != 0) return -1; name_ = src_delimiter + 1; _has_src_addr = true; } } // Find the ':' at end that separates address from the port number. const char *delimiter = strrchr (name_, ':'); if (!delimiter) { errno = EINVAL; return -1; } // Separate the address/port. std::string addr_str (name_, delimiter - name_); std::string port_str (delimiter + 1); // Remove square brackets around the address, if any, as used in IPv6 if (addr_str.size () >= 2 && addr_str [0] == '[' && addr_str [addr_str.size () - 1] == ']') addr_str = addr_str.substr (1, addr_str.size () - 2); // Test the '%' to know if we have an interface name / zone_id in the address // Reference: https://tools.ietf.org/html/rfc4007 std::size_t pos = addr_str.rfind("%"); uint32_t zone_id = 0; if (pos != std::string::npos) { std::string if_str = addr_str.substr(pos + 1); addr_str = addr_str.substr(0, pos); if (isalpha (if_str.at (0))) zone_id = if_nametoindex(if_str.c_str()); else zone_id = (uint32_t) atoi (if_str.c_str ()); if (zone_id == 0) { errno = EINVAL; return -1; } } // Allow 0 specifically, to detect invalid port error in atoi if not uint16_t port; if (port_str == "*" || port_str == "0") // Resolve wildcard to 0 to allow autoselection of port port = 0; else { // Parse the port number (0 is not a valid port). port = (uint16_t) atoi (port_str.c_str ()); if (port == 0) { errno = EINVAL; return -1; } } // Resolve the IP address. int rc; if (local_ || is_src_) rc = resolve_interface (addr_str.c_str (), ipv6_, is_src_); else rc = resolve_hostname (addr_str.c_str (), ipv6_, is_src_); if (rc != 0) return -1; // Set the port into the address structure. if (is_src_) { if (source_address.generic.sa_family == AF_INET6) { source_address.ipv6.sin6_port = htons (port); source_address.ipv6.sin6_scope_id = zone_id; } else source_address.ipv4.sin_port = htons (port); } else { if (address.generic.sa_family == AF_INET6) { address.ipv6.sin6_port = htons (port); address.ipv6.sin6_scope_id = zone_id; } else address.ipv4.sin_port = htons (port); } return 0; }
void* web_loop( void* _ ) { int rc; int val; struct addrinfo hints, *servinfo, *p; struct timeval tv; UCHAR id[SHA_DIGEST_LENGTH]; char hexbuf[HEX_LEN+1]; int sockfd, clientfd; IP sockaddr, clientaddr; char clientbuf[1500]; struct request *request; char *hex_start, *hex_end; socklen_t addr_len = sizeof(IP); char addrbuf[FULL_ADDSTRLEN+1]; const char *addr = _main->conf->web_addr; const char *ifce = _main->conf->web_ifce; const char *port = _main->conf->web_port; memset( &hints, 0, sizeof(hints) ); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_STREAM; if( (rc = getaddrinfo( addr, port, &hints, &servinfo ) ) == 0 ) { for( p = servinfo; p != NULL; p = p->ai_next ) { memset( &sockaddr, 0, sizeof(IP) ); sockaddr = *((IP*) p->ai_addr); freeaddrinfo(servinfo); break; } } else { log_err( "Web: getaddrinfo failed: %s", gai_strerror( rc ) ); return NULL; } if( (sockfd = socket( PF_INET6, SOCK_STREAM, IPPROTO_TCP )) < 0 ) { log_err( "Web: Failed to create socket: %s", strerror( errno ) ); return NULL; } if( ifce && setsockopt( sockfd, SOL_SOCKET, SO_BINDTODEVICE, ifce, strlen( ifce )) ) { log_err( "Web: Unable to set interface '%s': %s", ifce, strerror( errno ) ); return NULL; } val = 1; if( (rc = setsockopt( sockfd, IPPROTO_IPV6, IPV6_V6ONLY, (char *) &val, sizeof(val) )) < 0 ) { log_err( "Web: Failed to set socket options: %s", strerror( errno ) ); return NULL; } val = 1; setsockopt( sockfd, SOL_SOCKET, SO_REUSEADDR, &val, sizeof(val) ); /* Set receive timeout */ tv.tv_sec = 1; tv.tv_usec = 0; if( (rc = setsockopt( sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv) )) < 0 ) { log_err( "Web: Failed to set socket options: %s", strerror( errno ) ); return NULL; } if( (rc = bind( sockfd, (struct sockaddr*) &sockaddr, sizeof(IP) )) < 0 ) { log_err( "Web: Failed to bind socket to address: %s", strerror( errno ) ); return NULL; } listen( sockfd, ntohs( sockaddr.sin6_port ) ); log_info( "Web: Bind socket to %s, interface %s.", addr ? addr_str( &sockaddr, addrbuf ) : "<any>", ifce ? ifce : "<any>" ); clientfd = 0; while( _main->status == MAIN_ONLINE ) { /* Close file descriptor that has not been used previously */ if( clientfd > 0 ) { close( clientfd ); } clientfd = accept( sockfd, (struct sockaddr*)&clientaddr, &addr_len ); rc = recv( clientfd, clientbuf, sizeof(clientbuf) - 1, 0 ); if( rc < 0 ) { continue; } /* Only handle GET requests. */ if( rc < 6 || strncmp( "GET /", clientbuf, 5 ) != 0 ) { continue; } /* Jump after slash */ hex_start = clientbuf + 5; clientbuf[rc] = ' '; hex_end = strchr( hex_start, ' ' ); if( hex_end == NULL ) { continue; } *hex_end = '\0'; if( strlen( hex_start ) == 0 || strcmp( hex_start, "favicon.ico" ) == 0 ) { continue; } /* That is the lookup key */ p2p_compute_id( id, hex_start ); log_debug( "Web: Lookup '%s' as '%s'.", hex_start, id_str( id, hexbuf ) ); request = (struct request *) myalloc( sizeof(struct request), "masalla-web" ); memcpy( &request->clientaddr, &clientaddr, sizeof(IP) ); request->clientfd = clientfd; web_lookup( &web_reply, request, id ); /* File descriptor is closed in callback */ clientfd = 0; } return NULL; }
/* listen for local connection */ void* nss_loop( void* _ ) { ssize_t rc; struct addrinfo hints, *servinfo, *p; struct timeval tv; int sockfd; IP clientaddr, sockaddr; char hostname[256]; UCHAR host_id[SHA_DIGEST_LENGTH]; char addrbuf[FULL_ADDSTRLEN+1]; socklen_t addr_len = sizeof(IP); memset( &hints, 0, sizeof(hints) ); hints.ai_flags = AI_PASSIVE; hints.ai_family = AF_INET6; hints.ai_socktype = SOCK_DGRAM; if( (rc = getaddrinfo( "::1", NULL, &hints, &servinfo )) == 0 ) { for( p = servinfo; p != NULL; p = p->ai_next ) { memset( &sockaddr, 0, sizeof(IP) ); sockaddr = *((IP*) p->ai_addr); sockaddr.sin6_port = htons( MASALA_NSS_PORT ); freeaddrinfo( servinfo ); break; } } else { log_err( "NSS: getaddrinfo failed: %s", gai_strerror( rc ) ); return NULL; } sockfd = socket( PF_INET6, SOCK_DGRAM, IPPROTO_UDP ); if( sockfd < 0 ) { log_err( "NSS: Failed to create socket: %s", strerror( errno ) ); return NULL; } /* Set receive timeout */ tv.tv_sec = 1; tv.tv_usec = 0; rc = setsockopt( sockfd, SOL_SOCKET, SO_RCVTIMEO, (char *)&tv, sizeof(tv) ); if( rc < 0 ) { log_err( "NSS: Failed to set socket option: %s", strerror( errno ) ); return NULL; } rc = bind( sockfd, (struct sockaddr*) &sockaddr, sizeof(IP) ); if( rc < 0 ) { log_err( "NSS: Failed to bind socket to address: %s", strerror( errno ) ); return NULL; } log_info( "NSS: Bind socket to %s.", addr_str( &sockaddr, addrbuf ) ); while( _main->status == MAIN_ONLINE ) { rc = recvfrom( sockfd, hostname, sizeof( hostname ), 0, (struct sockaddr *) &clientaddr, &addr_len ); if( rc <= 0 || rc >= sizeof( hostname ) ) { continue; } hostname[rc] = '\0'; /* Validate hostname */ if ( !str_isValidHostname( (char*) hostname, strlen( hostname ) ) ) { log_warn( "NSS: Invalid hostname for lookup: '%s'", hostname ); continue; } /* That is the lookup key */ p2p_compute_id( host_id, hostname ); nss_lookup( sockfd, &clientaddr, host_id ); } return NULL; }
int zmq::udp_address_t::resolve (const char *name_, bool bind_) { // Find the ':' at end that separates address from the port number. const char *delimiter = strrchr (name_, ':'); if (!delimiter) { errno = EINVAL; return -1; } // Separate the address/port. std::string addr_str (name_, delimiter - name_); std::string port_str (delimiter + 1); // Parse the port number (0 is not a valid port). uint16_t port = (uint16_t) atoi (port_str.c_str ()); if (port == 0) { errno = EINVAL; return -1; } dest_address.sin_family = AF_INET; dest_address.sin_port = htons (port); // Only when the udp should bind we allow * as the address if (addr_str == "*" && bind_) dest_address.sin_addr.s_addr = htonl (INADDR_ANY); else dest_address.sin_addr.s_addr = inet_addr (addr_str.c_str ()); if (dest_address.sin_addr.s_addr == INADDR_NONE) { errno = EINVAL; return -1; } // we will check only first byte of IP // and if it from 224 to 239, then it can // represent multicast IP. int i = dest_address.sin_addr.s_addr & 0xFF; if(i >= 224 && i <= 239) { multicast = dest_address.sin_addr; is_multicast = true; } else is_multicast = false; iface.s_addr = htonl (INADDR_ANY); if (iface.s_addr == INADDR_NONE) { errno = EINVAL; return -1; } // If a should bind and not a multicast, the dest address // is actually the bind address if (bind_ && !is_multicast) bind_address = dest_address; else { bind_address.sin_family = AF_INET; bind_address.sin_port = htons (port); bind_address.sin_addr.s_addr = htonl (INADDR_ANY); } address = name_; return 0; }
void print_adv_fields(const struct ble_hs_adv_fields *fields) { char s[BLE_HS_ADV_MAX_SZ]; const uint8_t *u8p; int i; if (fields->flags != 0) { BSNCENT_LOG(DEBUG, " flags=0x%02x\n", fields->flags); } if (fields->uuids16 != NULL) { BSNCENT_LOG(DEBUG, " uuids16(%scomplete)=", fields->uuids16_is_complete ? "" : "in"); for (i = 0; i < fields->num_uuids16; i++) { print_uuid(&fields->uuids16[i].u); BSNCENT_LOG(DEBUG, " "); } BSNCENT_LOG(DEBUG, "\n"); } if (fields->uuids32 != NULL) { BSNCENT_LOG(DEBUG, " uuids32(%scomplete)=", fields->uuids32_is_complete ? "" : "in"); for (i = 0; i < fields->num_uuids32; i++) { print_uuid(&fields->uuids32[i].u); BSNCENT_LOG(DEBUG, " "); } BSNCENT_LOG(DEBUG, "\n"); } if (fields->uuids128 != NULL) { BSNCENT_LOG(DEBUG, " uuids128(%scomplete)=", fields->uuids128_is_complete ? "" : "in"); for (i = 0; i < fields->num_uuids128; i++) { print_uuid(&fields->uuids128[i].u); BSNCENT_LOG(DEBUG, " "); } BSNCENT_LOG(DEBUG, "\n"); } if (fields->name != NULL) { assert(fields->name_len < sizeof s - 1); memcpy(s, fields->name, fields->name_len); s[fields->name_len] = '\0'; BSNCENT_LOG(DEBUG, " name(%scomplete)=%s\n", fields->name_is_complete ? "" : "in", s); } if (fields->tx_pwr_lvl_is_present) { BSNCENT_LOG(DEBUG, " tx_pwr_lvl=%d\n", fields->tx_pwr_lvl); } if (fields->slave_itvl_range != NULL) { BSNCENT_LOG(DEBUG, " slave_itvl_range="); print_bytes(fields->slave_itvl_range, BLE_HS_ADV_SLAVE_ITVL_RANGE_LEN); BSNCENT_LOG(DEBUG, "\n"); } if (fields->svc_data_uuid16 != NULL) { BSNCENT_LOG(DEBUG, " svc_data_uuid16="); print_bytes(fields->svc_data_uuid16, fields->svc_data_uuid16_len); BSNCENT_LOG(DEBUG, "\n"); } if (fields->public_tgt_addr != NULL) { BSNCENT_LOG(DEBUG, " public_tgt_addr="); u8p = fields->public_tgt_addr; for (i = 0; i < fields->num_public_tgt_addrs; i++) { BSNCENT_LOG(DEBUG, "public_tgt_addr=%s ", addr_str(u8p)); u8p += BLE_HS_ADV_PUBLIC_TGT_ADDR_ENTRY_LEN; } BSNCENT_LOG(DEBUG, "\n"); } if (fields->appearance_is_present) { BSNCENT_LOG(DEBUG, " appearance=0x%04x\n", fields->appearance); } if (fields->adv_itvl_is_present) { BSNCENT_LOG(DEBUG, " adv_itvl=0x%04x\n", fields->adv_itvl); } if (fields->svc_data_uuid32 != NULL) { BSNCENT_LOG(DEBUG, " svc_data_uuid32="); print_bytes(fields->svc_data_uuid32, fields->svc_data_uuid32_len); BSNCENT_LOG(DEBUG, "\n"); } if (fields->svc_data_uuid128 != NULL) { BSNCENT_LOG(DEBUG, " svc_data_uuid128="); print_bytes(fields->svc_data_uuid128, fields->svc_data_uuid128_len); BSNCENT_LOG(DEBUG, "\n"); } if (fields->uri != NULL) { BSNCENT_LOG(DEBUG, " uri="); print_bytes(fields->uri, fields->uri_len); BSNCENT_LOG(DEBUG, "\n"); } if (fields->mfg_data != NULL) { BSNCENT_LOG(DEBUG, " mfg_data="); print_bytes(fields->mfg_data, fields->mfg_data_len); BSNCENT_LOG(DEBUG, "\n"); } }