X-Git-Url: https://gerrit.o-ran-sc.org/r/gitweb?a=blobdiff_plain;ds=inline;f=src%2Fcommon%2Fsrc%2Fmbuf_api.c;h=e801ac95dc3381f53f36eb7d3d4a0b2a8ad494fe;hb=f7d44570f8de6e15f768e8e2d9b6061cd0bff11f;hp=930d74d5059608c5ec480bd91a247e1a45e3ec8b;hpb=fd9cc7a5b3355146388ebdf4d558cb284c66c5f1;p=ric-plt%2Flib%2Frmr.git diff --git a/src/common/src/mbuf_api.c b/src/common/src/mbuf_api.c index 930d74d..e801ac9 100644 --- a/src/common/src/mbuf_api.c +++ b/src/common/src/mbuf_api.c @@ -1,14 +1,14 @@ // : vi ts=4 sw=4 noet : /* ================================================================================== - Copyright (c) 2019 Nokia + Copyright (c) 2019 Nokia Copyright (c) 2018-2019 AT&T Intellectual Property. Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with the License. You may obtain a copy of the License at - http://www.apache.org/licenses/LICENSE-2.0 + http://www.apache.org/licenses/LICENSE-2.0 Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on an "AS IS" BASIS, @@ -20,8 +20,8 @@ /* Mnemonic: mbuf_api.c - Abstract: These are common functions which work only on the mbuf and - thus (because they do not touch an endpoint or context) + Abstract: These are common functions which work only on the mbuf and + thus (because they do not touch an endpoint or context) can be agnostic to the underlying transport. Author: E. Scott Daniels @@ -33,6 +33,7 @@ #include #include #include +#include #include "rmr.h" // things the users see #include "rmr_agnostic.h" // agnostic things (must be included before private) @@ -41,14 +42,14 @@ // ---------- some wrappers need explicit copy-in functions, also header field setters ----- /* - Allow the user programme to set the meid in the header. The meid is a fixed + Allow the user programme to set the meid in the header. The meid is a fixed length buffer in the header and thus we must ensure it is not overrun. If the user gives a source buffer that is too large, we truncate. The return value is the number of bytes copied, or -1 for absolute failure (bad pointer etc.). Errno is set: EINVAL id poitner, buf or buf header are bad. EOVERFLOW if the bytes given would have overrun - + */ extern int rmr_bytes2meid( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) { uta_mhdr_t* hdr; @@ -90,7 +91,7 @@ extern int rmr_str2meid( rmr_mbuf_t* mbuf, unsigned char const* str ) { errno = EOVERFLOW; return RMR_ERR_OVERFLOW; } - + rmr_bytes2meid( mbuf, str, len+1 ); return RMR_OK; } @@ -99,7 +100,7 @@ extern int rmr_str2meid( rmr_mbuf_t* mbuf, unsigned char const* str ) { /* This will copy n bytes from source into the payload. If len is larger than - the payload only the bytes which will fit are copied, The user should + the payload only the bytes which will fit are copied, The user should check errno on return to determine success or failure. */ extern void rmr_bytes2payload( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) { @@ -137,7 +138,7 @@ extern void rmr_str2payload( rmr_mbuf_t* mbuf, unsigned char const* str ) { etc.). Errno is set: EINVAL id poitner, buf or buf header are bad. EOVERFLOW if the bytes given would have overrun - + */ extern int rmr_bytes2xact( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) { uta_mhdr_t* hdr; @@ -181,7 +182,7 @@ extern int rmr_str2xact( rmr_mbuf_t* mbuf, unsigned char const* str ) { errno = EOVERFLOW; return RMR_ERR_OVERFLOW; } - + rmr_bytes2xact( mbuf, str, len+1 ); return RMR_OK; } @@ -215,3 +216,128 @@ extern unsigned char* rmr_get_meid( rmr_mbuf_t* mbuf, unsigned char* dest ) { return dest; } + +// ------------------- trace related access functions -------------------------------------- +/* + The set_trace function will copy the supplied data for size bytes into the + header. If the header trace area is not large enough, a new one will be allocated + which will cause a payload copy based on the msg->len value (if 0 no payload + data is copied). + + The return value is the number of bytes actually coppied. If 0 bytes are coppied + errno should indicate the reason. If 0 is returned and errno is 0, then size + passed was 0. The state in the message is left UNCHANGED. +*/ +extern int rmr_set_trace( rmr_mbuf_t* msg, unsigned const char* data, int size ) { + uta_mhdr_t* hdr; + rmr_mbuf_t* nm; // new message if larger is needed + int len; + void* old_tp_buf; // if we need to realloc, must hold old to free + void* old_hdr; + + if( msg == NULL ) { + errno = EINVAL; + return 0; + } + + errno = 0; + if( size <= 0 ) { + return 0; + } + + hdr = (uta_mhdr_t *) msg->header; + len = RMR_TR_LEN( hdr ); + + if( len != size ) { // different sized trace data, must realloc the buffer + nm = rmr_realloc_msg( msg, size ); // realloc with changed trace size + old_tp_buf = msg->tp_buf; + old_hdr = msg->header; + + msg->tp_buf = nm->tp_buf; // reference the reallocated buffer + msg->header = nm->header; + msg->id = NULL; // currently unused + msg->xaction = nm->xaction; + msg->payload = nm->payload; + + nm->tp_buf = old_tp_buf; // set to free + nm->header = old_hdr; // nano frees on hdr, so must set both + rmr_free_msg( nm ); + + hdr = (uta_mhdr_t *) msg->header; // header WILL be different + len = RMR_TR_LEN( hdr ); + } + + memcpy( TRACE_ADDR( hdr ), data, size ); + + return size; +} + + +/* + Copies the trace bytes from the message header into the buffer provided by + the user. If the trace data in the header is less than size, then only + that number of bytes are copied, else exactly size bytes are copied. The + number actually copied is returned. +*/ +extern int rmr_get_trace( rmr_mbuf_t* msg, unsigned char* dest, int size ) { + uta_mhdr_t* hdr = NULL; + int n2copy = 0; + + if( msg == NULL ) { + return 0; + } + + if( size <= 0 || dest == NULL ) { + return 0; + } + + hdr = msg->header; + if( (n2copy = size < RMR_TR_LEN( hdr ) ? size : RMR_TR_LEN( hdr )) <= 0 ) { + return 0; + } + + memcpy( dest, TRACE_ADDR( hdr ), n2copy ); + + return n2copy; +} + +/* + Returns the number of bytes currently allocated for trace data in the message + buffer. +*/ +extern int rmr_get_trlen( rmr_mbuf_t* msg ) { + uta_mhdr_t* hdr; + + if( msg == NULL ) { + return 0; + } + + hdr = msg->header; + + return RMR_TR_LEN( hdr ); +} + +/* + Returns the string in the source portion of the header. This is assumed to be + something that can be used for direct sends (hostname:port). Regardless, it + will be a nil terminated, ascii string with max of 64 characters including + the final nil. So, the user must ensure that dest is at least 64 bytes. + + As a convenience, the pointer to dest is returned on success; nil on failure + with errno set. +*/ +extern unsigned char* rmr_get_src( rmr_mbuf_t* msg, unsigned char* dest ) { + uta_mhdr_t* hdr = NULL; + + if( msg == NULL ) { + errno = EINVAL; + return NULL; + } + + if( dest != NULL ) { + hdr = msg->header; + strcpy( dest, hdr->src ); + } + + return dest; +}