File: dns_decode.c

package info (click to toggle)
dns2tcp 0.5.2-4
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 1,268 kB
  • sloc: ansic: 5,320; sh: 3,553; makefile: 63
file content (148 lines) | stat: -rw-r--r-- 3,686 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
/*
** Copyright (C) 2006 Olivier DEMBOUR
** $Id: dns_decode.c,v 1.7.4.4 2010/02/11 15:05:49 collignon Exp $
**
** 
** This program is free software; you can redistribute it and/or modify
** it under the terms of the GNU General Public License as published by
** the Free Software Foundation; either version 2 of the License, or
** (at your option) any later version.
**
** 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
*/

#include <string.h>
#include <stdio.h>
#include <arpa/inet.h>

#include "packet.h"
#include "dns.h"
#include "server.h"
#include "myerror.h"
#include "mystrnlen.h"
#include "log.h"
#include "debug.h"
#include "server.h"
#include "requests.h"

extern const t_command dns_commands[];

/* 
   Strip dot and domain name 
   extract the dns command
   request is always ended by NULL
*/

static int	dns_strip_dot_and_domain(t_conf *conf, t_request *req, char *output)
{
  char		*ptr;
  char		*ptr2;
  unsigned int 	i=0, j, len;
  int		req_len, domain_len;

  req_len = strlen(output);
  domain_len = strlen(conf->my_domain);
  ptr = strstr(output + (req_len - domain_len), conf->my_domain);
  if (!ptr)
    {
      // FIXME bug ipv6 support ?
      LOG("Query from %u.%u.%u.%u for unknown domain %s",
#ifndef WORDS_BIGENDIAN
	  (unsigned int) ((req->sa.sin_addr.s_addr) & 0xff),
	  (unsigned int) ((req->sa.sin_addr.s_addr >> 8) & 0xff),
	  (unsigned int) ((req->sa.sin_addr.s_addr >> 16) & 0xff),
	  (unsigned int) ((req->sa.sin_addr.s_addr >> 24) & 0xff),
#else
	  (unsigned int) ((req->sa.sin_addr.s_addr >> 24) & 0xff),
	  (unsigned int) ((req->sa.sin_addr.s_addr >> 16) & 0xff),
	  (unsigned int) ((req->sa.sin_addr.s_addr >> 8) & 0xff),
	  (unsigned int) ((req->sa.sin_addr.s_addr) & 0xff),
#endif
	  output);
      return (-1);
    }
  /* 
     look for :
	resource.<domain> 
	auth.<domain> 
	...
	cf. request.c
  */
  while (dns_commands[i++].str)
    {
      if ( ( dns_commands[i-1].str_len + 1 + domain_len <= req_len )
	   && ((ptr2 = strstr(ptr - dns_commands[i-1].str_len, dns_commands[i-1].str))))
	{
	  ptr = ptr2;
	  req->cmd = &dns_commands[i-1];
	  break;
	}
    }
  strcpy(req->domain, ptr);
  *ptr = 0;

  len = (unsigned int) (ptr - output);
  /* delete dots */
  for (i=0,j=0; i < len; ++i)
    {
      if (output[i] != '.')
	{
          if (i != j)
            output[j] = output[i];
	  ++j;
	}
    }
  output[j] = 0;
  return (0);
}


/* 
   Not a RFC compatible decoder 
   return -1 if domain is incorrect 
   dns_decode check max size of host
   strip the domain name
*/

int		dns_decode(t_conf *conf, t_request *req, char *input, char *output)
{
  int		total_len = 0;
  uint8_t	len;
  char		*ptr;

  ptr = input;
  *output = 0;

  if (strlen(ptr) < (strlen(conf->my_domain) + 1))
    return (-1);
  while (*ptr)
    {
      len = (uint8_t) *ptr;
      total_len += len;
      if ((len > 63) || (total_len > MAX_HOST_NAME_ENCODED))
	{
	  MYERROR("NAME TOO long %d %d", len, total_len + len);
	  return (-1);
	}
      strncat(output, ptr + 1, len);
      output[total_len] = 0;
      if (len)
	{
	  if (++total_len > MAX_HOST_NAME_ENCODED)
	    return (-1);
	  strcat(output , ".");
	  len++;
	}
      ptr += (len);
    }
  if (total_len > 0)
    output[total_len -1 ] = 0;
  return (dns_strip_dot_and_domain(conf, req, output));
}