home *** CD-ROM | disk | FTP | other *** search
/ Super Net 1 / SUPERNET_1.iso / PC / OTROS / EXTRAS / UUCODE / UUPC / TEST / UPC12ES2.ZIP / UUCICO / dcptpkt.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-11-13  |  8.6 KB  |  248 lines

  1. /*--------------------------------------------------------------------*/
  2. /*       d c p t p k t . c                                            */
  3. /*                                                                    */
  4. /*       UUCP 't' protocol support                                    */
  5. /*--------------------------------------------------------------------*/
  6.  
  7. /*--------------------------------------------------------------------*/
  8. /*       Changes Copyright (c) 1989-1993 by Kendra Electronic         */
  9. /*       Wonderworks.                                                 */
  10. /*                                                                    */
  11. /*       All rights reserved except those explicitly granted by       */
  12. /*       the UUPC/extended license agreement.                         */
  13. /*--------------------------------------------------------------------*/
  14.  
  15. /*--------------------------------------------------------------------*/
  16. /*                          RCS Information                           */
  17. /*--------------------------------------------------------------------*/
  18.  
  19. /*
  20.  *    $Id: dcptpkt.c 1.7 1993/11/13 17:43:26 ahd Exp $
  21.  *
  22.  *    Revision history:
  23.  *    $Log: dcptpkt.c $
  24.  * Revision 1.7  1993/11/13  17:43:26  ahd
  25.  * Correct debug level on sending empty packet message
  26.  *
  27.  * Revision 1.6  1993/11/08  04:46:49  ahd
  28.  * Correct bug which prevented proper EOF being handled
  29.  *
  30.  * Revision 1.5  1993/10/12  01:32:46  ahd
  31.  * Normalize comments to PL/I style
  32.  *
  33.  * Revision 1.4  1993/09/24  03:43:27  ahd
  34.  * Correct byte reordering functions
  35.  *
  36.  * Revision 1.4  1993/09/24  03:43:27  ahd
  37.  * Correct byte reordering functions
  38.  *
  39.  * Revision 1.3  1993/09/21  01:42:13  ahd
  40.  * Delete functions duplicated from dcpgpkt.c
  41.  *
  42.  * Revision 1.2  1993/09/20  04:48:25  ahd
  43.  * TCP/IP support from Dave Watt
  44.  * 't' protocol support
  45.  * OS/2 2.x support (BC++ 1.0 for OS/2)
  46.  *
  47.  * Revision 1.1  1993/09/18  19:47:24  ahd
  48.  * Initial revision
  49.  *
  50.  */
  51.  
  52. /*--------------------------------------------------------------------*/
  53. /*       TCP/IP ("t") protocol.                                       */
  54. /*                                                                    */
  55. /*       Protocol for over reliable (TCP/IP) paths.                   */
  56. /*                                                                    */
  57. /*       't' procotol is done by simply transmitting the four byte    */
  58. /*       length of the packet in network byte order (big-endian)      */
  59. /*       followed by the packet data itself.  No padding is           */
  60. /*       performed.                                                   */
  61. /*                                                                    */
  62. /*       Note:  Many of the functions (msg write, msg read, start     */
  63. /*       of file, file eof) for this protocol are as the same as      */
  64. /*       the functions for 'g' protocol, and we use the actual 'g'    */
  65. /*       protocol copies as defined in dcpsys.c.                      */
  66. /*--------------------------------------------------------------------*/
  67.  
  68. /*--------------------------------------------------------------------*/
  69. /*                        System include files                        */
  70. /*--------------------------------------------------------------------*/
  71.  
  72. #include <stdio.h>
  73. #include <time.h>
  74. #include <string.h>
  75.  
  76. #if defined(WIN32) || defined(_Windows)
  77. #include "winsock.h"       /* Needed for byte ordering                 */
  78. #endif
  79.  
  80. /*--------------------------------------------------------------------*/
  81. /*                    UUPC/extended include files                     */
  82. /*--------------------------------------------------------------------*/
  83.  
  84. #include "lib.h"
  85. #include "dcp.h"
  86. #include "dcptpkt.h"
  87. #include "dcpsys.h"
  88. #include "hostable.h"
  89. #include "security.h"
  90. #include "ssleep.h"
  91. #include "modem.h"
  92. #include "commlib.h"
  93.  
  94. #ifdef _Windows
  95. #include "pwinsock.h"
  96. #endif
  97.  
  98. #ifndef _WINSOCKAPI_
  99.  
  100. /*--------------------------------------------------------------------*/
  101. /*     Network functions needed when no winsock functions available   */
  102. /*--------------------------------------------------------------------*/
  103.  
  104. static unsigned long htonl( const unsigned long input );
  105. static unsigned long ntohl( const unsigned long input );
  106.  
  107. /*--------------------------------------------------------------------*/
  108. /*       h t o n l                                                    */
  109. /*                                                                    */
  110. /*       Convert unsigned long from host to network byte order        */
  111. /*--------------------------------------------------------------------*/
  112.  
  113. static unsigned long htonl( const unsigned long input )
  114. {
  115.    unsigned long result;
  116.    unsigned char *p = (unsigned char *) &result;
  117.    int i;
  118.  
  119.    for (i = 0 ; i < sizeof input; i++ )
  120.       p[3 - i] = (unsigned char) ((input >> (i*8)) & 0xff);
  121.  
  122.    printmsg(15,"htonl: %lx = %x %x %x %x",input, p[0], p[1], p[2], p[3] );
  123.  
  124.    return result;
  125.  
  126. } /* htonl */
  127.  
  128. /*--------------------------------------------------------------------*/
  129. /*       n t o h l                                                    */
  130. /*                                                                    */
  131. /*       Convert unsigned long from network to host byte order        */
  132. /*--------------------------------------------------------------------*/
  133.  
  134. static unsigned long ntohl( const unsigned long input )
  135. {
  136.    unsigned char *p = (unsigned char *) &input;
  137.    unsigned long result = 0;
  138.    int i;
  139.  
  140.    for (i = 0 ; i < sizeof input; i++ )
  141.       result = (result << 8) + p[i];
  142.  
  143.    printmsg(15,"ntonh: %x %x %x %x = %lx",p[0], p[1], p[2], p[3], result );
  144.  
  145.    return result;
  146.  
  147. } /* ntohl */
  148.  
  149. #endif
  150.  
  151. /*--------------------------------------------------------------------*/
  152. /*    t o p e n p k                                                   */
  153. /*                                                                    */
  154. /*    Open "t" protocol to other system                               */
  155. /*--------------------------------------------------------------------*/
  156.  
  157. #ifdef __TURBOC__
  158. #pragma argsused
  159. #endif
  160.  
  161. short topenpk(const boolean master)
  162. {
  163.    s_pktsize = r_pktsize = 1024;    /* Fixed for 't' procotol         */
  164.  
  165.    return DCP_OK;
  166.  
  167. } /* topenpk */
  168.  
  169. /*--------------------------------------------------------------------*/
  170. /*    t g e t p k t                                                   */
  171. /*                                                                    */
  172. /*    Receive an "t" protocol packet of data from the other system    */
  173. /*--------------------------------------------------------------------*/
  174.  
  175. short tgetpkt(char *packet, short *bytes)
  176. {
  177.    unsigned short recv;
  178.    unsigned long nrecv;
  179.  
  180.    if (sread( (char *) &nrecv, sizeof nrecv, M_tPacketTimeout) < sizeof nrecv)
  181.    {
  182.       printmsg(0,"tgetpkt: Length read failed");
  183.       return -1;
  184.    }
  185.  
  186.    recv = (short) ntohl( nrecv );
  187.  
  188.    if ( recv > r_pktsize )
  189.    {
  190.       printmsg(0,"tgetpkt: Buffer overrun!  Wanted %d bytes, %d queued",
  191.                   (int) r_pktsize,
  192.                   (int) recv );
  193.       return -1;
  194.    }
  195.  
  196.    if ( ! recv )
  197.       printmsg(4,"tgetpkt: Received empty packet");
  198.    else if (sread( packet, recv, M_tPacketTimeout) < recv)
  199.    {
  200.       printmsg(0,"tgetpkt: Data read failed for %d bytes", (int) recv);
  201.       return -1;
  202.    }
  203.  
  204.    remote_stats.packets++;
  205.  
  206.    *bytes = recv;
  207.  
  208.    return 0;
  209.  
  210. } /* tgetpkt */
  211.  
  212. /*--------------------------------------------------------------------*/
  213. /*    t s e n d p k t                                                 */
  214. /*                                                                    */
  215. /*    Send an "t" protocol packet to the other system                 */
  216. /*--------------------------------------------------------------------*/
  217.  
  218. short tsendpkt(char *ip, short len)
  219. {
  220.  
  221.    unsigned long nxmit = htonl((unsigned long) len);
  222.  
  223.    if ( swrite( (char *) &nxmit, sizeof nxmit ) != sizeof nxmit )
  224.       return -1;
  225.  
  226.    if ( ! len )
  227.       printmsg(4,"tsendpkt: Sending empty packet");
  228.    else if ( swrite( ip , len ) != len )
  229.       return -1;
  230.  
  231.    remote_stats.packets++;
  232.  
  233.    return 0;
  234.  
  235. } /* tsendpkt */
  236.  
  237.  
  238. /*--------------------------------------------------------------------*/
  239. /*    t c l o s e p k                                                 */
  240. /*                                                                    */
  241. /*    Shutdown "t" procotol with other system                         */
  242. /*--------------------------------------------------------------------*/
  243.  
  244. short tclosepk()
  245. {
  246.    return DCP_OK;
  247. } /* tclosepk */
  248.