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
37 #include <semaphore.h>
39 #include "rmr.h" // things the users see
40 #include "rmr_agnostic.h" // agnostic things (must be included before private)
43 // ---------- some wrappers need explicit copy-in functions, also header field setters -----
46 Allow the user programme to set the meid in the header. The meid is a fixed
47 length buffer in the header and thus we must ensure it is not overrun. If
48 the user gives a source buffer that is too large, we truncate. The return
49 value is the number of bytes copied, or -1 for absolute failure (bad pointer
51 EINVAL id poitner, buf or buf header are bad.
52 EOVERFLOW if the bytes given would have overrun
55 extern int rmr_bytes2meid( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
58 if( src == NULL || mbuf == NULL || mbuf->header == NULL ) {
64 if( len > RMR_MAX_MEID ) {
69 hdr = (uta_mhdr_t *) mbuf->header;
70 memcpy( hdr->meid, src, len );
76 Allows the user programme to set the meid from a string. The end of string
77 (nil) will be included UNLESS the total length including the end of string
78 would exceed the size of the space in the header for the meid. The return
79 value is RMR_OK for success and !RMR_OK on failure. Errno will be set
82 extern int rmr_str2meid( rmr_mbuf_t* mbuf, unsigned char const* str ) {
83 int len; // len moved -- we do validate
85 if( str == NULL || mbuf == NULL || mbuf->header == NULL ) {
87 return RMR_ERR_BADARG;
91 if( (len = strlen( (char *) str )) > RMR_MAX_MEID-1 ) {
93 return RMR_ERR_OVERFLOW;
96 rmr_bytes2meid( mbuf, str, len+1 );
103 This will copy n bytes from source into the payload. If len is larger than
104 the payload only the bytes which will fit are copied, The user should
105 check errno on return to determine success or failure.
107 extern void rmr_bytes2payload( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
108 if( src == NULL || mbuf == NULL || mbuf->payload == NULL ) {
114 mbuf->state = RMR_OK;
115 if( len > mbuf->alloc_len - sizeof( uta_mhdr_t ) ) {
116 mbuf->state = RMR_ERR_OVERFLOW;
118 len = mbuf->alloc_len - sizeof( uta_mhdr_t );
122 memcpy( mbuf->payload, src, len );
126 This will copy a nil terminated string to the mbuf payload. The buffer length
127 is set to the string length.
129 extern void rmr_str2payload( rmr_mbuf_t* mbuf, unsigned char const* str ) {
130 rmr_bytes2payload( mbuf, str, strlen( (char *) str ) + 1 );
135 Allow the user programme to set the xaction field in the header. The xaction
136 is a fixed length buffer in the header and thus we must ensure it is not overrun.
137 If the user gives a source buffer that is too large, we truncate. The return
138 value is the number of bytes copied, or -1 for absolute failure (bad pointer
140 EINVAL id poitner, buf or buf header are bad.
141 EOVERFLOW if the bytes given would have overrun
144 extern int rmr_bytes2xact( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
147 if( src == NULL || mbuf == NULL || mbuf->header == NULL ) {
153 if( len > RMR_MAX_XID ) {
158 hdr = (uta_mhdr_t *) mbuf->header;
159 memcpy( hdr->xid, src, len );
167 Allows the user programme to set the xaction (xid) field from a string. The end
168 of string (nil) will be included UNLESS the total length including the end of string
169 would exceed the size of the space in the header for the xaction. The return
170 value is RMR_OK for success and !RMR_OK on failure. Errno will be set
173 extern int rmr_str2xact( rmr_mbuf_t* mbuf, unsigned char const* str ) {
174 int len; // len moved -- we do validate
176 if( str == NULL || mbuf == NULL || mbuf->header == NULL ) {
178 return RMR_ERR_BADARG;
182 if( (len = strlen( (char *) str )) > RMR_MAX_XID-1 ) {
184 return RMR_ERR_OVERFLOW;
187 rmr_bytes2xact( mbuf, str, len+1 );
192 Extracts the meid (managed equipment) from the header and copies the bytes
193 to the user supplied area. If the user supplied pointer is nil, then
194 a buffer will be allocated and it is the user's responsibilty to free.
195 A pointer is returned to the destination memory (allocated or not)
196 for consistency. If the user programme supplies a destination it is
197 the responsibility of the programme to ensure that the space is large
200 extern unsigned char* rmr_get_meid( rmr_mbuf_t* mbuf, unsigned char* dest ) {
203 if( mbuf == NULL || mbuf->header == NULL ) {
209 if( (dest = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) {
215 hdr = (uta_mhdr_t *) mbuf->header;
216 memcpy( dest, hdr->meid, RMR_MAX_XID );
221 // ------------------- trace related access functions --------------------------------------
223 The set_trace function will copy the supplied data for size bytes into the
224 header. If the header trace area is not large enough, a new one will be allocated
225 which will cause a payload copy based on the msg->len value (if 0 no payload
228 The return value is the number of bytes actually coppied. If 0 bytes are coppied
229 errno should indicate the reason. If 0 is returned and errno is 0, then size
230 passed was 0. The state in the message is left UNCHANGED.
232 extern int rmr_set_trace( rmr_mbuf_t* msg, unsigned const char* data, int size ) {
234 rmr_mbuf_t* nm; // new message if larger is needed
236 void* old_tp_buf; // if we need to realloc, must hold old to free
249 hdr = (uta_mhdr_t *) msg->header;
255 len = RMR_TR_LEN( hdr );
257 if( len != size ) { // different sized trace data, must realloc the buffer
258 nm = rmr_realloc_msg( msg, size ); // realloc with changed trace size
259 old_tp_buf = msg->tp_buf;
260 old_hdr = msg->header;
262 msg->tp_buf = nm->tp_buf; // reference the reallocated buffer
263 msg->header = nm->header;
264 msg->id = NULL; // currently unused
265 msg->xaction = nm->xaction;
266 msg->payload = nm->payload;
268 nm->tp_buf = old_tp_buf; // set to free
269 nm->header = old_hdr; // nano frees on hdr, so must set both
272 hdr = (uta_mhdr_t *) msg->header; // header WILL be different
273 len = RMR_TR_LEN( hdr );
276 memcpy( TRACE_ADDR( hdr ), data, size );
283 Copies the trace bytes from the message header into the buffer provided by
284 the user. If the trace data in the header is less than size, then only
285 that number of bytes are copied, else exactly size bytes are copied. The
286 number actually copied is returned.
288 extern int rmr_get_trace( rmr_mbuf_t* msg, unsigned char* dest, int size ) {
289 uta_mhdr_t* hdr = NULL;
296 if( size <= 0 || dest == NULL ) {
301 if( (n2copy = size < RMR_TR_LEN( hdr ) ? size : RMR_TR_LEN( hdr )) <= 0 ) {
305 memcpy( dest, TRACE_ADDR( hdr ), n2copy );
311 Returns the number of bytes currently allocated for trace data in the message
314 extern int rmr_get_trlen( rmr_mbuf_t* msg ) {
323 return RMR_TR_LEN( hdr );
327 Returns the string in the source portion of the header. This is assumed to be
328 something that can be used for direct sends (hostname:port). Regardless, it
329 will be a nil terminated, ascii string with max of 64 characters including
330 the final nil. So, the user must ensure that dest is at least 64 bytes.
332 As a convenience, the pointer to dest is returned on success; nil on failure
335 extern unsigned char* rmr_get_src( rmr_mbuf_t* msg, unsigned char* dest ) {
336 uta_mhdr_t* hdr = NULL;
345 strcpy( dest, hdr->src );
352 Returns the string with the IP address as reported by the sender. This is
353 the IP address that the sender has sussed off of one of the interfaces
354 and cannot be guarenteed to be the acutal IP address which was used to
355 establish the connection. The caller must provide a buffer of at least
356 64 bytes; the string will be nil terminated. A pointer to the user's buffer
357 is returned on success, nil on failure.
359 extern unsigned char* rmr_get_srcip( rmr_mbuf_t* msg, unsigned char* dest ) {
360 uta_mhdr_t* hdr = NULL;
365 if( dest != NULL && msg != NULL ) {
367 if( HDR_VERSION( msg->header ) > 2 ) { // src ip was not present in hdr until ver 3
369 strcpy( dest, hdr->srcip );
373 strcpy( dest, hdr->src ); // reutrn the name:port for old messages