1 // : vi ts=4 sw=4 noet :
3 ==================================================================================
4 Copyright (c) 2019 Nokia
5 Copyright (c) 2018-2019 AT&T Intellectual Property.
7 Licensed under the Apache License, Version 2.0 (the "License");
8 you may not use this file except in compliance with the License.
9 You may obtain a copy of the License at
11 http://www.apache.org/licenses/LICENSE-2.0
13 Unless required by applicable law or agreed to in writing, software
14 distributed under the License is distributed on an "AS IS" BASIS,
15 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 See the License for the specific language governing permissions and
17 limitations under the License.
18 ==================================================================================
23 Abstract: These are common functions which work only on the mbuf and
24 thus (because they do not touch an endpoint or context)
25 can be agnostic to the underlying transport.
27 Author: E. Scott Daniels
32 #include <netdb.h> // uint* types
38 #include "rmr.h" // things the users see
39 #include "rmr_agnostic.h" // agnostic things (must be included before private)
42 // ---------- some wrappers need explicit copy-in functions, also header field setters -----
45 Allow the user programme to set the meid in the header. The meid is a fixed
46 length buffer in the header and thus we must ensure it is not overrun. If
47 the user gives a source buffer that is too large, we truncate. The return
48 value is the number of bytes copied, or -1 for absolute failure (bad pointer
50 EINVAL id poitner, buf or buf header are bad.
51 EOVERFLOW if the bytes given would have overrun
54 extern int rmr_bytes2meid( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
57 if( src == NULL || mbuf == NULL || mbuf->header == NULL ) {
63 if( len > RMR_MAX_MEID ) {
68 hdr = (uta_mhdr_t *) mbuf->header;
69 memcpy( hdr->meid, src, len );
75 Allows the user programme to set the meid from a string. The end of string
76 (nil) will be included UNLESS the total length including the end of string
77 would exceed the size of the space in the header for the meid. The return
78 value is RMR_OK for success and !RMR_OK on failure. Errno will be set
81 extern int rmr_str2meid( rmr_mbuf_t* mbuf, unsigned char const* str ) {
82 int len; // len moved -- we do validate
84 if( str == NULL || mbuf == NULL || mbuf->header == NULL ) {
86 return RMR_ERR_BADARG;
90 if( (len = strlen( (char *) str )) > RMR_MAX_MEID-1 ) {
92 return RMR_ERR_OVERFLOW;
95 rmr_bytes2meid( mbuf, str, len+1 );
102 This will copy n bytes from source into the payload. If len is larger than
103 the payload only the bytes which will fit are copied, The user should
104 check errno on return to determine success or failure.
106 extern void rmr_bytes2payload( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
107 if( src == NULL || mbuf == NULL || mbuf->payload == NULL ) {
113 mbuf->state = RMR_OK;
114 if( len > mbuf->alloc_len - sizeof( uta_mhdr_t ) ) {
115 mbuf->state = RMR_ERR_OVERFLOW;
117 len = mbuf->alloc_len - sizeof( uta_mhdr_t );
121 memcpy( mbuf->payload, src, len );
125 This will copy a nil terminated string to the mbuf payload. The buffer length
126 is set to the string length.
128 extern void rmr_str2payload( rmr_mbuf_t* mbuf, unsigned char const* str ) {
129 rmr_bytes2payload( mbuf, str, strlen( (char *) str ) + 1 );
134 Allow the user programme to set the xaction field in the header. The xaction
135 is a fixed length buffer in the header and thus we must ensure it is not overrun.
136 If the user gives a source buffer that is too large, we truncate. The return
137 value is the number of bytes copied, or -1 for absolute failure (bad pointer
139 EINVAL id poitner, buf or buf header are bad.
140 EOVERFLOW if the bytes given would have overrun
143 extern int rmr_bytes2xact( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
146 if( src == NULL || mbuf == NULL || mbuf->header == NULL ) {
152 if( len > RMR_MAX_XID ) {
157 hdr = (uta_mhdr_t *) mbuf->header;
158 memcpy( hdr->xid, src, len );
166 Allows the user programme to set the xaction (xid) field from a string. The end
167 of string (nil) will be included UNLESS the total length including the end of string
168 would exceed the size of the space in the header for the xaction. The return
169 value is RMR_OK for success and !RMR_OK on failure. Errno will be set
172 extern int rmr_str2xact( rmr_mbuf_t* mbuf, unsigned char const* str ) {
173 int len; // len moved -- we do validate
175 if( str == NULL || mbuf == NULL || mbuf->header == NULL ) {
177 return RMR_ERR_BADARG;
181 if( (len = strlen( (char *) str )) > RMR_MAX_XID-1 ) {
183 return RMR_ERR_OVERFLOW;
186 rmr_bytes2xact( mbuf, str, len+1 );
191 Extracts the meid (managed equipment) from the header and copies the bytes
192 to the user supplied area. If the user supplied pointer is nil, then
193 a buffer will be allocated and it is the user's responsibilty to free.
194 A pointer is returned to the destination memory (allocated or not)
195 for consistency. If the user programme supplies a destination it is
196 the responsibility of the programme to ensure that the space is large
199 extern unsigned char* rmr_get_meid( rmr_mbuf_t* mbuf, unsigned char* dest ) {
202 if( mbuf == NULL || mbuf->header == NULL ) {
208 if( (dest = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) {
214 hdr = (uta_mhdr_t *) mbuf->header;
215 memcpy( dest, hdr->meid, RMR_MAX_XID );
220 // ------------------- trace related access functions --------------------------------------
222 The set_trace function will copy the supplied data for size bytes into the
223 header. If the header trace area is not large enough, a new one will be allocated
224 which will cause a payload copy based on the msg->len value (if 0 no payload
227 The return value is the number of bytes actually coppied. If 0 bytes are coppied
228 errno should indicate the reason. If 0 is returned and errno is 0, then size
229 passed was 0. The state in the message is left UNCHANGED.
231 extern int rmr_set_trace( rmr_mbuf_t* msg, unsigned const char* data, int size ) {
233 rmr_mbuf_t* nm; // new message if larger is needed
235 void* old_tp_buf; // if we need to realloc, must hold old to free
248 hdr = (uta_mhdr_t *) msg->header;
249 len = RMR_TR_LEN( hdr );
251 if( len != size ) { // different sized trace data, must realloc the buffer
252 nm = rmr_realloc_msg( msg, size ); // realloc with changed trace size
253 old_tp_buf = msg->tp_buf;
254 old_hdr = msg->header;
256 msg->tp_buf = nm->tp_buf; // reference the reallocated buffer
257 msg->header = nm->header;
258 msg->id = NULL; // currently unused
259 msg->xaction = nm->xaction;
260 msg->payload = nm->payload;
262 nm->tp_buf = old_tp_buf; // set to free
263 nm->header = old_hdr; // nano frees on hdr, so must set both
266 hdr = (uta_mhdr_t *) msg->header; // header WILL be different
267 len = RMR_TR_LEN( hdr );
270 memcpy( TRACE_ADDR( hdr ), data, size );
277 Copies the trace bytes from the message header into the buffer provided by
278 the user. If the trace data in the header is less than size, then only
279 that number of bytes are copied, else exactly size bytes are copied. The
280 number actually copied is returned.
282 extern int rmr_get_trace( rmr_mbuf_t* msg, unsigned char* dest, int size ) {
283 uta_mhdr_t* hdr = NULL;
290 if( size <= 0 || dest == NULL ) {
295 if( (n2copy = size < RMR_TR_LEN( hdr ) ? size : RMR_TR_LEN( hdr )) <= 0 ) {
299 memcpy( dest, TRACE_ADDR( hdr ), n2copy );
305 Returns the number of bytes currently allocated for trace data in the message
308 extern int rmr_get_trlen( rmr_mbuf_t* msg ) {
317 return RMR_TR_LEN( hdr );
321 Returns the string in the source portion of the header. This is assumed to be
322 something that can be used for direct sends (hostname:port). Regardless, it
323 will be a nil terminated, ascii string with max of 64 characters including
324 the final nil. So, the user must ensure that dest is at least 64 bytes.
326 As a convenience, the pointer to dest is returned on success; nil on failure
329 extern unsigned char* rmr_get_src( rmr_mbuf_t* msg, unsigned char* dest ) {
330 uta_mhdr_t* hdr = NULL;
339 strcpy( dest, hdr->src );