Line data Source code
1 : #include "fd_resolv.h" 2 : #include "../fd_fqdn.h" 3 : #include "../../util/log/fd_log.h" /* fd_tickcount() support */ 4 : #include <string.h> 5 : 6 : #pragma GCC diagnostic ignored "-Wconversion" 7 : #pragma GCC diagnostic ignored "-Wsign-compare" 8 : #pragma GCC diagnostic ignored "-Wsign-conversion" 9 : 10 : int 11 : fd_res_mkquery( int op, 12 : char const * dname, 13 : int class, 14 : int type, 15 : uchar * buf, 16 18 : int buflen ) { 17 18 : size_t l = strnlen( dname, FD_FQDN_BUF_MAX-1UL ); 18 : 19 18 : if( l && dname[l-1]=='.' ) l--; 20 18 : if( l && dname[l-1]=='.' ) return -1; 21 18 : int n = 17+l+!!l; 22 18 : if( l>253 || buflen<n || op>15u || class>255u || type>255u ) 23 0 : return -1; 24 : 25 : /* Construct query template - ID will be filled later */ 26 18 : uchar q[280]; 27 18 : memset( q, 0, n ); 28 18 : q[2] = op*8 + 1; 29 18 : q[3] = 32; /* AD */ 30 18 : q[5] = 1; 31 18 : memcpy( (char *)q+13, dname, l ); 32 18 : int i, j; 33 66 : for( i=13; q[i]; i=j+1 ) { 34 243 : for( j=i; q[j] && q[j] != '.'; j++ ); 35 48 : if( j-i-1u > 62u ) return -1; 36 48 : q[i-1] = j-i; 37 48 : } 38 18 : q[i+1] = type; 39 18 : q[i+3] = class; 40 : 41 : /* Make a reasonably unpredictable id */ 42 18 : ulong ts = fd_ulong_hash( (ulong)fd_tickcount() ); 43 18 : q[0] = (uchar)( ts ); 44 18 : q[1] = (uchar)( ts>>8 ); 45 : 46 18 : memcpy( buf, q, n ); 47 18 : return n; 48 18 : }