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 "rmr.h" // things the users see
38 #include "rmr_agnostic.h" // agnostic things (must be included before private)
41 // ---------- some wrappers need explicit copy-in functions, also header field setters -----
44 Allow the user programme to set the meid in the header. The meid is a fixed
45 length buffer in the header and thus we must ensure it is not overrun. If
46 the user gives a source buffer that is too large, we truncate. The return
47 value is the number of bytes copied, or -1 for absolute failure (bad pointer
49 EINVAL id poitner, buf or buf header are bad.
50 EOVERFLOW if the bytes given would have overrun
53 extern int rmr_bytes2meid( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
56 if( src == NULL || mbuf == NULL || mbuf->header == NULL ) {
62 if( len > RMR_MAX_MEID ) {
67 hdr = (uta_mhdr_t *) mbuf->header;
68 memcpy( hdr->meid, src, len );
74 Allows the user programme to set the meid from a string. The end of string
75 (nil) will be included UNLESS the total length including the end of string
76 would exceed the size of the space in the header for the meid. The return
77 value is RMR_OK for success and !RMR_OK on failure. Errno will be set
80 extern int rmr_str2meid( rmr_mbuf_t* mbuf, unsigned char const* str ) {
81 int len; // len moved -- we do validate
83 if( str == NULL || mbuf == NULL || mbuf->header == NULL ) {
85 return RMR_ERR_BADARG;
89 if( (len = strlen( (char *) str )) > RMR_MAX_MEID-1 ) {
91 return RMR_ERR_OVERFLOW;
94 rmr_bytes2meid( mbuf, str, len+1 );
101 This will copy n bytes from source into the payload. If len is larger than
102 the payload only the bytes which will fit are copied, The user should
103 check errno on return to determine success or failure.
105 extern void rmr_bytes2payload( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
106 if( src == NULL || mbuf == NULL || mbuf->payload == NULL ) {
112 mbuf->state = RMR_OK;
113 if( len > mbuf->alloc_len - sizeof( uta_mhdr_t ) ) {
114 mbuf->state = RMR_ERR_OVERFLOW;
116 len = mbuf->alloc_len - sizeof( uta_mhdr_t );
120 memcpy( mbuf->payload, src, len );
124 This will copy a nil terminated string to the mbuf payload. The buffer length
125 is set to the string length.
127 extern void rmr_str2payload( rmr_mbuf_t* mbuf, unsigned char const* str ) {
128 rmr_bytes2payload( mbuf, str, strlen( (char *) str ) + 1 );
133 Allow the user programme to set the xaction field in the header. The xaction
134 is a fixed length buffer in the header and thus we must ensure it is not overrun.
135 If the user gives a source buffer that is too large, we truncate. The return
136 value is the number of bytes copied, or -1 for absolute failure (bad pointer
138 EINVAL id poitner, buf or buf header are bad.
139 EOVERFLOW if the bytes given would have overrun
142 extern int rmr_bytes2xact( rmr_mbuf_t* mbuf, unsigned char const* src, int len ) {
145 if( src == NULL || mbuf == NULL || mbuf->header == NULL ) {
151 if( len > RMR_MAX_XID ) {
156 hdr = (uta_mhdr_t *) mbuf->header;
157 memcpy( hdr->xid, src, len );
165 Allows the user programme to set the xaction (xid) field from a string. The end
166 of string (nil) will be included UNLESS the total length including the end of string
167 would exceed the size of the space in the header for the xaction. The return
168 value is RMR_OK for success and !RMR_OK on failure. Errno will be set
171 extern int rmr_str2xact( rmr_mbuf_t* mbuf, unsigned char const* str ) {
172 int len; // len moved -- we do validate
174 if( str == NULL || mbuf == NULL || mbuf->header == NULL ) {
176 return RMR_ERR_BADARG;
180 if( (len = strlen( (char *) str )) > RMR_MAX_XID-1 ) {
182 return RMR_ERR_OVERFLOW;
185 rmr_bytes2xact( mbuf, str, len+1 );
190 Extracts the meid (managed equipment) from the header and copies the bytes
191 to the user supplied area. If the user supplied pointer is nil, then
192 a buffer will be allocated and it is the user's responsibilty to free.
193 A pointer is returned to the destination memory (allocated or not)
194 for consistency. If the user programme supplies a destination it is
195 the responsibility of the programme to ensure that the space is large
198 extern unsigned char* rmr_get_meid( rmr_mbuf_t* mbuf, unsigned char* dest ) {
201 if( mbuf == NULL || mbuf->header == NULL ) {
207 if( (dest = (unsigned char *) malloc( sizeof( unsigned char ) * RMR_MAX_MEID )) == NULL ) {
213 hdr = (uta_mhdr_t *) mbuf->header;
214 memcpy( dest, hdr->meid, RMR_MAX_XID );