home *** CD-ROM | disk | FTP | other *** search
/ The World of Computer Software / World_Of_Computer_Software-02-385-Vol-1of3.iso / t / tel2305s.zip / ENGINE / UDP.C < prev    next >
C/C++ Source or Header  |  1992-02-22  |  4KB  |  157 lines

  1. /*
  2.  *    UDP.C
  3.  *
  4.  *    UDP Protocol Routines
  5.  *
  6.  ***************************************************************************
  7.  *                                                                         *
  8.  *     part of:                                                            *
  9.  *     TCP/IP kernel for NCSA Telnet                                       *
  10.  *     by Tim Krauskopf                                                    *
  11.  *                                                                         *
  12.  *     National Center for Supercomputing Applications                     *
  13.  *     152 Computing Applications Building                                 *
  14.  *     605 E. Springfield Ave.                                             *
  15.  *     Champaign, IL  61820                                                *
  16.  *                                                                         *
  17.  ***************************************************************************
  18.  *
  19.  *    Revision history:
  20.  *
  21.  *    5/88    split out of ip.c for 2.3 release, JKM
  22.  *
  23.  */
  24.  
  25. /*
  26.  *    Includes
  27.  */
  28. #include <stdio.h>
  29. #include "protocol.h"
  30. #include "data.h"
  31. #include "externs.h"
  32.  
  33. /* 
  34.  *    udpinterpret ( p, ulen )
  35.  *
  36.  *    Take an incoming UDP packet and make the available to the user level 
  37.  * routines.  Currently keeps the last packet coming in to a port.
  38.  *
  39.  *    Limitations :
  40.  *
  41.  * Can only listen to one UDP port at a time, only saves the last packet
  42.  * received on that port.  Port numbers should be assigned like TCP ports.
  43.  *
  44.  */
  45. int udpinterpret(UDPKT *p,int ulen)
  46. {
  47.     uint hischeck,mycheck;
  48. /*
  49.  *  did we want this data ?  If not, then let it go, no comment
  50.  *  If we want it, copy the relevent information into our structure
  51.  */
  52.     if(intswap(p->u.dest)!=ulist.listen) 
  53.         return(1);
  54. /*
  55.  *  first compute the checksum to see if it is a valid packet
  56.  */
  57.     hischeck=p->u.check;
  58.     p->u.check=0;
  59.     if (hischeck) {
  60.         movebytes(tcps.source,p->i.ipsource,8);
  61.         tcps.z=0;
  62.         tcps.proto=p->i.protocol;
  63.         tcps.tcplen=intswap(ulen);
  64.         mycheck=tcpcheck((char *)&tcps,(char *)&p->u,ulen);
  65.         if(hischeck!=mycheck) {
  66.             netposterr(700);
  67.             return(2);
  68.           }
  69.         p->u.check=hischeck;                    /* put it back */
  70.       }
  71.     ulen-=8;                        /* account for header */
  72.     if(ulen>UMAXLEN)                /* most data that we can accept */
  73.         ulen=UMAXLEN;
  74.     movebytes(ulist.who,p->i.ipsource,4);
  75.     movebytes(ulist.data,p->data,ulen);
  76.     ulist.length=ulen;
  77.     ulist.stale=0;
  78.     netputuev(USERCLASS,UDPDATA,ulist.listen);        /* post that it is here */
  79.     return(0);
  80. }
  81.  
  82. /*
  83.  *    neturead ( buffer )
  84.  *
  85.  *    Get the data from the UDP buffer and transfer it into your buffer.
  86.  *
  87.  *    Returns the number of bytes transferred or -1 of none available
  88.  *
  89.  */
  90. int neturead(char *buffer)
  91. {
  92.     if(ulist.stale)
  93.         return(-1);
  94.     movebytes(buffer,ulist.data,ulist.length);
  95.     ulist.stale=1;
  96.     return((int)ulist.length);
  97. }
  98.  
  99. /*
  100.  *    netulisten ( port )
  101.  *
  102.  *    Specify which UDP port number to listen to -- can only listen to one port
  103.  * at a time.
  104.  *
  105.  */
  106. void netulisten(int port)
  107. {
  108.     ulist.listen=port;
  109. }
  110.  
  111. /*
  112.  *    netusend ( machine, port, retport, buffer, n )
  113.  *
  114.  *    Send some data out in a udp packet ( uses the preinitialized data in the
  115.  * port packet *ulist.udpout* )
  116.  *
  117.  *    Returns 0 on ok send, non-zero for an error
  118.  *
  119.  */
  120. int netusend(uint8 *machine,uint16 port,uint16 retport,uint8 *buffer,int n)
  121. {
  122.     unsigned char *pc;
  123.     if(n>UMAXLEN)
  124.         n=UMAXLEN;
  125. /*
  126.  *  make sure that we have the right dlayer address
  127.  */
  128.     if(!comparen(machine,ulist.udpout.i.ipdest,4)) {
  129.         pc=netdlayer(machine);
  130.         if(pc==NULL) 
  131.             return(-2);
  132.         movebytes(ulist.udpout.d.dest,pc,DADDLEN);
  133.         movebytes(ulist.udpout.i.ipdest,machine,4);
  134.         movebytes(ulist.tcps.dest,machine,4);
  135.       }
  136.     ulist.udpout.u.dest=intswap(port);
  137.     ulist.udpout.u.source=intswap(retport);
  138.     ulist.tcps.tcplen=ulist.udpout.u.length=intswap(n+sizeof(UDPLAYER));
  139.     movenbytes(ulist.udpout.data,buffer,n);
  140. /*
  141.  *  put in checksum
  142.  */
  143.     ulist.udpout.u.check=0;
  144.     ulist.udpout.u.check=tcpcheck((char *)&ulist.tcps,(char *)&ulist.udpout.u,n+sizeof(UDPLAYER));
  145. /*
  146.  *   iplayer for send
  147.  */
  148.     ulist.udpout.i.tlen=intswap(n+sizeof(IPLAYER)+sizeof(UDPLAYER));
  149.     ulist.udpout.i.ident=intswap(nnipident++);
  150.     ulist.udpout.i.check=0;
  151.     ulist.udpout.i.check=ipcheck((char *)&ulist.udpout.i,10);
  152. /*
  153.  *  send it
  154.  */
  155.     return(dlayersend((DLAYER *)&ulist.udpout,sizeof(DLAYER)+sizeof(IPLAYER)+sizeof(UDPLAYER)+n));
  156. }
  157.