⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 osip_message_to_str.c

📁 SIP协议栈实现
💻 C
📖 第 1 页 / 共 2 页
字号:
/*  The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)  Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org    This library is free software; you can redistribute it and/or  modify it under the terms of the GNU Lesser General Public  License as published by the Free Software Foundation; either  version 2.1 of the License, or (at your option) any later version.    This library is distributed in the hope that it will be useful,  but WITHOUT ANY WARRANTY; without even the implied warranty of  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU  Lesser General Public License for more details.    You should have received a copy of the GNU Lesser General Public  License along with this library; if not, write to the Free Software  Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA*/#include <stdio.h>#include <stdlib.h>#include <osipparser2/osip_port.h>#include <osipparser2/osip_parser.h>extern const char *osip_protocol_version;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 -1;  i = osip_uri_to_str (sip->req_uri, &rquri);  if (i != 0)    return -1;  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);  tmp = *dest;  osip_strncpy (tmp, sip->sip_method, strlen (sip->sip_method));  tmp = tmp + strlen (sip->sip_method);  osip_strncpy (tmp, " ", 1);  tmp = tmp + 1;  osip_strncpy (tmp, rquri, strlen (rquri));  tmp = tmp + strlen (rquri);  osip_strncpy (tmp, " ", 1);  tmp = tmp + 1;  osip_strncpy (tmp, sip_version, strlen (sip_version));  osip_free (rquri);  return 0;}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 -1;  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);  tmp = *dest;  osip_strncpy (tmp, sip_version, strlen (sip_version));  tmp = tmp + strlen (sip_version);  osip_strncpy (tmp, " ", 1);  tmp = tmp + 1;  osip_strncpy (tmp, status_code, 3);  tmp = tmp + 3;  osip_strncpy (tmp, " ", 1);  tmp = tmp + 1;  osip_strncpy (tmp, sip->reason_phrase, strlen (sip->reason_phrase));  return 0;}static int__osip_message_startline_to_str (osip_message_t *sip, char **dest){  if (sip->sip_method != NULL)    return __osip_message_startline_to_strreq (sip, dest);  if (sip->status_code != 0)    return __osip_message_startline_to_strresp (sip, dest);  OSIP_TRACE (osip_trace	      (__FILE__, __LINE__, TRACE_LEVEL1, NULL,	       "ERROR method has no value or status code is 0!\n"));  return -1;			/* should never come here */}char *osip_message_get_reason_phrase (const osip_message_t * sip){  return sip->reason_phrase;}intosip_message_get_status_code (const osip_message_t * sip){  return sip->status_code;}char *osip_message_get_method (const osip_message_t * sip){  return sip->sip_method;}char *osip_message_get_version (const osip_message_t * sip){  return sip->sip_version;}osip_uri_t *osip_message_get_uri (const osip_message_t * sip){  return sip->req_uri;}intstrcat_simple_header (char **_string, int *malloc_size,		      char **_message, void *ptr_header, char *header_name,		      int 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 realloc too much often */	{			/* should not happen often */	  int size = message - string;	  *malloc_size = message - string + size_of_header + 100;	  string = realloc (string, *malloc_size);	  if (string == NULL)	    {	      *_string = NULL;	      *_message = NULL;	      return -1;	    }	  message = string + size;	}      osip_strncpy (message, header_name, size_of_header);      message = message + strlen (message);      i = xxx_to_str (ptr_header, &tmp);      if (i == -1)	{	  *_string = string;	  *_message = message;	  *next = NULL;	  return -1;	}      if (*malloc_size < message - string + (int) strlen (tmp) + 100)	{	  int size = message - string;	  *malloc_size = message - string + strlen (tmp) + 100;	  string = realloc (string, *malloc_size);	  if (string == NULL)	    {	      *_string = NULL;	      *_message = NULL;	      return -1;	    }	  message = string + size;	}      osip_strncpy (message, tmp, strlen (tmp));      osip_free (tmp);      message = message + strlen (message);      osip_strncpy (message, CRLF, 2);      message = message + 2;    }  *_string = string;  *_message = message;  *next = message;  return 0;}intstrcat_headers_one_per_line (char **_string, int *malloc_size,			     char **_message, osip_list_t * headers,			     char *header, int 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;  while (!osip_list_eol (headers, pos))    {      void *elt;      elt = (void *) osip_list_get (headers, pos);      if (*malloc_size < message - string + 100 + size_of_header)	/* take some memory avoid to realloc too much often */	{			/* should not happen often */	  int size = message - string;	  *malloc_size = message - string + size_of_header + 100;	  string = realloc (string, *malloc_size);	  if (string == NULL)	    {	      *_string = NULL;	      *_message = NULL;	      return -1;	    }	  message = string + size;	}      osip_strncpy (message, header, size_of_header);      i = xxx_to_str (elt, &tmp);      if (i == -1)	{	  *_string = string;	  *_message = message;	  *next = NULL;	  return -1;	}      message = message + strlen (message);      if (*malloc_size < message - string + (int) strlen (tmp) + 100)	{	  int size = message - string;	  *malloc_size = message - string + strlen (tmp) + 100;	  string = realloc (string, *malloc_size);	  if (string == NULL)	    {	      *_string = NULL;	      *_message = NULL;	      return -1;	    }	  message = string + size;	}      osip_strncpy (message, tmp, strlen (tmp));      osip_free (tmp);      message = message + strlen (message);      osip_strncpy (message, CRLF, 2);      message = message + 2;      pos++;    }  *_string = string;  *_message = message;  *next = message;  return 0;}intstrcat_headers_all_on_one_line (char **_string, int *malloc_size,				char **_message, osip_list_t * headers,				char *header, int 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 realloc too much often */	{			/* should not happen often */	  int size = message - string;	  *malloc_size = message - string + size_of_header + 100;	  string = realloc (string, *malloc_size);	  if (string == NULL)	    {	      *_string = NULL;	      *_message = NULL;	      return -1;	    }	  message = string + size;	}      osip_strncpy (message, header, size_of_header);      message = message + strlen (message);      while (!osip_list_eol (headers, pos))	{	  void *elt;	  elt = (void *) osip_list_get (headers, pos);	  i = xxx_to_str (elt, &tmp);	  if (i == -1)	    {	      *_string = string;	      *_message = message;	      *next = NULL;	      return -1;	    }	  if (*malloc_size < message - string + (int) strlen (tmp) + 100)	    {	      int size = message - string;	      *malloc_size = message - string + (int) strlen (tmp) + 100;	      string = realloc (string, *malloc_size);	      if (string == NULL)		{		  *_string = NULL;		  *_message = NULL;		  return -1;		}	      message = string + size;	    }	  osip_strncpy (message, tmp, strlen (tmp));	  osip_free (tmp);	  message = message + strlen (message);	  pos++;	  if (!osip_list_eol (headers, pos))	    {	      strncpy (message, ", ", 2);	      message = message + 2;	    }	}      osip_strncpy (message, CRLF, 2);      message = message + 2;    }  *_string = string;  *_message = message;  *next = message;  return 0;}#ifdef USE_TMP_BUFFER /* return values:    1: structure and buffer "message" are identical.    2: buffer "message" is not up to date with the structure info (call osip_message_to_str to update it).    -1 on error.  */intosip_message_get__property (const osip_message_t * sip){  if (sip == NULL)    return -1;  return sip->message_property;}#endifintosip_message_force_update (osip_message_t * sip){#ifdef USE_TMP_BUFFER  if (sip == NULL)    return -1;  sip->message_property = 2;#endif  return 0;}intosip_message_to_str (osip_message_t * sip, char **dest){  int malloc_size;  /* Added at SIPit day1 */  char *start_of_bodies;  char *content_length_to_modify = NULL;  char *message;  char *next;  char *tmp;  int pos;  int i;  malloc_size = SIP_MESSAGE_MAX_LENGTH;  *dest = NULL;  if ((sip == NULL) || (sip->to == NULL)      || (sip->from == NULL))    return -1;#ifdef USE_TMP_BUFFER  {#ifdef ENABLE_DEBUG    static int number_of_call = 0;    static int number_of_call_avoided = 0;    number_of_call++;#endif    if (1 == osip_message_get__property (sip))      {				/* message is already available in "message" */#ifdef ENABLE_DEBUG	number_of_call_avoided++;#endif	*dest = osip_strdup (sip->message);	/* is we just return the pointer, people will						   get issues while upgrading the library. This						   is because they are used to call free(*dest)						   right after using the buffer. This may be						   changed while moving to another branch						   developpement. replacement line will be:						   *dest = sip->message; */#ifdef ENABLE_DEBUG	/* if (number_of_call_avoided % 1000 == 0)	  printf ("number of call osip_message_to_str avoided: %i\n",	     number_of_call_avoided); */#endif	return 0;      }    else      {	/* message should be rebuilt: delete the old one if exists. */	osip_free (sip->message);	sip->message = NULL;      }  }#endif  message = (char *) osip_malloc (SIP_MESSAGE_MAX_LENGTH);	/* ???? message could be > 4000  */  *dest = message;  /* add the first line of message */  i = __osip_message_startline_to_str (sip, &tmp);  if (i == -1)    {      osip_free (*dest);      *dest = NULL;      return -1;    }  osip_strncpy (message, tmp, strlen (tmp));  osip_free (tmp);  message = message + strlen (message);  osip_strncpy (message, CRLF, 2);  message = message + 2;  i =

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -