/*
 * Copyright (C) 2015 by the University of Southern California
 * $Id: dns_util.h 24638 2015-11-09 20:24:08Z liangzhu $
 *
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License,
 * version 2, as published by the Free Software Foundation.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 59 Temple Place, Suite 330, Boston, MA 02111-1307, USA. 
 * 
 */

#ifndef DNS_UTIL_H
#define DNS_UTIL_H

#include <stdint.h>
#include <string.h>
#include <netinet/in.h>

enum {CONN_NONE, CONN_UDP, CONN_TCP, CONN_TLS, CONN_STARTTLS};

#define EDNS_Z_DO  0x8000U
#define EDNS_Z_TO  0x4000U

struct hdr_t
{
  struct in_addr ip_src;
  struct in_addr ip_dst;
  uint16_t spt;
  uint16_t dpt;
};

struct query_t
{
  const char *qname;
  const char *qtype;
  const char *qclass;
};

struct pseudo_hdr
{
  struct in_addr ip_src;
  struct in_addr ip_dst;
  uint8_t reserved;
  uint8_t protocol;
  uint16_t length;
};

int format_dns_query(uint8_t **, size_t *, query_t *, uint16_t);
int format_dns_query(uint8_t **, size_t *, query_t *);
int create_dns_raw(hdr_t *, int, uint8_t *, int, query_t *);
int create_dns_tcp(uint8_t *, int, query_t *);
void print_dns_pkt(uint8_t *, int);
bool has_to_bit(uint8_t *p, size_t l);
uint16_t cksum (uint16_t *, int);
uint16_t udp_tcp_cksum (uint8_t *, uint16_t, struct in_addr, struct in_addr, uint8_t);

#endif //DNS_UTIL_H
