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