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