From ced4a52c5dc3e4d60bd4224634bbcb63ba243bc0 Mon Sep 17 00:00:00 2001 From: Donat Zenichev Date: Tue, 9 Jul 2024 15:58:21 +0200 Subject: [PATCH] MT#56772 AmSdp: covert sessId / sessV to llu Use `unsigned long long` for SDP session id and version instead of unsigned int. Refactor all usage of them accordingly. Additionally intrdouce new utils functions for conversion: - `ulonglong2str()` - converts `unsigned long long` to `string` - `str2ull()` - converts `string` to `unsigned long long` Change-Id: I4210349a5442d4173b14227497f4a01d68cad7a4 --- apps/sbc/CallLeg.cpp | 2 +- apps/sbc/SBCCallLeg.cpp | 2 +- core/AmB2BSession.cpp | 6 ++-- core/AmOfferAnswer.cpp | 2 +- core/AmSdp.cpp | 8 +++--- core/AmSdp.h | 4 +-- core/AmUtils.cpp | 63 +++++++++++++++++++++++++++++++++++++++++ core/AmUtils.h | 27 ++++++++++++++++++ 8 files changed, 102 insertions(+), 12 deletions(-) diff --git a/apps/sbc/CallLeg.cpp b/apps/sbc/CallLeg.cpp index e635f479..a10fd89b 100644 --- a/apps/sbc/CallLeg.cpp +++ b/apps/sbc/CallLeg.cpp @@ -1891,7 +1891,7 @@ void CallLeg::createResumeRequest(AmSdp &sdp) * be good enough for unholding (might be held already with zero conncetions) */ /* keep sessV incremented each time sending SDP offer (hold/resume) */ non_hold_sdp.origin.sessV++; - DBG("Increasing session version in SDP origin line to %lld", non_hold_sdp.origin.sessV); + DBG("Increasing session version in SDP origin line to %llu", non_hold_sdp.origin.sessV); if (!non_hold_sdp.media.empty()) { sdp = non_hold_sdp; diff --git a/apps/sbc/SBCCallLeg.cpp b/apps/sbc/SBCCallLeg.cpp index 2e54050a..25a73b79 100644 --- a/apps/sbc/SBCCallLeg.cpp +++ b/apps/sbc/SBCCallLeg.cpp @@ -1847,7 +1847,7 @@ void SBCCallLeg::createHoldRequest(AmSdp &sdp) } else { /* increase sessV */ sdp.origin.sessV++; - TRACE("Increasing session version in SDP origin line to %lld", sdp.origin.sessV); + TRACE("Increasing session version in SDP origin line to %llu", sdp.origin.sessV); } AmB2BMedia *ms = getMediaSession(); diff --git a/core/AmB2BSession.cpp b/core/AmB2BSession.cpp index a753d63f..0f00acca 100644 --- a/core/AmB2BSession.cpp +++ b/core/AmB2BSession.cpp @@ -616,7 +616,7 @@ void AmB2BSession::saveLocalSdpOrigin(const AmSdp& sdp) previous_origin_sessId = sdp.origin.sessId; previous_origin_sessV = sdp.origin.sessV; DBG("Remembering initial SDP Origin (Id %s V %s)\n", - longlong2str(sdp.origin.sessId).c_str(), longlong2str(sdp.origin.sessV).c_str()); + ulonglong2str(sdp.origin.sessId).c_str(), ulonglong2str(sdp.origin.sessV).c_str()); } } @@ -656,10 +656,10 @@ void AmB2BSession::updateLocalSdpOrigin(AmSdp& sdp) { // remember the current SDP for the next time previous_sdp = sdp; DBG("SDP changed; updating Origin (Id %s V %s)\n", - longlong2str(sdp.origin.sessId).c_str(), longlong2str(sdp.origin.sessV).c_str()); + ulonglong2str(sdp.origin.sessId).c_str(), ulonglong2str(sdp.origin.sessV).c_str()); } else { DBG("SDP unchanged; keeping Origin (Id %s V %s)\n", - longlong2str(sdp.origin.sessId).c_str(), longlong2str(sdp.origin.sessV).c_str()); + ulonglong2str(sdp.origin.sessId).c_str(), ulonglong2str(sdp.origin.sessV).c_str()); } } } diff --git a/core/AmOfferAnswer.cpp b/core/AmOfferAnswer.cpp index d34e0806..27f25a27 100644 --- a/core/AmOfferAnswer.cpp +++ b/core/AmOfferAnswer.cpp @@ -380,7 +380,7 @@ int AmOfferAnswer::onReplyOut(AmSipReply& reply) } else { force_no_sdp_update = (sdp_local.origin.sessV == parser_sdp.origin.sessV); if (force_no_sdp_update) - DBG("Forcing no OA state update (no SDP changes, same session version: was <%u>, now is <%u>).\n", + DBG("Forcing no OA state update (no SDP changes, same session version: was <%llu>, now is <%llu>).\n", sdp_local.origin.sessV, parser_sdp.origin.sessV); } } diff --git a/core/AmSdp.cpp b/core/AmSdp.cpp index b2915482..4668cfd3 100644 --- a/core/AmSdp.cpp +++ b/core/AmSdp.cpp @@ -315,8 +315,8 @@ int AmSdp::parse(const char* _sdp_msg) void AmSdp::print(string& body) const { string out_buf = "v="+int2str(version)+"\r\n" - "o="+origin.user+" "+int2str(origin.sessId)+" "+ - int2str(origin.sessV)+" IN "; + "o="+origin.user+" "+ulonglong2str(origin.sessId)+" "+ + ulonglong2str(origin.sessV)+" IN "; if (!origin.conn.address.empty()) if (origin.conn.address.find(".") != std::string::npos) @@ -1285,7 +1285,7 @@ static void parse_sdp_origin(AmSdp* sdp_msg, char* s) break; } string id(origin_line, int(next-origin_line)-1); - str2i(id, origin.sessId); + str2ull(id, origin.sessId); origin_line = next; origin_st = VERSION_ST; break; @@ -1300,7 +1300,7 @@ static void parse_sdp_origin(AmSdp* sdp_msg, char* s) break; } string version(origin_line, int(next-origin_line)-1); - str2i(version, origin.sessV); + str2ull(version, origin.sessV); origin_line = next; origin_st = NETTYPE; break; diff --git a/core/AmSdp.h b/core/AmSdp.h index 68a95d19..52298f48 100644 --- a/core/AmSdp.h +++ b/core/AmSdp.h @@ -81,8 +81,8 @@ struct SdpConnection struct SdpOrigin { string user; - unsigned int sessId; - unsigned int sessV; + unsigned long long sessId; + unsigned long long sessV; SdpConnection conn; SdpOrigin() : user(), conn() {} diff --git a/core/AmUtils.cpp b/core/AmUtils.cpp index 7377785c..b1476bde 100644 --- a/core/AmUtils.cpp +++ b/core/AmUtils.cpp @@ -57,6 +57,13 @@ static char _int2str_lookup[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9' }; +string ull2str(unsigned long long val) +{ + char buffer[64] = {0}; + sprintf(buffer, "%llu", val); + return string((char*)(buffer)); +} + string int2str(unsigned int val) { char buffer[64] = {0}; @@ -96,6 +103,7 @@ string signed2str(T val, T (*abs_func) (T), DT (*div_func) (T, T)) string int2str(int val) { return signed2str(val, abs, div); } string long2str(long int val) { return signed2str(val, labs, ldiv); } string longlong2str(long long int val) { return signed2str(val, llabs, lldiv); } +string ulonglong2str(unsigned long long val) { return ull2str(val); } static char _int2hex_lookup[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9','A','B','C','D','E','F' }; static char _int2hex_lookup_l[] = { '0', '1', '2', '3', '4', '5', '6' , '7', '8', '9','a','b','c','d','e','f' }; @@ -367,6 +375,61 @@ bool str2long(char*& str, long& result, char sep) return false; } +// long int could probably be the same size as int +bool str2ull(const string& str, unsigned long long& result) +{ + char* s = (char*)str.c_str(); + return str2ull(s,result); +} + +bool str2ull(char*& str, unsigned long long& result, char sep) +{ + unsigned long long ret=0; + int i=0; + char* init = str; + long sign = 1; + + for(; (*str != '\0') && (*str == ' '); str++); + + if (*str == '-') { + sign = -1; + str++; + for(; (*str != '\0') && (*str == ' '); str++); + } + + for(; *str != '\0';str++){ + if ( (*str <= '9' ) && (*str >= '0') ){ + ret=ret*10+*str-'0'; + i++; + if (i>20) goto error_digits; + } else { + + bool eol = false; + switch(*str){ + case 0xd: + case 0xa: + case 0x0: + eol = true; + } + + if( (*str != sep) && !eol ) + goto error_char; + + break; + } + } + + result = ret * sign; + return true; + + error_digits: + DBG("str2ull: too many digits in [%s]\n", init); + return false; + error_char: + DBG("str2ull: unexpected char 0x%x in %s\n", *str, init); + return false; +} + bool str2bool(const string &s, bool &dst) { // TODO: optimize diff --git a/core/AmUtils.h b/core/AmUtils.h index 9c5a8388..5e3d55d9 100644 --- a/core/AmUtils.h +++ b/core/AmUtils.h @@ -59,6 +59,11 @@ typedef unsigned char HASHHEX[HASHHEXLEN+1]; /** @file AmUtils.h */ +/** + * Convert an unsigned unsigned long long to a string. + */ +string ull2str(unsigned long long val); + /** * Convert an int to a string. */ @@ -79,6 +84,11 @@ string long2str(long int val); */ string longlong2str(long long int val); +/** + * Convert a unsigned long long to a string. + */ +string ulonglong2str(unsigned long long val); + /** * Convert a a byte to a string using hexdecimal representation. */ @@ -160,6 +170,23 @@ bool str2long(const string& str, long& result); */ bool str2long(char*& str, long& result, char sep = ' '); +/** + * Convert a string to a unsigned long long int. + * @param str [in] string to convert. + * @param result [out] result integer. + * @return true if on success (!!!). + */ +bool str2ull(const string& str, unsigned long long& result); + +/** + * Internal version of preceeding 'std2ull' method. + * @param str [in,out] gets incremented until sep char or error occurs + * @param result [out] result of the function + * @param sep [in] character seprating the number to convert and the next token + * @return true on success + */ +bool str2ull(char*& str, unsigned long long& result, char sep = ' '); + /* translates string value into bool, returns false on error */ bool str2bool(const string &s, bool &dst);