isc_udp.c 6.71 KB
Newer Older
Per Cederqvist's avatar
Per Cederqvist committed
1 2 3 4 5 6 7 8 9 10 11 12
/*
** isc_udp.c                          Routines to handle UDP 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 <sys/types.h>
#include <errno.h>
13 14 15
#ifdef HAVE_STDLIB_H
#  include <stdlib.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
16 17 18
#ifdef HAVE_STDDEF_H
#  include <stddef.h>
#endif
19 20 21
#ifdef HAVE_STDARG_H
#  include <stdarg.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
22 23 24 25 26
#include <ctype.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <sys/file.h>
#include <sys/ioctl.h>
27
#include <sys/socket.h>
Per Cederqvist's avatar
Per Cederqvist committed
28
#include <netdb.h>
29 30 31
#ifdef HAVE_STRING_H
#  include <string.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
32
#include <fcntl.h>
33 34 35
#ifndef NULL
#  include <stdio.h>
#endif
Per Cederqvist's avatar
Per Cederqvist committed
36 37 38 39 40 41

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



42 43
/*extern void *memset(void *, int, int);*/
/*extern void *memcpy(void *, const void *, int);*/
Per Cederqvist's avatar
Per Cederqvist committed
44
extern int socket(int, int, int);
45
/*extern int setsockopt(int, int, int, char *, int);*/
46 47
/*extern int connect(int, struct sockaddr *, int);*/
/*extern int bind(int, struct sockaddr *, int);*/
Per Cederqvist's avatar
Per Cederqvist committed
48
extern int close(int);
49 50 51 52 53
/*extern int atoi(const char *str);*/
/*extern int recvfrom(int fd, char *buf, int len, int flags,
		    struct sockaddr *from, int *flen);*/
/*extern int sendto(int fd, char *buf, int len, int flags,
		  struct sockaddr *from, int flen);*/
Per Cederqvist's avatar
Per Cederqvist committed
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
extern int sprintf(char *, const char *, ...);



/*
** Receive an UDP message and put it into an IscMessage
*/
IscMessage *isc_udp_read_fn(IscHandlerList *hl,
			    IscSession *scb)
{
  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
*/
int isc_udp_write_fn(IscHandlerList *hl,
		     IscSession *scb,
		     IscMessage *msg)
{
  /* 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));
}



void
isc_udp_destroy_fn(IscHandlerList *hl,
		   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
*/
IscAddress *
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);
  }
  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, "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;
  
  
  if ((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0)
    return NULL;
  
  /* Set non blocking write mode */
  if ((res = fcntl(fd, F_GETFL, 0)) == -1)
  {
    close(fd);
    return NULL;
  }
    
223
#ifdef FNDELAY
Per Cederqvist's avatar
Per Cederqvist committed
224
  if (fcntl(fd, F_SETFL, res | FNDELAY) == -1)
225 226 227
#else
  if (fcntl(fd, F_SETFL, res | O_NONBLOCK) == -1)
#endif      
Per Cederqvist's avatar
Per Cederqvist committed
228 229 230 231 232
  {
    close(fd);
    return NULL;
  }
  
233
#ifdef SO_DONTLINGER
Per Cederqvist's avatar
Per Cederqvist committed
234
  setsockopt(fd, SOL_SOCKET, SO_DONTLINGER, 0, 0); 
235
#endif
Per Cederqvist's avatar
Per Cederqvist committed
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 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
  setsockopt(fd, SOL_SOCKET, SO_REUSEADDR, 0, 0);
  
  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;
}