isc_tcp.c 6.16 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
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
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
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
/*
** isc_tcp.c                          Routines to handle TCP ISC sessions
**
** Copyright (c) 1992 Peter Eriksson and Per Cederqvist of the
**                    Lysator Academic Computer Association.
**
** history:
** 920209 pen      code extracted from isc_session.c
*/

#include <errno.h>
#include <stdlib.h>
#include <stddef.h>
#include <ctype.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include <sys/ioctl.h>
#include <netdb.h>
#include <string.h>
#include <fcntl.h>

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



/*
** External function declarations
*/
extern void *memset(void *, int, int);
extern void *memcpy(void *, const void *, int);
extern int socket(int, int, int);
extern int setsockopt(int, int, int, char *, int);
extern int listen(int, int);
extern int connect(int, struct sockaddr *, int);
extern int bind(int, struct sockaddr *, int);
extern int close(int);
extern int atoi(const char *str);
extern time_t sleep(time_t);
extern int accept(int fd, struct sockaddr *sock, int *len);



IscSession *
isc_tcp_accept_fn(IscHandlerList *hl,
		  IscSession *scb,
		  IscMessage *msg)
{
  IscSession *new_scb;
  int fd;
  

  if ((fd = accept(scb->fd,
		   (struct sockaddr *) NULL,
		   (int *) NULL)) < 0)
    return NULL;
  
  new_scb = isc_createtcp(scb->cfg, fd);
  if (!new_scb)
  {
    close(fd);
    return NULL;
  }
  
  /* Fill in the session info structure */
  new_scb->fd    = fd;
  new_scb->state = ISC_STATE_RUNNING;
  new_scb->mode  = O_RDWR;
  new_scb->info.tcp.raddr = isc_getraddress(new_scb);
  new_scb->info.tcp.laddr = isc_getladdress(new_scb);
  
  return new_scb;
}





void
isc_tcp_destroy_fn(IscHandlerList *hl,
		   IscSession *scb)
{
  if (scb->info.tcp.raddr)
  {
    isc_freeaddress(scb->info.tcp.raddr);
    scb->info.tcp.raddr = NULL;
  }
  
  if (scb->info.tcp.laddr)
  {
    isc_freeaddress(scb->info.tcp.laddr);
    scb->info.tcp.laddr = NULL;
  }
}



/*
** TCP I/O functions structure
*/
static IscHandler isc_tcp_funs =
{
  &isc_default_read_fn,
  &isc_default_write_fn,
  &isc_default_close_fn,
  NULL,
  &isc_tcp_accept_fn,
  &isc_tcp_destroy_fn,
  NULL
};



/*
** Create a TCP Session Address
*/
IscAddress *
isc_mktcpaddress(const char *address,
		 const char *service)
{
  struct sockaddr_in addr;
  struct hostent *hp;
  struct servent *sp;


  memset(&addr, 0, sizeof(addr));
  
  /* Any local address? */
  if (address == NULL)
  {
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_ANY);
  }
  else if (isdigit(address[0]))
  {
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(address);
    if (addr.sin_addr.s_addr == -1)
      return NULL;
  }
  else if ((hp = gethostbyname(address)) == NULL)
    return NULL;
  else
  {
    addr.sin_family = hp->h_addrtype;
    memcpy(&addr.sin_addr, hp->h_addr, sizeof(addr.sin_addr));
  }
  
  if (isdigit(service[0]))
    addr.sin_port = htons(atoi(service));
  else if ((sp = getservbyname(service, "tcp")) == NULL)
    return NULL;
  else
    addr.sin_port = sp->s_port;

  return isc_mkipaddress((struct sockaddr *)&addr);
}


		  
/*
** Create a TCP session
*/
IscSession *
isc_createtcp(IscSessionConfig *cfg, int fd)
{
  IscSession *scb;
  int res;
  

  if (fd == -1)
    if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
      return NULL;
  
  /* Set non blocking write mode */
  if ((res = fcntl(fd, F_GETFL, 0)) == -1)
  {
    close(fd);
    return NULL;
  }
    
  if (fcntl(fd, F_SETFL, res | FNDELAY) == -1)
  {
    close(fd);
    return NULL;
  }
  
  setsockopt(fd, SOL_SOCKET, SO_DONTLINGER, 0, 0); 
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 0, 0);
  
  scb = isc_create(cfg, &isc_tcp_funs);
  if (!scb)
    return NULL;

  scb->type = ISC_TYPE_TCP;
  scb->fd = fd;
  
  scb->info.tcp.raddr = NULL;
  scb->info.tcp.laddr = NULL;
    
  return scb;
}



/*
** Bind a TCP session to a local port and address
*/
int
isc_bindtcp(IscSession *scb,
	    const char *address,
	    const char *service)
{
  IscAddress *ia;


  if (scb->type != ISC_TYPE_TCP)
    return -1;
  
  ia = isc_mktcpaddress(address, service);
			
  if (!ia)
    return -1;
  
  if (bind(scb->fd, &ia->ip.saddr, sizeof(ia->ip.saddr)) < 0)
    return -1;

  scb->info.tcp.laddr = ia;
    
  return 0;
}


  
/*
** Connect a TCP session to a remove service.
*/
int
isc_connecttcp(IscSession *scb,
	       const char *address,
	       const char *service)
{
  IscAddress *ia;
  int res;

  
  if (scb->type != ISC_TYPE_TCP)
    return -1;
  
  /* Local connection? */
  if (address == NULL)
    address = "localhost";

  ia = isc_mktcpaddress(address, service);

  if (!ia)
    return -1;
  
  res = connect(scb->fd, &ia->ip.saddr, sizeof(ia->ip.saddr));
  if (res < 0 && errno != EINPROGRESS)
    return -1;

  scb->state = (res < 0 ? ISC_STATE_CONNECTING : ISC_STATE_RUNNING);
  
  scb->info.tcp.raddr = ia;
  if (scb->info.tcp.laddr == NULL)
    scb->info.tcp.laddr = isc_getladdress(scb);
  
  return 0;
}



/*
** Establish a session with a remote TCP service, then insert it
** into a master control structure-
*/
IscSession *
isc_opentcp(IscMaster *mcb,
	    const char *address,
	    const char *service)
{
  IscSession *scb;

  
  scb = isc_createtcp(&mcb->scfg, -1);
  if (!scb)
    return NULL;

  if (isc_connecttcp(scb, address, service) < 0)
  {
    isc_destroy(NULL, scb);
    return NULL;
  }

  (void) isc_insert(mcb, scb);

  return scb;
}



/*
** Establish a port to listen at for new TCP connections
*/
IscSession *
isc_listentcp(IscMaster  *mcb,
	      const char *address,
	      const char *service)
{
  IscSession *scb;
  int retries;
  int errcode;
  

  scb = isc_createtcp(&mcb->scfg, -1);
  if (!scb)
    return NULL;

  errno = 0;
  for (retries = 0; retries < scb->cfg->max.openretries; sleep(1), retries++)
    if (isc_bindtcp(scb, address, service) >= 0 || errno != EADDRINUSE)
      break;

  if (retries >= scb->cfg->max.openretries || errno != 0)
  {
    errcode = errno;
    isc_destroy(NULL, scb);
    errno = errcode;
    return NULL;
  }

  if (listen(scb->fd, scb->cfg->max.backlog) < 0)
  {
    errcode = errno;
    isc_destroy(NULL, scb);
    errno = errcode;
    return NULL;
  }

  scb->state = ISC_STATE_LISTENING;

  (void) isc_insert(mcb, scb);
  
  return scb;
}