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 372 : __fd_cstr_to_uchar( char const * cstr ) { 10 372 : char const * endptr = NULL; 11 372 : ulong value = strtoul( cstr, (char **)&endptr, 10 ); 12 372 : if( FD_UNLIKELY( cstr==endptr || endptr[0] || value>UCHAR_MAX ) ) return -1; 13 321 : return (int)value; 14 372 : } 15 : 16 : int 17 : fd_cstr_to_ip4_addr( char const * s, 18 1761 : uint * out ) { 19 : 20 1761 : char _s[ 16 ]; 21 1761 : ulong len = strnlen( s, sizeof(_s) ); 22 1761 : if( FD_UNLIKELY( len==sizeof(_s) ) ) return 0; 23 : 24 : /* The tokenizer ignores a trailing delimiter. */ 25 1740 : ulong dot_cnt = 0UL; 26 24048 : for( ulong i=0UL; i<len; i++ ) dot_cnt += (ulong)( s[i]=='.' ); 27 1740 : if( FD_UNLIKELY( dot_cnt!=3UL ) ) return 0; 28 93 : memcpy( _s, s, len+1UL ); 29 : 30 93 : char *tok[ 5 ]; 31 93 : if( FD_UNLIKELY( fd_cstr_tokenize( tok, 5UL, _s, '.' )!=4UL ) ) 32 0 : return 0; 33 : 34 93 : int v0 = __fd_cstr_to_uchar( tok[ 0 ] ); 35 93 : int v1 = __fd_cstr_to_uchar( tok[ 1 ] ); 36 93 : int v2 = __fd_cstr_to_uchar( tok[ 2 ] ); 37 93 : int v3 = __fd_cstr_to_uchar( tok[ 3 ] ); 38 93 : if( FD_UNLIKELY( (v0<0)|(v1<0)|(v2<0)|(v3<0) ) ) return 0; 39 69 : *out = FD_IP4_ADDR( v0, v1, v2, v3 ); 40 69 : return 1; 41 93 : }