home *** CD-ROM | disk | FTP | other *** search
/ Celestin Apprentice 5 / Apprentice-Release5.iso / Source Code / Libraries / DCLAP 6d / dclap6d / DNet / dnettcp.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-07-05  |  6.8 KB  |  319 lines  |  [TEXT/R*ch]

  1. /* dnettcp.c
  2.      d.g.gilbert
  3.      used as interface b/n "dirty" c and c++
  4.      for some of the tcp sockets basic stuff
  5. */
  6.  
  7.  
  8. #include "dnettcp.h"
  9. #include <ncbi.h>
  10.  
  11. #if defined(COMP_CODEWAR) && defined(OS_MAC)
  12. #define __STDC__
  13.  
  14. /* weird, this proto isn't in any of those many tcp headers !! */
  15. int gethostname(char * machname, long buflen);
  16. #endif
  17.  
  18. typedef struct sockaddr *SockAddPtr; /* codewar 4 hates (struct *) casts !? */
  19.  
  20. #define __NI_LIB__
  21. #define _NCBINET_
  22. #define _NI_TYPES_
  23.  
  24. #ifdef WIN_MSWIN
  25. #define NETP_INET_WSOCK 1
  26.     /* those who want non-winsock will have to fiddle here... */
  27. #endif
  28.  
  29. #ifdef OS_MAC
  30.  
  31. /* #include "macsockd.h" << this defines s_socketstuff as socketstuff, conflicting standard c lib */
  32. /* just do a subset.... */
  33. #define socket                s_socket
  34. #define connect                s_connect
  35. #define getsockname        s_getsockname
  36. #define setsockopt        s_setsockopt
  37.  
  38. #define __TYPES__       /* avoid Mac <Types.h> */
  39. #define __MEMORY__      /* avoid Mac <Memory.h> */
  40. #define ipBadLapErr     /* avoid Mac <MacTCPCommonTypes.h> */
  41. #define APPL_SOCK_DEF
  42. #define DONT_DEFINE_INET
  43. #include "sock_ext.h"
  44. extern void bzero PROTO((CharPtr target, long numbytes));
  45. #endif /* OS_MAC */
  46.  
  47.  
  48.     /* this is from NCBI network/nsclilib/ -- it defines & includes system-specific headers */
  49. #include "ni_net.h"
  50.  
  51.  
  52.  
  53.  
  54. int InitSocks PROTO((void));
  55.  
  56. static int gSocks_on = 0;
  57.  
  58. int InitSocks()
  59. {
  60. #ifdef WIN_MSWIN
  61.     if (!gSocks_on) {
  62.         int err;
  63.         WSADATA data;
  64.         WORD version = (1 << 8) + 1; /* MAKEWORD(1,1); */
  65.         err= WSAStartup(version, &data);
  66.         gSocks_on= (err == 0);
  67.         }
  68.     return gSocks_on;
  69. #else
  70.     return 1;
  71. #endif
  72. }
  73.  
  74.  
  75.  
  76. long SockOpen(char* hostname, unsigned short port)
  77. {
  78.             /* from Paul Lindner, UMinnesota gopher Socket.c  */
  79.      struct sockaddr_in Server;
  80.      struct hostent*    hostPtr;
  81.      long sockfd = 0;
  82.      long ERRinet = -1;
  83. #ifdef _CRAY
  84.      ERRinet = 0xFFFFFFFF;  /* -1 doesn't sign extend on 64 bit machines */
  85. #endif
  86.      long errnum = 0;
  87.  
  88.      /*** Find the hostname address ***/
  89.     if (!InitSocks()) return -1;
  90.      
  91. #ifdef WIN_MSWIN
  92.     if (WSAIsBlocking()) WSACancelBlockingCall(); /* ??? */
  93. #endif
  94.  
  95.     MemFill( &Server, '\0', sizeof(Server));  /* dgg, mswin fix? */
  96.     Server.sin_addr.s_addr = inet_addr(hostname);
  97.     if (Server.sin_addr.s_addr == ERRinet) {
  98.         hostPtr = gethostbyname(hostname);
  99.         if (hostPtr != NULL) {
  100.             MemFill( &Server, '\0', sizeof(Server));
  101.             MemCopy( &Server.sin_addr, hostPtr->h_addr, hostPtr->h_length);
  102.             Server.sin_family = hostPtr->h_addrtype;
  103.             }
  104.         else {
  105.             errnum= errHost;
  106.             goto errexit; /* return (errHost); */
  107.             }
  108.     } 
  109.     else
  110.       Server.sin_family = AF_INET;
  111.  
  112.     Server.sin_port = (unsigned short) htons(port);
  113.  
  114.      /*** Open the socket ***/
  115.  
  116.     if ((sockfd = socket(AF_INET, SOCK_STREAM, 0)) < 0) {
  117.         errnum= errSocket;
  118.         goto errexit; /* return (errSocket);  */
  119.         }
  120.  
  121. #ifndef WIN_MSWIN
  122. /* ?? winsock is having some problems */
  123. #ifndef UCX
  124.     setsockopt(sockfd, SOL_SOCKET, ~SO_LINGER, 0, 0);
  125. #endif
  126.     setsockopt(sockfd, SOL_SOCKET, SO_REUSEADDR, 0, 0);
  127.     setsockopt(sockfd, SOL_SOCKET, SO_KEEPALIVE, 0, 0);
  128. #endif
  129.     
  130.         /*** Connect ***/
  131.  
  132.     if (connect(sockfd, (SockAddPtr) &Server, sizeof(Server)) < 0) {
  133.         SockClose(sockfd);
  134.         errnum= errConnect;
  135.         goto errexit; /* return (errnum); */
  136.         }
  137.     
  138.     return(sockfd);
  139.  
  140. errexit:
  141. #ifdef WIN_MSWIN
  142.     WSACancelBlockingCall(); /* ??? */
  143. #endif
  144.     return(errnum);
  145. /* #else -- !NETLIB && !CMUIP  << left out these versions, dgg */
  146. }
  147.  
  148.  
  149. short SockClose(long theSocket)
  150. {
  151. #ifdef WIN_MSWIN
  152.     return closesocket(theSocket);
  153. #else
  154. #ifdef WIN_MAC
  155.     return s_close(theSocket);
  156. #else
  157.     return close(theSocket);
  158. #endif
  159. #endif
  160. }
  161.  
  162.     
  163.     
  164. long    MyIPaddress(void)
  165. {
  166.     if (!InitSocks()) return 0;
  167. #if defined(WIN_MSWIN) || defined(OS_UNIX_SYSV)
  168.       /* winsok seems to lack gethostid() */
  169.     /* also missing in Solaris2 */
  170.     return inet_addr("localhost");
  171. #else
  172.     return gethostid();
  173. #endif
  174. }
  175.  
  176.  
  177. short    MyHostname( char* name, short namelen)
  178. {
  179.     if (!InitSocks()) return -1;
  180.     return (short) gethostname( name, namelen);
  181. }
  182.  
  183. long    Hostname2IP( char* hostname)
  184. {
  185.     if (!InitSocks()) return -1;
  186.     return (long) gethostbyname( (char*)hostname);
  187. }
  188.  
  189.  
  190. short    SockHostname( long itsSocket, char* name, short namelen)
  191. {
  192. #if defined(OS_UNIX_IRIX) || defined(COMP_SYMC)
  193.     int    len= namelen;
  194. #else
  195.     long    len= namelen;
  196. #endif
  197.     short err;
  198.     err= getsockname( itsSocket, (SockAddPtr) name, &len); 
  199.     if (!err) { if (len>=namelen) --len; name[len]= '\0'; }
  200.     return err;
  201. }
  202.  
  203. long    SockRead( long itsSocket, void *buffer, long buflen)
  204. {
  205. #ifdef WIN_MSWIN
  206.     return recv(itsSocket, buffer, buflen, 0);
  207. #else
  208. #ifdef WIN_MAC
  209.     return s_read(itsSocket, buffer, buflen);
  210. #else
  211.     return read(itsSocket, buffer, buflen);
  212. #endif
  213. #endif
  214. }
  215.  
  216. long    SockWrite( long itsSocket, void *buffer, long buflen)
  217. {
  218. #ifdef WIN_MSWIN
  219.     return send(itsSocket, buffer, buflen, 0);
  220. #else
  221. #ifdef WIN_MAC
  222.     return s_write(itsSocket, buffer, buflen);
  223. #else
  224.     return write(itsSocket, buffer, buflen);
  225. #endif
  226. #endif
  227. }
  228.  
  229. short    SockSelect( short numsocks, long readsocks[],long writesocks[],
  230.                                 long errsocks[], long time)
  231. {
  232.     return select( numsocks, (fd_set*)readsocks, (fd_set*)writesocks, 
  233.                         (fd_set*)errsocks, (struct timeval *) time);
  234. }
  235.  
  236.  
  237. #if defined(NOT_COMP_CODEWAR) && defined(OS_MAC)
  238. /* damn that buggy linker, can't *find* this code in netdb.c */
  239.  
  240. //# include <Stdio.h>
  241. //# include <Types.h>
  242. //# include <Resources.h>
  243. //# include <Errors.h>
  244. //# include <OSUtils.h>
  245.  
  246. # include <s_types.h>
  247. //# include <netdb.h>
  248. //# include <neti_in.h>
  249. //# include <s_socket.h>
  250. # include <s_time.h>
  251. # include <neterrno.h>
  252.  
  253. //# include "sock_str.h"
  254. /*# include "sock_int.h"*/
  255.  
  256. //#include <Ctype.h>
  257. #include <a_namesr.h>
  258. #include <s_param.h>
  259.  
  260.  
  261.  
  262. extern SpinFn spinroutine;
  263. extern pascal void DNRDone(struct hostInfo *hostinfoPtr,Boolean *done);
  264.  
  265. extern int h_errno;
  266.  
  267. struct hostent * gethostbyname(char *name)
  268. {
  269.     Boolean done;
  270.     int i;
  271.     
  272.     sock_init();
  273.  
  274.     for (i=0; i<NUM_ALT_ADDRS; i++)
  275.         macHost.addr[i] = 0;
  276.     done = false;
  277.     if (StrToAddr(name,&macHost,(ResultProcPtr) DNRDone,(char *) &done) == cacheFault)
  278.     {
  279.         SPINP(!done,SP_NAME,0L)
  280.     }
  281.     switch (macHost.rtnCode)
  282.     {
  283.         case noErr: break;
  284.         
  285.         case nameSyntaxErr:    h_errno = HOST_NOT_FOUND;    return(NULL);
  286.         case cacheFault:    h_errno = NO_RECOVERY;        return(NULL);
  287.         case noResultProc:    h_errno = NO_RECOVERY;        return(NULL);
  288.         case noNameServer:    h_errno = HOST_NOT_FOUND;    return(NULL);
  289.         case authNameErr:    h_errno = HOST_NOT_FOUND;    return(NULL);
  290.         case noAnsErr:        h_errno = TRY_AGAIN;        return(NULL);
  291.         case dnrErr:        h_errno = NO_RECOVERY;        return(NULL);
  292.         case outOfMemory:    h_errno = TRY_AGAIN;        return(NULL);
  293.         case notOpenErr:    h_errno = NO_RECOVERY;        return(NULL);
  294.         default:            h_errno = NO_RECOVERY;        return(NULL);
  295.     }
  296.     
  297.     /* was the 'name' an IP address? */
  298.     if (macHost.cname[0] == 0)
  299.     {
  300.         h_errno = HOST_NOT_FOUND;
  301.         return(NULL);
  302.     }
  303.     
  304.     /* for some reason there is a dot at the end of the name */
  305.     i = strlen(macHost.cname) - 1;
  306.     if (macHost.cname[i] == '.')
  307.         macHost.cname[i] = 0;
  308.     
  309.     for (i=0; i<NUM_ALT_ADDRS && macHost.addr[i]!=0; i++)
  310.     {
  311.         addrPtrs[i] = &macHost.addr[i];
  312.     }
  313.     addrPtrs[i] = NULL;
  314.     
  315.     return(&unixHost);
  316. }
  317.  
  318. #endif
  319.