void send_register_to_as(pjsip_rx_data *received_register, pjsip_tx_data *ok_response, AsInvocation& as, int expires, const std::string& served_user, SAS::TrailId trail) { pj_status_t status; pjsip_tx_data *tdata; pjsip_method method; pjsip_method_set(&method, PJSIP_REGISTER_METHOD); pj_str_t user_uri; pj_cstr(&user_uri, served_user.c_str()); pj_str_t as_uri; pj_cstr(&as_uri, as.server_name.c_str()); status = pjsip_endpt_create_request(stack_data.endpt, &method, // Method &as_uri, // Target &stack_data.scscf_uri, // From &user_uri, // To &stack_data.scscf_uri, // Contact NULL, // Auto-generate Call-ID 1, // CSeq NULL, // No body &tdata); // OUT if (status != PJ_SUCCESS) { //LCOV_EXCL_START LOG_ERROR("Failed to build third-party REGISTER request for server %s", as.server_name.c_str()); return; //LCOV_EXCL_STOP } // Expires header based on 200 OK response pjsip_expires_hdr_create(tdata->pool, expires); pjsip_expires_hdr* expires_hdr = pjsip_expires_hdr_create(tdata->pool, expires); pjsip_msg_add_hdr(tdata->msg, (pjsip_hdr*)expires_hdr); // TODO: modify orig-ioi of P-Charging-Vector and remove term-ioi if (received_register && ok_response) { // Copy P-Access-Network-Info, P-Visited-Network-Id and P-Charging-Vector // from original message PJUtils::clone_header(&STR_P_A_N_I, received_register->msg_info.msg, tdata->msg, tdata->pool); PJUtils::clone_header(&STR_P_V_N_I, received_register->msg_info.msg, tdata->msg, tdata->pool); PJUtils::clone_header(&STR_P_C_V, received_register->msg_info.msg, tdata->msg, tdata->pool); // Copy P-Charging-Function-Addresses from the OK response. PJUtils::clone_header(&STR_P_C_F_A, ok_response->msg, tdata->msg, tdata->pool); // Generate a message body based on Filter Criteria values char buf[MAX_SIP_MSG_SIZE]; pj_str_t sip_type = pj_str("message"); pj_str_t sip_subtype = pj_str("sip"); pj_str_t xml_type = pj_str("application"); pj_str_t xml_subtype = pj_str("3gpp-ims+xml"); // Build up this multipart body incrementally, based on the ServiceInfo, IncludeRegisterRequest and IncludeRegisterResponse fields pjsip_msg_body *final_body = pjsip_multipart_create(tdata->pool, NULL, NULL); // If we only have one part, we don't want a multipart MIME body - store the reference to each one here to use instead pjsip_msg_body *possible_final_body = NULL; int multipart_parts = 0; if (!as.service_info.empty()) { pjsip_multipart_part *xml_part = pjsip_multipart_create_part(tdata->pool); std::string xml_str = "<ims-3gpp><service-info>"+as.service_info+"</service-info></ims-3gpp>"; pj_str_t xml_pj_str; pj_cstr(&xml_pj_str, xml_str.c_str()); xml_part->body = pjsip_msg_body_create(tdata->pool, &xml_type, &xml_subtype, &xml_pj_str), possible_final_body = xml_part->body; multipart_parts++; pjsip_multipart_add_part(tdata->pool, final_body, xml_part); } if (as.include_register_request) { pjsip_multipart_part *request_part = pjsip_multipart_create_part(tdata->pool); pjsip_msg_print(received_register->msg_info.msg, buf, sizeof(buf)); pj_str_t request_str = pj_str(buf); request_part->body = pjsip_msg_body_create(tdata->pool, &sip_type, &sip_subtype, &request_str), possible_final_body = request_part->body; multipart_parts++; pjsip_multipart_add_part(tdata->pool, final_body, request_part); } if (as.include_register_response) { pjsip_multipart_part *response_part = pjsip_multipart_create_part(tdata->pool); pjsip_msg_print(ok_response->msg, buf, sizeof(buf)); pj_str_t response_str = pj_str(buf); response_part->body = pjsip_msg_body_create(tdata->pool, &sip_type, &sip_subtype, &response_str), possible_final_body = response_part->body; multipart_parts++; pjsip_multipart_add_part(tdata->pool, final_body, response_part); } if (multipart_parts == 0) { final_body = NULL; } else if (multipart_parts == 1) { final_body = possible_final_body; } else { // Just use the multipart MIME body you've built up } tdata->msg->body = final_body; } // Set the SAS trail on the request. set_trail(tdata, trail); // Allocate a temporary structure to record the default handling for this // REGISTER, and send it statefully. ThirdPartyRegData* tsxdata = new ThirdPartyRegData; tsxdata->default_handling = as.default_handling; tsxdata->trail = trail; tsxdata->public_id = served_user; pj_status_t resolv_status = PJUtils::send_request(tdata, 0, tsxdata, &send_register_cb); if (resolv_status != PJ_SUCCESS) { delete tsxdata; // LCOV_EXCL_LINE } }
/* Public function to parse multipart message bodies into its parts */ PJ_DEF(pjsip_msg_body*) pjsip_multipart_parse(pj_pool_t *pool, char *buf, pj_size_t len, const pjsip_media_type *ctype, unsigned options) { pj_str_t boundary, delim; char *curptr, *endptr; const pjsip_param *ctype_param; const pj_str_t STR_BOUNDARY = { "boundary", 8 }; pjsip_msg_body *body = NULL; PJ_ASSERT_RETURN(pool && buf && len && ctype && !options, NULL); TRACE_((THIS_FILE, "Started parsing multipart body")); /* Get the boundary value in the ctype */ boundary.ptr = NULL; boundary.slen = 0; ctype_param = pjsip_param_find(&ctype->param, &STR_BOUNDARY); if (ctype_param) { boundary = ctype_param->value; if (boundary.slen>2 && *boundary.ptr=='"') { /* Remove quote */ boundary.ptr++; boundary.slen -= 2; } TRACE_((THIS_FILE, "Boundary is specified: '%.*s'", (int)boundary.slen, boundary.ptr)); } if (!boundary.slen) { /* Boundary not found or not specified. Try to be clever, get * the boundary from the body. */ char *p=buf, *end=buf+len; PJ_LOG(4,(THIS_FILE, "Warning: boundary parameter not found or " "not specified when parsing multipart body")); /* Find the first "--". This "--" must be right after a CRLF, unless * it really appears at the start of the buffer. */ for (;;) { while (p!=end && *p!='-') ++p; if (p!=end && *(p+1)=='-' && ((p>buf && *(p-1)=='\n') || (p==buf))) { p+=2; break; } else { ++p; } } if (p==end) { /* Unable to determine boundary. Maybe this is not a multipart * message? */ PJ_LOG(4,(THIS_FILE, "Error: multipart boundary not specified and" " unable to calculate from the body")); return NULL; } boundary.ptr = p; while (p!=end && !pj_isspace(*p)) ++p; boundary.slen = p - boundary.ptr; TRACE_((THIS_FILE, "Boundary is calculated: '%.*s'", (int)boundary.slen, boundary.ptr)); } /* Build the delimiter: * delimiter = "--" boundary */ delim.slen = boundary.slen+2; delim.ptr = (char*)pj_pool_alloc(pool, (int)delim.slen); delim.ptr[0] = '-'; delim.ptr[1] = '-'; pj_memcpy(delim.ptr+2, boundary.ptr, boundary.slen); /* Start parsing the body, skip until the first delimiter. */ curptr = buf; endptr = buf + len; { pj_str_t body; body.ptr = buf; body.slen = len; curptr = pj_strstr(&body, &delim); if (!curptr) return NULL; } body = pjsip_multipart_create(pool, ctype, &boundary); for (;;) { char *start_body, *end_body; pjsip_multipart_part *part; /* Eat the boundary */ curptr += delim.slen; if (*curptr=='-' && curptr<endptr-1 && *(curptr+1)=='-') { /* Found the closing delimiter */ curptr += 2; break; } /* Optional whitespace after delimiter */ while (curptr!=endptr && IS_SPACE(*curptr)) ++curptr; /* Mandatory CRLF */ if (*curptr=='\r') ++curptr; if (*curptr!='\n') { /* Expecting a newline here */ return NULL; } ++curptr; /* We now in the start of the body */ start_body = curptr; /* Find the next delimiter */ { pj_str_t subbody; subbody.ptr = curptr; subbody.slen = endptr - curptr; curptr = pj_strstr(&subbody, &delim); if (!curptr) { /* We're really expecting end delimiter to be found. */ return NULL; } } end_body = curptr; /* The newline preceeding the delimiter is conceptually part of * the delimiter, so trim it from the body. */ if (*(end_body-1) == '\n') --end_body; if (*(end_body-1) == '\r') --end_body; /* Now that we have determined the part's boundary, parse it * to get the header and body part of the part. */ part = parse_multipart_part(pool, start_body, end_body - start_body, ctype); if (part) { pjsip_multipart_add_part(pool, body, part); } } return body; }