home *** CD-ROM | disk | FTP | other *** search
/ Gold Fish 3 / goldfish_volume_3.bin / files / comm / tcp / amitcp-sdk / src / rpclib / clnt_generic.c < prev    next >
Encoding:
C/C++ Source or Header  |  1994-09-29  |  2.2 KB  |  92 lines

  1. /*
  2.  *      $Id: clnt_generic.c,v 4.2 1994/09/29 23:48:50 jraja Exp $
  3.  *
  4.  *      Copyright © 1994 AmiTCP/IP Group,
  5.  *                       Network Solutions Development Inc.
  6.  *                       All rights reserved. 
  7.  */
  8.  
  9. /* @(#)clnt_generic.c    2.2 88/08/01 4.0 RPCSRC */
  10. #if !defined(lint) && defined(SCCSIDS)
  11. static char sccsid[] = "@(#)clnt_generic.c 1.4 87/08/11 (C) 1987 SMI";
  12. #endif
  13. /*
  14.  * Copyright (C) 1987, Sun Microsystems, Inc.
  15.  */
  16. #include <sys/param.h>
  17. #include <rpc/rpc.h>
  18. #include <sys/socket.h>
  19. #include <sys/errno.h>
  20. #include <netdb.h>
  21.  
  22. /*
  23.  * Generic client creation: takes (hostname, program-number, protocol) and
  24.  * returns client handle. Default options are set, which the user can 
  25.  * change using the rpc equivalent of ioctl()'s.
  26.  */
  27. CLIENT *
  28. clnt_create(hostname, prog, vers, proto)
  29.     char *hostname;
  30.     u_long prog;
  31.     u_long vers;
  32.     char *proto;
  33. {
  34.     struct hostent *h;
  35.     struct protoent *p;
  36.     struct sockaddr_in sin;
  37.     int sock;
  38.     struct timeval tv;
  39.     CLIENT *client;
  40.  
  41.     h = gethostbyname(hostname);
  42.     if (h == NULL) {
  43.         rpc_createerr.cf_stat = RPC_UNKNOWNHOST;
  44.         return (NULL);
  45.     }
  46.     if (h->h_addrtype != AF_INET) {
  47.         /*
  48.          * Only support INET for now
  49.          */
  50.         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  51.         rpc_createerr.cf_error.re_errno = EAFNOSUPPORT; 
  52.         return (NULL);
  53.     }
  54.     sin.sin_family = h->h_addrtype;
  55.     sin.sin_port = 0;
  56.     bzero(sin.sin_zero, sizeof(sin.sin_zero));
  57.     bcopy(h->h_addr, (char*)&sin.sin_addr, h->h_length);
  58.     p = getprotobyname(proto);
  59.     if (p == NULL) {
  60.         rpc_createerr.cf_stat = RPC_UNKNOWNPROTO;
  61.         rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; 
  62.         return (NULL);
  63.     }
  64.     sock = RPC_ANYSOCK;
  65.     switch (p->p_proto) {
  66.     case IPPROTO_UDP:
  67.         tv.tv_sec = 5;
  68.         tv.tv_usec = 0;
  69.         client = clntudp_create(&sin, prog, vers, tv, &sock);
  70.         if (client == NULL) {
  71.             return (NULL);
  72.         }
  73.         tv.tv_sec = 25;
  74.         clnt_control(client, CLSET_TIMEOUT, (caddr_t)&tv);
  75.         break;
  76.     case IPPROTO_TCP:
  77.         client = clnttcp_create(&sin, prog, vers, &sock, 0, 0);
  78.         if (client == NULL) {
  79.             return (NULL);
  80.         }
  81.         tv.tv_sec = 25;
  82.         tv.tv_usec = 0;
  83.         clnt_control(client, CLSET_TIMEOUT, (caddr_t)&tv);
  84.         break;
  85.     default:
  86.         rpc_createerr.cf_stat = RPC_SYSTEMERROR;
  87.         rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; 
  88.         return (NULL);
  89.     }
  90.     return (client);
  91. }
  92.