File: server.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 (205 lines) | stat: -rw-r--r-- 4,586 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
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
/*
** Copyright (C) 2006 Olivier DEMBOUR
** $Id: server.c,v 1.20.4.1 2010/01/06 12:50:40 dembour 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 <stdio.h>
#include <sys/select.h>
#include <sys/time.h>
#include <time.h>
#include <string.h>

#include "server.h"
#include "dns.h"
#include "list.h"
#include "myerror.h"
#include "requests.h"
#include "queue.h"
#include "debug.h"
#include "log.h"


#define SOCKET_TIMEOUT	0 /* 1s */
#define SOCKET_UTIMEOUT	500000 /* 1s */

/**
 * @brief prepare the fd_set for select
 * @param[in] conf configuration
 * @param[in] rfds the fd_set
 * @param[in] tv timeval structure
 * @retval last file descriptor
 */

static int      prepare_select(t_conf *conf, fd_set *rfds, struct timeval *tv)
{
  int           max_fd = 0;
  t_simple_list *client;

  FD_ZERO(rfds);
  client = conf->client;

  while (client)
    {
      if ((client->sd_tcp != -1) && (!(client->control.queue_full)))
        {
          FD_SET(client->sd_tcp, rfds);
          max_fd = MAX(max_fd, client->sd_tcp);
        }
      client = client->next;
    }
  FD_SET(conf->sd_udp, rfds);
  if (conf->foreground)
    FD_SET(0, rfds);
  max_fd = MAX(max_fd, conf->sd_udp);
  tv->tv_sec = SOCKET_TIMEOUT;
  tv->tv_usec = SOCKET_UTIMEOUT;
  return (max_fd);
}

/**
 * @brief delete a client
 * @param[in] conf configuration
 * @param[in] client client to delete
 **/

int		delete_client(t_conf *conf, t_simple_list *client)
{
  t_simple_list	*tmp;

  if (conf->client == client)
    {
      tmp = client->next;
      LOG("delete_client 0x%x %s\n", client->session_id, 
	  client->control.authenticated ? "" : "(not authenticated)");
      delete_queue(client->saved_queue);      
      list_destroy_simple_cell(conf->client);
      conf->client = tmp;
      return (0);
    }
  for (tmp = conf->client; tmp; tmp = tmp->next)
    {
      if (tmp->next == client)
	{
	  tmp->next = client->next;
	  LOG("delete_client 0x%x\n", client->session_id);
	  delete_queue(client->saved_queue);
	  return (list_destroy_simple_cell(client));
	}
    }
  return (-1);
}


/**
 * @brief destroy dead client
 * @param[in] conf configuration
 **/

// Warning Zombie ahead
void			delete_zombie(t_conf *conf)
{
  t_simple_list		*client;
  t_simple_list		*tmp;
  struct timeval	tv;
  struct timezone	tz;
  
  if (!(gettimeofday(&tv, &tz)))
    for (client = conf->client; client; client = tmp)
      {
	tmp = client->next;
	if (tv.tv_sec > client->control.tv.tv_sec)
	  {
	    if (client->sd_tcp != -1)
	      close(client->sd_tcp);
	    delete_client(conf, client);
	  }
      }
}

#define MINI_BUFF 64

/**
 * @brief main loop
 * @param[in] conf configuration
 **/

int			do_server(t_conf *conf)
{
  fd_set		rfds;
  int			retval;
  int			max_fd;
  t_simple_list		*client;
  t_simple_list		*tmp;
  struct timeval	tv;
  struct timezone	tz;

#ifdef DEBUG
  char		        buffer[MINI_BUFF];
#endif

  while (1)
    {     
      max_fd = prepare_select(conf, &rfds, &tv);
      retval = select(max_fd + 1 , &rfds, NULL, NULL, &tv);
      queue_flush_expired_data(conf);
      if (retval == -1)
	{
	  perror("");
	  MYERROR("select");
	  return (-1);
	}
      if (!retval)
	{ 
	  if (gettimeofday(&tv, &tz))
	    {
	      MYERROR("Time Error");
	      return (-1);
	    }
	  delete_zombie(conf);
	  continue;
	}
#ifdef DEBUG
      if ((conf->foreground) && (FD_ISSET(0, &rfds)))
	{
	  read(0, buffer, MINI_BUFF); 
	  queue_dump(conf->client);
	  continue;
	}
#endif
      if (FD_ISSET(conf->sd_udp, &rfds))
	get_incoming_request(conf);
      else
	{
	  for (client = conf->client;  client; client = tmp)
	    {
	      tmp = client->next;
	      if ((client->sd_tcp != -1) && (FD_ISSET(client->sd_tcp, &rfds)))
	      {
		if (queue_read_tcp(conf, client))
		  {
		    if (client->sd_tcp != -1)
		      close(client->sd_tcp);
		    delete_client(conf, client);
		  }
	      }
	    }
	}
      delete_zombie(conf);
    }
}