/* * Returns a new instance of an LDM proxy. Can take a while because it * establishes a connection to the LDM. * * Arguments: * host Identifier of the host on which an LDM server is * running. * instance Pointer to a pointer to the new instance. "*instance" * is set upon successful return. * Returns: * 0 Success. "*instance" is set. * LP_SYSTEM System error. "log_start()" called. * LP_TIMEDOUT Connection attempt timed-out. "log_start()" called. * LP_HOSTUNREACH Host is unreachable. "log_start()" called. * LP_RPC_ERROR RPC error. "log_start()" called. * LP_LDM_ERROR LDM error. "log_start()" called. */ LdmProxyStatus lp_new( const char* const host, LdmProxy** const instance) { LdmProxyStatus status = 0; /* success */ size_t nbytes = sizeof(LdmProxy); LdmProxy* proxy = (LdmProxy*)malloc(nbytes); if (NULL == proxy) { log_serror("Couldn't allocate %lu bytes for new LdmProxy", nbytes); status = LP_SYSTEM; } else { proxy->host = strdup(host); if (NULL == proxy->host) { LOG_SERROR1("Couldn't duplicate string \"%s\"", host); status = LP_SYSTEM; } else { CLIENT* clnt = NULL; ErrorObj* error = ldm_clnttcp_create_vers(host, LDM_PORT, 6, &clnt, NULL, NULL); if (!error) { proxy->version = 6; proxy->hiya = my_hiya_6; proxy->send = my_send_6; proxy->flush = my_flush_6; } else if (LDM_CLNT_BAD_VERSION == err_code(error)) { /* Couldn't connect due to protocol version. */ err_free(error); error = ldm_clnttcp_create_vers(host, LDM_PORT, 5, &clnt, NULL, NULL); if (!error) { proxy->version = 5; proxy->hiya = my_hiya_5; proxy->send = my_send_5; proxy->flush = my_flush_5; } } if (error) { LOG_START1("%s", err_message(error)); err_free(error); free(proxy->host); status = convertStatus(error); } else { proxy->clnt = clnt; proxy->rpcTimeout = rpcTimeout; } } /* "proxy->host" allocated */ if (LP_OK == status) { *instance = proxy; } else { free(proxy); } } /* "proxy" allocated */ return status; }
int main(int ac, char *av[]) { char myname[HOSTNAMESIZE]; char *progname = av[0]; char *logfname; unsigned version; prod_class_t clss; prod_spec spec; int seq_start = 0; int status; ErrorObj* error; unsigned remotePort = LDM_PORT; logfname = "-"; remote = "localhost"; (void)set_timestamp(&clss.from); clss.to = TS_ENDT; clss.psa.psa_len = 1; clss.psa.psa_val = &spec; spec.feedtype = DEFAULT_FEEDTYPE; spec.pattern = ".*"; { extern int optind; extern char *optarg; int ch; int logmask = (LOG_MASK(LOG_ERR) | LOG_MASK(LOG_WARNING) | LOG_MASK(LOG_NOTICE)); while ((ch = getopt(ac, av, "vxl:h:f:P:s:")) != EOF) switch (ch) { case 'v': logmask |= LOG_MASK(LOG_INFO); break; case 'x': logmask |= LOG_MASK(LOG_DEBUG); break; case 'l': logfname = optarg; break; case 'h': remote = optarg; break; case 'f': spec.feedtype = atofeedtypet(optarg); if(spec.feedtype == NONE) { fprintf(stderr, "Unknown feedtype \"%s\"\n", optarg); usage(progname); } break; case 'P': { char* suffix = ""; long port; errno = 0; port = strtol(optarg, &suffix, 0); if (0 != errno || 0 != *suffix || 0 >= port || 0xffff < port) { (void)fprintf(stderr, "%s: invalid port %s\n", av[0], optarg); usage(av[0]); } remotePort = (unsigned)port; break; } case 's': seq_start = atoi(optarg); break; case '?': usage(progname); break; } ac -= optind; av += optind; if(ac < 1) usage(progname); (void) setulogmask(logmask); } /* * Set up error logging */ (void) openulog(ubasename(progname), LOG_NOTIME, LOG_LDM, logfname); /* * register exit handler */ if(atexit(cleanup) != 0) { serror("atexit"); exit(1); } /* * set up signal handlers */ set_sigactions(); (void) strcpy(myname, ghostname()); /* * Contact the server. */ error = ldm_clnttcp_create_vers(remote, remotePort, SIX, &clnt, NULL, NULL); (void)exitIfDone(1); if (!error) { version = SIX; hiya = my_hiya_6; send_product = send_product_6; nullproc = nullproc_6; } else if (LDM_CLNT_BAD_VERSION == err_code(error)) { err_free(error); error = ldm_clnttcp_create_vers(remote, remotePort, FIVE, &clnt, NULL, NULL); (void)exitIfDone(1); if (!error) { version = FIVE; hiya = my_hiya_5; send_product = send_product_5; nullproc = NULL; } } if (error) { err_log(error, ERR_FAILURE); err_free(error); status = 1; } else { udebug("version %u", version); status = ldmsend(clnt, &clss, myname, seq_start, ac, av); } return status != 0; }