int serve_get(ec_server_t *srv, ec_rep_t *rep) { ec_res_t *res = ec_rep_get_res(rep); /* Set response code, payload, etag and content-type. */ (void) set_payload(srv, rep->data, rep->data_sz); (void) ec_response_set_code(srv, EC_CONTENT); (void) ec_response_add_etag(srv, rep->etag, sizeof rep->etag); (void) ec_response_add_content_type(srv, rep->media_type); /* Add max-age if != from default. */ if (res->max_age != EC_COAP_DEFAULT_MAX_AGE) (void) ec_response_add_max_age(srv, res->max_age); /* See if the client asked for Observing the resource. */ if (ec_request_get_observe(srv) == 0) { uint16_t o_cnt; /* Add a NON notifier attached to ob_serve callback. */ if (!ec_add_observer(srv, ob_serve, NULL, res->max_age, rep->media_type, EC_COAP_NON, rep->etag, sizeof rep->etag)) { /* Get counter from time */ (void) ec_get_observe_counter(&o_cnt); (void) ec_response_add_observe(srv, o_cnt); } else u_dbg("could not add requested observation"); } return 0; }
void Packet::build_icmp_hdr_(uint8_t type, uint8_t code, uint16_t id, uint16_t seq) { icmp_hdr = (struct icmphdr*)(buf_+IPV4_H); icmp_hdr->type=type; icmp_hdr->code=code; icmp_hdr->checksum=0; icmp_hdr->un.echo.id=htons(id); icmp_hdr->un.echo.sequence=htons(seq); ip_hdr->ip_p = IPPROTO_ICMP; update_ip_len_(ICMP_H); //include the ICMP header len to ip_len set_payload(buf_ + IPV4_H + ICMP_H); //update the pointer to the ICMP payload }
/* for constructing a packet that is captured */ void Packet::build_pkt(const uint8_t *b, uint16_t len) { reset(); memcpy(buf_, b, len); payload = buf_; ip_hdr = (struct ip*)buf_; if(is_tcp()) { tcp_hdr = (struct tcphdr*)((uint8_t *)(ip_hdr) + get_ip_hdr_len()); set_payload((uint8_t *)((uint8_t *)(tcp_hdr) + get_tcp_hdr_len())); } else if (is_icmp()) { icmp_hdr = (struct icmphdr*)((uint8_t *)(ip_hdr) + get_ip_hdr_len()); set_payload((uint8_t *)((uint8_t *)(icmp_hdr) + ICMP_H)); } else { logger->PrintErr("[%s:%d] Only support TCP/ICMP/IP now!\n", __FILE__, __LINE__); } }
void Packet::build_tcp_option_(uint8_t *opt, uint16_t len) { uint16_t opt_len; opt_len = len; if(opt_len%4) { opt_len = 4 - (opt_len%4); //add padding } memcpy(payload, opt, opt_len); update_tcp_off_(opt_len); //include the TCP option len to doff update_ip_len_(opt_len); //include the TCP option len to ip_len set_payload(payload + opt_len); //update the pointer to the TCP payload }
//! initializes to a default, empty UDP packet, given //! a valid MTU-sized buffer void init() { PacketIP4::init(); // source and destination ports header().sport = 0; header().dport = 0; // set zero length set_length(0); // zero the optional checksum header().checksum = 0; // set UDP payload location (!?) set_payload(buffer() + sizeof(UDP::full_header)); set_protocol(IP4::IP4_UDP); }
void Packet::build_tcp_hdr_(uint16_t sp, uint16_t dp, uint32_t seq, \ uint32_t ack, uint16_t win, uint8_t flag) { tcp_hdr = (struct tcphdr *)(buf_+IPV4_H); tcp_hdr->source = htons(sp); tcp_hdr->dest = htons(dp); tcp_hdr->seq = htonl(seq); tcp_hdr->ack_seq = htonl(ack); tcp_hdr->res1 = 0; tcp_hdr->res2 = 0; tcp_hdr->window = htons(win); tcp_hdr->check = 0; tcp_hdr->urg_ptr = 0; ip_hdr->ip_p = IPPROTO_TCP; update_tcp_flag_(flag); //include the tcp flag update_tcp_off_(TCP_H); //include the TCP header len to doff update_ip_len_(TCP_H); //include the TCP header len to ip_len set_payload(buf_ + IPV4_H + TCP_H); //update the pointer to the TCP payload }
std::unique_ptr<Reply> M4Handler::HandleMessage(Message* message) { SinkMediaManager* sink_media_manager = ToSinkMediaManager(manager_); auto payload = ToPropertyMapPayload(message->payload()); if (!payload) { WDS_ERROR("Failed to obtain payload in M4 handler."); return nullptr; } auto presentation_url = static_cast<rtsp::PresentationUrl*>(payload->GetProperty(rtsp::PresentationURLPropertyType).get()); if (presentation_url) { sink_media_manager->SetPresentationUrl(presentation_url->presentation_url_1()); } auto video_formats = static_cast<rtsp::VideoFormats*>(payload->GetProperty(rtsp::VideoFormatsPropertyType).get()); if (!video_formats) { WDS_ERROR("Failed to obtain 'wfd-video-formats' in M4 handler."); return nullptr; } const auto& selected_formats = video_formats->GetH264Formats(); if (selected_formats.size() != 1) { WDS_ERROR("Failed to obtain optimal video format from 'wfd-video-formats' in M4 handler."); return nullptr; } if (!sink_media_manager->SetOptimalVideoFormat(selected_formats[0])) { auto reply = std::unique_ptr<Reply>(new Reply(rtsp::STATUS_SeeOther)); auto payload = new rtsp::PropertyErrorPayload(); std::vector<unsigned short> error_codes = {rtsp::STATUS_UnsupportedMediaType}; auto property_errors = std::make_shared<rtsp::PropertyErrors>(rtsp::VideoFormatsPropertyType, error_codes); payload->AddPropertyError(property_errors); reply->set_payload(std::unique_ptr<rtsp::Payload>(payload)); return reply; } return std::unique_ptr<Reply>(new Reply(rtsp::STATUS_OK)); }
/* void add_mime_media_record(stru_message *message, char *mimeType, unsigned char *payload) { unsigned char payloadBytes[strlen(payload) + 1]; memcpy(payloadBytes, payload,strlen(payload)); stru_record *record = create_record(); set_tnf(record, TNF_MIME_MEDIA); unsigned char type[strlen(mimeType) + 1]; memcpy(type, mimeType, strlen(mimeType)); set_type(record, type, strlen(mimeType)); set_payload(record, payloadBytes, strlen(payload)); add_record(message, record); } */ void add_text_record(stru_message *message, char *text) { stru_record *record = create_record(); set_tnf(record, TNF_WELL_KNOWN); unsigned char RTD_TEXT[1] = { 0x54 }; // this should be a constant or preprocessor set_type(record, RTD_TEXT, sizeof(RTD_TEXT)); // X is a placeholder for encoding length // is it more efficient to build w/o string concatenation? unsigned char payload[strlen(text) + 4]; payload[strlen(text) + 3] = '\0'; memcpy(payload, "Xen", 3); memcpy(payload + 3, text, strlen(text)); // replace X with the real encoding length payload[0] = 2; set_payload(record, payload, strlen(text) + 3); add_record(message, record); }
/* Stateless Block2 handling */ static int set_payload(ec_server_t *srv, const uint8_t *data, size_t data_sz) { const uint8_t *p; size_t p_sz; size_t bsz = g_ctx.bsz ? g_ctx.bsz : EC_COAP_BLOCK_MAX; blockopt_t b2 = { .bnum = 0, .more = false, .bsz = 0 }; /* If Block2 option was received (early negotiation), use its info. */ if (ec_request_get_block2(srv, &b2.bnum, &b2.more, &b2.bsz) == 0) if (b2.bsz) bsz = U_MIN(bsz, b2.bsz); /* Single block if data fits. */ if (data_sz <= bsz) { p = data; p_sz = data_sz; } else /* Otherwise we have > 1 blocks and add Block2 option. */ { p = data + (b2.bnum * bsz); b2.more = (b2.bnum < (data_sz / bsz)); if (b2.more) p_sz = bsz; else p_sz = data_sz - b2.bnum * bsz; dbg_err_if (ec_response_add_block2(srv, b2.bnum, b2.more, bsz)); } (void) ec_response_set_payload(srv, p, p_sz); return 0; err: return -1; } int serve_wkc(ec_server_t *srv, ec_method_t method) { dbg_return_if(srv == NULL, -1); char wkc[EC_WKC_MAX] = { '\0' }; /* No operation other than GET is allowed on the /.well-known/core. */ if (method != EC_COAP_GET) { (void) ec_response_set_code(srv, EC_METHOD_NOT_ALLOWED); return 0; } dbg_err_if(ec_filesys_well_known_core(g_ctx.fs, ec_request_get_uri_origin(srv), ec_request_get_uri_query(srv), wkc) == NULL); (void) set_payload(srv, (uint8_t *) wkc, strlen(wkc)); (void) ec_response_set_code(srv, EC_CONTENT); (void) ec_response_add_content_type(srv, EC_MT_APPLICATION_LINK_FORMAT); return 0; err: return -1; }
std::unique_ptr<Reply> M3Handler::HandleMessage(Message* message) { // FIXME : resolve clashes between wds exported and internal rtsp type names. //using namespace rtsp; auto received_payload = ToGetParameterPayload(message->payload()); if (!received_payload) return nullptr; auto reply = std::unique_ptr<Reply>(new Reply(rtsp::STATUS_OK)); auto reply_payload = new rtsp::PropertyMapPayload(); for (const std::string& property : received_payload->properties()) { std::shared_ptr<rtsp::Property> new_prop; if (property == GetPropertyName(rtsp::AudioCodecsPropertyType)){ // FIXME: declare that we support absolutely every audio codec/format, // but there should be a MediaManager API for it auto codec_lpcm = AudioCodec(LPCM, AudioModes(3), 0); auto codec_aac = AudioCodec(AAC, AudioModes(15), 0); auto codec_ac3 = AudioCodec(AC3, AudioModes(7), 0); std::vector<AudioCodec> codec_list; codec_list.push_back(codec_lpcm); codec_list.push_back(codec_aac); codec_list.push_back(codec_ac3); new_prop.reset(new rtsp::AudioCodecs(codec_list)); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::VideoFormatsPropertyType)){ new_prop.reset(new rtsp::VideoFormats(ToSinkMediaManager(manager_)->GetNativeVideoFormat(), false, ToSinkMediaManager(manager_)->GetSupportedH264VideoCodecs())); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::Video3DFormatsPropertyType)){ new_prop.reset(new rtsp::Formats3d()); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::ContentProtectionPropertyType)){ new_prop.reset(new rtsp::ContentProtection()); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::DisplayEdidPropertyType)){ new_prop.reset(new rtsp::DisplayEdid()); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::CoupledSinkPropertyType)){ new_prop.reset(new rtsp::CoupledSink()); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::ClientRTPPortsPropertyType)){ new_prop.reset(new rtsp::ClientRtpPorts( ToSinkMediaManager(manager_)->GetLocalRtpPorts().first, ToSinkMediaManager(manager_)->GetLocalRtpPorts().second)); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::I2CPropertyType)){ new_prop.reset(new rtsp::I2C(0)); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::UIBCCapabilityPropertyType)){ new_prop.reset(new rtsp::UIBCCapability()); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::ConnectorTypePropertyType)){ new_prop.reset(new rtsp::ConnectorType(ToSinkMediaManager(manager_)->GetConnectorType())); reply_payload->AddProperty(new_prop); } else if (property == GetPropertyName(rtsp::StandbyResumeCapabilityPropertyType)){ new_prop.reset(new rtsp::StandbyResumeCapability(false)); reply_payload->AddProperty(new_prop); } else { WDS_WARNING("** GET_PARAMETER: Ignoring unsupported property '%s'.", property.c_str()); } } reply->set_payload(std::unique_ptr<Payload>(reply_payload)); return reply; }
void message::set_payload(const std::string& payload) { set_payload(payload.data(), payload.length()); }
void set_pass_plugin (int arg) { pass_plugin_arg = arg; set_payload (arg); }
void set_verbose (int arg) { verbose_arg = arg; set_payload (arg); }
stru_message *create_message_b(const unsigned char* data, const int numBytes) { stru_message *message = &message_buffer; //message = (stru_message *)malloc(sizeof(stru_message)); int index = 0; message->recordCount = 0; while(index <= numBytes) { // decode tnf - first byte is tnf with bit flags // see the NFDEF spec for more info unsigned char tnf_byte = data[index]; bool mb = (tnf_byte & 0x80) != 0; bool me = (tnf_byte & 0x40) != 0; bool cf = (tnf_byte & 0x20) != 0; bool sr = (tnf_byte & 0x10) != 0; bool il = (tnf_byte & 0x8) != 0; unsigned char tnf = (tnf_byte & 0x7); stru_record *record = create_record(); set_tnf(record, tnf); index++; int typeLength = data[index]; int payloadLength = 0; if(sr) { index++; payloadLength = data[index]; } else { payloadLength =((0xFF & data[++index]) << 24) | ((0xFF & data[++index]) << 16) | ((0xFF & data[++index]) << 8) | (0xFF & data[++index]); } int idLength = 0; if(il) { index++; idLength = data[index]; } index++; set_type(record, &data[index], typeLength); index += typeLength; if(il) { set_id(record, &data[index], idLength); index += idLength; } set_payload(record, &data[index], payloadLength); index += payloadLength; add_record(message, record); if(me)break; // last message } return message; }