Commit 43764778 authored by Ghislain MARY's avatar Ghislain MARY
Browse files

Improve marshalling.

 - Use our own implementation of snprintf() called belle_sip_snprintf() to handle the various native implementations whose return values are not consistent.
 - Simplify error handling in marshalling.
parent 1c7e3e47
...@@ -21,6 +21,7 @@ ...@@ -21,6 +21,7 @@
#include "belle-sip/defs.h" #include "belle-sip/defs.h"
#include "belle-sip/uri.h" #include "belle-sip/uri.h"
#include "belle-sip/utils.h"
#include <time.h> #include <time.h>
...@@ -75,7 +76,7 @@ typedef struct _belle_sip_header belle_sip_header_t; ...@@ -75,7 +76,7 @@ typedef struct _belle_sip_header belle_sip_header_t;
BELLESIP_EXPORT belle_sip_header_t* belle_sip_header_create (const char* name,const char* value); BELLESIP_EXPORT belle_sip_header_t* belle_sip_header_create (const char* name,const char* value);
BELLESIP_EXPORT const char* belle_sip_header_get_name (const belle_sip_header_t* obj); BELLESIP_EXPORT const char* belle_sip_header_get_name (const belle_sip_header_t* obj);
BELLESIP_EXPORT void belle_sip_header_set_name (belle_sip_header_t* obj,const char* value); BELLESIP_EXPORT void belle_sip_header_set_name (belle_sip_header_t* obj,const char* value);
BELLESIP_EXPORT int belle_sip_header_marshal(belle_sip_header_t* header, char* buff, unsigned int offset,unsigned int buff_size); BELLESIP_EXPORT belle_sip_error_code belle_sip_header_marshal(belle_sip_header_t* header, char* buff, size_t buff_size, unsigned int *offset);
BELLESIP_EXPORT const char *belle_sip_header_get_unparsed_value(belle_sip_header_t* obj); BELLESIP_EXPORT const char *belle_sip_header_get_unparsed_value(belle_sip_header_t* obj);
#define BELLE_SIP_HEADER(t) BELLE_SIP_CAST(t,belle_sip_header_t) #define BELLE_SIP_HEADER(t) BELLE_SIP_CAST(t,belle_sip_header_t)
......
...@@ -20,6 +20,7 @@ ...@@ -20,6 +20,7 @@
#define belle_sip_object_h #define belle_sip_object_h
#include "belle-sip/defs.h" #include "belle-sip/defs.h"
#include "belle-sip/utils.h"
/* /*
* typedefs, macros and functions for object definition and manipulation. * typedefs, macros and functions for object definition and manipulation.
...@@ -113,7 +114,7 @@ typedef struct _belle_sip_object belle_sip_object_t; ...@@ -113,7 +114,7 @@ typedef struct _belle_sip_object belle_sip_object_t;
typedef void (*belle_sip_object_destroy_t)(belle_sip_object_t*); typedef void (*belle_sip_object_destroy_t)(belle_sip_object_t*);
typedef void (*belle_sip_object_clone_t)(belle_sip_object_t* obj, const belle_sip_object_t *orig); typedef void (*belle_sip_object_clone_t)(belle_sip_object_t* obj, const belle_sip_object_t *orig);
typedef int (*belle_sip_object_marshal_t)(belle_sip_object_t* obj, char* buff,unsigned int offset,size_t buff_size); typedef int (*belle_sip_object_marshal_t)(belle_sip_object_t* obj, char* buff, size_t buff_size, unsigned int *offset);
struct _belle_sip_object_vptr{ struct _belle_sip_object_vptr{
belle_sip_type_id_t id; belle_sip_type_id_t id;
...@@ -230,7 +231,7 @@ BELLESIP_EXPORT char* belle_sip_object_to_string(void* obj); ...@@ -230,7 +231,7 @@ BELLESIP_EXPORT char* belle_sip_object_to_string(void* obj);
* Writes a string representation of the object into the supplied buffer. * Writes a string representation of the object into the supplied buffer.
* Same as belle_sip_object_to_string(), but without allocating space for the output string. * Same as belle_sip_object_to_string(), but without allocating space for the output string.
**/ **/
BELLESIP_EXPORT int belle_sip_object_marshal(belle_sip_object_t* obj, char* buff,unsigned int offset,size_t buff_size); BELLESIP_EXPORT belle_sip_error_code belle_sip_object_marshal(belle_sip_object_t* obj, char* buff, size_t buff_size, unsigned int *offset);
BELLESIP_EXPORT int belle_sip_object_is_instance_of(belle_sip_object_t * obj,belle_sip_type_id_t id); BELLESIP_EXPORT int belle_sip_object_is_instance_of(belle_sip_object_t * obj,belle_sip_type_id_t id);
......
...@@ -19,6 +19,7 @@ ...@@ -19,6 +19,7 @@
#ifndef PARAMETERS_H_ #ifndef PARAMETERS_H_
#define PARAMETERS_H_ #define PARAMETERS_H_
#include "belle-sip/utils.h"
#include "belle-sip/list.h" #include "belle-sip/list.h"
/*** /***
* parameters * parameters
...@@ -51,7 +52,7 @@ BELLESIP_EXPORT const belle_sip_list_t * belle_sip_parameters_get_parameters(con ...@@ -51,7 +52,7 @@ BELLESIP_EXPORT const belle_sip_list_t * belle_sip_parameters_get_parameters(con
BELLESIP_EXPORT void belle_sip_parameters_remove_parameter(belle_sip_parameters_t* obj,const char* name); BELLESIP_EXPORT void belle_sip_parameters_remove_parameter(belle_sip_parameters_t* obj,const char* name);
BELLESIP_EXPORT int belle_sip_parameters_marshal(const belle_sip_parameters_t* obj, char* buff,unsigned int offset,unsigned int buff_size); BELLESIP_EXPORT belle_sip_error_code belle_sip_parameters_marshal(const belle_sip_parameters_t* obj, char* buff, size_t buff_size, unsigned int *offset);
#define BELLE_SIP_PARAMETERS(obj) BELLE_SIP_CAST(obj,belle_sip_parameters_t) #define BELLE_SIP_PARAMETERS(obj) BELLE_SIP_CAST(obj,belle_sip_parameters_t)
......
...@@ -10,6 +10,7 @@ ...@@ -10,6 +10,7 @@
#include "belle-sip/defs.h" #include "belle-sip/defs.h"
#include "belle-sip/list.h" #include "belle-sip/list.h"
#include "belle-sip/utils.h"
/*inherite from belle_sip_parameters_t*/ /*inherite from belle_sip_parameters_t*/
typedef struct _belle_sip_uri belle_sip_uri_t; typedef struct _belle_sip_uri belle_sip_uri_t;
...@@ -184,7 +185,7 @@ BELLESIP_EXPORT void belle_sip_uri_fix(belle_sip_uri_t *uri); ...@@ -184,7 +185,7 @@ BELLESIP_EXPORT void belle_sip_uri_fix(belle_sip_uri_t *uri);
*/ */
BELLESIP_EXPORT char* belle_sip_uri_to_string(belle_sip_uri_t* uri) ; BELLESIP_EXPORT char* belle_sip_uri_to_string(belle_sip_uri_t* uri) ;
int belle_sip_uri_marshal(const belle_sip_uri_t* uri, char* buff,unsigned int offset,unsigned int buff_size); belle_sip_error_code belle_sip_uri_marshal(const belle_sip_uri_t* uri, char* buff, size_t buff_size, unsigned int *offset);
#define BELLE_SIP_URI(obj) BELLE_SIP_CAST(obj,belle_sip_uri_t) #define BELLE_SIP_URI(obj) BELLE_SIP_CAST(obj,belle_sip_uri_t)
......
...@@ -41,6 +41,13 @@ typedef enum { ...@@ -41,6 +41,13 @@ typedef enum {
typedef void (*belle_sip_log_function_t)(belle_sip_log_level lev, const char *fmt, va_list args); typedef void (*belle_sip_log_function_t)(belle_sip_log_level lev, const char *fmt, va_list args);
typedef enum {
BELLE_SIP_NOT_IMPLEMENTED = -2,
BELLE_SIP_BUFFER_OVERFLOW = -1,
BELLE_SIP_OK = 0
} belle_sip_error_code;
#ifdef __GNUC__ #ifdef __GNUC__
#define BELLE_SIP_CHECK_FORMAT_ARGS(m,n) __attribute__((format(printf,m,n))) #define BELLE_SIP_CHECK_FORMAT_ARGS(m,n) __attribute__((format(printf,m,n)))
#else #else
...@@ -137,6 +144,8 @@ BELLESIP_EXPORT void belle_sip_set_log_handler(belle_sip_log_function_t func); ...@@ -137,6 +144,8 @@ BELLESIP_EXPORT void belle_sip_set_log_handler(belle_sip_log_function_t func);
BELLESIP_EXPORT char * BELLE_SIP_CHECK_FORMAT_ARGS(1,2) belle_sip_strdup_printf(const char *fmt,...); BELLESIP_EXPORT char * BELLE_SIP_CHECK_FORMAT_ARGS(1,2) belle_sip_strdup_printf(const char *fmt,...);
BELLESIP_EXPORT belle_sip_error_code belle_sip_snprintf(char *buff, unsigned int buff_size, unsigned int *offset, const char *fmt, ...);
BELLESIP_EXPORT void belle_sip_set_log_level(int level); BELLESIP_EXPORT void belle_sip_set_log_level(int level);
BELLESIP_EXPORT char * belle_sip_random_token(char *ret, size_t size); BELLESIP_EXPORT char * belle_sip_random_token(char *ret, size_t size);
......
...@@ -37,22 +37,17 @@ void belle_sdp_attribute_clone(belle_sdp_attribute_t *attribute, const belle_sdp ...@@ -37,22 +37,17 @@ void belle_sdp_attribute_clone(belle_sdp_attribute_t *attribute, const belle_sdp
CLONE_STRING(belle_sdp_attribute,name,attribute,orig) CLONE_STRING(belle_sdp_attribute,name,attribute,orig)
CLONE_STRING(belle_sdp_attribute,value,attribute,orig) CLONE_STRING(belle_sdp_attribute,value,attribute,orig)
} }
int belle_sdp_attribute_marshal(belle_sdp_attribute_t* attribute, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_attribute_marshal(belle_sdp_attribute_t* attribute, char* buff, size_t buff_size, unsigned int *offset) {
current_offset+=snprintf( buff+current_offset belle_sip_error_code error=belle_sip_snprintf(buff,buff_size,offset,"a=%s",attribute->name);
,buff_size-current_offset if (error!=BELLE_SIP_OK) return error;
,"a=%s"
,attribute->name);
if (current_offset>=buff_size) return buff_size-offset;
if (attribute->value) { if (attribute->value) {
current_offset+=snprintf( buff+current_offset error=belle_sip_snprintf(buff,buff_size,offset,":%s",attribute->value);
,buff_size-current_offset if (error!=BELLE_SIP_OK) return error;
,":%s"
,attribute->value);
if (current_offset>=buff_size) return buff_size-offset;
} }
return current_offset-offset; return error;
} }
BELLE_SDP_NEW(attribute,belle_sip_object) BELLE_SDP_NEW(attribute,belle_sip_object)
belle_sdp_attribute_t* belle_sdp_attribute_create (const char* name,const char* value) { belle_sdp_attribute_t* belle_sdp_attribute_create (const char* name,const char* value) {
belle_sdp_attribute_t* attribute = belle_sdp_attribute_new(); belle_sdp_attribute_t* attribute = belle_sdp_attribute_new();
...@@ -83,15 +78,11 @@ void belle_sdp_bandwidth_clone(belle_sdp_bandwidth_t *bandwidth, const belle_sdp ...@@ -83,15 +78,11 @@ void belle_sdp_bandwidth_clone(belle_sdp_bandwidth_t *bandwidth, const belle_sdp
CLONE_STRING(belle_sdp_bandwidth,type,bandwidth,orig) CLONE_STRING(belle_sdp_bandwidth,type,bandwidth,orig)
bandwidth->value=orig->value; bandwidth->value=orig->value;
} }
int belle_sdp_bandwidth_marshal(belle_sdp_bandwidth_t* bandwidth, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_bandwidth_marshal(belle_sdp_bandwidth_t* bandwidth, char* buff, size_t buff_size, unsigned int *offset) {
current_offset+=snprintf( buff+current_offset return belle_sip_snprintf(buff,buff_size,offset,"b=%s:%i",bandwidth->type,bandwidth->value);
,buff_size-current_offset
,"b=%s:%i"
,bandwidth->type,bandwidth->value);
if (current_offset>=buff_size) return buff_size-offset;
return current_offset-offset;
} }
BELLE_SDP_NEW(bandwidth,belle_sip_object) BELLE_SDP_NEW(bandwidth,belle_sip_object)
BELLE_SDP_PARSE(bandwidth) BELLE_SDP_PARSE(bandwidth)
GET_SET_STRING(belle_sdp_bandwidth,type); GET_SET_STRING(belle_sdp_bandwidth,type);
...@@ -119,17 +110,11 @@ void belle_sdp_connection_clone(belle_sdp_connection_t *connection, const belle_ ...@@ -119,17 +110,11 @@ void belle_sdp_connection_clone(belle_sdp_connection_t *connection, const belle_
CLONE_STRING(belle_sdp_connection,address,connection,orig) CLONE_STRING(belle_sdp_connection,address,connection,orig)
} }
int belle_sdp_connection_marshal(belle_sdp_connection_t* connection, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_connection_marshal(belle_sdp_connection_t* connection, char* buff, size_t buff_size, unsigned int *offset) {
current_offset+=snprintf( buff+current_offset return belle_sip_snprintf(buff,buff_size,offset,"c=%s %s %s",connection->network_type,connection->address_type,connection->address);
,buff_size-current_offset
,"c=%s %s %s"
,connection->network_type
,connection->address_type
,connection->address);
if (current_offset>=buff_size) return buff_size-offset;
return current_offset-offset;
} }
BELLE_SDP_NEW(connection,belle_sip_object) BELLE_SDP_NEW(connection,belle_sip_object)
BELLE_SDP_PARSE(connection) BELLE_SDP_PARSE(connection)
belle_sdp_connection_t* belle_sdp_connection_create(const char* net_type, const char* addr_type, const char* addr) { belle_sdp_connection_t* belle_sdp_connection_create(const char* net_type, const char* addr_type, const char* addr) {
...@@ -157,15 +142,11 @@ void belle_sdp_email_destroy(belle_sdp_email_t* email) { ...@@ -157,15 +142,11 @@ void belle_sdp_email_destroy(belle_sdp_email_t* email) {
void belle_sdp_email_clone(belle_sdp_email_t *email, const belle_sdp_email_t *orig){ void belle_sdp_email_clone(belle_sdp_email_t *email, const belle_sdp_email_t *orig){
CLONE_STRING(belle_sdp_email,value,email,orig) CLONE_STRING(belle_sdp_email,value,email,orig)
} }
int belle_sdp_email_marshal(belle_sdp_email_t* email, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_email_marshal(belle_sdp_email_t* email, char* buff, size_t buff_size, unsigned int *offset) {
current_offset+=snprintf( buff+current_offset return belle_sip_snprintf(buff,buff_size,offset,"e=%s",email->value);
,buff_size-current_offset
,"e=%s"
,email->value);
if (current_offset>=buff_size) return buff_size-offset;
return current_offset-offset;
} }
BELLE_SDP_NEW(email,belle_sip_object) BELLE_SDP_NEW(email,belle_sip_object)
BELLE_SDP_PARSE(email) BELLE_SDP_PARSE(email)
GET_SET_STRING(belle_sdp_email,value); GET_SET_STRING(belle_sdp_email,value);
...@@ -184,15 +165,11 @@ void belle_sdp_info_destroy(belle_sdp_info_t* info) { ...@@ -184,15 +165,11 @@ void belle_sdp_info_destroy(belle_sdp_info_t* info) {
void belle_sdp_info_clone(belle_sdp_info_t *info, const belle_sdp_info_t *orig){ void belle_sdp_info_clone(belle_sdp_info_t *info, const belle_sdp_info_t *orig){
CLONE_STRING(belle_sdp_info,value,info,orig) CLONE_STRING(belle_sdp_info,value,info,orig)
} }
int belle_sdp_info_marshal(belle_sdp_info_t* info, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_info_marshal(belle_sdp_info_t* info, char* buff, size_t buff_size, unsigned int *offset) {
current_offset+=snprintf( buff+current_offset return belle_sip_snprintf(buff,buff_size,offset,"i=%s",info->value);
,buff_size-current_offset
,"i=%s"
,info->value);
if (current_offset>=buff_size) return buff_size-offset;
return current_offset-offset;
} }
BELLE_SDP_NEW(info,belle_sip_object) BELLE_SDP_NEW(info,belle_sip_object)
BELLE_SDP_PARSE(info) BELLE_SDP_PARSE(info)
GET_SET_STRING(belle_sdp_info,value); GET_SET_STRING(belle_sdp_info,value);
...@@ -230,37 +207,24 @@ void belle_sdp_media_clone(belle_sdp_media_t *media, const belle_sdp_media_t *or ...@@ -230,37 +207,24 @@ void belle_sdp_media_clone(belle_sdp_media_t *media, const belle_sdp_media_t *or
media->port_count=orig->port_count; media->port_count=orig->port_count;
CLONE_STRING(belle_sdp_media,protocol,media,orig) CLONE_STRING(belle_sdp_media,protocol,media,orig)
} }
int belle_sdp_media_marshal(belle_sdp_media_t* media, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_media_marshal(belle_sdp_media_t* media, char* buff, size_t buff_size, unsigned int *offset) {
belle_sip_list_t* list=media->media_formats; belle_sip_list_t* list=media->media_formats;
current_offset+=snprintf( buff+current_offset belle_sip_error_code error=belle_sip_snprintf(buff,buff_size,offset,"m=%s %i",media->media_type,media->media_port);
,buff_size-current_offset if (error!=BELLE_SIP_OK) return error;
,"m=%s %i"
,media->media_type
,media->media_port
);
if (current_offset>=buff_size) return buff_size-offset;
if (media->port_count>1) { if (media->port_count>1) {
current_offset+=snprintf(buff+current_offset error=belle_sip_snprintf(buff,buff_size,offset,"/%i",media->port_count);
,buff_size-current_offset if (error!=BELLE_SIP_OK) return error;
,"/%i"
,media->port_count);
if (current_offset>=buff_size) return buff_size-offset;
} }
current_offset+=snprintf( buff+current_offset error=belle_sip_snprintf(buff,buff_size,offset," %s",media->protocol);
,buff_size-current_offset if (error!=BELLE_SIP_OK) return error;
," %s"
,media->protocol);
if (current_offset>=buff_size) return buff_size-offset;
for(;list!=NULL;list=list->next){ for(;list!=NULL;list=list->next){
current_offset+=snprintf( buff+current_offset error=belle_sip_snprintf(buff,buff_size,offset," %li",(long)list->data);
,buff_size-current_offset if (error!=BELLE_SIP_OK) return error;
," %li"
,(long)list->data);
if (current_offset>=buff_size) return buff_size-offset;
} }
return current_offset-offset; return error;
} }
BELLE_SDP_NEW_WITH_CTR(media,belle_sip_object) BELLE_SDP_NEW_WITH_CTR(media,belle_sip_object)
BELLE_SDP_PARSE(media) BELLE_SDP_PARSE(media)
belle_sdp_media_t* belle_sdp_media_create(const char* media_type belle_sdp_media_t* belle_sdp_media_create(const char* media_type
...@@ -314,33 +278,34 @@ static void belle_sdp_base_description_clone(belle_sdp_base_description_t *base_ ...@@ -314,33 +278,34 @@ static void belle_sdp_base_description_clone(belle_sdp_base_description_t *base_
base_description->attributes = belle_sip_list_copy_with_data(orig->attributes,belle_sip_object_copyfunc); base_description->attributes = belle_sip_list_copy_with_data(orig->attributes,belle_sip_object_copyfunc);
} }
int belle_sdp_base_description_marshal(belle_sdp_base_description_t* base_description, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_base_description_marshal(belle_sdp_base_description_t* base_description, char* buff, size_t buff_size, unsigned int *offset) {
belle_sip_error_code error=BELLE_SIP_OK;
belle_sip_list_t* bandwidths; belle_sip_list_t* bandwidths;
// belle_sip_list_t* attributes; // belle_sip_list_t* attributes;
if (base_description->info) { if (base_description->info) {
current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(base_description->info),buff,current_offset,buff_size); error=belle_sip_object_marshal(BELLE_SIP_OBJECT(base_description->info),buff,buff_size,offset);
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); error=belle_sip_snprintf(buff, buff_size, offset, "\r\n");
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
} }
if (base_description->connection) { if (base_description->connection) {
current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(base_description->connection),buff,current_offset,buff_size); error=belle_sip_object_marshal(BELLE_SIP_OBJECT(base_description->connection),buff,buff_size,offset);
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); error=belle_sip_snprintf(buff, buff_size, offset, "\r\n");
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
} }
for(bandwidths=base_description->bandwidths;bandwidths!=NULL;bandwidths=bandwidths->next){ for(bandwidths=base_description->bandwidths;bandwidths!=NULL;bandwidths=bandwidths->next){
current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(bandwidths->data),buff,current_offset,buff_size); error=belle_sip_object_marshal(BELLE_SIP_OBJECT(bandwidths->data),buff,buff_size,offset);
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); error=belle_sip_snprintf(buff, buff_size, offset, "\r\n");
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
} }
// for(attributes=base_description->attributes;attributes!=NULL;attributes=attributes->next){ // for(attributes=base_description->attributes;attributes!=NULL;attributes=attributes->next){
// current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(attributes->data),buff,current_offset,buff_size); // current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(attributes->data),buff,current_offset,buff_size);
// current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); // current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n");
// } // }
return current_offset-offset; return error;
} }
BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sdp_base_description_t); BELLE_SIP_DECLARE_NO_IMPLEMENTED_INTERFACES(belle_sdp_base_description_t);
...@@ -458,24 +423,23 @@ void belle_sdp_media_description_destroy(belle_sdp_media_description_t* media_de ...@@ -458,24 +423,23 @@ void belle_sdp_media_description_destroy(belle_sdp_media_description_t* media_de
void belle_sdp_media_description_clone(belle_sdp_media_description_t *media_description, const belle_sdp_media_description_t *orig){ void belle_sdp_media_description_clone(belle_sdp_media_description_t *media_description, const belle_sdp_media_description_t *orig){
if (orig->media) media_description->media = BELLE_SDP_MEDIA(belle_sip_object_clone_and_ref(BELLE_SIP_OBJECT((orig->media)))); if (orig->media) media_description->media = BELLE_SDP_MEDIA(belle_sip_object_clone_and_ref(BELLE_SIP_OBJECT((orig->media))));
} }
int belle_sdp_media_description_marshal(belle_sdp_media_description_t* media_description, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_media_description_marshal(belle_sdp_media_description_t* media_description, char* buff, size_t buff_size, unsigned int *offset) {
belle_sip_list_t* attributes; belle_sip_list_t* attributes;
belle_sip_error_code error=belle_sip_object_marshal(BELLE_SIP_OBJECT(media_description->media),buff,buff_size,offset);
current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(media_description->media),buff,current_offset,buff_size); if (error!=BELLE_SIP_OK) return error;
if (current_offset>=buff_size) return buff_size-offset; error=belle_sip_snprintf(buff, buff_size, offset, "\r\n");
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); if (error!=BELLE_SIP_OK) return error;
if (current_offset>=buff_size) return buff_size-offset; error=belle_sdp_base_description_marshal(BELLE_SIP_CAST(media_description,belle_sdp_base_description_t),buff,buff_size,offset);
current_offset+=belle_sdp_base_description_marshal(BELLE_SIP_CAST(media_description,belle_sdp_base_description_t),buff,current_offset,buff_size); if (error!=BELLE_SIP_OK) return error;
if (current_offset>=buff_size) return buff_size-offset;
for(attributes=media_description->base_description.attributes;attributes!=NULL;attributes=attributes->next){ for(attributes=media_description->base_description.attributes;attributes!=NULL;attributes=attributes->next){
current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(attributes->data),buff,current_offset,buff_size); error=belle_sip_object_marshal(BELLE_SIP_OBJECT(attributes->data),buff,buff_size,offset);
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); error=belle_sip_snprintf(buff, buff_size, offset, "\r\n");
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
} }
return current_offset-offset; return error;
} }
BELLE_SDP_NEW(media_description,belle_sdp_base_description) BELLE_SDP_NEW(media_description,belle_sdp_base_description)
...@@ -854,20 +818,20 @@ void belle_sdp_origin_clone(belle_sdp_origin_t *origin, const belle_sdp_origin_t ...@@ -854,20 +818,20 @@ void belle_sdp_origin_clone(belle_sdp_origin_t *origin, const belle_sdp_origin_t
origin->session_id = orig->session_id; origin->session_id = orig->session_id;
origin->session_version = orig->session_version; origin->session_version = orig->session_version;
} }
int belle_sdp_origin_marshal(belle_sdp_origin_t* origin, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_origin_marshal(belle_sdp_origin_t* origin, char* buff, size_t buff_size, unsigned int *offset) {
current_offset+=snprintf( buff+current_offset return belle_sip_snprintf( buff
,buff_size-current_offset ,buff_size
,"o=%s %i %i %s %s %s" ,offset
,origin->username ,"o=%s %i %i %s %s %s"
,origin->session_id ,origin->username
,origin->session_version ,origin->session_id
,origin->network_type ,origin->session_version
,origin->address_type ,origin->network_type
,origin->address); ,origin->address_type
if (current_offset>=buff_size) return buff_size-offset; ,origin->address);
return current_offset-offset;
} }
BELLE_SDP_NEW(origin,belle_sip_object) BELLE_SDP_NEW(origin,belle_sip_object)
belle_sdp_origin_t* belle_sdp_origin_create(const char* user_name belle_sdp_origin_t* belle_sdp_origin_create(const char* user_name
, unsigned int session_id , unsigned int session_id
...@@ -906,15 +870,11 @@ void belle_sdp_session_name_destroy(belle_sdp_session_name_t* session_name) { ...@@ -906,15 +870,11 @@ void belle_sdp_session_name_destroy(belle_sdp_session_name_t* session_name) {
void belle_sdp_session_name_clone(belle_sdp_session_name_t *session_name, const belle_sdp_session_name_t *orig){ void belle_sdp_session_name_clone(belle_sdp_session_name_t *session_name, const belle_sdp_session_name_t *orig){
CLONE_STRING(belle_sdp_session_name,value,session_name,orig); CLONE_STRING(belle_sdp_session_name,value,session_name,orig);
} }
int belle_sdp_session_name_marshal(belle_sdp_session_name_t* session_name, char* buff,unsigned int offset,unsigned int buff_size) {
unsigned int current_offset=offset; belle_sip_error_code belle_sdp_session_name_marshal(belle_sdp_session_name_t* session_name, char* buff, size_t buff_size, unsigned int *offset) {
current_offset+=snprintf( buff+current_offset return belle_sip_snprintf(buff,buff_size,offset,"s=%s",session_name->value);
,buff_size-current_offset
,"s=%s"
,session_name->value);
if (current_offset>=buff_size) return buff_size-offset;
return current_offset-offset;
} }
BELLE_SDP_NEW(session_name,belle_sip_object) BELLE_SDP_NEW(session_name,belle_sip_object)
belle_sdp_session_name_t* belle_sdp_session_name_create (const char* name) { belle_sdp_session_name_t* belle_sdp_session_name_create (const char* name) {
belle_sdp_session_name_t* n=belle_sdp_session_name_new(); belle_sdp_session_name_t* n=belle_sdp_session_name_new();
...@@ -964,7 +924,8 @@ void belle_sdp_session_description_clone(belle_sdp_session_description_t *sessio ...@@ -964,7 +924,8 @@ void belle_sdp_session_description_clone(belle_sdp_session_description_t *sessio
if (orig->zone_adjustments) session_description->zone_adjustments = BELLE_SDP_URI(belle_sip_object_clone_and_ref(BELLE_SIP_OBJECT(orig->zone_adjustments))); if (orig->zone_adjustments) session_description->zone_adjustments = BELLE_SDP_URI(belle_sip_object_clone_and_ref(BELLE_SIP_OBJECT(orig->zone_adjustments)));
session_description->media_descriptions = belle_sip_list_copy_with_data(orig->media_descriptions,belle_sip_object_copyfunc); session_description->media_descriptions = belle_sip_list_copy_with_data(orig->media_descriptions,belle_sip_object_copyfunc);
} }
int belle_sdp_session_description_marshal(belle_sdp_session_description_t* session_description, char* buff,unsigned int offset,unsigned int buff_size) {
belle_sip_error_code belle_sdp_session_description_marshal(belle_sdp_session_description_t* session_description, char* buff, size_t buff_size, unsigned int *offset) {
/*session_description: proto_version CR LF /*session_description: proto_version CR LF
origin_field origin_field
session_name_field session_name_field
...@@ -981,51 +942,52 @@ int belle_sdp_session_description_marshal(belle_sdp_session_description_t* sessi ...@@ -981,51 +942,52 @@ int belle_sdp_session_description_marshal(belle_sdp_session_description_t* sessi
(attribute CR LF)* (attribute CR LF)*
media_descriptions; media_descriptions;
*/ */
unsigned int current_offset=offset; belle_sip_error_code error=BELLE_SIP_OK;
belle_sip_list_t* media_descriptions; belle_sip_list_t* media_descriptions;
belle_sip_list_t* times; belle_sip_list_t* times;
belle_sip_list_t* attributes; belle_sip_list_t* attributes;
current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(session_description->version),buff,current_offset,buff_size); error=belle_sip_object_marshal(BELLE_SIP_OBJECT(session_description->version),buff,buff_size,offset);
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); error=belle_sip_snprintf(buff, buff_size, offset, "\r\n");
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(session_description->origin),buff,current_offset,buff_size); error=belle_sip_object_marshal(BELLE_SIP_OBJECT(session_description->origin),buff,buff_size,offset);
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); error=belle_sip_snprintf(buff, buff_size, offset, "\r\n");
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=belle_sip_object_marshal(BELLE_SIP_OBJECT(session_description->session_name),buff,current_offset,buff_size); error=belle_sip_object_marshal(BELLE_SIP_OBJECT(session_description->session_name),buff,buff_size,offset);
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "\r\n"); error=belle_sip_snprintf(buff, buff_size, offset, "\r\n");
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=belle_sdp_base_description_marshal((belle_sdp_base_description_t*)(&session_description->base_description),buff,current_offset,buff_size); error=belle_sdp_base_description_marshal((belle_sdp_base_description_t*)(&session_description->base_description),buff,buff_size, offset);
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;
current_offset+=snprintf(buff+current_offset, buff_size-current_offset, "t="); error=belle_sip_snprintf(buff, buff_size, offset, "t=");
if (current_offset>=buff_size) return buff_size-offset; if (error!=BELLE_SIP_OK) return error;