home *** CD-ROM | disk | FTP | other *** search
/ High Voltage Shareware / high1.zip / high1 / DIR3 / KA9Q212.ZIP / UDP.C < prev    next >
C/C++ Source or Header  |  1993-07-16  |  7KB  |  296 lines

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