static int __osip_message_startline_to_strresp(osip_message_t * sip, char **dest) { char *tmp; const char *sip_version; char status_code[5]; *dest = NULL; if ((sip == NULL) || (sip->reason_phrase == NULL) || (sip->status_code < 100) || (sip->status_code > 699)) return OSIP_BADPARAMETER; if (sip->sip_version == NULL) sip_version = osip_protocol_version; else sip_version = sip->sip_version; sprintf(status_code, "%u", sip->status_code); *dest = (char *) osip_malloc(strlen(sip_version) + 3 + strlen(sip->reason_phrase) + 4); if (*dest == NULL) return OSIP_NOMEM; tmp = *dest; tmp = osip_str_append(tmp, sip_version); *tmp = ' '; tmp++; tmp = osip_strn_append(tmp, status_code, 3); *tmp = ' '; tmp++; strcpy(tmp, sip->reason_phrase); return OSIP_SUCCESS; }
static int strcat_simple_header (char **_string, size_t * malloc_size, char **_message, void *ptr_header, char *header_name, size_t size_of_header, int (*xxx_to_str) (void *, char **), char **next) { char *string; char *message; char *tmp; int i; string = *_string; message = *_message; if (ptr_header != NULL) { if (*malloc_size < message - string + 100 + size_of_header) /* take some memory avoid to osip_realloc too much often */ { /* should not happen often */ size_t size = message - string; *malloc_size = message - string + size_of_header + 100; string = osip_realloc (string, *malloc_size); if (string == NULL) { *_string = NULL; *_message = NULL; return OSIP_NOMEM; } message = string + size; } message = osip_strn_append (message, header_name, size_of_header); i = xxx_to_str (ptr_header, &tmp); if (i != 0) { *_string = string; *_message = message; *next = NULL; return i; } if (*malloc_size < message - string + strlen (tmp) + 100) { size_t size = message - string; *malloc_size = message - string + strlen (tmp) + 100; string = osip_realloc (string, *malloc_size); if (string == NULL) { *_string = NULL; *_message = NULL; return OSIP_NOMEM; } message = string + size; } message = osip_str_append (message, tmp); osip_free (tmp); message = osip_strn_append (message, CRLF, 2); } *_string = string; *_message = message; *next = message; return OSIP_SUCCESS; }
static int __osip_message_startline_to_strreq (osip_message_t * sip, char **dest) { const char *sip_version; char *tmp; char *rquri; int i; *dest = NULL; if ((sip == NULL) || (sip->req_uri == NULL) || (sip->sip_method == NULL)) return OSIP_BADPARAMETER; i = osip_uri_to_str (sip->req_uri, &rquri); if (i != 0) return i; if (sip->sip_version == NULL) sip_version = osip_protocol_version; else sip_version = sip->sip_version; *dest = (char *) osip_malloc (strlen (sip->sip_method) + strlen (rquri) + strlen (sip_version) + 3); if (*dest == NULL) { osip_free (rquri); return OSIP_NOMEM; } tmp = *dest; tmp = osip_str_append (tmp, sip->sip_method); *tmp = ' '; tmp++; tmp = osip_str_append (tmp, rquri); *tmp = ' '; tmp++; strcpy (tmp, sip_version); osip_free (rquri); return OSIP_SUCCESS; }
/* returns null on error. */ int osip_authorization_to_str (const osip_authorization_t * auth, char **dest) { size_t len; char *tmp; *dest = NULL; /* DO NOT REALLY KNOW THE LIST OF MANDATORY PARAMETER: Please HELP! */ #if 0 if ((auth == NULL) || (auth->auth_type == NULL) || (auth->realm == NULL) || (auth->nonce == NULL)) return OSIP_BADPARAMETER; #else /* IMS requirement: send authorization like in: Digest uri="sip:sip.antisip.com", username="******", response="" */ if ((auth == NULL) || (auth->auth_type == NULL)) return OSIP_BADPARAMETER; #endif len = strlen (auth->auth_type) + 1; if (auth->username != NULL) len = len + 10 + strlen (auth->username); if (auth->realm != NULL) len = len + 8 + strlen (auth->realm); if (auth->nonce != NULL) len = len + 8 + strlen (auth->nonce); if (auth->uri != NULL) len = len + 6 + strlen (auth->uri); if (auth->response != NULL) len = len + 11 + strlen (auth->response); len = len + 2; if (auth->digest != NULL) len = len + strlen (auth->digest) + 9; if (auth->algorithm != NULL) len = len + strlen (auth->algorithm) + 12; if (auth->cnonce != NULL) len = len + strlen (auth->cnonce) + 9; if (auth->opaque != NULL) len = len + 9 + strlen (auth->opaque); if (auth->nonce_count != NULL) len = len + strlen (auth->nonce_count) + 5; if (auth->message_qop != NULL) len = len + strlen (auth->message_qop) + 6; tmp = (char *) osip_malloc (len); if (tmp == NULL) return OSIP_NOMEM; *dest = tmp; tmp = osip_str_append (tmp, auth->auth_type); if (auth->username != NULL) { tmp = osip_strn_append (tmp, " username="******", realm=", 8); /* !! realm-value must be a quoted string !! */ tmp = osip_str_append (tmp, auth->realm); } if (auth->nonce != NULL) { tmp = osip_strn_append (tmp, ", nonce=", 8); /* !! nonce-value must be a quoted string !! */ tmp = osip_str_append (tmp, auth->nonce); } if (auth->uri != NULL) { tmp = osip_strn_append (tmp, ", uri=", 6); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append (tmp, auth->uri); } if (auth->response != NULL) { tmp = osip_strn_append (tmp, ", response=", 11); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append (tmp, auth->response); } if (auth->digest != NULL) { tmp = osip_strn_append (tmp, ", digest=", 9); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append (tmp, auth->digest); } if (auth->algorithm != NULL) { tmp = osip_strn_append (tmp, ", algorithm=", 12); tmp = osip_str_append (tmp, auth->algorithm); } if (auth->cnonce != NULL) { tmp = osip_strn_append (tmp, ", cnonce=", 9); tmp = osip_str_append (tmp, auth->cnonce); } if (auth->opaque != NULL) { tmp = osip_strn_append (tmp, ", opaque=", 9); tmp = osip_str_append (tmp, auth->opaque); } if (auth->message_qop != NULL) { tmp = osip_strn_append (tmp, ", qop=", 6); tmp = osip_str_append (tmp, auth->message_qop); } if (auth->nonce_count != NULL) { tmp = osip_strn_append (tmp, ", nc=", 5); tmp = osip_str_append (tmp, auth->nonce_count); } return OSIP_SUCCESS; }
static int _osip_message_to_str (osip_message_t * sip, char **dest, size_t * message_length, int sipfrag) { size_t malloc_size; size_t total_length = 0; /* Added at SIPit day1 */ char *start_of_bodies; char *content_length_to_modify = NULL; char *message; char *next; char *tmp; int pos; int i; char *boundary = NULL; malloc_size = SIP_MESSAGE_MAX_LENGTH; *dest = NULL; if (sip == NULL) return OSIP_BADPARAMETER; { if (1 == osip_message_get__property (sip)) { /* message is already available in "message" */ *dest = osip_malloc (sip->message_length + 1); if (*dest == NULL) return OSIP_NOMEM; memcpy (*dest, sip->message, sip->message_length); (*dest)[sip->message_length] = '\0'; if (message_length != NULL) *message_length = sip->message_length; return OSIP_SUCCESS; } else { /* message should be rebuilt: delete the old one if exists. */ osip_free (sip->message); sip->message = NULL; } } message = (char *) osip_malloc (SIP_MESSAGE_MAX_LENGTH); /* ???? message could be > 4000 */ if (message == NULL) return OSIP_NOMEM; *dest = message; /* add the first line of message */ i = __osip_message_startline_to_str (sip, &tmp); if (i != 0) { if (!sipfrag) { osip_free (*dest); *dest = NULL; return i; } /* A start-line isn't required for message/sipfrag parts. */ } else { message = osip_str_append (message, tmp); osip_free (tmp); message = osip_strn_append (message, CRLF, 2); } { struct to_str_table { char header_name[30]; int header_length; osip_list_t *header_list; void *header_data; int (*to_str) (void *, char **); } #ifndef MINISIZE table[25] = #else table[15] = #endif { { "Via: ", 5, NULL, NULL, (int (*)(void *, char **)) &osip_via_to_str}, { "Record-Route: ", 14, NULL, NULL, (int (*)(void *, char **)) &osip_record_route_to_str}, { "Route: ", 7, NULL, NULL, (int (*)(void *, char **)) &osip_route_to_str}, { "From: ", 6, NULL, NULL, (int (*)(void *, char **)) &osip_from_to_str}, { "To: ", 4, NULL, NULL, (int (*)(void *, char **)) &osip_to_to_str}, { "Call-ID: ", 9, NULL, NULL, (int (*)(void *, char **)) &osip_call_id_to_str}, { "CSeq: ", 6, NULL, NULL, (int (*)(void *, char **)) &osip_cseq_to_str}, { "Contact: ", 9, NULL, NULL, (int (*)(void *, char **)) &osip_contact_to_str}, { "Authorization: ", 15, NULL, NULL, (int (*)(void *, char **)) &osip_authorization_to_str}, { "WWW-Authenticate: ", 18, NULL, NULL, (int (*)(void *, char **)) &osip_www_authenticate_to_str}, { "Proxy-Authenticate: ", 20, NULL, NULL, (int (*)(void *, char **)) &osip_www_authenticate_to_str}, { "Proxy-Authorization: ", 21, NULL, NULL, (int (*)(void *, char **)) &osip_authorization_to_str}, { "Content-Type: ", 14, NULL, NULL, (int (*)(void *, char **)) &osip_content_type_to_str}, { "Mime-Version: ", 14, NULL, NULL, (int (*)(void *, char **)) &osip_content_length_to_str}, #ifndef MINISIZE { "Allow: ", 7, NULL, NULL, (int (*)(void *, char **)) &osip_allow_to_str}, { "Content-Encoding: ", 18, NULL, NULL, (int (*)(void *, char **)) &osip_content_encoding_to_str}, { "Call-Info: ", 11, NULL, NULL, (int (*)(void *, char **)) &osip_call_info_to_str}, { "Alert-Info: ", 12, NULL, NULL, (int (*)(void *, char **)) &osip_call_info_to_str}, { "Error-Info: ", 12, NULL, NULL, (int (*)(void *, char **)) &osip_call_info_to_str}, { "Accept: ", 8, NULL, NULL, (int (*)(void *, char **)) &osip_accept_to_str}, { "Accept-Encoding: ", 17, NULL, NULL, (int (*)(void *, char **)) &osip_accept_encoding_to_str}, { "Accept-Language: ", 17, NULL, NULL, (int (*)(void *, char **)) &osip_accept_language_to_str}, { "Authentication-Info: ", 21, NULL, NULL, (int (*)(void *, char **)) &osip_authentication_info_to_str}, { "Proxy-Authentication-Info: ", 27, NULL, NULL, (int (*)(void *, char **)) &osip_authentication_info_to_str}, #endif { { '\0'}, 0, NULL, NULL, NULL} }; /* BEGIN: Added by l00183184, 2013/8/5 PN:DTS2013071906746 alg升级 */ /* 判断list是否需要强制分为多行写 */ #ifndef MINISIZE char MultiLine[25] = {1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 0, 0, 0, 0, 0}; #else char MultiLine[15] = {1, 1, 1, 0, 0, 0, 0, 1, 1, 1, 1, 1, 0, 0}; #endif /* END: Added by l00183184, 2013/8/5 PN:DTS2013071906746 alg升级 */ table[0].header_list = &sip->vias; table[1].header_list = &sip->record_routes; table[2].header_list = &sip->routes; table[3].header_data = sip->from; table[4].header_data = sip->to; table[5].header_data = sip->call_id; table[6].header_data = sip->cseq; table[7].header_list = &sip->contacts; table[8].header_list = &sip->authorizations; table[9].header_list = &sip->www_authenticates; table[10].header_list = &sip->proxy_authenticates; table[11].header_list = &sip->proxy_authorizations; table[12].header_data = sip->content_type; table[13].header_data = sip->mime_version; #ifndef MINISIZE table[14].header_list = &sip->allows; table[15].header_list = &sip->content_encodings; table[16].header_list = &sip->call_infos; table[17].header_list = &sip->alert_infos; table[18].header_list = &sip->error_infos; table[19].header_list = &sip->accepts; table[20].header_list = &sip->accept_encodings; table[21].header_list = &sip->accept_languages; table[22].header_list = &sip->authentication_infos; table[23].header_list = &sip->proxy_authentication_infos; #endif pos = 0; while (table[pos].header_name[0] != '\0') { if (table[13].header_list == NULL) i = strcat_simple_header (dest, &malloc_size, &message, table[pos].header_data, table[pos].header_name, table[pos].header_length, ((int (*)(void *, char **)) table[pos].to_str), &next); /* BEGIN: Modified by l00183184, 2013/8/5 PN:DTS2013071906746 alg升级 */ /* 只有对应的routes等这几个头域的list分行写,其他的list合在一行 */ /* 根据Rfc3261: Multiple header field rows with the same field-name MAY be present in a message if and only if the entire field-value for that header field is defined as a comma-separated list (that is, if follows the grammar defined in Section 7.3).*/ if (1 == MultiLine[pos]) { i = strcat_headers_one_per_line (dest, &malloc_size, &message, table[pos].header_list, table[pos].header_name, table[pos].header_length, ((int (*)(void *, char **)) table[pos].to_str), &next); } else { i = strcat_headers_all_on_one_line (dest, &malloc_size, &message, table[pos].header_list, table[pos].header_name, table[pos].header_length, ((int (*)(void *, char **)) table[pos].to_str), &next); } /* END: Modified by l00183184, 2013/8/5 PN:DTS2013071906746 alg升级 */ if (i != 0) { osip_free (*dest); *dest = NULL; return i; } message = next; pos++; } } pos = 0; while (!osip_list_eol (&sip->headers, pos)) { osip_header_t *header; size_t header_len = 0; header = (osip_header_t *) osip_list_get (&sip->headers, pos); i = osip_header_to_str (header, &tmp); if (i != 0) { osip_free (*dest); *dest = NULL; return i; } header_len = strlen (tmp); if (_osip_message_realloc (&message, dest, header_len + 3, &malloc_size) < 0) { osip_free (tmp); *dest = NULL; return OSIP_NOMEM; } message = osip_str_append (message, tmp); osip_free (tmp); message = osip_strn_append (message, CRLF, 2); pos++; } /* we have to create the body before adding the contentlength */ /* add enough lenght for "Content-Length: " */ if (_osip_message_realloc (&message, dest, 16, &malloc_size) < 0) return OSIP_NOMEM; if (sipfrag && osip_list_eol (&sip->bodies, 0)) { /* end of headers */ osip_strncpy (message, CRLF, 2); message = message + 2; /* same remark as at the beginning of the method */ sip->message_property = 1; sip->message = osip_strdup (*dest); sip->message_length = message - *dest; if (message_length != NULL) *message_length = message - *dest; return OSIP_SUCCESS; /* it's all done */ } osip_strncpy (message, "Content-Length: ", 16); message = message + 16; /* SIPit Day1 ALWAYS RECALCULATE? if (sip->contentlength!=NULL) { i = osip_content_length_to_str(sip->contentlength, &tmp); if (i!=0) { osip_free(*dest); *dest = NULL; return i; } osip_strncpy(message,tmp,strlen(tmp)); osip_free(tmp); } else { */ if (osip_list_eol (&sip->bodies, 0)) /* no body */ message = osip_strn_append (message, "0", 1); else { /* BUG: p130 (rfc2543bis-04) "No SP after last token or quoted string" In fact, if extra spaces exist: the stack can't be used to make user-agent that wants to make authentication... This should be changed... */ content_length_to_modify = message; message = osip_str_append (message, " "); } /* } */ message = osip_strn_append (message, CRLF, 2); /* end of headers */ message = osip_strn_append (message, CRLF, 2); start_of_bodies = message; total_length = start_of_bodies - *dest; if (osip_list_eol (&sip->bodies, 0)) { /* same remark as at the beginning of the method */ sip->message_property = 1; sip->message = osip_strdup (*dest); sip->message_length = total_length; if (message_length != NULL) *message_length = total_length; return OSIP_SUCCESS; /* it's all done */ } if (sip->mime_version != NULL && sip->content_type && sip->content_type->type && !osip_strcasecmp (sip->content_type->type, "multipart")) { osip_generic_param_t *ct_param = NULL; /* find the boundary */ i = osip_generic_param_get_byname (&sip->content_type->gen_params, "boundary", &ct_param); if ((i >= 0) && ct_param && ct_param->gvalue) { size_t len = strlen (ct_param->gvalue); if (len > MIME_MAX_BOUNDARY_LEN) { osip_free (*dest); *dest = NULL; return OSIP_SYNTAXERROR; } boundary = osip_malloc (len + 5); if (boundary == NULL) { osip_free (*dest); *dest = NULL; return OSIP_NOMEM; } osip_strncpy (boundary, CRLF, 2); osip_strncpy (boundary + 2, "--", 2); if (ct_param->gvalue[0] == '"' && ct_param->gvalue[len - 1] == '"') osip_strncpy (boundary + 4, ct_param->gvalue + 1, len - 2); else osip_strncpy (boundary + 4, ct_param->gvalue, len); } } pos = 0; while (!osip_list_eol (&sip->bodies, pos)) { osip_body_t *body; size_t body_length; body = (osip_body_t *) osip_list_get (&sip->bodies, pos); if (boundary) { /* Needs at most 77 bytes, last realloc allocate at least 100 bytes extra */ message = osip_str_append (message, boundary); message = osip_strn_append (message, CRLF, 2); } i = osip_body_to_str (body, &tmp, &body_length); if (i != 0) { osip_free (*dest); *dest = NULL; if (boundary) osip_free (boundary); return i; } if (malloc_size < message - *dest + 100 + body_length) { size_t size = message - *dest; int offset_of_body; int offset_content_length_to_modify = 0; offset_of_body = (int) (start_of_bodies - *dest); if (content_length_to_modify != NULL) offset_content_length_to_modify = (int) (content_length_to_modify - *dest); malloc_size = message - *dest + body_length + 100; *dest = osip_realloc (*dest, malloc_size); if (*dest == NULL) { osip_free (tmp); /* fixed 09/Jun/2005 */ if (boundary) osip_free (boundary); return OSIP_NOMEM; } start_of_bodies = *dest + offset_of_body; if (content_length_to_modify != NULL) content_length_to_modify = *dest + offset_content_length_to_modify; message = *dest + size; } memcpy (message, tmp, body_length); message[body_length] = '\0'; osip_free (tmp); message = message + body_length; pos++; } if (boundary) { /* Needs at most 79 bytes, last realloc allocate at least 100 bytes extra */ message = osip_str_append (message, boundary); message = osip_strn_append (message, "--", 2); message = osip_strn_append (message, CRLF, 2); osip_free (boundary); boundary = NULL; } if (content_length_to_modify == NULL) { osip_free (*dest); *dest = NULL; return OSIP_SYNTAXERROR; } /* we NOW have the length of bodies: */ { size_t size = message - start_of_bodies; char tmp2[15]; total_length += size; snprintf (tmp2, 15, "%i", (int) size); /* do not use osip_strncpy here! */ strncpy (content_length_to_modify + 5 - strlen (tmp2), tmp2, strlen (tmp2)); } /* same remark as at the beginning of the method */ sip->message_property = 1; sip->message = osip_malloc (total_length + 1); if (sip->message != NULL) { memcpy (sip->message, *dest, total_length); sip->message[total_length] = '\0'; sip->message_length = total_length; if (message_length != NULL) *message_length = total_length; } return OSIP_SUCCESS; }
static int strcat_headers_all_on_one_line (char **_string, size_t * malloc_size, char **_message, osip_list_t * headers, char *header, size_t size_of_header, int (*xxx_to_str) (void *, char **), char **next) { char *string; char *message; char *tmp; int pos = 0; int i; string = *_string; message = *_message; pos = 0; while (!osip_list_eol (headers, pos)) { if (*malloc_size < message - string + 100 + size_of_header) /* take some memory avoid to osip_realloc too much often */ { /* should not happen often */ size_t size = message - string; *malloc_size = message - string + size_of_header + 100; string = osip_realloc (string, *malloc_size); if (string == NULL) { *_string = NULL; *_message = NULL; return OSIP_NOMEM; } message = string + size; } message = osip_strn_append (message, header, size_of_header); while (!osip_list_eol (headers, pos)) { void *elt; elt = (void *) osip_list_get (headers, pos); i = xxx_to_str (elt, &tmp); if (i != 0) { *_string = string; *_message = message; *next = NULL; return i; } if (*malloc_size < message - string + strlen (tmp) + 100) { size_t size = message - string; *malloc_size = message - string + (int) strlen (tmp) + 100; string = osip_realloc (string, *malloc_size); if (string == NULL) { *_string = NULL; *_message = NULL; return OSIP_NOMEM; } message = string + size; } message = osip_str_append (message, tmp); osip_free (tmp); pos++; if (!osip_list_eol (headers, pos)) { message = osip_strn_append (message, ", ", 2); } } message = osip_strn_append (message, CRLF, 2); } *_string = string; *_message = message; *next = message; return OSIP_SUCCESS; }
/* returns null on error. */ int CAuthorization::ToStr(char** dest) { size_t len; char* tmp; *dest = NULL; /* DO NOT REALLY KNOW THE LIST OF MANDATORY PARAMETER: Please HELP! */ if ((this->auth_type == NULL) || (this->realm == NULL) || (this->nonce == NULL)) return -1; len = strlen(this->auth_type) + 1; if (this->username != NULL) len = len + 10 + strlen(this->username); if (this->realm != NULL) len = len + 8 + strlen(this->realm); if (this->nonce != NULL) len = len + 8 + strlen(this->nonce); if (this->uri != NULL) len = len + 6 + strlen(this->uri); if (this->response != NULL) len = len + 11 + strlen(this->response); len = len + 2; if (this->digest != NULL) len = len + strlen(this->digest) + 9; if (this->algorithm != NULL) len = len + strlen(this->algorithm) + 12; if (this->cnonce != NULL) len = len + strlen(this->cnonce) + 9; if (this->opaque != NULL) len = len + 9 + strlen(this->opaque); if (this->nonce_count != NULL) len = len + strlen(this->nonce_count) + 5; if (this->message_qop != NULL) len = len + strlen(this->message_qop) + 6; tmp = (char *) mm_malloc(len); if (tmp == NULL) return -1; *dest = tmp; tmp = osip_str_append(tmp, this->auth_type); if (this->username != NULL) { tmp = osip_strn_append(tmp, " username="******", realm=", 8); /* !! realm-value must be a quoted string !! */ tmp = osip_str_append(tmp, this->realm); } if (this->nonce != NULL) { tmp = osip_strn_append(tmp, ", nonce=", 8); /* !! nonce-value must be a quoted string !! */ tmp = osip_str_append(tmp, this->nonce); } if (this->uri != NULL) { tmp = osip_strn_append(tmp, ", uri=", 6); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append(tmp, this->uri); } if (this->response != NULL) { tmp = osip_strn_append(tmp, ", response=", 11); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append(tmp, this->response); } if (this->digest != NULL) { tmp = osip_strn_append(tmp, ", digest=", 9); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append(tmp, this->digest); } if (this->algorithm != NULL) { tmp = osip_strn_append(tmp, ", algorithm=", 12); tmp = osip_str_append(tmp, this->algorithm); } if (this->cnonce != NULL) { tmp = osip_strn_append(tmp, ", cnonce=", 9); tmp = osip_str_append(tmp, this->cnonce); } if (this->opaque != NULL) { tmp = osip_strn_append(tmp, ", opaque=", 9); tmp = osip_str_append(tmp, this->opaque); } if (this->message_qop != NULL) { tmp = osip_strn_append(tmp, ", qop=", 6); tmp = osip_str_append(tmp, this->message_qop); } if (this->nonce_count != NULL) { tmp = osip_strn_append(tmp, ", nc=", 5); tmp = osip_str_append(tmp, this->nonce_count); } return 0; }
/* returns null on error. */ int osip_www_authenticate_to_str (const osip_www_authenticate_t * wwwa, char **dest) { size_t len; char *tmp; *dest = NULL; if ((wwwa == NULL) || (wwwa->auth_type == NULL)) return OSIP_BADPARAMETER; len = strlen (wwwa->auth_type) + 1; if (wwwa->realm != NULL) len = len + strlen (wwwa->realm) + 7; if (wwwa->nonce != NULL) len = len + strlen (wwwa->nonce) + 8; len = len + 2; if (wwwa->domain != NULL) len = len + strlen (wwwa->domain) + 9; if (wwwa->opaque != NULL) len = len + strlen (wwwa->opaque) + 9; if (wwwa->stale != NULL) len = len + strlen (wwwa->stale) + 8; if (wwwa->algorithm != NULL) len = len + strlen (wwwa->algorithm) + 12; if (wwwa->qop_options != NULL) len = len + strlen (wwwa->qop_options) + 6; if (wwwa->version != NULL) len = len + strlen (wwwa->version) + 10; if (wwwa->targetname != NULL) len = len + strlen (wwwa->targetname) + 13; if (wwwa->gssapi_data != NULL) len = len + strlen (wwwa->gssapi_data) + 14; tmp = (char *) osip_malloc (len); if (tmp == NULL) return OSIP_NOMEM; *dest = tmp; tmp = osip_str_append (tmp, wwwa->auth_type); if (wwwa->realm != NULL) { tmp = osip_strn_append (tmp, " realm=", 7); tmp = osip_str_append (tmp, wwwa->realm); } if (wwwa->domain != NULL) { tmp = osip_strn_append (tmp, ", domain=", 9); tmp = osip_str_append (tmp, wwwa->domain); } if (wwwa->nonce != NULL) { tmp = osip_strn_append (tmp, ", nonce=", 8); tmp = osip_str_append (tmp, wwwa->nonce); } if (wwwa->opaque != NULL) { tmp = osip_strn_append (tmp, ", opaque=", 9); tmp = osip_str_append (tmp, wwwa->opaque); } if (wwwa->stale != NULL) { tmp = osip_strn_append (tmp, ", stale=", 8); tmp = osip_str_append (tmp, wwwa->stale); } if (wwwa->algorithm != NULL) { tmp = osip_strn_append (tmp, ", algorithm=", 12); tmp = osip_str_append (tmp, wwwa->algorithm); } if (wwwa->qop_options != NULL) { tmp = osip_strn_append (tmp, ", qop=", 6); tmp = osip_str_append (tmp, wwwa->qop_options); } if (wwwa->version != NULL) { tmp = osip_strn_append (tmp, ", version=", 10); tmp = osip_str_append (tmp, wwwa->version); } if (wwwa->targetname != NULL) { tmp = osip_strn_append (tmp, ", targetname=", 13); tmp = osip_str_append (tmp, wwwa->targetname); } if (wwwa->gssapi_data != NULL) { tmp = osip_strn_append (tmp, ", gssapi-data=", 14); tmp = osip_str_append (tmp, wwwa->gssapi_data); } if (wwwa->realm == NULL) { /* remove comma */ len = strlen (wwwa->auth_type); if ((*dest)[len] == ',') (*dest)[len] = ' '; } return OSIP_SUCCESS; }
/* returns null on error. */ int osip_authentication_info_to_str (const osip_authentication_info_t * ainfo, char **dest) { size_t len; char *tmp; *dest = NULL; if (ainfo == NULL) return -1; len = 0; if (ainfo->nextnonce != NULL) len = len + strlen (ainfo->nextnonce) + 11; if (ainfo->rspauth != NULL) len = len + strlen (ainfo->rspauth) + 10; if (ainfo->cnonce != NULL) len = len + strlen (ainfo->cnonce) + 9; if (ainfo->nonce_count != NULL) len = len + strlen (ainfo->nonce_count) + 5; if (ainfo->qop_options != NULL) len = len + strlen (ainfo->qop_options) + 6; if (len == 0) return -1; tmp = (char *) osip_malloc (len); if (tmp == NULL) return -1; *dest = tmp; if (ainfo->qop_options != NULL) { tmp = osip_strn_append (tmp, "qop=", 4); tmp = osip_str_append (tmp, ainfo->qop_options); } if (ainfo->nextnonce != NULL) { if (tmp != *dest) { tmp = osip_strn_append (tmp, ", ", 2); } tmp = osip_strn_append (tmp, "nextnonce=", 10); tmp = osip_str_append (tmp, ainfo->nextnonce); } if (ainfo->rspauth != NULL) { if (tmp != *dest) { tmp = osip_strn_append (tmp, ", ", 2); } tmp = osip_strn_append (tmp, "rspauth=", 8); tmp = osip_str_append (tmp, ainfo->rspauth); } if (ainfo->cnonce != NULL) { if (tmp != *dest) { tmp = osip_strn_append (tmp, ", ", 2); } tmp = osip_strn_append (tmp, "cnonce=", 7); tmp = osip_str_append (tmp, ainfo->cnonce); } if (ainfo->nonce_count != NULL) { if (tmp != *dest) { tmp = osip_strn_append (tmp, ", ", 2); } tmp = osip_strn_append (tmp, "nc=", 3); tmp = osip_str_append (tmp, ainfo->nonce_count); } return 0; }
/* returns null on error. */ int osip_authorization_to_str (const osip_authorization_t * auth, char **dest) { size_t len; char *tmp; int first = 1; *dest = NULL; /* DO NOT REALLY KNOW THE LIST OF MANDATORY PARAMETER: Please HELP! */ #if 0 if ((auth == NULL) || (auth->auth_type == NULL) || (auth->realm == NULL) || (auth->nonce == NULL)) return OSIP_BADPARAMETER; #else /* IMS requirement: send authorization like in: Digest uri="sip:sip.antisip.com", username="******", response="" */ if ((auth == NULL) || (auth->auth_type == NULL)) return OSIP_BADPARAMETER; #endif len = strlen (auth->auth_type) + 1; if (auth->username != NULL) len = len + 10 + strlen (auth->username); if (auth->realm != NULL) len = len + 8 + strlen (auth->realm); if (auth->nonce != NULL) len = len + 8 + strlen (auth->nonce); if (auth->uri != NULL) len = len + 6 + strlen (auth->uri); if (auth->response != NULL) len = len + 11 + strlen (auth->response); len = len + 2; if (auth->digest != NULL) len = len + strlen (auth->digest) + 9; if (auth->algorithm != NULL) len = len + strlen (auth->algorithm) + 12; if (auth->cnonce != NULL) len = len + strlen (auth->cnonce) + 9; if (auth->opaque != NULL) len = len + 9 + strlen (auth->opaque); if (auth->nonce_count != NULL) len = len + strlen (auth->nonce_count) + 5; if (auth->message_qop != NULL) len = len + strlen (auth->message_qop) + 6; if (auth->version != NULL) len = len + strlen (auth->version) + 10; if (auth->targetname != NULL) len = len + strlen (auth->targetname) + 13; if (auth->gssapi_data != NULL) len = len + strlen (auth->gssapi_data) + 14; if (auth->crand != NULL) len = len + strlen (auth->crand) + 8; if (auth->cnum != NULL) len = len + strlen (auth->cnum) + 7; tmp = (char *) osip_malloc (len); if (tmp == NULL) return OSIP_NOMEM; *dest = tmp; tmp = osip_str_append (tmp, auth->auth_type); if (auth->username != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " username="******",", 1); first = 0; tmp = osip_strn_append (tmp, " realm=", 7); /* !! realm-value must be a quoted string !! */ tmp = osip_str_append (tmp, auth->realm); } if (auth->nonce != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " nonce=", 7); /* !! nonce-value must be a quoted string !! */ tmp = osip_str_append (tmp, auth->nonce); } if (auth->uri != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " uri=", 5); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append (tmp, auth->uri); } if (auth->response != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " response=", 10); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append (tmp, auth->response); } if (auth->digest != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " digest=", 8); /* !! domain-value must be a list of URI in a quoted string !! */ tmp = osip_str_append (tmp, auth->digest); } if (auth->algorithm != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " algorithm=", 11); tmp = osip_str_append (tmp, auth->algorithm); } if (auth->cnonce != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " cnonce=", 8); tmp = osip_str_append (tmp, auth->cnonce); } if (auth->opaque != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " opaque=", 8); tmp = osip_str_append (tmp, auth->opaque); } if (auth->message_qop != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " qop=", 5); tmp = osip_str_append (tmp, auth->message_qop); } if (auth->nonce_count != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " nc=", 4); tmp = osip_str_append (tmp, auth->nonce_count); } if (auth->version != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " version=", 9); tmp = osip_str_append (tmp, auth->version); } if (auth->targetname != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " targetname=", 12); tmp = osip_str_append (tmp, auth->targetname); } if (auth->gssapi_data != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " gssapi-data=", 13); tmp = osip_str_append (tmp, auth->gssapi_data); } if (auth->crand != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " crand=", 7); tmp = osip_str_append (tmp, auth->crand); } if (auth->cnum != NULL) { if(!first) tmp = osip_strn_append (tmp, ",", 1); first = 0; tmp = osip_strn_append (tmp, " cnum=", 6); tmp = osip_str_append (tmp, auth->cnum); } return OSIP_SUCCESS; }
static int _osip_message_to_str (osip_message_t * sip, char **dest, size_t * message_length, int sipfrag) { size_t malloc_size; size_t total_length = 0; /* Added at SIPit day1 */ char *start_of_bodies; char *content_length_to_modify = NULL; char *message; char *next; char *tmp; int pos; int i; char *boundary = NULL; malloc_size = SIP_MESSAGE_MAX_LENGTH; *dest = NULL; if ((sip == NULL)) return -1; { if (1 == osip_message_get__property (sip)) { /* message is already available in "message" */ *dest = osip_malloc (sip->message_length + 1); if (*dest == NULL) return -1; memcpy (*dest, sip->message, sip->message_length); (*dest)[sip->message_length] = '\0'; if (message_length != NULL) *message_length = sip->message_length; return 0; } else { /* message should be rebuilt: delete the old one if exists. */ osip_free (sip->message); sip->message = NULL; } } message = (char *) osip_malloc (SIP_MESSAGE_MAX_LENGTH); /* ???? message could be > 4000 */ if (message == NULL) return -1; *dest = message; /* add the first line of message */ i = __osip_message_startline_to_str (sip, &tmp); if (i == -1) { if (!sipfrag) { osip_free (*dest); *dest = NULL; return -1; } /* A start-line isn't required for message/sipfrag parts. */ } else { message = osip_str_append (message, tmp); osip_free (tmp); message = osip_strn_append (message, CRLF, 2); } i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->vias, "Via: ", 5, ((int (*)(void *, char **)) &osip_via_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->record_routes, "Record-Route: ", 14, ((int (*)(void *, char **)) &osip_record_route_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->routes, "Route: ", 7, ((int (*)(void *, char **)) &osip_route_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_simple_header (dest, &malloc_size, &message, sip->from, "From: ", 6, ((int (*)(void *, char **)) &osip_from_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_simple_header (dest, &malloc_size, &message, sip->to, "To: ", 4, ((int (*)(void *, char **)) &osip_to_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_simple_header (dest, &malloc_size, &message, sip->call_id, "Call-ID: ", 9, ((int (*)(void *, char **)) &osip_call_id_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_simple_header (dest, &malloc_size, &message, sip->cseq, "CSeq: ", 6, ((int (*)(void *, char **)) &osip_cseq_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->contacts, "Contact: ", 9, ((int (*)(void *, char **)) &osip_contact_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->authorizations, "Authorization: ", 15, ((int (*)(void *, char **)) &osip_authorization_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->www_authenticates, "WWW-Authenticate: ", 18, ((int (*)(void *, char **)) &osip_www_authenticate_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->proxy_authenticates, "Proxy-Authenticate: ", 20, ((int (*)(void *, char **)) &osip_www_authenticate_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->proxy_authorizations, "Proxy-Authorization: ", 21, ((int (*)(void *, char **)) &osip_authorization_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; pos = 0; while (!osip_list_eol (&sip->headers, pos)) { osip_header_t *header; size_t header_len = 0; header = (osip_header_t *) osip_list_get (&sip->headers, pos); i = osip_header_to_str (header, &tmp); if (i == -1) { osip_free (*dest); *dest = NULL; return -1; } header_len = strlen (tmp); if (_osip_message_realloc (&message, dest, header_len + 3, &malloc_size) < 0) { osip_free (tmp); *dest = NULL; return -1; } message = osip_str_append (message, tmp); osip_free (tmp); message = osip_strn_append (message, CRLF, 2); pos++; } i = strcat_headers_all_on_one_line (dest, &malloc_size, &message, &sip->allows, "Allow: ", 7, ((int (*)(void *, char **)) &osip_content_length_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_simple_header (dest, &malloc_size, &message, sip->content_type, "Content-Type: ", 14, ((int (*)(void *, char **)) &osip_content_type_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_all_on_one_line (dest, &malloc_size, &message, &sip->content_encodings, "Content-Encoding: ", 18, ((int (*)(void *, char **)) &osip_content_length_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_simple_header (dest, &malloc_size, &message, sip->mime_version, "Mime-Version: ", 14, ((int (*)(void *, char **)) &osip_content_length_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->call_infos, "Call-Info: ", 11, ((int (*)(void *, char **)) &osip_call_info_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->alert_infos, "Alert-Info: ", 12, ((int (*)(void *, char **)) &osip_call_info_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->error_infos, "Error-Info: ", 12, ((int (*)(void *, char **)) &osip_call_info_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_all_on_one_line (dest, &malloc_size, &message, &sip->accepts, "Accept: ", 8, ((int (*)(void *, char **)) &osip_accept_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_all_on_one_line (dest, &malloc_size, &message, &sip->accept_encodings, "Accept-Encoding: ", 17, ((int (*)(void *, char **)) &osip_accept_encoding_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_all_on_one_line (dest, &malloc_size, &message, &sip->accept_languages, "Accept-Language: ", 17, ((int (*)(void *, char **)) &osip_accept_encoding_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->authentication_infos, "Authentication-Info: ", 21, ((int (*)(void *, char **)) &osip_authentication_info_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; i = strcat_headers_one_per_line (dest, &malloc_size, &message, &sip->proxy_authentication_infos, "Proxy-Authentication-Info: ", 27, ((int (*)(void *, char **)) &osip_authentication_info_to_str), &next); if (i != 0) { osip_free (*dest); *dest = NULL; return -1; } message = next; /* we have to create the body before adding the contentlength */ /* add enough lenght for "Content-Length: " */ if (_osip_message_realloc (&message, dest, 16, &malloc_size) < 0) return -1; if (sipfrag && osip_list_eol (&sip->bodies, 0)) { /* end of headers */ osip_strncpy (message, CRLF, 2); message = message + 2; /* same remark as at the beginning of the method */ sip->message_property = 1; sip->message = osip_strdup (*dest); sip->message_length = message - *dest; if (message_length != NULL) *message_length = message - *dest; return 0; /* it's all done */ } osip_strncpy (message, "Content-Length: ", 16); message = message + 16; /* SIPit Day1 ALWAYS RECALCULATE? if (sip->contentlength!=NULL) { i = osip_content_length_to_str(sip->contentlength, &tmp); if (i==-1) { osip_free(*dest); *dest = NULL; return -1; } osip_strncpy(message,tmp,strlen(tmp)); osip_free(tmp); } else { */ if (osip_list_eol (&sip->bodies, 0)) /* no body */ message = osip_strn_append (message, "0", 1); else { /* BUG: p130 (rfc2543bis-04) "No SP after last token or quoted string" In fact, if extra spaces exist: the stack can't be used to make user-agent that wants to make authentication... This should be changed... */ content_length_to_modify = message; message = osip_str_append (message, " "); } /* } */ message = osip_strn_append (message, CRLF, 2); /* end of headers */ message = osip_strn_append (message, CRLF, 2); start_of_bodies = message; total_length = start_of_bodies - *dest; if (osip_list_eol (&sip->bodies, 0)) { /* same remark as at the beginning of the method */ sip->message_property = 1; sip->message = osip_strdup (*dest); sip->message_length = total_length; if (message_length != NULL) *message_length = total_length; return 0; /* it's all done */ } if (sip->mime_version != NULL && sip->content_type && sip->content_type->type && !osip_strcasecmp (sip->content_type->type, "multipart")) { osip_generic_param_t *ct_param = NULL; /* find the boundary */ i = osip_generic_param_get_byname (&sip->content_type->gen_params, "boundary", &ct_param); if ((i >= 0) && ct_param && ct_param->gvalue) { size_t len = strlen (ct_param->gvalue); if (len > MIME_MAX_BOUNDARY_LEN) { osip_free (*dest); *dest = NULL; return -1; } boundary = osip_malloc (len + 5); osip_strncpy (boundary, CRLF, 2); osip_strncpy (boundary + 2, "--", 2); if (ct_param->gvalue[0] == '"' && ct_param->gvalue[len - 1] == '"') osip_strncpy (boundary + 4, ct_param->gvalue + 1, len - 2); else osip_strncpy (boundary + 4, ct_param->gvalue, len); } } pos = 0; while (!osip_list_eol (&sip->bodies, pos)) { osip_body_t *body; size_t body_length; body = (osip_body_t *) osip_list_get (&sip->bodies, pos); if (boundary) { /* Needs at most 77 bytes, last realloc allocate at least 100 bytes extra */ message = osip_str_append (message, boundary); message = osip_strn_append (message, CRLF, 2); } i = osip_body_to_str (body, &tmp, &body_length); if (i != 0) { osip_free (*dest); *dest = NULL; if (boundary) osip_free (boundary); return -1; } if (malloc_size < message - *dest + 100 + body_length) { size_t size = message - *dest; int offset_of_body; int offset_content_length_to_modify = 0; offset_of_body = (int) (start_of_bodies - *dest); if (content_length_to_modify != NULL) offset_content_length_to_modify = (int) (content_length_to_modify - *dest); malloc_size = message - *dest + body_length + 100; *dest = osip_realloc (*dest, malloc_size); if (*dest == NULL) { osip_free (tmp); /* fixed 09/Jun/2005 */ if (boundary) osip_free (boundary); return -1; } start_of_bodies = *dest + offset_of_body; if (content_length_to_modify != NULL) content_length_to_modify = *dest + offset_content_length_to_modify; message = *dest + size; } memcpy (message, tmp, body_length); message[body_length] = '\0'; osip_free (tmp); message = message + body_length; pos++; } if (boundary) { /* Needs at most 79 bytes, last realloc allocate at least 100 bytes extra */ message = osip_str_append (message, boundary); message = osip_strn_append (message, "--", 2); message = osip_strn_append (message, CRLF, 2); osip_free (boundary); boundary = NULL; } if (content_length_to_modify == NULL) { osip_free (*dest); *dest = NULL; return -1; } /* we NOW have the length of bodies: */ { size_t size = message - start_of_bodies; char tmp2[15]; total_length += size; sprintf (tmp2, "%i", size); /* do not use osip_strncpy here! */ strncpy (content_length_to_modify + 5 - strlen (tmp2), tmp2, strlen (tmp2)); } /* same remark as at the beginning of the method */ sip->message_property = 1; sip->message = osip_malloc (total_length + 1); if (sip->message != NULL) { memcpy (sip->message, *dest, total_length); sip->message[total_length] = '\0'; sip->message_length = total_length; if (message_length != NULL) *message_length = total_length; } return 0; }