home *** CD-ROM | disk | FTP | other *** search
/ DP Tool Club 15 / CD_ASCQ_15_070894.iso / vrac / wsftp2.zip / WS_IP.C < prev    next >
C/C++ Source or Header  |  1993-12-04  |  5KB  |  134 lines

  1. /***************************************************************************
  2.   Windows Sockets Client Application Support Module
  3.  
  4.   Written by:
  5.       John A. Junod             Internet: <junodj@gordon-emh2.army.mil>
  6.       267 Hillwood Street                 <zj8549@trotter.usma.edu>
  7.       Martinez, GA 30907      Compuserve: 72321,366 
  8.  
  9.   This program executable and all source code is released into the public
  10.   domain.  It would be nice (but is not required) to give me a little 
  11.   credit for any use of this code.
  12.  
  13.   THE INFORMATION AND CODE PROVIDED IS PROVIDED AS IS WITHOUT WARRANTY 
  14.   OF ANY KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO 
  15.   THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR 
  16.   PURPOSE. IN NO EVENT SHALL JOHN A. JUNOD BE LIABLE FOR ANY DAMAGES 
  17.   WHATSOEVER INCLUDING DIRECT, INDIRECT, INCIDENTAL, CONSEQUENTIAL, LOSS 
  18.   OF BUSINESS PROFITS OR SPECIAL DAMAGES, EVEN IF JOHN A. JUNOD HAS BEEN 
  19.   ADVISED OF THE POSSIBILITY OF SUCH DAMAGES.
  20.  
  21. *****************************************************************************/
  22. /*
  23.   MODULE: WS_IP.C
  24.  
  25.   Based on code published in UNIX Network Programming by W. Richard Stevens
  26.  
  27.   These are common routines that will be needed by many different programs
  28.   and should not be modified for a specific instance.
  29. */
  30. #include "ws_glob.h"
  31. #include "ws_ftp.h"
  32. #include <stdlib.h>
  33.  
  34. SOCKET connectTCP(char *host,char *service)
  35. {
  36.   return connectsock(host,service,"tcp");
  37. }
  38.  
  39. SOCKET connectUDP(char *host,char *service)
  40. {
  41.   return connectsock(host,service,"udp");
  42. }
  43.  
  44. SOCKET connectsock(char *host,char *service,char *protocol)
  45. {
  46.   struct hostent *pHostEntry;      // pointer to host entry
  47.   struct servent FAR *pServiceEntry;   // pointer to service entry
  48.   struct protoent FAR *pProtoEntry;    // pointer to protocol entry
  49.   SOCKET sSocket;                  // socket
  50.   int nSocketType;                 // socket type
  51.   short pproto;
  52.  
  53.   DoPrintf("  ");
  54.   // initialize socket address structure
  55.   memset((char *)&saSockAddr,0,sizeof(saSockAddr));
  56.   saSockAddr.sin_family=AF_INET;
  57.   // get service port number from name
  58.   if(pServiceEntry=getservbyname(service,protocol))
  59.     saSockAddr.sin_port=pServiceEntry->s_port;
  60.   else if((saSockAddr.sin_port=htons((u_short)atoi(service)))==0) {
  61.     if(strcmpi(service,"FTP")==0)
  62.       saSockAddr.sin_port=htons(21);
  63.     else {
  64.       DoPrintf("can't get \"%s\" service entry",service);
  65.       return INVALID_SOCKET;
  66.     }
  67.   }
  68.   // map host to ip, allow ip
  69.   if(pHostEntry=gethostbyname(host))
  70.     memcpy((char *)&saSockAddr.sin_addr,pHostEntry->h_addr,
  71.              pHostEntry->h_length);
  72. //  memcpy(pHostEntry->h_addr,(char *)&saSockAddr.sin_addr,pHostEntry->h_length);
  73.   else if((saSockAddr.sin_addr.s_addr=inet_addr(host))==INADDR_NONE) {
  74.     DoPrintf("can't get \"%s\" host entry",host);
  75.     return INVALID_SOCKET;
  76.   }
  77.   // map protocol name to protocol number
  78.   if((pProtoEntry=getprotobyname(protocol))==0) {
  79.     if(strcmpi(protocol,"TCP")==0)
  80.       pproto=6;
  81.     else {
  82.       DoPrintf("can't get \"%s\" protocol entry",protocol);
  83.       return INVALID_SOCKET;
  84.     }
  85.   } else pproto=pProtoEntry->p_proto;
  86.   // use protocol to choose socket type
  87.   if(strcmp(protocol,"udp")==0)
  88.     nSocketType=SOCK_DGRAM;
  89.   else
  90.     nSocketType=SOCK_STREAM;
  91.   // allocate a socket
  92.   sSocket=socket(AF_INET,nSocketType,pproto);
  93.   if(sSocket==INVALID_SOCKET) {
  94.     ReportWSError("create socket",WSAGetLastError());
  95.     return INVALID_SOCKET;
  96.   }
  97.  
  98.   memcpy((LPSTR)&saSockAddr1,(LPSTR)&saSockAddr,sizeof(saSockAddr));
  99.   saSockAddr1.sin_port=htons(20);
  100.  
  101.   DoPrintf("connecting to %s ...",inet_ntoa(saSockAddr.sin_addr));
  102.   // connect the socket
  103.   if(connect(sSocket,(struct sockaddr *)&saSockAddr,sizeof(saSockAddr))
  104.       ==SOCKET_ERROR) {
  105. //  DoPrintf("Can't connect to %s %s service.",host,service);
  106.     ReportWSError("Connect failed -",WSAGetLastError());
  107.     return INVALID_SOCKET;
  108.   }
  109.  
  110.   DoPrintf("Connected to %s port %u",
  111.     inet_ntoa(saSockAddr.sin_addr),ntohs(saSockAddr.sin_port));
  112.  
  113.   return sSocket;
  114. }
  115.  
  116. int sendstr(SOCKET sSocket,LPSTR pBuf,int nBytesToWrite)
  117. {
  118.   int nBytesLeft,nWritten;
  119.  
  120.   nBytesLeft=nBytesToWrite;
  121.   while(nBytesLeft > 0) {                   // while we haven't written enough
  122.     nWritten=send(sSocket,pBuf,((nBytesLeft>1024)?1024:nBytesLeft),0); // write what we can
  123.     DoPrintf("[%u] %u send %u",sSocket,nBytesLeft,nWritten);
  124.     if(nWritten <= 0) {
  125.       ReportWSError("send",WSAGetLastError());
  126.       return(nWritten);  // error occured
  127.     }
  128.     nBytesLeft -= nWritten;                 // count what we wrote
  129.     pBuf   += nWritten;                     // adjust buffer pointer
  130.   }
  131.   return(nBytesToWrite - nBytesLeft);       // return count of bytes written
  132. }
  133.  
  134.