home *** CD-ROM | disk | FTP | other *** search
/ ProfitPress Mega CDROM2 …eeware (MSDOS)(1992)(Eng) / ProfitPress-MegaCDROM2.B6I / MISC / NETWORK / TEL23SRC.ZIP / ENGINE / UDP.C < prev    next >
Encoding:
C/C++ Source or Header  |  1990-10-14  |  4.5 KB  |  167 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. #ifdef MEMORY_DEBUG
  30. #include "memdebug.h"
  31. #endif
  32. #include "protocol.h"
  33. #include "data.h"
  34. #include "externs.h"
  35.  
  36. /* 
  37.  *    udpinterpret ( p, ulen )
  38.  *
  39.  *    Take an incoming UDP packet and make the available to the user level 
  40.  * routines.  Currently keeps the last packet coming in to a port.
  41.  *
  42.  *    Limitations :
  43.  *
  44.  * Can only listen to one UDP port at a time, only saves the last packet
  45.  * received on that port.  Port numbers should be assigned like TCP ports.
  46.  *
  47.  */
  48. int udpinterpret ( p, ulen )
  49. UDPKT *p;
  50. int ulen;
  51. {
  52.     uint hischeck,mycheck;
  53. /*
  54.  *  did we want this data ?  If not, then let it go, no comment
  55.  *  If we want it, copy the relevent information into our structure
  56.  */
  57.     if(intswap(p->u.dest)!=ulist.listen) 
  58.         return(1);
  59. /*
  60.  *  first compute the checksum to see if it is a valid packet
  61.  */
  62.     hischeck=p->u.check;
  63.     p->u.check=0;
  64.     if (hischeck) {
  65.         movebytes(tcps.source,p->i.ipsource,8);
  66.         tcps.z=0;
  67.         tcps.proto=p->i.protocol;
  68.         tcps.tcplen=intswap(ulen);
  69.         mycheck=tcpcheck((char *)&tcps,(char *)&p->u,ulen);
  70.         if(hischeck!=mycheck) {
  71.             netposterr(700);
  72.             return(2);
  73.           }
  74.         p->u.check=hischeck;                    /* put it back */
  75.       }
  76.     ulen-=8;                        /* account for header */
  77.     if(ulen>UMAXLEN)                /* most data that we can accept */
  78.         ulen=UMAXLEN;
  79.     movebytes(ulist.who,p->i.ipsource,4);
  80.     movebytes(ulist.data,p->data,ulen);
  81.     ulist.length=ulen;
  82.     ulist.stale=0;
  83.     netputuev(USERCLASS,UDPDATA,ulist.listen);        /* post that it is here */
  84.     return(0);
  85. }
  86.  
  87. /*
  88.  *    neturead ( buffer )
  89.  *
  90.  *    Get the data from the UDP buffer and transfer it into your buffer.
  91.  *
  92.  *    Returns the number of bytes transferred or -1 of none available
  93.  *
  94.  */
  95. int neturead(buffer)
  96. char *buffer;
  97. {
  98.     if(ulist.stale)
  99.         return(-1);
  100.     movebytes(buffer,ulist.data,ulist.length);
  101.     ulist.stale=1;
  102.     return((int)ulist.length);
  103. }
  104.  
  105. /*
  106.  *    netulisten ( port )
  107.  *
  108.  *    Specify which UDP port number to listen to -- can only listen to one port
  109.  * at a time.
  110.  *
  111.  */
  112. void netulisten ( port )
  113. int port;
  114. {
  115.     ulist.listen=port;
  116. }
  117.  
  118. /*
  119.  *    netusend ( machine, port, retport, buffer, n )
  120.  *
  121.  *    Send some data out in a udp packet ( uses the preinitialized data in the
  122.  * port packet *ulist.udpout* )
  123.  *
  124.  *    Returns 0 on ok send, non-zero for an error
  125.  *
  126.  */
  127. int netusend(machine,port,retport,buffer,n)
  128. unsigned char *machine,*buffer;
  129. unsigned int port,retport;
  130. int n;
  131. {
  132.     unsigned char *pc;
  133.     if(n>UMAXLEN)
  134.         n=UMAXLEN;
  135. /*
  136.  *  make sure that we have the right dlayer address
  137.  */
  138.     if(!comparen(machine,ulist.udpout.i.ipdest,4)) {
  139.         pc=netdlayer(machine);
  140.         if(pc==NULL) 
  141.             return(-2);
  142.         movebytes(ulist.udpout.d.dest,pc,DADDLEN);
  143.         movebytes(ulist.udpout.i.ipdest,machine,4);
  144.         movebytes(ulist.tcps.dest,machine,4);
  145.       }
  146.     ulist.udpout.u.dest=intswap(port);
  147.     ulist.udpout.u.source=intswap(retport);
  148.     ulist.tcps.tcplen=ulist.udpout.u.length=intswap(n+sizeof(UDPLAYER));
  149.     movenbytes(ulist.udpout.data,buffer,n);
  150. /*
  151.  *  put in checksum
  152.  */
  153.     ulist.udpout.u.check=0;
  154.     ulist.udpout.u.check=tcpcheck((char *)&ulist.tcps,(char *)&ulist.udpout.u,n+sizeof(UDPLAYER));
  155. /*
  156.  *   iplayer for send
  157.  */
  158.     ulist.udpout.i.tlen=intswap(n+sizeof(IPLAYER)+sizeof(UDPLAYER));
  159.     ulist.udpout.i.ident=intswap(nnipident++);
  160.     ulist.udpout.i.check=0;
  161.     ulist.udpout.i.check=ipcheck((char *)&ulist.udpout.i,10);
  162. /*
  163.  *  send it
  164.  */
  165.     return(dlayersend((DLAYER *)&ulist.udpout,sizeof(DLAYER)+sizeof(IPLAYER)+sizeof(UDPLAYER)+n));
  166. }
  167.