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

📄 msg_read.c

📁 libosip-0.9.7源码
💻 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 <osip/smsg.h>#include <osip/port.h>static intstartline_2charreq (startline_t * strtline, char **dest){  char *tmp;  char *rquri;  int i;  *dest = NULL;  if ((strtline == NULL) || (strtline->rquri == NULL)      || (strtline->sipmethod == NULL) || (strtline->sipversion == NULL))    return -1;  i = url_2char (strtline->rquri, &rquri);  if (i == -1)    return -1;  *dest = (char *) smalloc (strlen (strtline->sipmethod)			    + strlen (rquri) + strlen (strtline->sipversion) +			    3);  tmp = *dest;  sstrncpy (tmp, strtline->sipmethod, strlen (strtline->sipmethod));  tmp = tmp + strlen (strtline->sipmethod);  sstrncpy (tmp, " ", 1);  tmp = tmp + 1;  sstrncpy (tmp, rquri, strlen (rquri));  tmp = tmp + strlen (rquri);  sstrncpy (tmp, " ", 1);  tmp = tmp + 1;  sstrncpy (tmp, strtline->sipversion, strlen (strtline->sipversion));  sfree (rquri);  return 0;}static intstartline_2charresp (startline_t * strtline, char **dest){  char *tmp;  *dest = NULL;  if ((strtline == NULL) || (strtline->reasonphrase == NULL)      || (strtline->statuscode == NULL) || (strtline->sipversion == NULL))    return -1;  *dest = (char *) smalloc (strlen (strtline->sipversion)			    + strlen (strtline->statuscode)			    + strlen (strtline->reasonphrase) + 4);  tmp = *dest;  sstrncpy (tmp, strtline->sipversion, strlen (strtline->sipversion));  tmp = tmp + strlen (strtline->sipversion);  sstrncpy (tmp, " ", 1);  tmp = tmp + 1;  sstrncpy (tmp, strtline->statuscode, strlen (strtline->statuscode));  tmp = tmp + strlen (strtline->statuscode);  sstrncpy (tmp, " ", 1);  tmp = tmp + 1;  sstrncpy (tmp, strtline->reasonphrase, strlen (strtline->reasonphrase));  return 0;}static intmsg_startline_2char (startline_t * strtline, char **dest){  if (strtline->sipmethod != NULL)    return startline_2charreq (strtline, dest);  if (strtline->statuscode != NULL)    return startline_2charresp (strtline, dest);  OSIP_TRACE (osip_trace	      (__FILE__, __LINE__, TRACE_LEVEL1, NULL,	       "ERROR strtline->method has no value!\n"));  return -1;			/* should never come here */}char *msg_getreasonphrase (sip_t * sip){  return sip->strtline->reasonphrase;}char *msg_getstatuscode (sip_t * sip){  return sip->strtline->statuscode;}char *msg_getmethod (sip_t * sip){  return sip->strtline->sipmethod;}char *msg_getversion (sip_t * sip){  return sip->strtline->sipversion;}url_t *msg_geturi (sip_t * sip){  return sip->strtline->rquri;}intstrcat_simple_header (char **_string, int *malloc_size,		      char **_message, void *ptr_header, char *header_name,		      int size_of_header, int (*xxx_2char) (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;	}      sstrncpy (message, header_name, size_of_header);      message = message + strlen (message);      i = xxx_2char (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;	}      sstrncpy (message, tmp, strlen (tmp));      sfree (tmp);      message = message + strlen (message);      sstrncpy (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, list_t * headers,			     char *header, int size_of_header,			     int (*xxx_2char) (void *, char **), char **next){  char *string;  char *message;  char *tmp;  int pos = 0;  int i;  string = *_string;  message = *_message;  while (!list_eol (headers, pos))    {      void *elt;      elt = (void *) 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;	}      sstrncpy (message, header, size_of_header);      i = xxx_2char (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;	}      sstrncpy (message, tmp, strlen (tmp));      sfree (tmp);      message = message + strlen (message);      sstrncpy (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, list_t * headers,				char *header, int size_of_header,				int (*xxx_2char) (void *, char **),				char **next){  char *string;  char *message;  char *tmp;  int pos = 0;  int i;  string = *_string;  message = *_message;  pos = 0;  while (!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;	}      sstrncpy (message, header, size_of_header);      message = message + strlen (message);      while (!list_eol (headers, pos))	{	  void *elt;	  elt = (void *) list_get (headers, pos);	  i = xxx_2char (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;	    }	  sstrncpy (message, tmp, strlen (tmp));	  sfree (tmp);	  message = message + strlen (message);	  pos++;	  if (!list_eol (headers, pos))	    {	      strncpy (message, ", ", 2);	      message = message + 2;	    }	}      sstrncpy (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 msg_2char to update it).    -1 on error.  */intmsg_get_property (sip_t * sip){  if (sip == NULL)    return -1;  return sip->message_property;}#endifintmsg_force_update (sip_t * sip){#ifdef USE_TMP_BUFFER  if (sip == NULL)    return -1;  sip->message_property = 2;#endif  return 0;}intmsg_2char (sip_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->strtline == NULL) || (sip->to == NULL)      || (sip->from == NULL))    return -1;#ifdef USE_TMP_BUFFER  {#ifdef ENABLE_DEBUG    static int number_of_call;    static int number_of_call_avoided;    number_of_call++;#endif    if (1 == msg_get_property (sip))      {				/* message is already available in "message" */#ifdef ENABLE_DEBUG	number_of_call_avoided++;#endif	*dest = sgetcopy (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 msg_2char avoided: %i\n",	     number_of_call_avoided); */#endif	return 0;      }    else      {	/* message should be rebuilt: delete the old one if exists. */	sfree (sip->message);	sip->message = NULL;      }  }#endif  message = (char *) smalloc (SIP_MESSAGE_MAX_LENGTH);	/* ???? message could be > 4000  */  *dest = message;  /* add the first line of message */  i = msg_startline_2char (sip->strtline, &tmp);  if (i == -1)    {      sfree (*dest);      *dest = NULL;      return -1;    }  sstrncpy (message, tmp, strlen (tmp));  sfree (tmp);  message = message + strlen (message);  sstrncpy (message, CRLF, 2);  message = message + 2;  i =    strcat_headers_one_per_line (dest, &malloc_size, &message, sip->vias,				 "Via: ", 5,				 ((int (*)(void *, char **)) &via_2char),				 &next);  if (i != 0)    {      sfree (*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 **))				  &record_route_2char), &next);  if (i != 0)    {      sfree (*dest);      *dest = NULL;      return -1;    }  message = next;  i =    strcat_headers_one_per_line (dest, &malloc_size, &message, sip->routes,				 "Route: ", 7,				 ((int (*)(void *, char **)) &route_2char),				 &next);  if (i != 0)    {      sfree (*dest);      *dest = NULL;      return -1;    }  message = next;

⌨️ 快捷键说明

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