isc_event.c 8.9 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
/*
** isc_event.c            		 definitions of ISC subsystem routines
**
Per Cederqvist's avatar
Per Cederqvist committed
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
** Copyright (C) 1990-1992, 1996, 1998-1999 by Peter Eriksson and
** Per Cederqvist of the Lysator Academic Computer Association.
**
**
** This library is free software; you can redistribute it and/or
** modify it under the terms of the GNU Library General Public
** License as published by the Free Software Foundation; either
** version 2 of the License, or (at your option) any later version.
**
** This library 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
** Library General Public License for more details.
**
** You should have received a copy of the GNU Library General Public
** License along with this library; if not, write to the Free
** Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
Per Cederqvist's avatar
Per Cederqvist committed
21
22
23
24
25
26
27
28
**
** history:
**    900403 pen       initial coding.
**    900612 pen       rewrote the message buffering.
**    901102 pen       fixed bug in isc_gethostname().
**    910303 ceder     clean up. Removed everything that is lyskom-specific.
**    910304 pen       really removed everything lyskom-specific.. :-)
**    920129 pen       added support for "lazy" connect
29
**    920805 pen       one unneccessary isc_pollqueue() removed.
Per Cederqvist's avatar
Per Cederqvist committed
30
** (See ChangeLog for recent history)
Per Cederqvist's avatar
Per Cederqvist committed
31
32
33
*/

#include <stdio.h>
Per Cederqvist's avatar
Per Cederqvist committed
34
35
36
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
37
#ifdef TIME_WITH_SYS_TIME
38
#  include <sys/time.h>
Per Cederqvist's avatar
Per Cederqvist committed
39
#  include <time.h>
40
41
42
43
44
45
#else
#  ifdef HAVE_SYS_TIME_H
#    include <sys/time.h>
#  else
#    include <time.h>
#  endif
Per Cederqvist's avatar
Per Cederqvist committed
46
#endif
Per Cederqvist's avatar
Per Cederqvist committed
47
#include <sys/types.h>
48
#include <sys/socket.h>
Per Cederqvist's avatar
Per Cederqvist committed
49
#include <errno.h>
Per Cederqvist's avatar
Per Cederqvist committed
50
51
52
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
53
54
55
#ifdef HAVE_SYS_SELECT_H
#  include <sys/select.h>
#endif
56
#ifdef HAVE_UNISTD_H
Per Cederqvist's avatar
Per Cederqvist committed
57
#  include <unistd.h>	/* Needed on NetBSD1.1/SPARC due to select */
58
59
#endif
#ifdef HAVE_STRING_H
Per Cederqvist's avatar
Per Cederqvist committed
60
#  include <string.h>	/* Needed on NetBSD1.1/SPARC due to bzero */
61
#endif
Per Cederqvist's avatar
Per Cederqvist committed
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

#include "isc.h"
#include "intern.h"



IscEvent *
isc_getnextevent(IscMaster *mcb, long timeout)
{
  struct timeval    wait;
  fd_set            read_set;
  fd_set            write_set;
  fd_set            error_set;
  IscEvent        * event;
  IscEvent        * event2;
  IscSessionList  * isl;
  IscSession      * scb;
  IscSession      * new_scb;
  IscMessage      * msg;
  IscAddress      * ia;
  int               nfds;
  int		    numwaitmsgs;
  

  /* Allocate an event */
  ISC_XNEW(event);
  event->msg     = NULL;
  event->session = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
90
  event->listen_session = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
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

RETRY:
  /* Set up the list of file descriptors to wait on */
  FD_ZERO(&read_set);
  FD_ZERO(&write_set);
  FD_ZERO(&error_set);

  /* Number of sessions with queued messages */
  numwaitmsgs = 0;
  
  /*
   * Go thru the list of sessions, add to read_set and handle LOGOUT
   */
  for (isl = mcb->sessions; isl != NULL; isl = isl->next)
  {
    scb = isl->scb;

    if (scb->state == ISC_STATE_CLOSING)
    {
      /*
      ** If we are in state CLOSING, return all outstanding messages
      ** before returning event LOGOUT unless fd still open and outgoing
      ** messages exists.
      */
      event->session = scb;
      event->msg = isc_popqueue(scb->rd_msg_q);
      if (event->msg)
      {
	event->event = ISC_EVENT_MESSAGE;
	
	/* Move the session pointer to the next in the circular queue */
	mcb->sessions = mcb->sessions->next;
	
	return event;
      }
      else
	if (scb->fd == -1 || !isc_pollqueue(scb->wr_msg_q))
	{
	  event->event = ISC_EVENT_LOGOUT;
Per Cederqvist's avatar
Per Cederqvist committed
130
	  scb->state = ISC_STATE_CLOSING2;
Per Cederqvist's avatar
Per Cederqvist committed
131
132
133
134
135
136
	
	  /* Move the session pointer to the next in the circular queue */
	  mcb->sessions = mcb->sessions->next;
	  
	  return event;
	}
137
138
        else
	  event->session = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
139
140
141
142
143
144
145
    }
      
    /*
     * Add to the write set if in state CONNECTING or RUNNING and there
     * are messages in the outgoing queue.
     */
    if (scb->state == ISC_STATE_CONNECTING ||
146
147
	((scb->state == ISC_STATE_RUNNING ||
	  scb->state == ISC_STATE_DISABLED) &&
Per Cederqvist's avatar
Per Cederqvist committed
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
206
207
208
209
210
211
212
	 (scb->sendindex > 0 || isc_pollqueue(scb->wr_msg_q))))
      FD_SET(scb->fd, &write_set);

    /*
     * Add to the read set if state RUNNING or LISTENING
     */
    if (scb->state == ISC_STATE_RUNNING ||
	scb->state == ISC_STATE_LISTENING)
      FD_SET(scb->fd, &read_set);

    /*
     * Increment the count of sessions with waiting messages
     */
    if (isc_pollqueue(scb->rd_msg_q))
      numwaitmsgs++;
    
    /*
     * Terminate at end of circular ring
     */
    if (isl->next == mcb->sessions)
      break;
  }

  /*
   * Set up the timeout structure: If there are queued messages,
   * do not wait in select, else use user supplied timeout limit
   */
  if (numwaitmsgs > 0)
  {
    wait.tv_sec  = 0;
    wait.tv_usec = 0;
  }
  else
  {
    wait.tv_sec  = timeout / 1000;
    wait.tv_usec = (timeout % 1000L) * 1000L;
  }

  /* Wait for event */
  nfds = select(FD_SETSIZE, &read_set, &write_set, &error_set, &wait);

  /* Error in select? */
  if (nfds < 0)
  {
    event->event = ISC_EVENT_ERROR;
    event->msg   = isc_mkstrmsg("Error in select()");
    return event;
  }
  
  /* Timeout? */
  if (nfds == 0 && numwaitmsgs == 0)
  {
    event->event = ISC_EVENT_TIMEOUT;
    return event;
  }
  
  for (isl = mcb->sessions; isl != NULL; isl = isl->next)
  {
    scb = isl->scb;

    
    if (FD_ISSET(scb->fd, &write_set))
      switch (scb->state)
      {
	case ISC_STATE_RUNNING:
213
        case ISC_STATE_DISABLED:
Per Cederqvist's avatar
Per Cederqvist committed
214
215
216
217
218
219
220
221
222
223
224
          isc_oflush(scb);
	  break;

	case ISC_STATE_CONNECTING:
	  ia = isc_getraddress(scb);
	  if (ia == NULL)
	  {
	    /*
	     * Connect request failed
	     */
	    scb->state = ISC_STATE_CLOSING;
David Byers's avatar
David Byers committed
225
	    scb->isc_errno = errno;
Per Cederqvist's avatar
Per Cederqvist committed
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
	    event->event = ISC_EVENT_REJECTED;
	    event->session = scb;
	  }
	  else
	  {
	    /*
	     * Connect request acknowledged
	     */
	    isc_free(ia);
	    scb->state = ISC_STATE_RUNNING;
	    event->event = ISC_EVENT_CONNECTED;
	    event->session = scb;
	  }
	  
	  /* Move the session pointer to the next in the circular queue */
	  mcb->sessions = mcb->sessions->next;
      
	  return event;

	default:
	  /* Major fuck-up somewhere, this should not happen... */
	  event->event   = ISC_EVENT_ERROR;
	  event->session = scb;
	  event->msg     = isc_mkstrmsg(
		       "Should not happen; illegal state for write FD_SET");
	  
	  /* Move the session pointer to the next in the circular queue */
	  mcb->sessions = mcb->sessions->next;
	  
	  return event;
      }
    
    if (isl->next == mcb->sessions)
      break;
  }
  
  /* Check for messages from sessions */
  for (isl = mcb->sessions; isl != NULL; isl = isl->next)
  {
    scb = isl->scb;

    if (FD_ISSET(scb->fd, &read_set) &&
	!(scb->type == ISC_TYPE_TCP && scb->state == ISC_STATE_LISTENING))
    {
270
271
#if 0
      /* lyskomd doesn't use information. */
Per Cederqvist's avatar
Per Cederqvist committed
272
      time(&scb->idlesince);
273
#endif
Per Cederqvist's avatar
Per Cederqvist committed
274
275
276
277
278
279
280
      
      msg = ISC_SCALLFUN1(scb, read, scb);
      if (msg)
      {
	scb->stats.rx.bytes += msg->length;
	scb->stats.rx.packets++;
	
281
	if (scb->handlers->current.parse)
Per Cederqvist's avatar
Per Cederqvist committed
282
283
284
285
286
287
288
289
290
291
292
293
294
295
	{
	  event2 = ISC_SCALLFUN2(scb, parse, scb, msg);
	  if (event2)
	  {
	    isc_free(event);
	    return event2;
	  }
	}
	
	isc_pushqueue(scb->rd_msg_q, msg);
      }
      else
      {
	scb->state = ISC_STATE_CLOSING;
David Byers's avatar
David Byers committed
296
	scb->isc_errno = errno;
Per Cederqvist's avatar
Per Cederqvist committed
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
      }
    }
    
    if (isl->next == mcb->sessions)
      break;
  }


  /* New connection? */
  for (isl = mcb->sessions; isl != NULL; isl = isl->next)
  {
    scb = isl->scb;

    if (scb->state == ISC_STATE_LISTENING &&
	FD_ISSET(scb->fd, &read_set) &&
312
	scb->handlers->current.accept != NULL)
Per Cederqvist's avatar
Per Cederqvist committed
313
314
315
316
317
318
    {
      new_scb = ISC_SCALLFUN2(scb, accept, scb, NULL);
      if (new_scb == NULL)
      {
	event->event   = ISC_EVENT_ERROR;
	event->session = NULL;
Per Cederqvist's avatar
Per Cederqvist committed
319
320
	event->msg     = isc_mkstrmsg("Failed isc_getnextevent() "
				      "in new session");
Per Cederqvist's avatar
Per Cederqvist committed
321
322
323
324
325
326
	return event;
      }
	    
      /* Fill in the event info structure */
      event->session = new_scb;
      event->event   = ISC_EVENT_LOGIN;
327
328
329
330
331

      if (new_scb->cfg->fd_relocate > 0
	  && new_scb->fd <= new_scb->cfg->fd_relocate)
	event->event = ISC_EVENT_LOGIN_UNRELOCATED;

Per Cederqvist's avatar
Per Cederqvist committed
332
      event->listen_session = scb;
Per Cederqvist's avatar
Per Cederqvist committed
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350

      /* Move the session pointer to the next in the circular queue */
      mcb->sessions = mcb->sessions->next;

      isc_insert(mcb, new_scb);
      return event;
    }
    
    if (isl->next == mcb->sessions)
      break;
  }
  
  /* Return one of the received messages from the clients */
  for (isl = mcb->sessions; isl != NULL; isl = isl->next)
  {
    scb = isl->scb;

    
351
    if ((event->msg = isc_popqueue(scb->rd_msg_q)))
Per Cederqvist's avatar
Per Cederqvist committed
352
353
354
355
356
357
358
359
360
361
362
363
364
365
    {
      event->session = scb;
      event->event = ISC_EVENT_MESSAGE;
      
      /* Move the session pointer to the next in the circular queue */
      mcb->sessions = mcb->sessions->next;
      
      return event;
    }

    if (isl->next == mcb->sessions)
      break;
  }

Per Cederqvist's avatar
Per Cederqvist committed
366
  goto RETRY; /* This happens if/when only transmits were pending... */
Per Cederqvist's avatar
Per Cederqvist committed
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
}



/*
** Get rid of the specified event
*/
void
isc_dispose(IscEvent *ep)
{
  if (!ep)
    return;

  if (ep->msg)
    isc_freemsg(ep->msg);

  isc_free(ep);
}