static void handle_ewouldblock(belle_sip_channel_t *obj, const char *buffer, size_t size){ belle_sip_source_set_events((belle_sip_source_t*)obj,BELLE_SIP_EVENT_READ|BELLE_SIP_EVENT_WRITE|BELLE_SIP_EVENT_ERROR); free_ewouldblock_buffer(obj); obj->ewouldblock_buffer=belle_sip_malloc(size); obj->ewouldblock_size=size; memcpy(obj->ewouldblock_buffer,buffer,size); }
/* * this function is to avoid logging too much or non-ascii data received. */ static char *make_logbuf(belle_sip_log_level level, const char *buffer, size_t size){ char *logbuf; char truncate_msg[128]={0}; int limit=3000; size_t non_ascii_pos; if (!belle_sip_log_level_enabled(level)){ return belle_sip_malloc0(1); } non_ascii_pos=find_non_asci(buffer,MIN(size,limit)); if (non_ascii_pos<2){ limit=0; snprintf(truncate_msg,sizeof(truncate_msg)-1,"... (binary data)"); }else limit=non_ascii_pos; if (limit!=0 && size>limit){ snprintf(truncate_msg,sizeof(truncate_msg)-1," ... (first %i bytes shown)",limit); size=limit; } if (size<limit) limit=size; if (truncate_msg[0]!=0){ size+=sizeof(truncate_msg); } logbuf=belle_sip_malloc(size+1); strncpy(logbuf,buffer,limit); if (truncate_msg[0]!=0){ strncpy(logbuf+limit,truncate_msg,sizeof(truncate_msg)); } logbuf[size]='\0'; return logbuf; }
void belle_sip_multipart_body_handler_add_part(belle_sip_multipart_body_handler_t *obj, belle_sip_body_handler_t *part){ obj->base.expected_size+=part->expected_size+strlen(obj->boundary) + 4; /* add the separator length to the body length as each part start with a separator. 4 is for "--" and "\r\n" */ if (part->headers != NULL) { /* there is a declared header for this part, add its length to the expected total length */ size_t headerStringBufferSize = DEFAULT_HEADER_STRING_SIZE; size_t offset = 0; belle_sip_list_t *headerList = part->headers; part->headerStringBuffer = (char *)belle_sip_malloc(DEFAULT_HEADER_STRING_SIZE); while (headerList != NULL) { size_t offsetBackup=offset; /* we must backup the offset as it will be messed up by the marshal function in case of failure */ belle_sip_error_code returnCode = belle_sip_object_marshal(headerList->data, part->headerStringBuffer, headerStringBufferSize-5, &offset); /* -5 to leave room for carriage returns */ if (returnCode == BELLE_SIP_BUFFER_OVERFLOW) { /* increase buffer size */ offset=offsetBackup; /* restore the offset, no data were written to the buffer */ headerStringBufferSize+=DEFAULT_HEADER_STRING_SIZE; part->headerStringBuffer = (char *)belle_sip_realloc(part->headerStringBuffer, headerStringBufferSize); } else if (returnCode == BELLE_SIP_OK) { /* add the carriage return chars */ part->headerStringBuffer[offset++]='\r'; part->headerStringBuffer[offset++]='\n'; headerList = belle_sip_list_next(headerList); } } part->headerStringBuffer[offset++]='\r'; part->headerStringBuffer[offset++]='\n'; obj->base.expected_size += offset; part->headerStringBuffer[offset++]='\0'; /* null terminate the buffer in order to be able to get it length later using strlen */ } obj->parts=belle_sip_list_append(obj->parts,belle_sip_object_ref(part)); }
static void queue_message_delayed(belle_sip_channel_t *obj, belle_sip_message_t *msg){ delay_send_t *ctx=belle_sip_malloc(sizeof(delay_send_t)); ctx->chan=(belle_sip_channel_t*)belle_sip_object_ref(obj); ctx->msg=(belle_sip_message_t*)belle_sip_object_ref(msg); belle_sip_main_loop_add_timeout(obj->stack->ml,(belle_sip_source_func_t)on_delayed_send_do,ctx,obj->stack->tx_delay); belle_sip_message("channel %p: message sending delayed by %i ms",obj,obj->stack->tx_delay); }
static void belle_sip_memory_body_handler_clone(belle_sip_memory_body_handler_t *obj, const belle_sip_memory_body_handler_t *orig){ if (orig->buffer) { obj->buffer=belle_sip_malloc(orig->base.expected_size+1); memcpy(obj->buffer,orig->buffer,orig->base.expected_size); obj->buffer[orig->base.expected_size]='\0'; } }
char * _belle_sip_object_describe_type(belle_sip_object_vptr_t *vptr){ const int maxbufsize=2048; char *ret=belle_sip_malloc(maxbufsize); belle_sip_object_vptr_t *it; size_t pos=0; belle_sip_list_t *l=NULL,*elem; belle_sip_snprintf(ret,maxbufsize,&pos,"Ownership:\n"); belle_sip_snprintf(ret,maxbufsize,&pos,"\t%s is created initially %s\n",vptr->type_name, vptr->initially_unowned ? "unowned" : "owned"); belle_sip_snprintf(ret,maxbufsize,&pos,"\nInheritance diagram:\n"); for(it=vptr;it!=NULL;it=it->get_parent()){ l=belle_sip_list_prepend(l,it); } for(elem=l;elem!=NULL;elem=elem->next){ it=(belle_sip_object_vptr_t*)elem->data; belle_sip_snprintf(ret,maxbufsize,&pos,"\t%s\n",it->type_name); if (elem->next) belle_sip_snprintf(ret,maxbufsize,&pos,"\t |\n"); } belle_sip_list_free(l); belle_sip_snprintf(ret,maxbufsize,&pos,"\nImplemented interfaces:\n"); for(it=vptr;it!=NULL;it=it->get_parent()){ belle_sip_interface_desc_t **desc=it->interfaces; if (desc!=NULL){ for(;*desc!=NULL;desc++){ belle_sip_snprintf(ret,maxbufsize,&pos,"\t* %s\n",(*desc)->ifname); } } } return ret; }
/* * this function is to avoid logging too much or non-ascii data received. */ static char *make_logbuf(belle_sip_log_level level, const char *buffer, size_t size){ char *logbuf; char truncate_msg[128]={0}; int limit=7000; /*big message when many ice candidates*/ if (!belle_sip_log_level_enabled(level)){ return belle_sip_malloc0(1); } limit=find_non_printable(buffer,MIN(size,limit)); if (limit==0){ snprintf(truncate_msg,sizeof(truncate_msg)-1,"... (binary data)"); } else if (size>limit){ snprintf(truncate_msg,sizeof(truncate_msg)-1,"... (first %i bytes shown)",limit); size=limit; } if (truncate_msg[0]!=0){ size+=sizeof(truncate_msg); } logbuf=belle_sip_malloc(size+1); strncpy(logbuf,buffer,limit); if (truncate_msg[0]!=0){ strncpy(logbuf+limit,truncate_msg,sizeof(truncate_msg)); } logbuf[size]='\0'; return logbuf; }
void linphone_content_set_buffer(LinphoneContent *content, const void *buffer, size_t size) { void *data; sal_body_handler_set_size(content->body_handler, size); data = belle_sip_malloc(size + 1); memcpy(data, buffer, size); ((char *)data)[size] = '\0'; sal_body_handler_set_data(content->body_handler, data); }
belle_sip_memory_body_handler_t *belle_sip_memory_body_handler_new_copy_from_buffer(const void *buffer, size_t bufsize, belle_sip_body_handler_progress_callback_t cb, void *user_data){ belle_sip_memory_body_handler_t *obj=belle_sip_object_new(belle_sip_memory_body_handler_t); belle_sip_body_handler_init((belle_sip_body_handler_t*)obj,cb,user_data); obj->buffer=(uint8_t*)belle_sip_malloc(bufsize+1); obj->buffer[bufsize]='\0'; obj->base.expected_size=bufsize; memcpy(obj->buffer,buffer,bufsize); return obj; }
void linphone_content_set_key(LinphoneContent *content, const char *key, const size_t keyLength) { if (content->key != NULL) { belle_sip_free(content->key); content->key = NULL; } if (key != NULL) { content->key = belle_sip_malloc(keyLength); memcpy(content->key, key, keyLength); } }
static char * belle_sip_object_to_alloc_string(belle_sip_object_t *obj, int size_hint){ char *buf=belle_sip_malloc(size_hint); size_t offset=0; belle_sip_error_code error = belle_sip_object_marshal(obj,buf,size_hint-1,&offset); obj->vptr->tostring_bufsize_hint=size_hint; if (error==BELLE_SIP_BUFFER_OVERFLOW){ belle_sip_message("belle_sip_object_to_alloc_string(): hint buffer was too short while doing to_string() for %s, retrying", obj->vptr->type_name); belle_sip_free(buf); return belle_sip_object_to_alloc_string(obj,2*size_hint); } buf=belle_sip_realloc(buf,offset+1); buf[offset]='\0'; return buf; }
static int set_sdp(belle_sip_message_t *msg,belle_sdp_session_description_t* session_desc) { belle_sip_header_content_type_t* content_type ; belle_sip_header_content_length_t* content_length; belle_sip_error_code error = BELLE_SIP_BUFFER_OVERFLOW; size_t length = 0; if (session_desc) { size_t bufLen = 2048; size_t hardlimit = 16*1024; /* 16k SDP limit seems reasonable */ char* buff = belle_sip_malloc(bufLen); content_type = belle_sip_header_content_type_create("application","sdp"); /* try to marshal the description. This could go higher than 2k so we iterate */ while( error != BELLE_SIP_OK && bufLen <= hardlimit && buff != NULL){ error = belle_sip_object_marshal(BELLE_SIP_OBJECT(session_desc),buff,bufLen,&length); if( error != BELLE_SIP_OK ){ bufLen *= 2; length = 0; buff = belle_sip_realloc(buff,bufLen); } } /* give up if hard limit reached */ if (error != BELLE_SIP_OK || buff == NULL) { ms_error("Buffer too small (%d) or not enough memory, giving up SDP", (int)bufLen); return -1; } content_length = belle_sip_header_content_length_create(length); belle_sip_message_add_header(msg,BELLE_SIP_HEADER(content_type)); belle_sip_message_add_header(msg,BELLE_SIP_HEADER(content_length)); belle_sip_message_assign_body(msg,buff,length); return 0; } else { return -1; } }
static int send_report(LinphoneCall* call, reporting_session_report_t * report, const char * report_event) { LinphoneContent *content; int expires = -1; size_t offset = 0; size_t size = 2048; char * buffer; int ret = 0; LinphoneEvent *lev; LinphoneAddress *request_uri; char * domain; const char* route; /*if we are on a low bandwidth network, do not send reports to not overload it*/ if (linphone_call_params_low_bandwidth_enabled(linphone_call_get_current_params(call))){ ms_warning("QualityReporting[%p]: Avoid sending reports on low bandwidth network", call); ret = 1; goto end; } /*if the call was hung up too early, we might have invalid IPs information in that case, we abort the report since it's not useful data*/ if (report->info.local_addr.ip == NULL || strlen(report->info.local_addr.ip) == 0 || report->info.remote_addr.ip == NULL || strlen(report->info.remote_addr.ip) == 0) { ms_warning("QualityReporting[%p]: Trying to submit a %s too early (call duration: %d sec) but %s IP could " "not be retrieved so dropping this report" , call , report_event , linphone_call_get_duration(call) , (report->info.local_addr.ip == NULL || strlen(report->info.local_addr.ip) == 0) ? "local" : "remote"); ret = 2; goto end; } buffer = (char *) belle_sip_malloc(size); content = linphone_content_new(); linphone_content_set_type(content, "application"); linphone_content_set_subtype(content, "vq-rtcpxr"); append_to_buffer(&buffer, &size, &offset, "%s\r\n", report_event); append_to_buffer(&buffer, &size, &offset, "CallID: %s\r\n", report->info.call_id); append_to_buffer(&buffer, &size, &offset, "LocalID: %s\r\n", report->info.local_addr.id); append_to_buffer(&buffer, &size, &offset, "RemoteID: %s\r\n", report->info.remote_addr.id); append_to_buffer(&buffer, &size, &offset, "OrigID: %s\r\n", report->info.orig_id); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "LocalGroup: %s\r\n", report->info.local_addr.group); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "RemoteGroup: %s\r\n", report->info.remote_addr.group); append_to_buffer(&buffer, &size, &offset, "LocalAddr: IP=%s PORT=%d SSRC=%u\r\n", report->info.local_addr.ip, report->info.local_addr.port, report->info.local_addr.ssrc); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "LocalMAC: %s\r\n", report->info.local_addr.mac); append_to_buffer(&buffer, &size, &offset, "RemoteAddr: IP=%s PORT=%d SSRC=%u\r\n", report->info.remote_addr.ip, report->info.remote_addr.port, report->info.remote_addr.ssrc); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "RemoteMAC: %s\r\n", report->info.remote_addr.mac); append_to_buffer(&buffer, &size, &offset, "LocalMetrics:\r\n"); append_metrics_to_buffer(&buffer, &size, &offset, report->local_metrics); if (are_metrics_filled(report->remote_metrics)!=0) { append_to_buffer(&buffer, &size, &offset, "RemoteMetrics:\r\n"); append_metrics_to_buffer(&buffer, &size, &offset, report->remote_metrics); } APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "DialogID: %s\r\n", report->dialog_id); if (report->qos_analyzer.timestamp!=NULL){ append_to_buffer(&buffer, &size, &offset, "AdaptiveAlg:"); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " NAME=\"%s\"", report->qos_analyzer.name); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " TS=\"%s\"", report->qos_analyzer.timestamp); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " IN_LEG=\"%s\"", report->qos_analyzer.input_leg); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " IN=\"%s\"", report->qos_analyzer.input); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " OUT_LEG=\"%s\"", report->qos_analyzer.output_leg); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " OUT=\"%s\"", report->qos_analyzer.output); append_to_buffer(&buffer, &size, &offset, "\r\n"); } #if TARGET_OS_IPHONE { size_t namesize; char *machine; sysctlbyname("hw.machine", NULL, &namesize, NULL, 0); machine = malloc(namesize); sysctlbyname("hw.machine", machine, &namesize, NULL, 0); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "Device: %s\r\n", machine); } #endif linphone_content_set_buffer(content, buffer, strlen(buffer)); ms_free(buffer); if (call->log->reporting.on_report_sent != NULL) { SalStreamType type = report == call->log->reporting.reports[0] ? LINPHONE_CALL_STATS_AUDIO : report == call->log->reporting.reports[1] ? LINPHONE_CALL_STATS_VIDEO : LINPHONE_CALL_STATS_TEXT; call->log->reporting.on_report_sent(call, type, content); } route = linphone_proxy_config_get_quality_reporting_collector(call->dest_proxy); domain = ms_strdup_printf("sip:%s", linphone_proxy_config_get_domain(call->dest_proxy)); request_uri = linphone_address_new(route ? route : domain); ms_free(domain); lev=linphone_core_create_publish(call->core, request_uri, "vq-rtcpxr", expires); if (route) { ms_message("Publishing report with custom route %s", route); sal_op_set_route(lev->op, route); } if (linphone_event_send_publish(lev, content) != 0){ linphone_event_unref(lev); lev=NULL; ret=4; } else { reset_avg_metrics(report); STR_REASSIGN(report->qos_analyzer.timestamp, NULL); STR_REASSIGN(report->qos_analyzer.input_leg, NULL); STR_REASSIGN(report->qos_analyzer.input, NULL); STR_REASSIGN(report->qos_analyzer.output_leg, NULL); STR_REASSIGN(report->qos_analyzer.output, NULL); } linphone_address_destroy(request_uri); linphone_content_unref(content); end: ms_message("QualityReporting[%p]: Send '%s' with status %d", call, report_event, ret ); return ret; }
static int send_report(LinphoneCall* call, reporting_session_report_t * report, const char * report_event) { LinphoneContent *content = linphone_content_new(); LinphoneAddress *addr; int expires = -1; size_t offset = 0; size_t size = 2048; char * buffer; int ret = 0; /*if we are on a low bandwidth network, do not send reports to not overload it*/ if (linphone_call_params_low_bandwidth_enabled(linphone_call_get_current_params(call))){ ms_warning("QualityReporting[%p]: Avoid sending reports on low bandwidth network", call); ret = 1; goto end; } /*if the call was hung up too early, we might have invalid IPs information in that case, we abort the report since it's not useful data*/ if (report->info.local_addr.ip == NULL || strlen(report->info.local_addr.ip) == 0 || report->info.remote_addr.ip == NULL || strlen(report->info.remote_addr.ip) == 0) { ms_warning("QualityReporting[%p]: Trying to submit a %s too early (call duration: %d sec) but %s IP could " "not be retrieved so dropping this report" , call , report_event , linphone_call_get_duration(call) , (report->info.local_addr.ip == NULL || strlen(report->info.local_addr.ip) == 0) ? "local" : "remote"); ret = 2; goto end; } addr = linphone_address_new(linphone_proxy_config_get_quality_reporting_collector(call->dest_proxy)); if (addr == NULL) { ms_warning("QualityReporting[%p]: Asked to submit reporting statistics but no collector address found" , call); ret = 3; goto end; } buffer = (char *) belle_sip_malloc(size); linphone_content_set_type(content, "application"); linphone_content_set_subtype(content, "vq-rtcpxr"); append_to_buffer(&buffer, &size, &offset, "%s\r\n", report_event); append_to_buffer(&buffer, &size, &offset, "CallID: %s\r\n", report->info.call_id); append_to_buffer(&buffer, &size, &offset, "LocalID: %s\r\n", report->info.local_addr.id); append_to_buffer(&buffer, &size, &offset, "RemoteID: %s\r\n", report->info.remote_addr.id); append_to_buffer(&buffer, &size, &offset, "OrigID: %s\r\n", report->info.orig_id); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "LocalGroup: %s\r\n", report->info.local_addr.group); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "RemoteGroup: %s\r\n", report->info.remote_addr.group); append_to_buffer(&buffer, &size, &offset, "LocalAddr: IP=%s PORT=%d SSRC=%u\r\n", report->info.local_addr.ip, report->info.local_addr.port, report->info.local_addr.ssrc); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "LocalMAC: %s\r\n", report->info.local_addr.mac); append_to_buffer(&buffer, &size, &offset, "RemoteAddr: IP=%s PORT=%d SSRC=%u\r\n", report->info.remote_addr.ip, report->info.remote_addr.port, report->info.remote_addr.ssrc); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "RemoteMAC: %s\r\n", report->info.remote_addr.mac); append_to_buffer(&buffer, &size, &offset, "LocalMetrics:\r\n"); append_metrics_to_buffer(&buffer, &size, &offset, report->local_metrics); if (are_metrics_filled(report->remote_metrics)!=0) { append_to_buffer(&buffer, &size, &offset, "RemoteMetrics:\r\n"); append_metrics_to_buffer(&buffer, &size, &offset, report->remote_metrics); } APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, "DialogID: %s\r\n", report->dialog_id); if (report->qos_analyzer.timestamp!=NULL){ append_to_buffer(&buffer, &size, &offset, "AdaptiveAlg:"); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " NAME=\"%s\"", report->qos_analyzer.name); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " TS=\"%s\"", report->qos_analyzer.timestamp); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " IN_LEG=\"%s\"", report->qos_analyzer.input_leg); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " IN=\"%s\"", report->qos_analyzer.input); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " OUT_LEG=\"%s\"", report->qos_analyzer.output_leg); APPEND_IF_NOT_NULL_STR(&buffer, &size, &offset, " OUT=\"%s\"", report->qos_analyzer.output); append_to_buffer(&buffer, &size, &offset, "\r\n"); } linphone_content_set_buffer(content, buffer, strlen(buffer)); ms_free(buffer); if (call->log->reporting.on_report_sent != NULL){ call->log->reporting.on_report_sent( call, (report==call->log->reporting.reports[0])?LINPHONE_CALL_STATS_AUDIO:LINPHONE_CALL_STATS_VIDEO, content); } if (! linphone_core_publish(call->core, addr, "vq-rtcpxr", expires, content)){ ret=4; } else { reset_avg_metrics(report); STR_REASSIGN(report->qos_analyzer.timestamp, NULL); STR_REASSIGN(report->qos_analyzer.input_leg, NULL); STR_REASSIGN(report->qos_analyzer.input, NULL); STR_REASSIGN(report->qos_analyzer.output_leg, NULL); STR_REASSIGN(report->qos_analyzer.output, NULL); } linphone_address_destroy(addr); linphone_content_unref(content); end: ms_message("QualityReporting[%p]: Send '%s' with status %d", call, report_event, ret ); return ret; }