isc_udp.c 7.67 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1
2
3
/*
** isc_udp.c                          Routines to handle UDP ISC sessions
**
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) 1992, 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
**
** history:
** 920209 pen      code extracted from isc_session.c
Per Cederqvist's avatar
Per Cederqvist committed
24
** (See ChangeLog for recent history)
Per Cederqvist's avatar
Per Cederqvist committed
25
26
27
28
*/

#include <sys/types.h>
#include <errno.h>
29
30
31
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
32
33
34
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
35
36
37
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
38
39
40
41
42
#include <ctype.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include <sys/ioctl.h>
43
#include <sys/socket.h>
Per Cederqvist's avatar
Per Cederqvist committed
44
#include <netdb.h>
45
46
47
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
48
#include <fcntl.h>
Per Cederqvist's avatar
Per Cederqvist committed
49
50
51
#ifdef HAVE_UNISTD_H
#  include <unistd.h>
#endif
52
53
54
#ifndef NULL
#  include <stdio.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
55
#include <time.h>
Per Cederqvist's avatar
Per Cederqvist committed
56
57
58

#include "isc.h"
#include "intern.h"
Per Cederqvist's avatar
Per Cederqvist committed
59
#include "unused.h"
Per Cederqvist's avatar
Per Cederqvist committed
60
61
62
63
64
65



/*
** Receive an UDP message and put it into an IscMessage
*/
Per Cederqvist's avatar
Per Cederqvist committed
66
67
68
static IscMessage *
isc_udp_read_fn(IscHandlerList *UNUSED(hl),
		IscSession *scb)
Per Cederqvist's avatar
Per Cederqvist committed
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
{
  IscMessage *msg;
  struct sockaddr addr;
  int len;
  int status;
  
  
  msg = isc_allocmsg(scb->cfg->max.msgsize);
  
  len = sizeof(addr);
  
  status = recvfrom(scb->fd, msg->buffer, msg->size,
		    0,
		    &addr, &len);
  if (status < 0)
  {
    isc_freemsg(msg);
    return NULL;
  }

  msg->length = status;
  msg->buffer[msg->length] = '\0';
  msg->address = isc_mkipaddress(&addr);
  
  return msg;
}



/*
** Transmit an UDP message to a remote service
*/
Per Cederqvist's avatar
Per Cederqvist committed
101
102
103
104
static int
isc_udp_write_fn(IscHandlerList *hl,
		 IscSession *scb,
		 IscMessage *msg)
Per Cederqvist's avatar
Per Cederqvist committed
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
{
  /* No target and no default target? Then fail. */
  if (!msg->address && !scb->info.udp.raddr)
  {
    errno = ENOTCONN;
    return -1;
  }

  /* Not target specified? Use default target */
  if (!msg->address)
    return ISC_HCALLFUN2(hl, write, scb, msg);

  return sendto(scb->fd, msg->buffer, msg->length,
		0,
		&msg->address->ip.saddr,
		sizeof(msg->address->ip.saddr));
}



Per Cederqvist's avatar
Per Cederqvist committed
125
126
static void
isc_udp_destroy_fn(IscHandlerList *UNUSED(hl),
Per Cederqvist's avatar
Per Cederqvist committed
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
		   IscSession *scb)
{
  if (scb->info.udp.raddr)
  {
    isc_freeaddress(scb->info.udp.raddr);
    scb->info.udp.raddr = NULL;
  }
  
  if (scb->info.udp.laddr)
  {
    isc_freeaddress(scb->info.udp.laddr);
    scb->info.udp.laddr = NULL;
  }
}



/*
** Create an UDP Session Address
*/
Per Cederqvist's avatar
Per Cederqvist committed
147
static IscAddress *
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
isc_mkudpaddress(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 (strcmp(address, "*") == 0)
  {
    /* Broadcast address */
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = htonl(INADDR_BROADCAST);
  }
Per Cederqvist's avatar
Per Cederqvist committed
170
  else if (isdigit((int)(unsigned char)address[0]))
Per Cederqvist's avatar
Per Cederqvist committed
171
172
173
  {
    addr.sin_family = AF_INET;
    addr.sin_addr.s_addr = inet_addr(address);
174
    if (addr.sin_addr.s_addr == INADDR_NONE)
Per Cederqvist's avatar
Per Cederqvist committed
175
176
177
178
179
180
181
182
183
184
      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));
  }
  
Per Cederqvist's avatar
Per Cederqvist committed
185
  if (isdigit((int)(unsigned char)service[0]))
Per Cederqvist's avatar
Per Cederqvist committed
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
    addr.sin_port = htons(atoi(service));
  else if ((sp = getservbyname(service, "udp")) == NULL)
    return NULL;
  else
    addr.sin_port = sp->s_port;

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


		  
static IscHandler isc_udp_funs =
{
  &isc_udp_read_fn,
  &isc_udp_write_fn,
  &isc_default_close_fn,
  NULL,
  NULL,
  &isc_udp_destroy_fn,
  NULL
};



/*
** Create an UDP session
*/
IscSession *
isc_createudp(IscSessionConfig *cfg)
{
  IscSession *scb;
  int fd, res;
Per Cederqvist's avatar
Per Cederqvist committed
218
219
  int flag;
  struct linger ling;
Per Cederqvist's avatar
Per Cederqvist committed
220
221
222
  
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    return NULL;
223
224

  fd = isc_relocate_fd(fd, cfg->fd_relocate);
Per Cederqvist's avatar
Per Cederqvist committed
225
226
227
228
229
230
231
232
  
  /* Set non blocking write mode */
  if ((res = fcntl(fd, F_GETFL, 0)) == -1)
  {
    close(fd);
    return NULL;
  }
    
Per Cederqvist's avatar
Per Cederqvist committed
233
234
235
236
  /* If compilation fails on the next line, please report it as a bug
     to ceder@lysator.liu.se.  I'd like to talk to you so that you can
     test an autoconf solution to this problem.  As a workaround, you
     can change "O_NONBLOCK" to "FNDELAY". */
237
  if (fcntl(fd, F_SETFL, res | O_NONBLOCK) == -1)
Per Cederqvist's avatar
Per Cederqvist committed
238
239
240
241
242
  {
    close(fd);
    return NULL;
  }
  
Per Cederqvist's avatar
Per Cederqvist committed
243
244
245
#if 0
  /* The setsockopt calls below used to look like this, but this style is
     apparently now obsolete.  It doesn't work on some Linux boxes. */
Per Cederqvist's avatar
Per Cederqvist committed
246
247
  setsockopt(fd, SOL_SOCKET, SO_DONTLINGER, 0, 0); 
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 0, 0);
Per Cederqvist's avatar
Per Cederqvist committed
248
249
250
251
252
253
254
255
#else
  /* This is the modern way to turn off linger and turn on reuseaddr. */
  ling.l_onoff = 0;
  ling.l_linger = 0;
  setsockopt(fd, SOL_SOCKET, SO_LINGER, &ling, sizeof(ling)); 
  flag = 1;
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, &flag, sizeof(flag));
#endif
Per Cederqvist's avatar
Per Cederqvist committed
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
  
  scb = isc_create(cfg, &isc_udp_funs);
  if (!scb)
    return NULL;

  scb->type = ISC_TYPE_UDP;
  scb->fd = fd;

  scb->info.udp.raddr = NULL;
  scb->info.udp.laddr = NULL;

  return scb;
}


	      



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


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

  scb->state = ISC_STATE_RUNNING;
  scb->info.udp.laddr = ia;

  return 0;
}


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

  
  if (scb->type != ISC_TYPE_UDP)
    return -1;

  if (address == NULL)
    address = "localhost";
  
  ia = isc_mkudpaddress(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.udp.raddr = ia;
  if (scb->info.udp.laddr == NULL)
    scb->info.udp.laddr = isc_getladdress(scb);
  
  return 0;
}



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

  
  scb = isc_createudp(&mcb->scfg);
  if (!scb)
    return NULL;

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

  (void) isc_insert(mcb, scb);

  return scb;
}



/*
** Create an UDP session, prepare it for incoming messages
** and insert it into the master structure
*/
IscSession *
isc_listenudp(IscMaster *mcb,
	      const char *address,
	      const char *service)
{
  IscSession *scb;


  scb = isc_createudp(&mcb->scfg);
  if (!scb)
    return NULL;

  if (isc_bindudp(scb, address, service) < 0)
  {
    isc_destroy(NULL, scb);
    return NULL;
  }
  
  (void) isc_insert(mcb, scb);
  
  return scb;
}