home *** CD-ROM | disk | FTP | other *** search
/ HAM Radio 1 / HamRadio.cdr / misc / src0131 / udp.c < prev    next >
C/C++ Source or Header  |  1991-01-26  |  7KB  |  286 lines

  1. /* Internet User Data Protocol (UDP)
  2.  * Copyright 1991 Phil Karn, KA9Q
  3.  */
  4. #include "global.h"
  5. #include "mbuf.h"
  6. #include "netuser.h"
  7. #include "iface.h"
  8. #include "udp.h"
  9. #include "ip.h"
  10. #include "internet.h"
  11. #include "icmp.h"
  12.  
  13. static struct udp_cb *lookup_udp __ARGS((struct socket *socket));
  14. static int16 hash_udp __ARGS((struct socket *socket));
  15.  
  16. struct mib_entry Udp_mib[] = {
  17.     "",            0,
  18.     "udpInDatagrams",    0,
  19.     "udpNoPorts",        0,
  20.     "udpInErrors",        0,
  21.     "udpOutDatagrams",    0,
  22. };
  23.  
  24. /* Hash table for UDP structures */
  25. struct udp_cb *Udps[NUDP] = { NULLUDP} ;
  26.  
  27. /* Create a UDP control block for lsocket, so that we can queue
  28.  * incoming datagrams.
  29.  */
  30. struct udp_cb *
  31. open_udp(lsocket,r_upcall)
  32. struct socket *lsocket;
  33. void (*r_upcall)();
  34. {
  35.     register struct udp_cb *up;
  36.     int16 hval; 
  37.  
  38.     if((up = lookup_udp(lsocket)) != NULLUDP)
  39.         return up;    /* Already exists */
  40.     up = (struct udp_cb *)callocw(1,sizeof (struct udp_cb));
  41.     up->socket.address = lsocket->address;
  42.     up->socket.port = lsocket->port;
  43.     up->r_upcall = r_upcall;
  44.  
  45.     hval = hash_udp(lsocket);
  46.     up->next = Udps[hval];
  47.     if(up->next != NULLUDP)
  48.         up->next->prev = up;
  49.     Udps[hval] = up;
  50.     return up;
  51. }
  52.  
  53. /* Send a UDP datagram */
  54. int
  55. send_udp(lsocket,fsocket,tos,ttl,data,length,id,df)
  56. struct socket *lsocket;        /* Source socket */
  57. struct socket *fsocket;        /* Destination socket */
  58. char tos;            /* Type-of-service for IP */
  59. char ttl;            /* Time-to-live for IP */
  60. struct mbuf *data;        /* Data field, if any */
  61. int16 length;            /* Length of data field */
  62. int16 id;            /* Optional ID field for IP */
  63. char df;            /* Don't Fragment flag for IP */
  64. {
  65.     struct mbuf *bp;
  66.     struct pseudo_header ph;
  67.     struct udp udp;
  68.     int32 laddr;
  69.  
  70.     if(length != 0 && data != NULLBUF)
  71.         trim_mbuf(&data,length);
  72.     else
  73.         length = len_p(data);
  74.  
  75.     length += UDPHDR;
  76.  
  77.     laddr = lsocket->address;
  78.     if(laddr == INADDR_ANY)
  79.         laddr = locaddr(fsocket->address);
  80.  
  81.     udp.source = lsocket->port;
  82.     udp.dest = fsocket->port;
  83.     udp.length = length;
  84.  
  85.     /* Create IP pseudo-header, compute checksum and send it */
  86.     ph.length = length;
  87.     ph.source = laddr;
  88.     ph.dest = fsocket->address;
  89.     ph.protocol = UDP_PTCL;
  90.  
  91.     if((bp = htonudp(&udp,data,&ph)) == NULLBUF){
  92.         Net_error = NO_MEM;
  93.         free_p(data);
  94.         return 0;
  95.     }
  96.     udpOutDatagrams++;
  97.     ip_send(laddr,fsocket->address,UDP_PTCL,tos,ttl,bp,length,id,df);
  98.     return (int)length;
  99. }
  100. /* Accept a waiting datagram, if available. Returns length of datagram */
  101. int
  102. recv_udp(up,fsocket,bp)
  103. register struct udp_cb *up;
  104. struct socket *fsocket;        /* Place to stash incoming socket */
  105. struct mbuf **bp;        /* Place to stash data packet */
  106. {
  107.     struct socket sp;
  108.     struct mbuf *buf;
  109.     int16 length;
  110.  
  111.     if(up == NULLUDP){
  112.         Net_error = NO_CONN;
  113.         return -1;
  114.     }
  115.     if(up->rcvcnt == 0){
  116.         Net_error = WOULDBLK;
  117.         return -1;
  118.     }
  119.     buf = dequeue(&up->rcvq);
  120.     up->rcvcnt--;
  121.  
  122.     /* Strip socket header */
  123.     pullup(&buf,(char *)&sp,sizeof(struct socket));
  124.  
  125.     /* Fill in the user's foreign socket structure, if given */
  126.     if(fsocket != NULLSOCK){
  127.         fsocket->address = sp.address;
  128.         fsocket->port = sp.port;
  129.     }
  130.     /* Hand data to user */
  131.     length = len_p(buf);
  132.     if(bp != NULLBUFP)
  133.         *bp = buf;
  134.     else
  135.         free_p(buf);
  136.     return (int)length;
  137. }
  138. /* Delete a UDP control block */
  139. int
  140. del_udp(up)
  141. struct udp_cb *up;
  142. {
  143.     struct mbuf *bp;
  144.     int16 hval;
  145.  
  146.     if(up == NULLUDP){
  147.         Net_error = INVALID;
  148.         return -1;
  149.     }        
  150.     /* Get rid of any pending packets */
  151.     while(up->rcvcnt != 0){
  152.         bp = up->rcvq;
  153.         up->rcvq = up->rcvq->anext;
  154.         free_p(bp);
  155.         up->rcvcnt--;
  156.     }
  157.     hval = hash_udp(&up->socket);
  158.     if(up->prev == NULLUDP)
  159.         Udps[hval] = up->next;        /* First on list */
  160.     else
  161.         up->prev->next = up->next;
  162.     if(up->next != NULLUDP)
  163.         up->next->prev = up->prev;
  164.  
  165.     free((char *)up);
  166.     return 0;
  167. }
  168. /* Process an incoming UDP datagram */
  169. void
  170. udp_input(iface,ip,bp,rxbroadcast)
  171. struct iface *iface;    /* Input interface */
  172. struct ip *ip;        /* IP header */
  173. struct mbuf *bp;    /* UDP header and data */
  174. int rxbroadcast;    /* The only protocol that accepts 'em */
  175. {
  176.     struct pseudo_header ph;
  177.     struct udp udp;
  178.     struct udp_cb *up;
  179.     struct socket lsocket;
  180.     struct socket fsocket;
  181.     struct mbuf *packet;
  182.     int ckfail = 0;
  183.     int16 length;
  184.  
  185.     if(bp == NULLBUF)
  186.         return;
  187.  
  188.     /* Create pseudo-header and verify checksum */
  189.     ph.source = ip->source;
  190.     ph.dest = ip->dest;
  191.     ph.protocol = ip->protocol;
  192.     length = ip->length - IPLEN - ip->optlen;
  193.     ph.length = length;
  194.  
  195.     if(cksum(&ph,bp,length) != 0)
  196.         /* Checksum apparently failed, note for later */
  197.         ckfail++;
  198.  
  199.     /* Extract UDP header in host order */
  200.     ntohudp(&udp,&bp);
  201.  
  202.     /* If the checksum field is zero, then ignore a checksum error.
  203.      * I think this is dangerously wrong, but it is in the spec.
  204.      */
  205.     if(ckfail && udp.checksum != 0){
  206.         udpInErrors++;
  207.         free_p(bp);
  208.         return;
  209.     }
  210.     /* If this was a broadcast packet, pretend it was sent to us */
  211.     if(rxbroadcast){
  212.         lsocket.address = iface->addr;
  213.     } else
  214.         lsocket.address = ip->dest;
  215.  
  216.     lsocket.port = udp.dest;
  217.     /* See if there's somebody around to read it */
  218.     if((up = lookup_udp(&lsocket)) == NULLUDP){
  219.         /* Nope, return an ICMP message */
  220.         if(!rxbroadcast){
  221.             bp = htonudp(&udp,bp,&ph);
  222.             icmp_output(ip,bp,ICMP_DEST_UNREACH,ICMP_PORT_UNREACH,NULL);
  223.         }
  224.         udpNoPorts++;
  225.         free_p(bp);
  226.         return;
  227.     }
  228.     /* Create space for the foreign socket info */
  229.     if((packet = pushdown(bp,sizeof(fsocket))) == NULLBUF){
  230.         /* No space, drop whole packet */
  231.         free_p(bp);
  232.         udpInErrors++;
  233.         return;
  234.     }
  235.     fsocket.address = ip->source;
  236.     fsocket.port = udp.source;
  237.     memcpy(&packet->data[0],(char *)&fsocket,sizeof(fsocket));
  238.  
  239.     /* Queue it */
  240.     enqueue(&up->rcvq,packet);
  241.     up->rcvcnt++;
  242.     udpInDatagrams++;
  243.     if(up->r_upcall)
  244.         (*up->r_upcall)(iface,up,up->rcvcnt);
  245. }
  246. /* Look up UDP socket, return control block pointer or NULLUDP if nonexistant */
  247. static
  248. struct udp_cb *
  249. lookup_udp(socket)
  250. struct socket *socket;
  251. {
  252.     register struct udp_cb *up;
  253.     
  254.     for(up = Udps[hash_udp(socket)];up != NULLUDP;up = up->next){
  255.         if(socket->port == up->socket.port
  256.          && (socket->address == up->socket.address
  257.              || up->socket.address == INADDR_ANY))
  258.             break;
  259.     }
  260.     return up;
  261. }
  262.  
  263. /* Hash a UDP socket (address and port) structure */
  264. static
  265. int16
  266. hash_udp(socket)
  267. struct socket *socket;
  268. {
  269.     /* Hash depends only on port number, to make addr wildcarding work */
  270.     return (int16)(socket->port % NUDP);
  271. }
  272.  
  273. void udp_garbage(red)
  274. int red;
  275. {
  276.     int i;
  277.     struct udp_cb *udp;
  278.  
  279.     for(i=0;i<NUDP;i++){
  280.         for(udp = Udps[i];udp != NULLUDP; udp = udp->next){
  281.             mbuf_crunch(&udp->rcvq);
  282.         }
  283.     }            
  284. }
  285.  
  286.