void ConnectAttempt::processResponse() { if (response_.result() == boost::beast::http::status::service_unavailable) { Json::Value json; Json::Reader r; std::string s; s.reserve(boost::asio::buffer_size(response_.body().data())); for(auto const& buffer : response_.body().data()) s.append( boost::asio::buffer_cast<char const*>(buffer), boost::asio::buffer_size(buffer)); auto const success = r.parse(s, json); if (success) { if (json.isObject() && json.isMember("peer-ips")) { Json::Value const& ips = json["peer-ips"]; if (ips.isArray()) { std::vector<boost::asio::ip::tcp::endpoint> eps; eps.reserve(ips.size()); for (auto const& v : ips) { if (v.isString()) { error_code ec; auto const ep = parse_endpoint(v.asString(), ec); if (!ec) eps.push_back(ep); } } overlay_.peerFinder().onRedirects( remote_endpoint_, eps); } } } } if (! OverlayImpl::isPeerUpgrade(response_)) { JLOG(journal_.info()) << "HTTP Response: " << response_.result() << " " << response_.reason(); return close(); } auto hello = parseHello (false, response_, journal_); if(! hello) return fail("processResponse: Bad TMHello"); auto sharedValue = makeSharedValue( ssl_bundle_->stream.native_handle(), journal_); if(! sharedValue) return close(); // makeSharedValue logs auto publicKey = verifyHello (*hello, *sharedValue, overlay_.setup().public_ip, beast::IPAddressConversion::from_asio(remote_endpoint_), journal_, app_); if(! publicKey) return close(); // verifyHello logs JLOG(journal_.info()) << "Public Key: " << toBase58 ( TokenType::NodePublic, *publicKey); auto const protocol = BuildInfo::make_protocol(hello->protoversion()); JLOG(journal_.info()) << "Protocol: " << to_string(protocol); auto member = app_.cluster().member(*publicKey); if (member) { JLOG(journal_.info()) << "Cluster name: " << *member; } auto const result = overlay_.peerFinder().activate (slot_, *publicKey, static_cast<bool>(member)); if (result != PeerFinder::Result::success) return fail("Outbound slots full"); auto const peer = std::make_shared<PeerImp>(app_, std::move(ssl_bundle_), read_buf_.data(), std::move(slot_), std::move(response_), usage_, *hello, *publicKey, id_, overlay_); overlay_.add_active (peer); }
static int parse_interface(struct usb_interface_t *interface, unsigned char *buffer, int size) { int i, len, numskipped, retval, parsed = 0; struct usb_descriptor_header_t *header; struct usb_interface_desc_t *ifp; uint32_t begin; interface->act_altsetting = 0; interface->num_altsetting = 0; interface->max_altsetting = USB_ALTSETTINGALLOC; interface->altsetting = vsf_bufmgr_malloc\ (sizeof(struct usb_interface_desc_t) * interface->max_altsetting); if (!interface->altsetting) { return -1; } while (size > 0) { if (interface->num_altsetting >= interface->max_altsetting) { void *ptr; int oldmas; oldmas = interface->max_altsetting; interface->max_altsetting += USB_ALTSETTINGALLOC; #ifdef USB_MAXALTSETTING if (interface->max_altsetting > USB_MAXALTSETTING) { return -1; } #endif ptr = interface->altsetting; interface->altsetting = vsf_bufmgr_malloc\ (sizeof(struct usb_interface_desc_t) * interface->max_altsetting); if (!interface->altsetting) { interface->altsetting = ptr; return -1; } memcpy(interface->altsetting, ptr, sizeof(struct usb_interface_desc_t) * oldmas); vsf_bufmgr_free(ptr); } ifp = interface->altsetting + interface->num_altsetting; interface->num_altsetting++; memcpy(ifp, buffer, USB_DT_INTERFACE_SIZE); /* Skip over the interface */ buffer += ifp->bLength; parsed += ifp->bLength; size -= ifp->bLength; begin = (uint32_t)buffer; numskipped = 0; /* Skip over any interface, class or vendor descriptors */ while (size >= sizeof(struct usb_descriptor_header_t)) { header = (struct usb_descriptor_header_t *)buffer; if (header->bLength < 2) { return -1; } /* If we find another "proper" descriptor then we're done */ if ((header->bDescriptorType == USB_DT_INTERFACE) || (header->bDescriptorType == USB_DT_ENDPOINT) || (header->bDescriptorType == USB_DT_CONFIG) || (header->bDescriptorType == USB_DT_DEVICE)) break; numskipped++; buffer += header->bLength; parsed += header->bLength; size -= header->bLength; } len = (int)((uint32_t)buffer - begin); if (len) { ifp->extra = (void *)begin; ifp->extralen = len; } else { ifp->extra = NULL; ifp->extralen = 0; } /* Did we hit an unexpected descriptor? */ header = (struct usb_descriptor_header_t *)buffer; if ((size >= sizeof(struct usb_descriptor_header_t)) && ((header->bDescriptorType == USB_DT_CONFIG) || (header->bDescriptorType == USB_DT_DEVICE))) return parsed; if (ifp->bNumEndpoints > USB_MAXENDPOINTS) { return -1; } if (ifp->bNumEndpoints != 0) { ifp->ep_desc = vsf_bufmgr_malloc(ifp->bNumEndpoints * sizeof(struct usb_endpoint_desc_t)); if (!ifp->ep_desc) { return -1; } memset(ifp->ep_desc, 0, ifp->bNumEndpoints * sizeof(struct usb_endpoint_desc_t)); for (i = 0; i < ifp->bNumEndpoints; i++) { header = (struct usb_descriptor_header_t *)buffer; if (header->bLength > size) { return -1; } retval = parse_endpoint(ifp->ep_desc + i, buffer, size); if (retval < 0) return retval; buffer += retval; parsed += retval; size -= retval; } } /* We check to see if it's an alternate to this one */ ifp = (struct usb_interface_desc_t *)buffer; if (size < USB_DT_INTERFACE_SIZE || ifp->bDescriptorType != USB_DT_INTERFACE || !ifp->bAlternateSetting) return parsed; } return parsed; }
static int sm_bootstrap_done(int index) { /* TODO: Fix this */ /* check that we should still use bootstrap */ if (clients[index].use_bootstrap) { #ifdef CONFIG_LWM2M_SECURITY_OBJ_SUPPORT int i; SYS_LOG_DBG("*** Bootstrap - checking for server info ..."); /* get the server URI */ if (sec_data->server_uri_len > 0) { /* TODO: Write endpoint parsing function */ #if 0 if (!parse_endpoint(sec_data->server_uri, sec_data->server_uri_len, &clients[index].reg_server)) { #else if (true) { #endif SYS_LOG_ERR("Failed to parse URI!"); } else { clients[index].has_registration_info = 1; clients[index].registered = 0; clients[index].bootstrapped++; } } else { SYS_LOG_ERR("** failed to parse URI"); } /* if we did not register above - then fail this and restart */ if (clients[index].bootstrapped == 0) { /* Not ready - Retry with the bootstrap server again */ set_sm_state(index, ENGINE_DO_BOOTSTRAP); } else { set_sm_state(index, ENGINE_DO_REGISTRATION); } } else { #endif set_sm_state(index, ENGINE_DO_REGISTRATION); } return 0; } static int sm_send_registration(int index, bool send_obj_support_data, zoap_reply_t reply_cb) { struct zoap_packet request; struct net_pkt *pkt = NULL; struct zoap_pending *pending = NULL; struct zoap_reply *reply = NULL; u8_t *payload; u16_t client_data_len, len; int ret = 0; /* remember the last reg time */ clients[index].last_update = k_uptime_get(); ret = lwm2m_init_message(clients[index].net_ctx, &request, &pkt, ZOAP_TYPE_CON, ZOAP_METHOD_POST, 0, NULL, 0); if (ret) { goto cleanup; } zoap_add_option(&request, ZOAP_OPTION_URI_PATH, LWM2M_RD_CLIENT_URI, strlen(LWM2M_RD_CLIENT_URI)); if (!clients[index].registered) { /* include client endpoint in URI QUERY on 1st registration */ zoap_add_option_int(&request, ZOAP_OPTION_CONTENT_FORMAT, LWM2M_FORMAT_APP_LINK_FORMAT); snprintf(query_buffer, sizeof(query_buffer) - 1, "lwm2m=%s", LWM2M_PROTOCOL_VERSION); zoap_add_option(&request, ZOAP_OPTION_URI_QUERY, query_buffer, strlen(query_buffer)); snprintf(query_buffer, sizeof(query_buffer) - 1, "ep=%s", clients[index].ep_name); zoap_add_option(&request, ZOAP_OPTION_URI_QUERY, query_buffer, strlen(query_buffer)); } else { /* include server endpoint in URI PATH otherwise */ zoap_add_option(&request, ZOAP_OPTION_URI_PATH, clients[index].server_ep, strlen(clients[index].server_ep)); } snprintf(query_buffer, sizeof(query_buffer) - 1, "lt=%d", clients[index].lifetime); zoap_add_option(&request, ZOAP_OPTION_URI_QUERY, query_buffer, strlen(query_buffer)); /* TODO: add supported binding query string */ if (send_obj_support_data) { /* generate the rd data */ client_data_len = lwm2m_get_rd_data(client_data, sizeof(client_data)); payload = zoap_packet_get_payload(&request, &len); if (!payload) { ret = -EINVAL; goto cleanup; } memcpy(payload, client_data, client_data_len); ret = zoap_packet_set_used(&request, client_data_len); if (ret) { goto cleanup; } } pending = lwm2m_init_message_pending(&request, &clients[index].reg_server, pendings, NUM_PENDINGS); if (!pending) { ret = -ENOMEM; goto cleanup; } reply = zoap_reply_next_unused(replies, NUM_REPLIES); if (!reply) { SYS_LOG_ERR("No resources for waiting for replies."); ret = -ENOMEM; goto cleanup; } zoap_reply_init(reply, &request); reply->reply = reply_cb; /* log the registration attempt */ SYS_LOG_DBG("registration sent [%s]", lwm2m_sprint_ip_addr(&clients[index].reg_server)); ret = lwm2m_udp_sendto(pkt, &clients[index].reg_server); if (ret < 0) { SYS_LOG_ERR("Error sending LWM2M packet (err:%d).", ret); goto cleanup; } zoap_pending_cycle(pending); k_delayed_work_submit(&retransmit_work, pending->timeout); return ret; cleanup: lwm2m_init_message_cleanup(pkt, pending, reply); return ret; } static int sm_do_registration(int index) { int ret = 0; if (clients[index].use_registration && !clients[index].registered && clients[index].has_registration_info) { ret = sm_send_registration(index, true, do_registration_reply_cb); if (!ret) { set_sm_state(index, ENGINE_REGISTRATION_SENT); } else { SYS_LOG_ERR("Registration err: %d", ret); } } return ret; }
void parse_magnet_uri(std::string const& uri, add_torrent_params& p, error_code& ec) { ec.clear(); std::string name; { error_code e; std::string display_name = url_has_argument(uri, "dn"); if (!display_name.empty()) name = unescape_string(display_name.c_str(), e); } // parse trackers out of the magnet link std::string::size_type pos = std::string::npos; std::string url = url_has_argument(uri, "tr", &pos); while (pos != std::string::npos) { error_code e; url = unescape_string(url, e); if (e) continue; p.trackers.push_back(url); pos = uri.find("&tr=", pos); if (pos == std::string::npos) break; pos += 4; url = uri.substr(pos, uri.find('&', pos) - pos); } // parse web seeds out of the magnet link pos = std::string::npos; url = url_has_argument(uri, "ws", &pos); while (pos != std::string::npos) { error_code e; url = unescape_string(url, e); if (e) continue; p.url_seeds.push_back(url); pos = uri.find("&ws=", pos); if (pos == std::string::npos) break; pos += 4; url = uri.substr(pos, uri.find('&', pos) - pos); } std::string btih = url_has_argument(uri, "xt"); if (btih.empty()) { ec = errors::missing_info_hash_in_uri; return; } if (btih.compare(0, 9, "urn:btih:") != 0) { ec = errors::missing_info_hash_in_uri; return; } std::string::size_type peer_pos = std::string::npos; std::string peer = url_has_argument(uri, "x.pe", &peer_pos); while (!peer.empty()) { error_code e; tcp::endpoint endp = parse_endpoint(peer, e); if (!e) p.peers.push_back(endp); peer_pos = uri.find("&x.pe=", peer_pos); if (peer_pos == std::string::npos) break; peer_pos += 6; peer = uri.substr(peer_pos, uri.find('&', peer_pos) - peer_pos); } #ifndef TORRENT_DISABLE_DHT std::string::size_type node_pos = std::string::npos; std::string node = url_has_argument(uri, "dht", &node_pos); while (!node.empty()) { std::string::size_type divider = node.find_last_of(':'); if (divider != std::string::npos) { int port = atoi(node.c_str() + divider + 1); if (port != 0) p.dht_nodes.push_back(std::make_pair(node.substr(0, divider), port)); } node_pos = uri.find("&dht=", node_pos); if (node_pos == std::string::npos) break; node_pos += 5; node = uri.substr(node_pos, uri.find('&', node_pos) - node_pos); } #endif sha1_hash info_hash; if (btih.size() == 40 + 9) from_hex(&btih[9], 40, info_hash.data()); else if (btih.size() == 32 + 9) { std::string ih = base32decode(btih.substr(9)); if (ih.size() != 20) { ec = errors::invalid_info_hash; return; } info_hash.assign(ih); } else { ec = errors::invalid_info_hash; return; } p.info_hash = info_hash; if (!name.empty()) p.name = name; }
static int parse_interface(libusb_context *ctx, struct libusb_interface *usb_interface, unsigned char *buffer, int size, int host_endian) { int i; int len; int r; int parsed = 0; int tmp; struct usb_descriptor_header header; struct libusb_interface_descriptor *ifp; unsigned char *begin; usb_interface->num_altsetting = 0; while (size >= INTERFACE_DESC_LENGTH) { struct libusb_interface_descriptor *altsetting = (struct libusb_interface_descriptor *) usb_interface->altsetting; altsetting = realloc(altsetting, sizeof(struct libusb_interface_descriptor) * (usb_interface->num_altsetting + 1)); if (!altsetting) { r = LIBUSB_ERROR_NO_MEM; goto err; } usb_interface->altsetting = altsetting; ifp = altsetting + usb_interface->num_altsetting; usb_interface->num_altsetting++; usbi_parse_descriptor(buffer, "bbbbbbbbb", ifp, 0); ifp->extra = NULL; ifp->extra_length = 0; ifp->endpoint = NULL; /* Skip over the interface */ buffer += ifp->bLength; parsed += ifp->bLength; size -= ifp->bLength; begin = buffer; /* Skip over any interface, class or vendor descriptors */ while (size >= DESC_HEADER_LENGTH) { usbi_parse_descriptor(buffer, "bb", &header, 0); if (header.bLength < 2) { usbi_err(ctx, "invalid descriptor of length %d", header.bLength); r = LIBUSB_ERROR_IO; goto err; } /* If we find another "proper" descriptor then we're done */ if ((header.bDescriptorType == LIBUSB_DT_INTERFACE) || (header.bDescriptorType == LIBUSB_DT_ENDPOINT) || (header.bDescriptorType == LIBUSB_DT_CONFIG) || (header.bDescriptorType == LIBUSB_DT_DEVICE)) break; buffer += header.bLength; parsed += header.bLength; size -= header.bLength; } /* Copy any unknown descriptors into a storage area for */ /* drivers to later parse */ len = (int)(buffer - begin); if (len) { ifp->extra = malloc(len); if (!ifp->extra) { r = LIBUSB_ERROR_NO_MEM; goto err; } memcpy((unsigned char *) ifp->extra, begin, len); ifp->extra_length = len; } /* Did we hit an unexpected descriptor? */ usbi_parse_descriptor(buffer, "bb", &header, 0); if ((size >= DESC_HEADER_LENGTH) && ((header.bDescriptorType == LIBUSB_DT_CONFIG) || (header.bDescriptorType == LIBUSB_DT_DEVICE))) return parsed; if (ifp->bNumEndpoints > USB_MAXENDPOINTS) { usbi_err(ctx, "too many endpoints (%d)", ifp->bNumEndpoints); r = LIBUSB_ERROR_IO; goto err; } if (ifp->bNumEndpoints > 0) { struct libusb_endpoint_descriptor *endpoint; tmp = ifp->bNumEndpoints * sizeof(struct libusb_endpoint_descriptor); endpoint = malloc(tmp); ifp->endpoint = endpoint; if (!endpoint) { r = LIBUSB_ERROR_NO_MEM; goto err; } memset(endpoint, 0, tmp); for (i = 0; i < ifp->bNumEndpoints; i++) { usbi_parse_descriptor(buffer, "bb", &header, 0); if (header.bLength > size) { usbi_err(ctx, "ran out of descriptors parsing"); r = LIBUSB_ERROR_IO; goto err; } r = parse_endpoint(ctx, endpoint + i, buffer, size, host_endian); if (r < 0) goto err; buffer += r; parsed += r; size -= r; } } /* We check to see if it's an alternate to this one */ ifp = (struct libusb_interface_descriptor *) buffer; if (size < LIBUSB_DT_INTERFACE_SIZE || ifp->bDescriptorType != LIBUSB_DT_INTERFACE || !ifp->bAlternateSetting) return parsed; } return parsed; err: clear_interface(usb_interface); return r; }