intern.h 8.84 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
/*
** intern.h                Definitions and prototypes used internally
**
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) 1991,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
**
** history:
** 910305 pen      initial coding
Per Cederqvist's avatar
Per Cederqvist committed
24
** (See ChangeLog for recent history)
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
29
*/

#ifndef __ISC_INTERNALS_H__
#define __ISC_INTERNALS_H__

30
31
32
33
34
35
36
37
38
/*
** Some nice defaults
*/
#define ISC_DEFAULT_MAX_MSG_SIZE      8176
#define ISC_DEFAULT_MAX_QUEUED_SIZE   50
#define ISC_DEFAULT_MAX_DEQUEUE_LEN   30
#define ISC_DEFAULT_MAX_OPEN_RETRIES  10
#define ISC_DEFAULT_MAX_BACKLOG       50

39
40
41
42
43
44
45
46
/*
** The Master Control Block
*/
struct isc_mcb
{
  struct isc_session_cfg  * scfg;
  struct isc_scb_entry    * sessions;
  oop_source              * event_source;
Per Cederqvist's avatar
Per Cederqvist committed
47
  oop_adapter_adns        * adns;
48
  isc_write_queue_change_cb *write_change_cb;
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
};


/*
** The Client Control Block
*/
struct isc_scb_internal
{
  struct isc_scb pub;

  enum isc_session_state state;

  struct isc_msgqueue *wr_msg_q;

  struct isc_session_cfg * cfg;
  
  /* Cheap transmit buffer */
  /* Should be dynamically allocated - into an struct isc_msg */
67
  char                sendbuf[ISC_DEFAULT_MAX_MSG_SIZE];
68
69
70
71
  int                 sendindex;
  
  isc_accept_callback *accept_cb;
  isc_write_error_cb  *write_err_cb;
72
  isc_stale_output_cb *stale_output_cb;
73
  isc_stale_output_cb *idle_cb;
74
75
76
77
78

  oop_call_fd         *data_available_callback;
  int                  data_available_registered;

  int                  write_cb_registered;
79
80
81

  int                  stale_output_cb_registered;
  struct timeval       stale_output_tv;
82
83
84
85

  int                  idle_cb_registered;
  struct timeval       idle_tv;
  struct timeval       acceptable_idle_time;
86
87

  isc_resolve_done_cb  *resolve_callback; /* Non-NULL when lookup pending. */
Per Cederqvist's avatar
Per Cederqvist committed
88
  oop_adns_query       *adns_query;
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
/*
** Generic message type
*/
struct isc_msg
{
  int               size;      	/* Maximum buffer size */
  int               length;    	/* Length of used buffer */
  char            * buffer;  	/* Pointer to buffer */
};

/*
** Entry in a message queue
*/
struct isc_msg_q_entry
{
  struct isc_msg_q_entry    * prev;
  struct isc_msg_q_entry    * next;
  struct isc_msg * msg;
};



/*
** The generic message queue
*/
struct isc_msgqueue
{
  struct isc_msg_q_entry  * head;
  struct isc_msg_q_entry  * tail;
  int         entries;
121
  int         bytes;
122
};
Per Cederqvist's avatar
Per Cederqvist committed
123

124
125
126
/*
** Free an allocated struct isc_msg
*/
Per Cederqvist's avatar
Per Cederqvist committed
127
extern void
128
129
130
131
132
133
134
135
136
isc_freemsg(struct isc_msg  * msg);

/*
** Configuration structure for sessions
*/
struct isc_session_cfg
{
  struct
  {
137
138
139
140
    int msgsize;		/* Size of a single message. */
    int queuedsize;		/* Number of messages. */
    int queuedsize_bytes;	/* Total number of bytes. */
    int dequeuelen;		/* Number of messages to write at once. */
141
142
143
144
    int openretries;
    int backlog;
  } max;
  int fd_relocate;
145
  struct timeval stale_timeout;
146
  struct timeval idle_timeout;
147
};
Per Cederqvist's avatar
Per Cederqvist committed
148

149
150
151
152
153
154
155
156
/*
** Session list entry
*/
struct isc_scb_entry
{
  struct isc_scb_entry  * prev;
  struct isc_scb_entry  * next;
  
157
  struct isc_scb_internal * scb;
158
};
Per Cederqvist's avatar
Per Cederqvist committed
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175

/* isc_abort.c */
extern void
isc_abort(const char *message);


/* isc_alloc.c */
extern void *
isc_malloc(size_t length);

extern void *
isc_realloc(void *buf, size_t length);

extern void
isc_free(void *buf);

/* isc_queue.c */
176
extern struct isc_msgqueue *
Per Cederqvist's avatar
Per Cederqvist committed
177
178
isc_newqueue(void);

179
/* Return TRUE if the queue is not empty. */
180
#define isc_pollqueue(qp)	((qp) != NULL && ((qp)->head != NULL))
Per Cederqvist's avatar
Per Cederqvist committed
181

182
extern int
183
isc_killqueue(struct isc_msgqueue *queue);
Per Cederqvist's avatar
Per Cederqvist committed
184
185

extern void
186
isc_pushqueue(struct isc_msgqueue *queue, struct isc_msg *msg);
Per Cederqvist's avatar
Per Cederqvist committed
187

188
189
extern struct isc_msg *
isc_topqueue(struct isc_msgqueue *queue);
Per Cederqvist's avatar
Per Cederqvist committed
190
191

extern int
192
isc_sizequeue(struct isc_msgqueue *queue);
Per Cederqvist's avatar
Per Cederqvist committed
193

194
195
extern struct isc_msg *
isc_popqueue(struct isc_msgqueue *queue);
Per Cederqvist's avatar
Per Cederqvist committed
196
197


198
extern void
199
isc_oflush(struct isc_scb_internal  * scb);
Per Cederqvist's avatar
Per Cederqvist committed
200
201


202
/* isc_session.c */
203
extern struct isc_scb_internal *
204
205
206
isc_create(struct isc_mcb *mcb,
	   struct isc_session_cfg *cfg,
	   enum isc_session_state initial_state);
Per Cederqvist's avatar
Per Cederqvist committed
207

208
extern void
209
isc_insert(struct isc_mcb *mcb, struct isc_scb_internal *scb);
Per Cederqvist's avatar
Per Cederqvist committed
210

211
extern int
212
isc_remove(struct isc_mcb *mcb, struct isc_scb_internal *scb);
Per Cederqvist's avatar
Per Cederqvist committed
213

214
215
216
/* isc_socket.c */
extern union isc_address *
isc_copyaddress(union isc_address *addr);
Per Cederqvist's avatar
Per Cederqvist committed
217

218
219
extern oop_call_time isc_dns_resolve_cb;

220
/* isc_tcp.c */
221
extern struct isc_scb_internal *
222
223
224
225
226
227
isc_createtcp(struct isc_mcb *mcb,
	      struct isc_session_cfg *cfg,
	      int fd,
	      enum isc_session_state initial_state);

extern int
228
229
isc_bindtcp(struct isc_scb_internal *scb,
	    const char *address, const char *port);
230
231

/* isc_message.c. */
Per Cederqvist's avatar
Per Cederqvist committed
232
/*
233
** Allocate a new struct isc_msg of specified size
Per Cederqvist's avatar
Per Cederqvist committed
234
*/
235
236
extern struct isc_msg *
isc_allocmsg(size_t  size);
Per Cederqvist's avatar
Per Cederqvist committed
237
238


239
#if 0
Per Cederqvist's avatar
Per Cederqvist committed
240
241
242
243
244
/*
** Setup a function to handle fatal abort requests
*/
extern void
isc_setabortfn(void (*abortfn)(const char  * msg));
245
#endif
Per Cederqvist's avatar
Per Cederqvist committed
246
247


248
249
/*
 * Move a file descriptor FD the first unused file descriptor higher
Per Cederqvist's avatar
Per Cederqvist committed
250
 * than or equal to LIMIT.  Return the new file descriptor.  Close FD.
251
252
253
254
255
256
257
 *
 * On failure, the old FD will be returned, and errno will be set.
 *
 * Do nothing (and return FD) if LIMIT is 0.
 */
extern int
isc_relocate_fd(int fd, int limit);
Per Cederqvist's avatar
Per Cederqvist committed
258

259
extern void
260
isc_check_read_callback(struct isc_scb_internal *session, int any_written);
Per Cederqvist's avatar
Per Cederqvist committed
261

262
extern void
263
isc_cancel_read_callback(struct isc_scb_internal *session);
Per Cederqvist's avatar
Per Cederqvist committed
264

265
extern void
266
isc_cancel_write_callback(struct isc_scb_internal *session);
Per Cederqvist's avatar
Per Cederqvist committed
267

268
#define ISC_XNEW(var)   (var = isc_malloc(sizeof(*var)))
Per Cederqvist's avatar
Per Cederqvist committed
269
	
270
#if !HAVE_INET_ATON
271
struct in_addr;
272
273
int inet_aton (const char *, struct in_addr *);
#endif
Per Cederqvist's avatar
Per Cederqvist committed
274

275
#if !HAVE_SOCKLEN_T
276
277
278
/* The configure script attempts to find the appropriate socklen_t
   type to use.  This can be size_t, unsigned long or unsigned int. */
typedef SOCKLEN_TYPE socklen_t;
279
280
#endif

Per Cederqvist's avatar
Per Cederqvist committed
281
282
283
284
285
286
287
288
289
290
291
292
#if !HAVE_UINT32_T
#  if SIZEOF_INT == 4

typedef unsigned int uint32_t;

#  elif SIZEOF_LONG == 4

typedef unsigned long uint32_t;

#  else
#    error Cannot find a 32-bit type on this platform
#  endif
Per Cederqvist's avatar
Per Cederqvist committed
293
#endif
Per Cederqvist's avatar
Per Cederqvist committed
294

295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314

/* IPv4/IPv6 compatibility macros:
 *
 * These are used as wrappers around IPv4/IPv6 specific socket
 * handling code. When USE_INET6 is not defined, IPv6 specific
 * parts will not be compiled.
 *
 * CHOOSE_IP4OR6:    Takes 3 args:
 *                     evaluates to second arg if first arg (of type
 *                     enum sockaddrs) contains an AF_INET address or
 *                     IPv6 is not used, otherwise to third arg.
 * FOR_EACH_AF:      Takes 2 args; first is an int lvalue, second
 *                     an int expression. For each available address
 *                     family (from most preferred to last preferred),
 *                     the second arg is evaluated with the address
 *                     family assigned to the first arg, until it
 *                     evaluates to false (in which case the entire
 *                     macro expansion evaluates to false), or until
 *                     there are no more address families, in which
 *                     case the entire macro expansion evaluates to
315
 *                     true.
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
 * FOR_EACH_PF:      As FOR_EACH_AF, but for protocol families
 *                     instead of address families.
 * STORE_ADDR:         First arg (enum sockaddrs) is checked for
 *                     address family (if several address families
 *                     are supported), and its address part
 *                     assigned the value of the second arg
 *                     (of type in_addr) for IPv4, otherwise
 *                     the third arg (of type in6_addr).
 */

#ifdef USE_INET6

#define CHOOSE_IP4OR6(saddrs, ifip4, ifip6) \
 ((((saddrs).sa.sa_family) == AF_INET) ? (ifip4) : (ifip6))
#define FOR_EACH_AF(af,expr) \
 (((af)=AF_INET6, (expr)) && ((af)=AF_INET, (expr)))
#define FOR_EACH_PF(pf,expr) \
 (((pf)=PF_INET6, (expr)) && ((pf)=PF_INET, (expr)))
#define STORE_ADDR(addr, IPv4addr, IPv6addr) \
 CHOOSE_IP4OR6((addr), \
   (addr).sa_in.sin_addr.s_addr = (IPv4addr), \
   (memcpy(&(addr).sa_in6.sin6_addr, \
	   &(IPv6addr), \
	   sizeof((addr).sa_in6.sin6_addr)), 0))

#else

#define CHOOSE_IP4OR6(saddrs, ifip4, ifip6) (ifip4)
#define FOR_EACH_AF(af,expr) ((af)=AF_INET, (expr))
#define FOR_EACH_PF(pf,expr) ((pf)=PF_INET, (expr))
#define STORE_ADDR(addr, IPv4addr, IPv6addr) \
 ((addr).sa_in.sin_addr.s_addr = (IPv4addr));

#endif /* USE_INET6 */

Per Cederqvist's avatar
Per Cederqvist committed
351
#endif