From b96a2130a43104195490f6b92e3feb709bc14ced Mon Sep 17 00:00:00 2001 From: =?utf8?q?Michal=20Voc=C5=AF?= Date: Thu, 2 Aug 2007 14:27:27 +0000 Subject: [PATCH] import --- org.glite.jobid.api-c/interface/cjobid.h | 121 +++++++++++++++ org.glite.jobid.api-c/src/cjobid.c | 258 +++++++++++++++++++++++++++++++ 2 files changed, 379 insertions(+) create mode 100755 org.glite.jobid.api-c/interface/cjobid.h create mode 100644 org.glite.jobid.api-c/src/cjobid.c diff --git a/org.glite.jobid.api-c/interface/cjobid.h b/org.glite.jobid.api-c/interface/cjobid.h new file mode 100755 index 0000000..03fc436 --- /dev/null +++ b/org.glite.jobid.api-c/interface/cjobid.h @@ -0,0 +1,121 @@ +#ifndef _GLITE_JOBID_H +#define _GLITE_JOBID_H + +/*! + * \file cjobid.h + * \brief L&B consumer API + */ + +#ident "$Header$" + +#ifdef __cplusplus +extern "C" { +#endif + +typedef struct _edg_wlc_JobId *glite_jobid_t; +typedef const struct _edg_wlc_JobId *glite_jobid_const_t; +typedef glite_jobid_t edg_wlc_JobId; + +#define edg_wlc_JobIdCreate glite_jobid_create +#define edg_wlc_JobIdRecreate glite_jobid_recreate +#define edg_wlc_JobIdDup glite_jobid_dup +#define edg_wlc_JobIdFree glite_jobid_free +#define edg_wlc_JobIdParse glite_jobid_parse +#define edg_wlc_JobIdUnparse glite_jobid_unparse +#define edg_wlc_JobIdGetServer glite_jobid_getServer +#define edg_wlc_JobIdGetServerParts glite_jobid_getServerParts +#define edg_wlc_JobIdGetUnique glite_jobid_getUnique + +#define GLITE_JOBID_DEFAULT_PORT 9000 /**< Default port where bookkeeping server listens */ +#define GLITE_JOBID_PROTO_PREFIX "https://" /**< JobId protocol prefix */ + + +/* All the pointer functions return malloc'ed objects (or NULL on error) */ + +/** + * Create a Job ID. + * See the lb_draft document for details on its construction and components + * \param bkserver book keeping server hostname + * \param port port for the bk service + * \param jobid new created job id + * \ret al 0 success + * \retval EINVAL invalid bkserver + * \retval ENOMEM if memory allocation fails + */ +int glite_jobid_create(const char * bkserver, int port, glite_jobid_t * jobid); + +/** + * Recreate a Job ID + * \param bkserver bookkeeping server hostname + * \param port port for the bk service + * \param unique string which represent created jobid (if NULL then new + * one is created) + * \param jobid new created job id + * \retval 0 success + * \retval EINVAL invalid bkserver + * \retval ENOMEM if memory allocation fails + */ +int glite_jobid_recreate(const char *bkserver, int port, const char * unique, edg_wlc_JobId * jobid); + +/** + * Create copy of Job ID + * \param in jobid for duplication + * \param jobid duplicated jobid + * \retval 0 for success + * \retval EINVAL invalid jobid + * \retval ENOMEM if memory allocation fails + */ +int glite_jobid_dup(glite_jobid_const_t in, glite_jobid_t * jobid); + +/* + * Free jobid structure + * \param jobid for dealocation + */ +void glite_jobid_free(glite_jobid_t jobid); + +/** + * Parse Job ID string and creates jobid structure + * \param jobidstr string representation of jobid + * \param jobid parsed job id + * \retval 0 for success + * \retval EINVAL jobidstr can't be parsed + * \retval ENOMEM if memory allocation fails + */ +int glite_jobid_parse(const char* jobidstr, glite_jobid_t * jobid); + +/** + * Unparse Job ID (produce the string form of JobId). + * \param jobid to be converted to string + * \return allocated string which represents jobid + */ +char* glite_jobid_unparse(const glite_jobid_t jobid); + +/** + * Extract bookkeeping server address (address:port) + * \param jobid from which the bkserver address should be extracted + * \retval pointer to allocated string with bkserver address + * \retval NULL if jobid is 0 or memory allocation fails + */ +char* glite_jobid_getServer(glite_jobid_const_t jobid); + +/** + * Extract bookkeeping server address and port + * \param jobid from which the bkserver address should be extracted + * \param srvName pointer where to return server name + * \param srvPort pointer where to return server port + * */ +void glite_jobid_getServerParts(glite_jobid_const_t jobid, char **srvName, unsigned int *srvPort); + +/** + * Extract unique string + * \param jobid + * \retval pointer to allocated unique string representing jobid + * \retval NULL if jobid is 0 or memory allocation fails + */ +char* glite_jobid_getUnique(glite_jobid_const_t jobid); + +#ifdef __cplusplus +} +#endif + +#endif /* _GLITE_JOBID_H */ diff --git a/org.glite.jobid.api-c/src/cjobid.c b/org.glite.jobid.api-c/src/cjobid.c new file mode 100644 index 0000000..ad8f2a3 --- /dev/null +++ b/org.glite.jobid.api-c/src/cjobid.c @@ -0,0 +1,258 @@ +#ident "$Header$" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include "glite/jobid/cjobid.h" +#include "strmd5.h" + +struct _edg_wlc_JobId { + char *id; /* unique job identification */ + /* additional information */ + char *BShost;/* bookkeeping server hostname */ + unsigned int BSport; /* bookkeeping server port */ + char *info; /* additional information (after ? in URI) */ +}; + +int glite_jobid_create(const char *bkserver, int port, glite_jobid_t *jobId) +{ + return glite_jobid_recreate(bkserver, port, NULL, jobId); +} + + +int glite_jobid_recreate(const char* bkserver, int port, const char *unique, glite_jobid_t *jobId) +{ + glite_jobid_t out; + char hostname[200]; /* used to hold string for encrypt */ + struct timeval tv; + int skip; + char* portbeg; + + struct hostent* he; + + if (!bkserver) + return EINVAL; + + if (unique == NULL) { + gethostname(hostname, 100); + he = gethostbyname(hostname); + assert(he->h_length > 0); + gettimeofday(&tv, NULL); + srandom(tv.tv_usec); + + skip = strlen(hostname); + skip += sprintf(hostname + skip, "-IP:0x%x-pid:%d-rnd:%d-time:%d:%d", + *((int*)he->h_addr_list[0]), getpid(), (int)random(), + (int)tv.tv_sec, (int)tv.tv_usec); + } + + *jobId = NULL; + out = (glite_jobid_t) malloc (sizeof(*out)); + if (!out) + return ENOMEM; + + memset(out, 0, sizeof(*out)); + + /* check if it begins with prefix */ + /* unsupported */ + if (strncmp(bkserver, GLITE_JOBID_PROTO_PREFIX, sizeof(GLITE_JOBID_PROTO_PREFIX)-1) == 0) + return EINVAL; + + out->BShost = strdup(bkserver); + portbeg = strchr(out->BShost, ':'); + if (portbeg) { + *portbeg = 0; + /* try to get port number */ + if (port == 0) + port = atoi(portbeg + 1); + } + + if (port == 0) + port = GLITE_JOBID_DEFAULT_PORT; + + out->BSport = port; + + out->id = (unique) ? strdup(unique) : str2md5base64(hostname); + //printf("Encrypt: %s\nBASE64 %s\n", hostname, out->id); + + if (!out->id || !out->BShost) { + glite_jobid_free(out); + return ENOMEM; + } + + *jobId = out; + return 0; +} + + +int glite_jobid_dup(glite_jobid_const_t in, glite_jobid_t *out) +{ + glite_jobid_t jid; + *out = NULL; + if (in == NULL) + return 0; + + jid = malloc(sizeof(*jid)); + if (!jid) + return ENOMEM; + + memset(jid, 0,sizeof(*jid)); + jid->BShost = strdup(in->BShost); + jid->id = strdup(in->id); + if (in->info) + jid->info = strdup(in->info); + + if (jid->BShost == NULL || jid->id == NULL) { + glite_jobid_free(jid); + return ENOMEM; + } + + jid->BSport = in->BSport; + *out = jid; + return 0; +} + + +// XXX +// use recreate +// parse name, port, unique +int glite_jobid_recreate(const char *idString, glite_jobid_t *jobId) +{ + char *pom, *pom1, *pom2; + glite_jobid_t out; + + *jobId = NULL; + + out = (glite_jobid_t) malloc (sizeof(*out)); + if (out == NULL ) + return ENOMEM; + + memset(out,0,sizeof(*out)); + + if (strncmp(idString, GLITE_JOBID_PROTO_PREFIX, sizeof(GLITE_JOBID_PROTO_PREFIX) - 1)) { + out->BShost = (char *) NULL; + out->BSport = 0; + + free(out); + return EINVAL; + } + + pom = strdup(idString + sizeof(GLITE_JOBID_PROTO_PREFIX) - 1); + pom1 = strchr(pom, '/'); + pom2 = strchr(pom, ':'); + + if (!pom1) { free(pom); free(out); return EINVAL; } + + if ( pom2 && (pom1 > pom2)) { + pom[pom2-pom] = '\0'; + out->BShost = strdup(pom); + pom[pom1-pom] = '\0'; + out->BSport = (unsigned int) strtoul(pom2 + 1,NULL,10); + } else { + pom[pom1-pom] = '\0'; + out->BShost = strdup(pom); + out->BSport = GLITE_JOBID_DEFAULT_PORT; + } + + /* XXX: localhost not supported in jobid + if (!strncmp(out->BShost,"localhost",9) { + free(pom); + free(out->BShost); + free(out); + return EINVAL; + } + */ + + /* additional info from URI */ + pom2 = strchr(pom1+1,'?'); + if (pom2) { + *pom2 = 0; + out->info = strdup(pom2+1); + } + + /* extract the unique part */ + out->id = strdup(pom1+1); + + for (pom1 = out->BShost; *pom1; pom1++) + if (isspace(*pom1)) break; + + for (pom2 = out->id; *pom2; pom2++) + if (isspace(*pom2)) break; + + if (*pom1 || *pom2) { + free(pom); + glite_jobid_free(out); + return EINVAL; + } + + free(pom); + *jobId = out; + return 0; +} + + +void glite_jobid_free(glite_jobid_t job) +{ + if (job) { + free(job->id); + free(job->BShost); + free(job->info); + free(job); + } +} + + +char* glite_jobid_unparse(glite_jobid_const_t jobid) +{ + char *out, port[40]; + + if (!jobid) + return NULL; + + if (jobid->BSport) + sprintf(port,":%d",jobid->BSport); + else + *port = 0; + + asprintf(&out, GLITE_JOBID_PROTO_PREFIX"%s%s/%s%s%s", + jobid->BShost,port, + jobid->id, + (jobid->info ? "?" : ""), + (jobid->info ? jobid->info : "")); + + return out; +} + + +char* glite_jobid_getServer(glite_jobid_const_t jobid) +{ + char *bs = NULL; + + if (jobid) + asprintf(&bs, "%s:%u", jobid->BShost, + jobid->BSport ? jobid->BSport : GLITE_JOBID_DEFAULT_PORT); + + return bs; +} + + +void glite_jobid_getServerParts(glite_jobid_const_t jobid, char **srvName, unsigned int *srvPort) +{ + if (jobid) { + *srvName = strdup(jobid->BShost); + *srvPort = jobid->BSport ? jobid->BSport : GLITE_JOBID_DEFAULT_PORT; + } +} + + +char* glite_jobid_getUnique(glite_jobid_const_t jobid) +{ + return jobid ? strdup(jobid->id) : NULL; +} -- 1.8.2.3