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 144 : __fd_cstr_to_uchar( char const * cstr ) { 10 144 : char const * endptr = NULL; 11 144 : ulong value = strtoul( cstr, (char **)&endptr, 10 ); 12 144 : if( FD_UNLIKELY( cstr==endptr || endptr[0] || value>UCHAR_MAX ) ) return -1; 13 132 : return (int)value; 14 144 : } 15 : 16 : int 17 : fd_cstr_to_ip4_addr( char const * s, 18 90 : uint * out ) { 19 : 20 90 : char _s[ 16 ]; 21 90 : strncpy( _s, s, 15 ); 22 90 : _s[ 15 ] = '\0'; 23 : 24 90 : char *tok[ 5 ]; 25 90 : if( FD_UNLIKELY( fd_cstr_tokenize( tok, 5UL, _s, '.' )!=4UL ) ) 26 54 : return 0; 27 : 28 36 : int v0 = __fd_cstr_to_uchar( tok[ 0 ] ); 29 36 : int v1 = __fd_cstr_to_uchar( tok[ 1 ] ); 30 36 : int v2 = __fd_cstr_to_uchar( tok[ 2 ] ); 31 36 : int v3 = __fd_cstr_to_uchar( tok[ 3 ] ); 32 36 : if( FD_UNLIKELY( (v0<0)|(v1<0)|(v2<0)|(v3<0) ) ) return 0; 33 24 : *out = FD_IP4_ADDR( v0, v1, v2, v3 ); 34 24 : return 1; 35 36 : }