home *** CD-ROM | disk | FTP | other *** search
/ Media Share 9 / MEDIASHARE_09.ISO / hamradio / s920603.zip / UDP.C < prev    next >
C/C++ Source or Header  |  1992-04-08  |  7KB  |  279 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.  
  15. struct mib_entry Udp_mib[] = {
  16.     "",            0,
  17.     "udpInDatagrams",    0,
  18.     "udpNoPorts",        0,
  19.     "udpInErrors",        0,
  20.     "udpOutDatagrams",    0,
  21. };
  22.  
  23. /* UDP control structures list */
  24. struct udp_cb *Udps;
  25.  
  26. /* Create a UDP control block for lsocket, so that we can queue
  27.  * incoming datagrams.
  28.  */
  29. struct udp_cb *
  30. open_udp(lsocket,r_upcall)
  31. struct socket *lsocket;
  32. void (*r_upcall)();
  33. {
  34.     register struct udp_cb *up;
  35.  
  36.     if((up = lookup_udp(lsocket)) != NULLUDP){
  37.         /* Already exists */
  38.         Net_error = CON_EXISTS;
  39.         return NULLUDP;
  40.     }
  41.     up = (struct udp_cb *)callocw(1,sizeof (struct udp_cb));
  42.     up->socket.address = lsocket->address;
  43.     up->socket.port = lsocket->port;
  44.     up->r_upcall = r_upcall;
  45.  
  46.     up->next = Udps;
  47.     Udps = up;
  48.     return up;
  49. }
  50.  
  51. /* Send a UDP datagram */
  52. int
  53. send_udp(lsocket,fsocket,tos,ttl,bp,length,id,df)
  54. struct socket *lsocket;        /* Source socket */
  55. struct socket *fsocket;        /* Destination socket */
  56. char tos;            /* Type-of-service for IP */
  57. char ttl;            /* Time-to-live for IP */
  58. struct mbuf *bp;        /* Data field, if any */
  59. int16 length;            /* Length of data field */
  60. int16 id;            /* Optional ID field for IP */
  61. char df;            /* Don't Fragment flag for IP */
  62. {
  63.     struct pseudo_header ph;
  64.     struct udp udp;
  65.     int32 laddr;
  66.  
  67.     if(length != 0 && bp != NULLBUF)
  68.         trim_mbuf(&bp,length);
  69.     else
  70.         length = len_p(bp);
  71.  
  72.     length += UDPHDR;
  73.  
  74.     laddr = lsocket->address;
  75.     if(laddr == INADDR_ANY)
  76.         laddr = locaddr(fsocket->address);
  77.  
  78.     udp.source = lsocket->port;
  79.     udp.dest = fsocket->port;
  80.     udp.length = length;
  81.  
  82.     /* Create IP pseudo-header, compute checksum and send it */
  83.     ph.length = length;
  84.     ph.source = laddr;
  85.     ph.dest = fsocket->address;
  86.     ph.protocol = UDP_PTCL;
  87.  
  88.     bp = htonudp(&udp,bp,&ph);
  89.     udpOutDatagrams++;
  90.     ip_send(laddr,fsocket->address,UDP_PTCL,tos,ttl,bp,length,id,df);
  91.     return (int)length;
  92. }
  93. /* Accept a waiting datagram, if available. Returns length of datagram */
  94. int
  95. recv_udp(up,fsocket,bp)
  96. register struct udp_cb *up;
  97. struct socket *fsocket;        /* Place to stash incoming socket */
  98. struct mbuf **bp;        /* Place to stash data packet */
  99. {
  100.     struct socket sp;
  101.     struct mbuf *buf;
  102.     int16 length;
  103.  
  104.     if(up == NULLUDP){
  105.         Net_error = NO_CONN;
  106.         return -1;
  107.     }
  108.     if(up->rcvcnt == 0){
  109.         Net_error = WOULDBLK;
  110.         return -1;
  111.     }
  112.     buf = dequeue(&up->rcvq);
  113.     up->rcvcnt--;
  114.  
  115.     /* Strip socket header */
  116.     pullup(&buf,(char *)&sp,sizeof(struct socket));
  117.  
  118.     /* Fill in the user's foreign socket structure, if given */
  119.     if(fsocket != NULLSOCK){
  120.         fsocket->address = sp.address;
  121.         fsocket->port = sp.port;
  122.     }
  123.     /* Hand data to user */
  124.     length = len_p(buf);
  125.     if(bp != NULLBUFP)
  126.         *bp = buf;
  127.     else
  128.         free_p(buf);
  129.     return (int)length;
  130. }
  131. /* Delete a UDP control block */
  132. int
  133. del_udp(conn)
  134. struct udp_cb *conn;
  135. {
  136.     struct mbuf *bp;
  137.     register struct udp_cb *up;
  138.     struct udp_cb *udplast = NULLUDP;
  139.  
  140.     for(up = Udps;up != NULLUDP;udplast = up,up = up->next){
  141.         if(up == conn)
  142.             break;
  143.     }
  144.     if(up == NULLUDP){
  145.         /* Either conn was NULL or not found on list */
  146.         Net_error = INVALID;
  147.         return -1;
  148.     }
  149.     /* Get rid of any pending packets */
  150.     while(up->rcvcnt != 0){
  151.         bp = up->rcvq;
  152.         up->rcvq = up->rcvq->anext;
  153.         free_p(bp);
  154.         up->rcvcnt--;
  155.     }
  156.     /* Remove from list */
  157.     if(udplast != NULLUDP)
  158.         udplast->next = up->next;
  159.     else
  160.         Udps = up->next;    /* was first on list */
  161.  
  162.     free((char *)up);
  163.     return 0;
  164. }
  165. /* Process an incoming UDP datagram */
  166. void
  167. udp_input(iface,ip,bp,rxbroadcast)
  168. struct iface *iface;    /* Input interface */
  169. struct ip *ip;        /* IP header */
  170. struct mbuf *bp;    /* UDP header and data */
  171. int rxbroadcast;    /* The only protocol that accepts 'em */
  172. {
  173.     struct pseudo_header ph;
  174.     struct udp udp;
  175.     struct udp_cb *up;
  176.     struct socket lsocket;
  177.     struct socket fsocket;
  178.     int16 length;
  179.  
  180.     if(bp == NULLBUF)
  181.         return;
  182.  
  183.     /* Create pseudo-header and verify checksum */
  184.     ph.source = ip->source;
  185.     ph.dest = ip->dest;
  186.     ph.protocol = ip->protocol;
  187.     length = ip->length - IPLEN - ip->optlen;
  188.     ph.length = length;
  189.  
  190.     /* Peek at header checksum before we extract the header. This
  191.      * allows us to bypass cksum() if the checksum field was not
  192.      * set by the sender.
  193.      */
  194.     udp.checksum = udpcksum(bp);
  195.     if(udp.checksum != 0 && cksum(&ph,bp,length) != 0){
  196.         /* Checksum non-zero, and wrong */
  197.         udpInErrors++;
  198.         free_p(bp);
  199.         return;
  200.     }
  201.     /* Extract UDP header in host order */
  202.     if(ntohudp(&udp,&bp) != 0){
  203.         /* Truncated header */
  204.         udpInErrors++;
  205.         free_p(bp);
  206.         return;
  207.     }
  208.     /* If this was a broadcast packet, pretend it was sent to us */
  209.     if(rxbroadcast){
  210.         lsocket.address = iface->addr;
  211.     } else
  212.         lsocket.address = ip->dest;
  213.  
  214.     lsocket.port = udp.dest;
  215.     /* See if there's somebody around to read it */
  216.     if((up = lookup_udp(&lsocket)) == NULLUDP){
  217.         /* Nope, return an ICMP message */
  218.         if(!rxbroadcast){
  219.             bp = htonudp(&udp,bp,&ph);
  220.             icmp_output(ip,bp,ICMP_DEST_UNREACH,ICMP_PORT_UNREACH,NULL);
  221.         }
  222.         udpNoPorts++;
  223.         free_p(bp);
  224.         return;
  225.     }
  226.     /* Create space for the foreign socket info */
  227.     bp = pushdown(bp,sizeof(fsocket));
  228.     fsocket.address = ip->source;
  229.     fsocket.port = udp.source;
  230.     memcpy(&bp->data[0],(char *)&fsocket,sizeof(fsocket));
  231.  
  232.     /* Queue it */
  233.     enqueue(&up->rcvq,bp);
  234.     up->rcvcnt++;
  235.     udpInDatagrams++;
  236.     if(up->r_upcall)
  237.         (*up->r_upcall)(iface,up,up->rcvcnt);
  238. }
  239. /* Look up UDP socket. 
  240.  * Return control block pointer or NULLUDP if nonexistant
  241.  * As side effect, move control block to top of list to speed future
  242.  * searches.
  243.  */
  244. static struct udp_cb *
  245. lookup_udp(socket)
  246. struct socket *socket;
  247. {
  248.     register struct udp_cb *up;
  249.     struct udp_cb *uplast = NULLUDP;
  250.  
  251.     for(up = Udps;up != NULLUDP;uplast = up,up = up->next){
  252.         if(socket->port == up->socket.port
  253.          && (socket->address == up->socket.address
  254.          || up->socket.address == INADDR_ANY)){
  255.             if(uplast != NULLUDP){
  256.                 /* Move to top of list */
  257.                 uplast->next = up->next;
  258.                 up->next = Udps;
  259.                 Udps = up;
  260.             }
  261.             return up;
  262.         }
  263.     }
  264.     return NULLUDP;
  265. }
  266.  
  267. /* Attempt to reclaim unused space in UDP receive queues */
  268. void
  269. udp_garbage(red)
  270. int red;
  271. {
  272.     register struct udp_cb *udp;
  273.  
  274.     for(udp = Udps;udp != NULLUDP; udp = udp->next){
  275.         mbuf_crunch(&udp->rcvq);
  276.     }
  277. }
  278.  
  279.