From 3df59a18a4317e408a57891751c843a99faf4c85 Mon Sep 17 00:00:00 2001 From: =?utf8?q?Franti=C5=A1ek=20Dvo=C5=99=C3=A1k?= Date: Fri, 25 May 2007 14:43:38 +0000 Subject: [PATCH] Initial import - moving from lb-utils.jobid. --- org.glite.lbjp-utils.jobid/.cvsignore | 2 + org.glite.lbjp-utils.jobid/LICENSE | 69 ++++++ org.glite.lbjp-utils.jobid/Makefile | 98 ++++++++ org.glite.lbjp-utils.jobid/interface/Exception.h | 138 +++++++++++ org.glite.lbjp-utils.jobid/interface/JobId.h | 126 ++++++++++ .../interface/JobIdExceptions.h | 80 +++++++ org.glite.lbjp-utils.jobid/interface/cjobid.h | 109 +++++++++ org.glite.lbjp-utils.jobid/interface/strmd5.h | 30 +++ org.glite.lbjp-utils.jobid/src/cjobid.c | 260 +++++++++++++++++++++ org.glite.lbjp-utils.jobid/src/strmd5.c | 122 ++++++++++ 10 files changed, 1034 insertions(+) create mode 100644 org.glite.lbjp-utils.jobid/.cvsignore create mode 100644 org.glite.lbjp-utils.jobid/LICENSE create mode 100644 org.glite.lbjp-utils.jobid/Makefile create mode 100644 org.glite.lbjp-utils.jobid/interface/Exception.h create mode 100644 org.glite.lbjp-utils.jobid/interface/JobId.h create mode 100644 org.glite.lbjp-utils.jobid/interface/JobIdExceptions.h create mode 100644 org.glite.lbjp-utils.jobid/interface/cjobid.h create mode 100644 org.glite.lbjp-utils.jobid/interface/strmd5.h create mode 100644 org.glite.lbjp-utils.jobid/src/cjobid.c create mode 100644 org.glite.lbjp-utils.jobid/src/strmd5.c diff --git a/org.glite.lbjp-utils.jobid/.cvsignore b/org.glite.lbjp-utils.jobid/.cvsignore new file mode 100644 index 0000000..1df717b --- /dev/null +++ b/org.glite.lbjp-utils.jobid/.cvsignore @@ -0,0 +1,2 @@ +.project +.cdtproject \ No newline at end of file diff --git a/org.glite.lbjp-utils.jobid/LICENSE b/org.glite.lbjp-utils.jobid/LICENSE new file mode 100644 index 0000000..01b973b --- /dev/null +++ b/org.glite.lbjp-utils.jobid/LICENSE @@ -0,0 +1,69 @@ +LICENSE file for EGEE Middleware +================================ + +Copyright (c) 2004 on behalf of the EU EGEE Project: +The European Organization for Nuclear Research (CERN), +Istituto Nazionale di Fisica Nucleare (INFN), Italy +Datamat Spa, Italy +Centre National de la Recherche Scientifique (CNRS), France +CS Systeme d'Information (CSSI), France +Royal Institute of Technology, Center for Parallel Computers (KTH-PDC), Sweden +Universiteit van Amsterdam (UvA), Netherlands +University of Helsinki (UH.HIP), Finlan +University of Bergen (UiB), Norway +Council for the Central Laboratory of the Research Councils (CCLRC), United Kingdom + +Redistribution and use in source and binary forms, with or without +modification, are permitted provided that the following conditions are +met: + +1. Redistributions of source code must retain the above copyright +notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright +notice, this list of conditions and the following disclaimer in the +documentation and/or other materials provided with the distribution. + +3. The end-user documentation included with the redistribution, if +any, must include the following acknowledgment: "This product includes +software developed by The EU EGEE Project (http://cern.ch/eu-egee/)." +Alternatively, this acknowledgment may appear in the software itself, if +and wherever such third-party acknowledgments normally appear. + +4. The names EGEE and the EU EGEE Project must not be +used to endorse or promote products derived from this software without +prior written permission. For written permission, please contact +. + +5. You are under no obligation whatsoever to provide anyone with any +bug fixes, patches, or upgrades to the features, functionality or +performance of the Software ("Enhancements") that you may develop over +time; however, if you choose to provide your Enhancements to The EU +EGEE Project, or if you choose to otherwise publish or distribute your +Enhancements, in source code form without contemporaneously requiring +end users of The EU EGEE Proejct to enter into a separate written license +agreement for such Enhancements, then you hereby grant The EU EGEE Project +a non-exclusive, royalty-free perpetual license to install, use, copy, +modify, prepare derivative works, incorporate into the EGEE Middleware +or any other computer software, distribute, and sublicense your +Enhancements or derivative works thereof, in binary and source code +form (if any), whether developed by The EU EGEE Project or third parties. + +THIS SOFTWARE IS PROVIDED "AS IS" AND ANY EXPRESSED OR IMPLIED +WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF +MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE +DISCLAIMED. IN NO EVENT SHALL PROJECT OR ITS CONTRIBUTORS BE LIABLE +FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR +CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF +SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR +BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, +WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE +OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN +IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + +This software consists of voluntary contributions made by many +individuals on behalf of the EU EGEE Prject. For more information on The +EU EGEE Project, please see http://cern.ch/eu-egee/. For more information on +EGEE Middleware, please see http://egee-jra1.web.cern.ch/egee-jra1/ + + diff --git a/org.glite.lbjp-utils.jobid/Makefile b/org.glite.lbjp-utils.jobid/Makefile new file mode 100644 index 0000000..986b5e0 --- /dev/null +++ b/org.glite.lbjp-utils.jobid/Makefile @@ -0,0 +1,98 @@ +# Default values +top_srcdir=. +builddir=build +top_builddir=${top_srcdir}/${builddir} +stagedir=. +distdir=. +globalprefix=glite +package=glite-lb-utils-jobid +version=0.0.0 +PREFIX=/opt/glite +lbuprefix=lbu + +-include Makefile.inc +-include ../project/version.properties + +version=${module.version} + +VPATH=${top_srcdir}/src:${top_srcdir}/test::${top_srcdir}/doc + +CC=gcc + +DEBUG:=-g -O0 -Wall + +CFLAGS:=${DEBUG} -I${top_srcdir}/src -I${top_srcdir}/interface -I. -D_GNU_SOURCE + +COMPILE:=libtool --mode=compile ${CC} +LINK:=libtool --mode=link ${CC} -rpath ${stagedir}/lib ${LDFLAGS} +INSTALL:=libtool --mode=install install + +OBJS:=cjobid.o strmd5.o +LOBJS:=${OBJS:.o=.lo} + +HDRS:=cjobid.h strmd5.h Exception.h JobId.h JobIdExceptions.h + +STATICLIB:=libglite_lbu_jobid.a +LTLIB:=libglite_lbu_jobid.la + +default: all + +all compile: ${STATICLIB} ${LTLIB} + +# to use libtool versioning correcty, we should have: +# +# current = major + minor + offset +# revision = patch +# age = minor +# +# where offset is a sum of maximal released minor's of all previous major's +# +# version_info=-version-info `echo ${version} | cut -d. -f1,2 | tr . :` + +# counted minors: n/a +offset=0 + +version_info:=-version-info ${shell \ + perl -e '$$,=":"; @F=split "\\.","${version}"; print $$F[0]+$$F[1]+${offset},$$F[2],$$F[1]' } + +${STATICLIB}: ${OBJS} + ar crv $@ ${OBJS} + ranlib $@ + +${LTLIB}: ${OBJS} + ${LINK} ${version_info} -o $@ ${LOBJS} + +stage: compile + $(MAKE) install PREFIX=${stagedir} DOSTAGE=yes + +check: + @true + +dist: distsrc distbin + +distsrc: + mkdir -p ${top_srcdir}/${package}-${version} + cd ${top_srcdir} && GLOBIGNORE="${package}-${version}" && cp -Rf * ${package}-${version} + cd ${top_srcdir} && tar -czf ${distdir}/${package}-${version}_src.tar.gz --exclude-from=project/tar_exclude ${package}-${version} + rm -rf ${top_srcdir}/${package}-${version} + +distbin: + $(MAKE) install PREFIX=`pwd`/tmpbuilddir${stagedir} + save_dir=`pwd`; cd tmpbuilddir${stagedir} && tar -czf $$save_dir/${top_srcdir}/${distdir}/${package}-${version}_bin.tar.gz *; cd $$save_dir + rm -rf tmpbuilddir + +install: + mkdir -p ${PREFIX}/lib + mkdir -p ${PREFIX}/share/doc/${package}-${version} + ${INSTALL} -m 644 ${LTLIB} ${PREFIX}/lib + ${INSTALL} -m 644 ${top_srcdir}/LICENSE ${PREFIX}/share/doc/${package}-${version} + if [ x${DOSTAGE} = xyes ]; then \ + mkdir -p ${PREFIX}/include/${globalprefix}/${lbuprefix} ; \ + (cd ${top_srcdir}/interface && install -m 644 ${HDRS} ${PREFIX}/include/${globalprefix}/${lbuprefix}) ; \ + install -m 644 ${STATICLIB} ${PREFIX}/lib; \ + fi + +clean: + +%.o: %.c + ${COMPILE} ${CFLAGS} -c $< diff --git a/org.glite.lbjp-utils.jobid/interface/Exception.h b/org.glite.lbjp-utils.jobid/interface/Exception.h new file mode 100644 index 0000000..21a9064 --- /dev/null +++ b/org.glite.lbjp-utils.jobid/interface/Exception.h @@ -0,0 +1,138 @@ +#ifndef GLITE_WMS_UTILS_EXCEPTION_EXCEPTION_H +#define GLITE_WMS_UTILS_EXCEPTION_EXCEPTION_H + +/* + * Exception.h + * Copyright (c) 2001 The European Datagrid Project - IST programme, all rights reserved. + * Contributors are mentioned in the code where appropriate. + */ + +#include +#include +//#include +#include // For logging exceptions to log file +#include // list the exception codes +#include +#include +#include // base ancestor stl::exception + + +namespace glite { + namespace lb_utils { + namespace exception { + +extern pthread_mutex_t METHOD_MUTEX; // used in order to store info into a file (rather then syslog) +#define GLITE_STACK_TRY(method_name) std::string METHOD = method_name ; int LINE = __LINE__ ; try { +#define GLITE_STACK_CATCH() } catch (glite::lb_utils::exception::Exception &exc){ exc.push_back ( __FILE__ , LINE, METHOD ); throw exc ; } catch (std::exception &ex){ glite::lb_utils::exception::Exception exc( __FILE__ , LINE, METHOD, 0, "Standard exception: " + std::string(ex.what()) ); throw exc; } + +/** + * The Exception base classe contains attributes into which are placed exception information and provides + * constructor that beyond the error code take parameters specifying the source file and line number + * (e.g. through __FILE__ and __LINE__) where the error has been generated and string messages, + * allowing an easy way of storing the origin of the exception. + * Moreover it provides methods for getting all the exception information and for logging them either + * in a log file or to the syslog daemon. + * Each of the derived types may contain its private attributes describing the actual error instance in detail. + * Moreover each exception has an attribute representing the exception identifier that is set by the + * class constructor and allows the identification of the original exception. + * + * @version 0.1 + * @date 22 July 2004 + * @author Alessandro Maraschini +*/ + +class Exception : public std::exception{ + public: + /** + * Constructor Update all mandatory fields + * @param method the name of the method that raised the exception + * @param source The source that raised the exception (could be the file path, the class Name, etc etc) + * @param exc the previous exception as in the stack trace */ + Exception ( const std::string& source, const std::string& method, Exception *exc); + /** + * Constructor Update all mandatory fields + * @param code the code representing the thrown exception + * @param exception the name of the thrown exception + * @param method the name of the method that raised the exception + * @param source The source that raised the exception (could be the file path, the class Name, etc etc) */ + Exception ( const std::string& source, const std::string& method, int code, const std::string& exception); + + /** + * Constructor Update all mandatory fields + * @param source the path of the file that raised the exception + * @param line_number the number of the line in the file that raised the exception + * @param method the name of the method that raised the exception + * @param code the code representing the thrown exception + * @param exception the name of the thrown exception */ + Exception (const std::string& source, int line_number, const std::string& method, int code, const std::string& exception); + /** + * Default Destructor + */ + virtual ~Exception() throw (); + /** + * Return a string debug message containing information about Exception thrown + * Debug message contains all the attributes stored in an exception instance such as the method, the file and the line + * that threw the exception. + *@return the debug message string representation + */ + virtual std::string dbgMessage(); + /** + * Return the error code + * @return The integer representing the code of the error that generated the exception + */ + virtual int getCode(); + + /** + * return the Error Message associated to the Exception + * @return The Exception string message representation + */ + virtual const char* what() const throw (); + + /** + * Print Exception error information into a log file + * @param logfile the file where to log exception information + */ + virtual void log(const std::string& logfile = ""); + /** + * Retrieve the Exception name + * @return the name of the Exception thrown + */ + virtual std::string getExceptionName(); + + /** + * Retrieve the Stack of the exception as a list of previous generated exceptions + *@return the string representation of the stack trace: each line correspond to an exception message + */ + virtual std::string printStackTrace() ; + /** + * Return the list of methods that caused the Exception + */ + virtual std::vector getStackTrace() ; + /** + * Update stack information + */ + virtual void push_back ( const std::string& source, int line_number, const std::string& method ) ; + protected: + /** Empty constructor*/ + Exception(); + /** integer error code representing the cause of the error */ + int error_code; + /** string exception message representation*/ + std::string error_message ; + /** line number where the exception was raised */ + int line; + /** The name of the file where the exception was raised */ + std::string source_file; + /** the name of the exception */ + std::string exception_name; + /** the name of the method where the expceiton was raised */ + std::string method_name ; + /** a string representation of the stacktrace */ + std::string stack; + /** the actual internal stacktrace representation */ + std::vector< std::string> stack_strings ; + /** the name of the ancestor exception */ + std::string ancestor ; +}; //End Exception Class +}}} // Closing namespace +#endif diff --git a/org.glite.lbjp-utils.jobid/interface/JobId.h b/org.glite.lbjp-utils.jobid/interface/JobId.h new file mode 100644 index 0000000..b99992d --- /dev/null +++ b/org.glite.lbjp-utils.jobid/interface/JobId.h @@ -0,0 +1,126 @@ +#ifndef GLITE_WMSUTILS_JOBID_JOBID_H +#define GLITE_WMSUTILS_JOBID_JOBID_H + +/* + * JobId.h + * Copyright (c) 2001 The European Datagrid Project - IST programme, all rights reserved. + * + */ + +#include +#include + +#include "glite/lb-utils/cjobid.h" + +typedef struct _glite_lbu_jobid_s* glite_lbu_jobid_t; + +namespace glite { +namespace lb_utils { +namespace jobid { + +/** + * Managing Identification, checking, retreiving info from a job + * File name: JobId.h + * The JobId class provides a representation of the Datagrid job identifier + * (dg_jobId) and the methods for manipulating it. + * We remind that the format of the dg_jobId is as follows: + * :/ + * + * @ingroup common + * @version 0.1 + * @date 15 April 2002 + * @author Alessandro Maraschini */ + +class JobId { +public: + /**@name Constructors/Destructor */ + //@{ + /** Instantiates an empty JobId object */ + JobId() ; + /** + * Instantiates a JobId object from the passed dg_jobId in string format. + * @param job_id_string a string representig a classAd expression + * @throws WrongIdException When a string is passed in a wrong format + */ + JobId(const std::string& job_id_string); + JobId(const JobId&); + JobId(const glite_lbu_JobId&); + /** + * Destructor + * Destroy the Job Id instance + */ + ~JobId() ; + //@} + + /**@name Miscellaneous */ + //@{ + /** Unsets the JobId instance. Clear all it's memebers */ + void clear() ; + /** + * Check wheater the jobId has been already created (true) or not (false) + *@return true (jobId created) or false (jobId not yet created) + */ + bool isSet() { return ( m_JobId != 0 ) ; } + /** + * Set the JobId instance according to the LB and RB server addresses and the unique string passed as input parameters. + * @param lb_server Loggin and Bookkeeping server address + * @param port Loggin and Bookkeeping port ( dafault value is 9000 ) + * @param unique A Unique identification ( automatically generatad by md5 protocol ) + * @throws WrongIdException When one parameter has been passed in a wrong format */ + void setJobId(const std::string& lb_server, int port = 0, const std::string& unique = ""); + //@} + /**@name Get Methods */ + //@{ + /** @return the LB address into its string format + * @throws EmptyIdException If the jobId has not been initialised yet */ + std::string getServer() const; + /** @return the Unique string into its string format + * @throws EmptyIdException If the jobId has not been initialised yet */ + std::string getUnique() const; + //@} + /** This method sets the JobId instance from the JobId in string format given + * as input. + * @param dg_JobId the string representing the job + * @throws WrongIdException When a string is passed in a wrong format */ + void fromString ( const std::string& dg_JobId ); + /** Converts the jobId into a string + @return the string representation of a JobId*/ + std::string toString() const; + /** casting operator */ + operator const glite_lbu_JobId() const { return m_JobId; } + /** Operator "=" create a deep copy of the JobId instance*/ + JobId & operator=(JobId const &); + /** Operator "=" create a deep copy of the JobId instance*/ + JobId & operator=(const glite_lbu_JobId &); + /** Retrieve the internal id reference + *@return the JobId internal reference used by some LB methods */ + glite_lbu_JobId getId() const ; +private: + // This Variable stores the Job unique identification String + glite_lbu_JobId m_JobId; + mutable char* m_pStr; + mutable char* m_pBkserver; + mutable char* m_pUnique; + /** Operator "<"*/ + friend bool operator<(JobId const& lhs, JobId const& rhs); + /** Operator "=="*/ + friend bool operator==(JobId const& lhs, JobId const& rhs); +}; + +inline bool operator<(JobId const& lhs, JobId const& rhs) +{ + return strcmp ( lhs.m_pStr , rhs.m_pStr ) <0 ; +} + +inline bool operator==(JobId const& lhs, JobId const& rhs) +{ +return strcmp ( lhs.m_pStr , rhs.m_pStr ) ==0 ; +} + +std::ostream& operator<<(std::ostream& os, JobId const& id); + +} // namespace jobid +} // namespace lb_utils +} // namespace glite + +#endif // GLITE_WMSUTILS_JOBID_JOBID_H diff --git a/org.glite.lbjp-utils.jobid/interface/JobIdExceptions.h b/org.glite.lbjp-utils.jobid/interface/JobIdExceptions.h new file mode 100644 index 0000000..d154adf --- /dev/null +++ b/org.glite.lbjp-utils.jobid/interface/JobIdExceptions.h @@ -0,0 +1,80 @@ +#ifndef GLITE_WMSUTILS_JOBID_EXCEPTIONS_H +#define GLITE_WMSUTILS_JOBID_EXCEPTIONS_H + +/* + * JobIdExceptions.h + * Copyright (c) 2001 The European Datagrid Project - IST programme, all rights reserved. + */ + +#include "glite/lb-utils/Exception.h" + +namespace glite { +namespace lb_utils { +namespace jobid { + +/** + * JobIdException - Exception thrown by JobId Class + * @ingroup Common + * @version 0.1 + * @date 15 April 2002 + * @author Alessandro Maraschini +*/ + +class JobIdException : public glite::lb_utils::exception::Exception { +public: + /** + * Update all mandatory Exception Information + */ + JobIdException (const std::string& file, + int line, + const std::string& method, + int code, + const std::string& exception_name) ; +};//End CLass JobIdException + +/** +* WrongIdFieldException +* This Exception is thrown when a Job Id syntax error is found +* A valid Job Identification string should be made as follows: +* :/ */ +class WrongIdException : public JobIdException { +public: + /** + * Constructor + * @param file - The source file which has generated the Exception + * @param line - The line number in the source file where the Exception has been thrown + * @param method - The Name of the method which has thrown the Exception + * @param code - The Code of the Error raised + * @param field - The wrong expression catched */ + WrongIdException(const std::string& file, + int line, + const std::string& method, + int code ); +}; //End CLass WrongIdException +/** +* EmptyIdException +* This Exception is thrown when the user tries to get information from a JobId +* which has not been initialized yet, i.e tries to use the get Methods +*/ +class EmptyIdException : public JobIdException { +public: + /** + * Constructor + * @param file - The source file which has generated the Exception + * @param line - The line number in the source file where the Exception has been thrown + * @param method - The Name of the method which has thrown the Exception + * @param code - The Code of the Error raised + * @param field - The Empty filed requested for */ + EmptyIdException::EmptyIdException(const std::string& file, + int line, + const std::string& method, + int code , + const std::string& field ); +}; //End CLass EmptyIdException + +} // namespace jobid +} // namespace lb_utils +} // namespace glite + +#endif // GLITE_WMSUTILS_JOBID_EXCEPTIONS_H + diff --git a/org.glite.lbjp-utils.jobid/interface/cjobid.h b/org.glite.lbjp-utils.jobid/interface/cjobid.h new file mode 100644 index 0000000..f802ce7 --- /dev/null +++ b/org.glite.lbjp-utils.jobid/interface/cjobid.h @@ -0,0 +1,109 @@ +#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 _glite_lbu_JobId *glite_lbu_JobId; + +#define GLITE_WMSC_JOBID_DEFAULT_PORT 9000 /**< Default port where bookkeeping server listens */ +#define GLITE_WMSC_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_lbu_JobIdCreate(const char * bkserver, int port, glite_lbu_JobId * 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_lbu_JobIdRecreate(const char *bkserver, int port, const char * unique, glite_lbu_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_lbu_JobIdDup(const glite_lbu_JobId in, glite_lbu_JobId * jobid); + +/* + * Free jobid structure + * \param jobid for dealocation + */ +void glite_lbu_JobIdFree(glite_lbu_JobId 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_lbu_JobIdParse(const char* jobidstr, glite_lbu_JobId * 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_lbu_JobIdUnparse(const glite_lbu_JobId 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_lbu_JobIdGetServer(const glite_lbu_JobId 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_lbu_JobIdGetServerParts(const glite_lbu_JobId 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_lbu_JobIdGetUnique(const glite_lbu_JobId jobid); + +#ifdef __cplusplus +} +#endif + +#endif /* _GLITE_JOBID_H */ diff --git a/org.glite.lbjp-utils.jobid/interface/strmd5.h b/org.glite.lbjp-utils.jobid/interface/strmd5.h new file mode 100644 index 0000000..27d8544 --- /dev/null +++ b/org.glite.lbjp-utils.jobid/interface/strmd5.h @@ -0,0 +1,30 @@ +#ifndef _GLITE_STRMD5_H +#define _GLITE_STRMD5_H + +#ident "$Header$" + +/* Compute MD5 sum of the first argument. + * The sum is returned in the 16-byte array pointed to by 2nd argument + * (if not NULL) + * + * Return value: ASCII string of the sum, i.e. 32 characters [0-9a-f] + * (pointer to static area, changed by subsequent calls) + */ + +#ifdef USE_MBUF +char *strmd5(const char *src, unsigned char *dst); +#endif + +/** + * Returns: allocated 32bytes long ASCII string with md5 sum + * of the first argument + */ +char *str2md5(const char *src); + +/** + * Returns: allocated 22bytes long ASCII string with md5 sum in base64 + * format of the source argument + */ +char *str2md5base64(const char *src); + +#endif /* _GLITE_STRMD5_H */ diff --git a/org.glite.lbjp-utils.jobid/src/cjobid.c b/org.glite.lbjp-utils.jobid/src/cjobid.c new file mode 100644 index 0000000..6d6a73f --- /dev/null +++ b/org.glite.lbjp-utils.jobid/src/cjobid.c @@ -0,0 +1,260 @@ +#ident "$Header$" + +#include +#include +#include +#include +#include + +#include +#include +#include + +#include + +#include "cjobid.h" +#include "strmd5.h" + +struct _glite_lbu_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_lbu_JobIdCreate(const char *bkserver, int port, glite_lbu_JobId *jobId) +{ + return glite_lbu_JobIdRecreate(bkserver, port, NULL, jobId); +} + + +int glite_lbu_JobIdRecreate(const char* bkserver, int port, const char *unique, glite_lbu_JobId *jobId) +{ + glite_lbu_JobId 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_lbu_JobId) malloc (sizeof(*out)); + if (!out) + return ENOMEM; + + memset(out, 0, sizeof(*out)); + + /* check if it begins with prefix */ + /* unsupported */ + if (strncmp(bkserver, GLITE_WMSC_JOBID_PROTO_PREFIX, sizeof(GLITE_WMSC_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_WMSC_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_lbu_JobIdFree(out); + return ENOMEM; + } + + *jobId = out; + return 0; +} + + +int glite_lbu_JobIdDup(const glite_lbu_JobId in, glite_lbu_JobId *out) +{ + glite_lbu_JobId 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_lbu_JobIdFree(jid); + return ENOMEM; + } + + jid->BSport = in->BSport; + *out = jid; + return 0; +} + + +// XXX +// use recreate +// parse name, port, unique +int glite_lbu_JobIdParse(const char *idString, glite_lbu_JobId *jobId) +{ + char *pom, *pom1, *pom2; + glite_lbu_JobId out; + + *jobId = NULL; + + out = (glite_lbu_JobId) malloc (sizeof(*out)); + if (out == NULL ) + return ENOMEM; + + memset(out,0,sizeof(*out)); + + if (strncmp(idString, GLITE_WMSC_JOBID_PROTO_PREFIX, sizeof(GLITE_WMSC_JOBID_PROTO_PREFIX) - 1)) { + out->BShost = (char *) NULL; + out->BSport = 0; + + free(out); + return EINVAL; + } + + pom = strdup(idString + sizeof(GLITE_WMSC_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_WMSC_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_lbu_JobIdFree(out); + return EINVAL; + } + + free(pom); + *jobId = out; + return 0; +} + + +void glite_lbu_JobIdFree(glite_lbu_JobId job) +{ + if (job) { + free(job->id); + free(job->BShost); + free(job->info); + free(job); + } +} + + +char* glite_lbu_JobIdUnparse(const glite_lbu_JobId jobid) +{ + char *out, port[40]; + + if (!jobid) + return NULL; + + if (jobid->BSport) + sprintf(port,":%d",jobid->BSport); + else + *port = 0; + + asprintf(&out, GLITE_WMSC_JOBID_PROTO_PREFIX"%s%s/%s%s%s", + jobid->BShost,port, + jobid->id, + (jobid->info ? "?" : ""), + (jobid->info ? jobid->info : "")); + + return out; +} + + +char* glite_lbu_JobIdGetServer(const glite_lbu_JobId jobid) +{ + char *bs = NULL; + + if (jobid) + asprintf(&bs, "%s:%u", jobid->BShost, + jobid->BSport ? jobid->BSport : GLITE_WMSC_JOBID_DEFAULT_PORT); + + return bs; +} + + +void glite_lbu_JobIdGetServerParts(const glite_lbu_JobId jobid, char **srvName, unsigned int *srvPort) +{ + if (jobid) { + *srvName = strdup(jobid->BShost); + *srvPort = jobid->BSport ? jobid->BSport : GLITE_WMSC_JOBID_DEFAULT_PORT; + } +} + + +char* glite_lbu_JobIdGetUnique(const glite_lbu_JobId jobid) +{ + return jobid ? strdup(jobid->id) : NULL; +} diff --git a/org.glite.lbjp-utils.jobid/src/strmd5.c b/org.glite.lbjp-utils.jobid/src/strmd5.c new file mode 100644 index 0000000..775aee8 --- /dev/null +++ b/org.glite.lbjp-utils.jobid/src/strmd5.c @@ -0,0 +1,122 @@ +#ident "$Header$" + +#include +#include +#include +#include + +#include "strmd5.h" + +#if USE_MBUF /* let's try without it */ +#warning Thread unsafe! +static char mbuf[33]; +#endif + +static int base64_encode(const void *enc, int enc_size, char *out, int out_max_size) +{ + static const char* b64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789-_"; + + unsigned char* enc_buf = (unsigned char*)enc; + int out_size = 0; + unsigned int bits = 0; + unsigned int shift = 0; + + while ( out_size < out_max_size ) { + if ( enc_size>0 ) { + // Shift in byte + bits <<= 8; + bits |= *enc_buf; + shift += 8; + // Next byte + enc_buf++; + enc_size--; + } else if ( shift>0 ) { + // Pad last bits to 6 bits - will end next loop + bits <<= 6 - shift; + shift = 6; + } else { + // Terminate with Mime style '=' + *out = '='; + out_size++; + + return out_size; + } + + // Encode 6 bit segments + while ( shift>=6 ) { + shift -= 6; + *out = b64[ (bits >> shift) & 0x3F ]; + out++; + out_size++; + } + } + + // Output overflow + return -1; +} + +#ifdef USE_MBUF +char *strmd5(const char *s, unsigned char *digest) +{ + MD5_CTX md5; + unsigned char d[16]; + int i; + + MD5_Init(&md5); + MD5_Update(&md5,s,strlen(s)); + MD5_Final(d,&md5); + + if (digest) memcpy(digest,d,sizeof(d)); + + for (i=0; i<16; i++) { + int dd = d[i] & 0x0f; + mbuf[2*i+1] = dd<10 ? dd+'0' : dd-10+'a'; + dd = d[i] >> 4; + mbuf[2*i] = dd<10 ? dd+'0' : dd-10+'a'; + } + mbuf[32] = 0; + return (char *) mbuf; +} +#endif + +char *str2md5(const char *s) +{ + MD5_CTX md5; + unsigned char d[16]; + char* ret = malloc(33); + int i; + + if (!ret) + return NULL; + + MD5_Init(&md5); + MD5_Update(&md5, s, strlen(s)); + MD5_Final(d, &md5); + + for (i=0; i<16; i++) { + int dd = d[i] & 0x0f; + ret[2*i+1] = dd<10 ? dd+'0' : dd-10+'a'; + dd = d[i] >> 4; + ret[2*i] = dd<10 ? dd+'0' : dd-10+'a'; + } + ret[32] = 0; + return ret; +} + +char *str2md5base64(const char *s) +{ + MD5_CTX md5; + unsigned char d[16]; + char buf[50]; + int l; + + MD5_Init(&md5); + MD5_Update(&md5, s, strlen(s)); + MD5_Final(d, &md5); + + l = base64_encode(d, 16, buf, sizeof(buf) - 1); + if (l < 1) + return NULL; + buf[l - 1] = 0; + return strdup(buf); +} -- 1.8.2.3