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 0 : int buflen ) { 17 0 : size_t l = strnlen( dname, FD_FQDN_BUF_MAX-1UL ); 18 : 19 0 : if( l && dname[l-1]=='.' ) l--; 20 0 : if( l && dname[l-1]=='.' ) return -1; 21 0 : int n = 17+l+!!l; 22 0 : 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 0 : uchar q[280]; 27 0 : memset( q, 0, n ); 28 0 : q[2] = op*8 + 1; 29 0 : q[3] = 32; /* AD */ 30 0 : q[5] = 1; 31 0 : memcpy( (char *)q+13, dname, l ); 32 0 : int i, j; 33 0 : for( i=13; q[i]; i=j+1 ) { 34 0 : for( j=i; q[j] && q[j] != '.'; j++ ); 35 0 : if( j-i-1u > 62u ) return -1; 36 0 : q[i-1] = j-i; 37 0 : } 38 0 : q[i+1] = type; 39 0 : q[i+3] = class; 40 : 41 : /* Make a reasonably unpredictable id */ 42 0 : ulong ts = fd_ulong_hash( (ulong)fd_tickcount() ); 43 0 : q[0] = (uchar)( ts ); 44 0 : q[1] = (uchar)( ts>>8 ); 45 : 46 0 : memcpy( buf, q, n ); 47 0 : return n; 48 0 : }