/* Schedule notifier expiration. */ static void sub_schedule_uas_expire( pjsip_event_sub *sub, int sec_delay) { pj_time_val delay = { 0, 0 }; pj_parsed_time pt; if (sub->timer.id != 0) pjsip_endpt_cancel_timer(sub->endpt, &sub->timer); pj_gettimeofday(&sub->expiry_time); sub->expiry_time.sec += sec_delay; sub->timer.id = TIMER_ID_UAS_EXPIRY; sub->timer.user_data = sub; sub->timer.cb = &uas_expire_timer_cb; delay.sec = sec_delay; pjsip_endpt_schedule_timer( sub->endpt, &sub->timer, &delay); pj_time_decode(&sub->expiry_time, &pt); PJ_LOG(4,(THIS_FILE, "event_sub%p (%s)(UAS): will expire at %02d:%02d:%02d (in %d secs)", sub, state[sub->state].ptr, pt.hour, pt.min, pt.sec, sec_delay)); }
/* * This is a utility function to create PIDF message body from PJSIP * presence status (pjsip_pres_status). */ PJ_DEF(pj_status_t) pjsip_pres_create_pidf( pj_pool_t *pool, const pjsip_pres_status *status, const pj_str_t *entity, pjsip_msg_body **p_body ) { pjpidf_pres *pidf; pjsip_msg_body *body; unsigned i; /* Create <presence>. */ pidf = pjpidf_create(pool, entity); /* Create <tuple> */ for (i=0; i<status->info_cnt; ++i) { pjpidf_tuple *pidf_tuple; pjpidf_status *pidf_status; pj_str_t id; /* Add tuple id. */ if (status->info[i].id.slen == 0) { /* xs:ID must start with letter */ //pj_create_unique_string(pool, &id); id.ptr = (char*)pj_pool_alloc(pool, PJ_GUID_STRING_LENGTH+2); id.ptr += 2; pj_generate_unique_string(&id); id.ptr -= 2; id.ptr[0] = 'p'; id.ptr[1] = 'j'; id.slen += 2; } else { id = status->info[i].id; } pidf_tuple = pjpidf_pres_add_tuple(pool, pidf, &id); /* Set <contact> */ if (status->info[i].contact.slen) pjpidf_tuple_set_contact(pool, pidf_tuple, &status->info[i].contact); /* Set basic status */ pidf_status = pjpidf_tuple_get_status(pidf_tuple); pjpidf_status_set_basic_open(pidf_status, status->info[i].basic_open); /* Add <timestamp> if configured */ #if defined(PJSIP_PRES_PIDF_ADD_TIMESTAMP) && PJSIP_PRES_PIDF_ADD_TIMESTAMP if (PJSIP_PRES_PIDF_ADD_TIMESTAMP) { char buf[50]; int tslen = 0; pj_time_val tv; pj_parsed_time pt; pj_gettimeofday(&tv); /* TODO: convert time to GMT! (unsupported by pjlib) */ pj_time_decode( &tv, &pt); tslen = pj_ansi_snprintf(buf, sizeof(buf), "%04d-%02d-%02dT%02d:%02d:%02d.%03dZ", pt.year, pt.mon+1, pt.day, pt.hour, pt.min, pt.sec, pt.msec); if (tslen > 0 && tslen < sizeof(buf)) { pj_str_t time = pj_str(buf); pjpidf_tuple_set_timestamp(pool, pidf_tuple, &time); } } #endif } /* Create <person> (RPID) */ if (status->info_cnt) { pjrpid_add_element(pidf, pool, 0, &status->info[0].rpid); } body = PJ_POOL_ZALLOC_T(pool, pjsip_msg_body); body->data = pidf; body->content_type.type = STR_APPLICATION; body->content_type.subtype = STR_PIDF_XML; body->print_body = &pres_print_body; body->clone_data = &xml_clone_data; *p_body = body; return PJ_SUCCESS; }
void systest_save_result(const char *filename) { unsigned i; pj_oshandle_t fd; pj_time_val tv; pj_parsed_time pt; pj_ssize_t size; const char *text; pj_status_t status; status = pj_file_open(NULL, filename, PJ_O_WRONLY | PJ_O_APPEND, &fd); if (status != PJ_SUCCESS) { pj_ansi_snprintf(textbuf, sizeof(textbuf), "Error opening file %s", filename); systest_perror(textbuf, status); return; } text = "\r\n\r\nPJSYSTEST Report\r\n"; size = strlen(text); pj_file_write(fd, text, &size); /* Put timestamp */ pj_gettimeofday(&tv); if (pj_time_decode(&tv, &pt) == PJ_SUCCESS) { pj_ansi_snprintf(textbuf, sizeof(textbuf), "Time: %04d/%02d/%02d %02d:%02d:%02d\r\n", pt.year, pt.mon+1, pt.day, pt.hour, pt.min, pt.sec); size = strlen(textbuf); pj_file_write(fd, textbuf, &size); } pj_ansi_snprintf(textbuf, sizeof(textbuf), "Tests invoked: %u\r\n" "-----------------------------------------------\r\n", test_item_count); size = strlen(textbuf); pj_file_write(fd, textbuf, &size); for (i=0; i<test_item_count; ++i) { test_item_t *ti = &test_items[i]; pj_ansi_snprintf(textbuf, sizeof(textbuf), "\r\nTEST %d: %s %s\r\n", i, ti->title, (ti->skipped? "Skipped" : (ti->success ? "Success" : "Failed"))); size = strlen(textbuf); pj_file_write(fd, textbuf, &size); size = strlen(ti->reason); pj_file_write(fd, ti->reason, &size); size = 2; pj_file_write(fd, "\r\n", &size); } pj_file_close(fd); pj_ansi_snprintf(textbuf, sizeof(textbuf), "Test result successfully appended to file %s", filename); gui_msgbox("Test result saved", textbuf, WITH_OK); }
static pj_status_t init_report(void) { char tmp[80]; pj_time_val timestamp; pj_parsed_time date_time; pj_ssize_t len; pj_status_t status; pj_ansi_sprintf(tmp, "pjsip-static-bench-%s-%s.htm", PJ_OS_NAME, PJ_CC_NAME); status = pj_file_open(NULL, tmp, PJ_O_WRONLY, &fd_report); if (status != PJ_SUCCESS) return status; /* Title */ len = pj_ansi_sprintf(buf, "<HTML>\n" " <HEAD>\n" " <TITLE>PJSIP %s (%s) - Static Benchmark</TITLE>\n" " </HEAD>\n" "<BODY>\n" "\n", PJ_VERSION, (PJ_DEBUG ? "Debug" : "Release")); pj_file_write(fd_report, buf, &len); /* Title */ len = pj_ansi_sprintf(buf, "<H1>PJSIP %s (%s) - Static Benchmark</H1>\n", PJ_VERSION, (PJ_DEBUG ? "Debug" : "Release")); pj_file_write(fd_report, buf, &len); len = pj_ansi_sprintf(buf, "<P>Below is the benchmark result generated " "by <b>test-pjsip</b> program. The program " "is single-threaded only.</P>\n"); pj_file_write(fd_report, buf, &len); /* Write table heading */ len = pj_ansi_sprintf(buf, "<TABLE border=\"1\" cellpadding=\"4\">\n" " <TR><TD bgColor=\"aqua\" align=\"center\">Variable</TD>\n" " <TD bgColor=\"aqua\" align=\"center\">Value</TD>\n" " <TD bgColor=\"aqua\" align=\"center\">Description</TD>\n" " </TR>\n"); pj_file_write(fd_report, buf, &len); /* Write version */ report_sval("version", PJ_VERSION, "", "PJLIB/PJSIP version"); /* Debug or release */ report_sval("build-type", (PJ_DEBUG ? "Debug" : "Release"), "", "Build type"); /* Write timestamp */ pj_gettimeofday(×tamp); report_ival("timestamp", timestamp.sec, "", "System timestamp of the test"); /* Write time of day */ pj_time_decode(×tamp, &date_time); len = pj_ansi_sprintf(tmp, "%04d-%02d-%02d %02d:%02d:%02d", date_time.year, date_time.mon+1, date_time.day, date_time.hour, date_time.min, date_time.sec); report_sval("date-time", tmp, "", "Date/time of the test"); /* Write System */ report_sval("system", system_name, "", "System description"); /* Write OS type */ report_sval("os-family", PJ_OS_NAME, "", "Operating system family"); /* Write CC name */ len = pj_ansi_sprintf(tmp, "%s-%d.%d.%d", PJ_CC_NAME, PJ_CC_VER_1, PJ_CC_VER_2, PJ_CC_VER_2); report_sval("cc-name", tmp, "", "Compiler name and version"); return PJ_SUCCESS; }
PJ_DEF(void) pj_log( const char *sender, int level, const char *format, va_list marker) { pj_time_val now; pj_parsed_time ptime; char *pre; #if PJ_LOG_USE_STACK_BUFFER char log_buffer[PJ_LOG_MAX_SIZE]; #endif int saved_level, len, print_len; PJ_CHECK_STACK(); if (level > pj_log_max_level) return; if (is_logging_suspended()) return; /* Temporarily disable logging for this thread. Some of PJLIB APIs that * this function calls below will recursively call the logging function * back, hence it will cause infinite recursive calls if we allow that. */ suspend_logging(&saved_level); /* Get current date/time. */ pj_gettimeofday(&now); pj_time_decode(&now, &ptime); pre = log_buffer; if (log_decor & PJ_LOG_HAS_LEVEL_TEXT) { static const char *ltexts[] = { "FATAL:", "ERROR:", " WARN:", " INFO:", "DEBUG:", "TRACE:", "DETRC:"}; pj_ansi_strcpy(pre, ltexts[level]); pre += 6; } if (log_decor & PJ_LOG_HAS_DAY_NAME) { static const char *wdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; pj_ansi_strcpy(pre, wdays[ptime.wday]); pre += 3; } if (log_decor & PJ_LOG_HAS_YEAR) { *pre++ = ' '; pre += pj_utoa(ptime.year, pre); } if (log_decor & PJ_LOG_HAS_MONTH) { *pre++ = '-'; pre += pj_utoa_pad(ptime.mon+1, pre, 2, '0'); } if (log_decor & PJ_LOG_HAS_DAY_OF_MON) { *pre++ = '-'; pre += pj_utoa_pad(ptime.day, pre, 2, '0'); } if (log_decor & PJ_LOG_HAS_TIME) { *pre++ = ' '; pre += pj_utoa_pad(ptime.hour, pre, 2, '0'); *pre++ = ':'; pre += pj_utoa_pad(ptime.min, pre, 2, '0'); *pre++ = ':'; pre += pj_utoa_pad(ptime.sec, pre, 2, '0'); } if (log_decor & PJ_LOG_HAS_MICRO_SEC) { *pre++ = '.'; pre += pj_utoa_pad(ptime.msec, pre, 3, '0'); } if (log_decor & PJ_LOG_HAS_SENDER) { enum { SENDER_WIDTH = 14 }; int sender_len = strlen(sender); *pre++ = ' '; if (sender_len <= SENDER_WIDTH) { while (sender_len < SENDER_WIDTH) *pre++ = ' ', ++sender_len; while (*sender) *pre++ = *sender++; } else { int i; for (i=0; i<SENDER_WIDTH; ++i) *pre++ = *sender++; } } if (log_decor & PJ_LOG_HAS_THREAD_ID) { enum { THREAD_WIDTH = 12 }; const char *thread_name = pj_thread_get_name(pj_thread_this()); int thread_len = strlen(thread_name); *pre++ = ' '; if (thread_len <= THREAD_WIDTH) { while (thread_len < THREAD_WIDTH) *pre++ = ' ', ++thread_len; while (*thread_name) *pre++ = *thread_name++; } else { int i; for (i=0; i<THREAD_WIDTH; ++i) *pre++ = *thread_name++; } } if (log_decor != 0 && log_decor != PJ_LOG_HAS_NEWLINE) *pre++ = ' '; if (log_decor & PJ_LOG_HAS_SPACE) { *pre++ = ' '; } len = pre - log_buffer; /* Print the whole message to the string log_buffer. */ print_len = pj_ansi_vsnprintf(pre, sizeof(log_buffer)-len, format, marker); if (print_len < 0) { level = 1; print_len = pj_ansi_snprintf(pre, sizeof(log_buffer)-len, "<logging error: msg too long>"); } len = len + print_len; if (len > 0 && len < (int)sizeof(log_buffer)-2) { if (log_decor & PJ_LOG_HAS_CR) { log_buffer[len++] = '\r'; } if (log_decor & PJ_LOG_HAS_NEWLINE) { log_buffer[len++] = '\n'; } log_buffer[len] = '\0'; } else { len = sizeof(log_buffer)-1; if (log_decor & PJ_LOG_HAS_CR) { log_buffer[sizeof(log_buffer)-3] = '\r'; } if (log_decor & PJ_LOG_HAS_NEWLINE) { log_buffer[sizeof(log_buffer)-2] = '\n'; } log_buffer[sizeof(log_buffer)-1] = '\0'; } /* It should be safe to resume logging at this point. Application can * recursively call the logging function inside the callback. */ resume_logging(&saved_level); if (log_writer) (*log_writer)(level, log_buffer, len); }
PJ_DEF(pj_ssize_t) pj_ssl_cert_info_dump(const pj_ssl_cert_info *ci, const char *indent, char *buf, pj_size_t buf_size) { const char *wdays[] = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; pj_parsed_time pt1; pj_parsed_time pt2; unsigned i; int len = 0; char *p, *end; p = buf; end = buf + buf_size; pj_time_decode(&ci->validity.start, &pt1); pj_time_decode(&ci->validity.end, &pt2); /* Version */ len = pj_ansi_snprintf(p, end-p, "%sVersion : v%d\n", indent, ci->version); CHECK_BUF_LEN(); /* Serial number */ len = pj_ansi_snprintf(p, end-p, "%sSerial : ", indent); CHECK_BUF_LEN(); for (i = 0; i < sizeof(ci->serial_no) && !ci->serial_no[i]; ++i); for (; i < sizeof(ci->serial_no); ++i) { len = pj_ansi_snprintf(p, end-p, "%02X ", ci->serial_no[i] & 0xFF); CHECK_BUF_LEN(); } *(p-1) = '\n'; /* Subject */ len = pj_ansi_snprintf( p, end-p, "%sSubject : %.*s\n", indent, (int)ci->subject.cn.slen, ci->subject.cn.ptr); CHECK_BUF_LEN(); len = pj_ansi_snprintf( p, end-p, "%s %.*s\n", indent, (int)ci->subject.info.slen, ci->subject.info.ptr); CHECK_BUF_LEN(); /* Issuer */ len = pj_ansi_snprintf( p, end-p, "%sIssuer : %.*s\n", indent, (int)ci->issuer.cn.slen, ci->issuer.cn.ptr); CHECK_BUF_LEN(); len = pj_ansi_snprintf( p, end-p, "%s %.*s\n", indent, (int)ci->issuer.info.slen, ci->issuer.info.ptr); CHECK_BUF_LEN(); /* Validity period */ len = pj_ansi_snprintf( p, end-p, "%sValid from : %s %4d-%02d-%02d " "%02d:%02d:%02d.%03d %s\n", indent, wdays[pt1.wday], pt1.year, pt1.mon+1, pt1.day, pt1.hour, pt1.min, pt1.sec, pt1.msec, (ci->validity.gmt? "GMT":"")); CHECK_BUF_LEN(); len = pj_ansi_snprintf( p, end-p, "%sValid to : %s %4d-%02d-%02d " "%02d:%02d:%02d.%03d %s\n", indent, wdays[pt2.wday], pt2.year, pt2.mon+1, pt2.day, pt2.hour, pt2.min, pt2.sec, pt2.msec, (ci->validity.gmt? "GMT":"")); CHECK_BUF_LEN(); /* Subject alternative name extension */ if (ci->subj_alt_name.cnt) { unsigned i; len = pj_ansi_snprintf(p, end-p, "%ssubjectAltName extension\n", indent); CHECK_BUF_LEN(); for (i = 0; i < ci->subj_alt_name.cnt; ++i) { const char *type = NULL; switch(ci->subj_alt_name.entry[i].type) { case PJ_SSL_CERT_NAME_RFC822: type = "MAIL"; break; case PJ_SSL_CERT_NAME_DNS: type = " DNS"; break; case PJ_SSL_CERT_NAME_URI: type = " URI"; break; case PJ_SSL_CERT_NAME_IP: type = " IP"; break; default: break; } if (type) { len = pj_ansi_snprintf( p, end-p, "%s %s : %.*s\n", indent, type, (int)ci->subj_alt_name.entry[i].name.slen, ci->subj_alt_name.entry[i].name.ptr); CHECK_BUF_LEN(); } } } return (p-buf); }
// // Parse to time of day. // static pj_status_t time_decode( const Pj_Time_Val *tv, pj_parsed_time *pt ) { return pj_time_decode(tv, pt); }
PJ_DEF(void) pj_log( const char *sender, int level, const char *format, va_list marker) { pj_time_val now; pj_parsed_time ptime; char *pre; #if PJ_LOG_USE_STACK_BUFFER char log_buffer[PJ_LOG_MAX_SIZE]; #endif int len, print_len; PJ_CHECK_STACK(); if (level > log_max_level) return; /* Get current date/time. */ pj_gettimeofday(&now); pj_time_decode(&now, &ptime); pre = log_buffer; if (log_decor & PJ_LOG_HAS_DAY_NAME) { static const char *wdays[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; pj_ansi_strcpy(pre, wdays[ptime.wday]); pre += 3; } if (log_decor & PJ_LOG_HAS_YEAR) { *pre++ = ' '; pre += pj_utoa(ptime.year, pre); } if (log_decor & PJ_LOG_HAS_MONTH) { *pre++ = '-'; pre += pj_utoa_pad(ptime.mon, pre, 2, '0'); } if (log_decor & PJ_LOG_HAS_DAY_OF_MON) { *pre++ = ' '; pre += pj_utoa_pad(ptime.day, pre, 2, '0'); } if (log_decor & PJ_LOG_HAS_TIME) { *pre++ = ' '; pre += pj_utoa_pad(ptime.hour, pre, 2, '0'); *pre++ = ':'; pre += pj_utoa_pad(ptime.min, pre, 2, '0'); *pre++ = ':'; pre += pj_utoa_pad(ptime.sec, pre, 2, '0'); } if (log_decor & PJ_LOG_HAS_MICRO_SEC) { *pre++ = '.'; pre += pj_utoa_pad(ptime.msec, pre, 3, '0'); } if (log_decor & PJ_LOG_HAS_SENDER) { enum { SENDER_WIDTH = 14 }; int sender_len = strlen(sender); *pre++ = ' '; if (sender_len <= SENDER_WIDTH) { while (sender_len < SENDER_WIDTH) *pre++ = ' ', ++sender_len; while (*sender) *pre++ = *sender++; } else { int i; for (i=0; i<SENDER_WIDTH; ++i) *pre++ = *sender++; } } if (log_decor != 0 && log_decor != PJ_LOG_HAS_NEWLINE) *pre++ = ' '; len = pre - log_buffer; /* Print the whole message to the string log_buffer. */ print_len = pj_ansi_vsnprintf(pre, sizeof(log_buffer)-len, format, marker); if (print_len < 0) { level = 1; print_len = pj_ansi_snprintf(pre, sizeof(log_buffer)-len, "<logging error: msg too long>"); } len = len + print_len; if (len > 0 && len < (int)sizeof(log_buffer)-2) { if (log_decor & PJ_LOG_HAS_CR) { log_buffer[len++] = '\r'; } if (log_decor & PJ_LOG_HAS_NEWLINE) { log_buffer[len++] = '\n'; } log_buffer[len] = '\0'; } else { len = sizeof(log_buffer)-1; if (log_decor & PJ_LOG_HAS_CR) { log_buffer[sizeof(log_buffer)-3] = '\r'; } if (log_decor & PJ_LOG_HAS_NEWLINE) { log_buffer[sizeof(log_buffer)-2] = '\n'; } log_buffer[sizeof(log_buffer)-1] = '\0'; } if (log_writer) (*log_writer)(level, log_buffer, len); }