1#include <sys/socket.h> 2#include <netinet/in.h> 3#include <arpa/inet.h> 4#include "dns_io.h" 5 6/*****************************************************************************/ 7int dns_read_packet(int sock, dns_request_t *m) 8{ 9 struct sockaddr_in sa; 10 int salen; 11 12 /* Read in the actual packet */ 13 salen = sizeof(sa); 14 15 m->numread = recvfrom(sock, m->original_buf, sizeof(m->original_buf), 0, 16 (struct sockaddr *)&sa, &salen); 17 18 if ( m->numread < 0) { 19 debug_perror("dns_read_packet: recvfrom\n"); 20 return -1; 21 } 22 23 /* TODO: check source addr against list of allowed hosts */ 24 25 /* record where it came from */ 26 memcpy( (void *)&m->src_addr, (void *)&sa.sin_addr, sizeof(struct in_addr)); 27 m->src_port = ntohs( sa.sin_port ); 28 29 /* check that the message is long enough */ 30 if( m->numread < sizeof (m->message.header) ){ 31 debug("dns_read_packet: packet from '%s' to short to be dns packet", 32 inet_ntoa (sa.sin_addr) ); 33 return -1; 34 } 35 36 /* pass on for full decode */ 37 dns_decode_request( m ); 38 39 return 0; 40} 41/*****************************************************************************/ 42int dns_write_packet(int sock, struct in_addr in, int port, dns_request_t *m) 43{ 44 struct sockaddr_in sa; 45 int retval; 46 47 /* Zero it out */ 48 memset((void *)&sa, 0, sizeof(sa)); 49 50 /* Fill in the information */ 51 //inet_aton( "203.12.160.35", &in ); 52 memcpy( &sa.sin_addr.s_addr, &in, sizeof(in) ); 53 sa.sin_port = htons(port); 54 sa.sin_family = AF_INET; 55 56 retval = sendto(sock, m->original_buf, m->numread, 0, 57 (struct sockaddr *)&sa, sizeof(sa)); 58 59 if( retval < 0 ){ 60 debug_perror("dns_write_packet: sendto"); 61 } 62 63 return retval; 64} 65