Add license here so shows up in pypi
[ric-plt/lib/rmr.git] / src / rmr / common / src / mbuf_api.c
index e801ac9..1074e37 100644 (file)
@@ -34,6 +34,7 @@
 #include <string.h>
 #include <unistd.h>
 #include <stdio.h>
+#include <semaphore.h>
 
 #include "rmr.h"                               // things the users see
 #include "rmr_agnostic.h"              // agnostic things (must be included before private)
@@ -246,6 +247,11 @@ extern int rmr_set_trace( rmr_mbuf_t* msg, unsigned const char* data, int size )
        }
 
        hdr = (uta_mhdr_t *) msg->header;
+       if( !hdr ) {
+               errno = EINVAL;
+               return 0;
+       }
+
        len = RMR_TR_LEN( hdr );
 
        if( len != size ) {                                                     // different sized trace data, must realloc the buffer
@@ -273,6 +279,41 @@ extern int rmr_set_trace( rmr_mbuf_t* msg, unsigned const char* data, int size )
 }
 
 
+/*
+       Returns a pointer (reference) to the trace data in the message. If sizeptr
+       is supplied, then the trace data size is assigned to the referenced
+       integer so that the caller doesn't need to make a second call to get the
+       value.
+
+       CAUTION:  user programmes should avoid writing to the referenced trace
+                       area (use rmr_set_trace() to avoid possible data overruns. This
+                       function is a convenience, and it is expected that users will
+                       use the trace data as read-only so as a copy is not necessary.
+*/
+extern void* rmr_trace_ref( rmr_mbuf_t* msg, int* sizeptr ) {
+       uta_mhdr_t*     hdr = NULL;
+       int size = 0;
+
+       if( sizeptr != NULL ) {
+               *sizeptr = 0;                                   // ensure reset if we bail
+       }
+
+       if( msg == NULL ) {
+               return NULL;
+       }
+
+       hdr = msg->header;
+       if( (size = RMR_TR_LEN( hdr )) <= 0 ) {
+               return NULL;
+       }
+
+       if( sizeptr != NULL ) {
+               *sizeptr = size;
+       }
+
+       return (void *) TRACE_ADDR( hdr );
+}
+
 /*
        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
@@ -341,3 +382,33 @@ extern unsigned char* rmr_get_src( rmr_mbuf_t* msg, unsigned char* dest ) {
 
        return dest;
 }
+
+/*
+       Returns the string with the IP address as reported by the sender. This is
+       the IP address that the sender has sussed off of one of the interfaces
+       and cannot be guarenteed to be the acutal IP address which was used to
+       establish the connection.   The caller must provide a buffer of at least
+       64 bytes; the string will be nil terminated. A pointer to the user's buffer
+       is returned on success, nil on failure.
+*/
+extern unsigned char* rmr_get_srcip( rmr_mbuf_t* msg, unsigned char* dest ) {
+       uta_mhdr_t*     hdr = NULL;
+       char*   rstr = NULL;
+
+       errno = EINVAL;
+
+       if( dest != NULL && msg != NULL ) {
+               hdr = msg->header;
+               if( HDR_VERSION( msg->header ) > 2 ) {          // src ip was not present in hdr until ver 3
+                       errno = 0;
+                       strcpy( dest, hdr->srcip );
+                       rstr = dest;
+               } else  {
+                       errno = 0;
+                       strcpy( dest, hdr->src );                               // reutrn the name:port for old messages
+                       rstr = dest;
+               }
+       }
+
+       return rstr;
+}