Line data Source code
1 : #include "fd_ip4.h" 2 : #include "../fd_util.h" 3 : 4 : #include <stdlib.h> 5 : 6 : 7 : /* FIXME add bounds checks to fd_util version */ 8 : static int 9 132 : __fd_cstr_to_uchar( char const * cstr ) { 10 132 : char const * endptr = NULL; 11 132 : ulong value = strtoul( cstr, (char **)&endptr, 10 ); 12 132 : if( FD_UNLIKELY( cstr==endptr || endptr[0] || value>UCHAR_MAX ) ) return -1; 13 120 : return (int)value; 14 132 : } 15 : 16 : int 17 : fd_cstr_to_ip4_addr( char const * s, 18 87 : uint * out ) { 19 : 20 87 : char _s[ 16 ]; 21 87 : strncpy( _s, s, 15 ); 22 87 : _s[ 15 ] = '\0'; 23 : 24 87 : char *tok[ 5 ]; 25 87 : if( FD_UNLIKELY( fd_cstr_tokenize( tok, 5UL, _s, '.' )!=4UL ) ) 26 54 : return 0; 27 : 28 33 : int v0 = __fd_cstr_to_uchar( tok[ 0 ] ); 29 33 : int v1 = __fd_cstr_to_uchar( tok[ 1 ] ); 30 33 : int v2 = __fd_cstr_to_uchar( tok[ 2 ] ); 31 33 : int v3 = __fd_cstr_to_uchar( tok[ 3 ] ); 32 33 : if( FD_UNLIKELY( (v0<0)|(v1<0)|(v2<0)|(v3<0) ) ) return 0; 33 21 : *out = FD_IP4_ADDR( v0, v1, v2, v3 ); 34 21 : return 1; 35 33 : }