From 8097c19e8e82dc09d33753182d765d02c5fb9413 Mon Sep 17 00:00:00 2001 From: miker Date: Mon, 19 Nov 2007 03:19:09 +0000 Subject: [PATCH] uber-patch from Scott McKellar cleans up large amounts of const-correctness issues and static-ifies several areas of unused header-based code; several cleanups to use simpler APIs such as buffer_release git-svn-id: svn://svn.open-ils.org/OpenSRF/trunk@1130 9efc2488-bf62-4759-914b-345cdb29e865 --- include/opensrf/osrf_app_session.h | 33 ++++++++-------- include/opensrf/osrf_application.h | 36 +++++++++--------- include/opensrf/osrf_cache.h | 10 ++--- include/opensrf/transport_client.h | 4 +- include/opensrf/transport_session.h | 8 ---- include/opensrf/utils.h | 14 +++---- src/c-apps/osrf_dbmath.c | 4 +- src/c-apps/osrf_math.c | 4 +- src/c-apps/osrf_version.c | 8 ++-- src/libopensrf/osrf_app_session.c | 46 ++++++++++++---------- src/libopensrf/osrf_application.c | 56 +++++++++++++-------------- src/libopensrf/osrf_cache.c | 38 +++++++++---------- src/libopensrf/osrf_json_tools.c | 25 +++++------- src/libopensrf/osrf_json_xml.c | 35 ++++++----------- src/libopensrf/osrf_system.c | 6 +-- src/libopensrf/transport_client.c | 15 +++----- src/libopensrf/transport_session.c | 59 +++++++++++++++++++++++------ src/libopensrf/utils.c | 14 +++---- src/router/osrf_router.c | 34 ++++++++++------- src/router/osrf_router.h | 27 +++++++------ 20 files changed, 250 insertions(+), 226 deletions(-) diff --git a/include/opensrf/osrf_app_session.h b/include/opensrf/osrf_app_session.h index 561454e..3f9c391 100644 --- a/include/opensrf/osrf_app_session.h +++ b/include/opensrf/osrf_app_session.h @@ -93,46 +93,46 @@ typedef struct osrf_app_session_struct osrfAppSession; // -------------------------------------------------------------------------- /** Allocates a initializes a new app_session */ -osrf_app_session* osrfAppSessionClientInit( char* remote_service ); -osrf_app_session* osrf_app_client_session_init( char* remote_service ); +osrf_app_session* osrfAppSessionClientInit( const char* remote_service ); +osrf_app_session* osrf_app_client_session_init( const char* remote_service ); /** Allocates and initializes a new server session. The global session cache * is checked to see if this session already exists, if so, it's returned */ osrf_app_session* osrf_app_server_session_init( - char* session_id, char* our_app, char* remote_id ); + const char* session_id, const char* our_app, const char* remote_id ); /** sets the default locale for a session **/ char* osrf_app_session_set_locale( osrf_app_session*, const char* ); /** returns a session from the global session hash */ -osrf_app_session* osrf_app_session_find_session( char* session_id ); +osrf_app_session* osrf_app_session_find_session( const char* session_id ); /** Builds a new app_request object with the given payload andn returns * the id of the request. This id is then used to perform work on the * requeset. */ int osrfAppSessionMakeRequest( - osrf_app_session* session, jsonObject* params, - char* method_name, int protocol, string_array* param_strings); + osrf_app_session* session, const jsonObject* params, + const char* method_name, int protocol, string_array* param_strings); int osrf_app_session_make_req( - osrf_app_session* session, jsonObject* params, - char* method_name, int protocol, string_array* param_strings); + osrf_app_session* session, const jsonObject* params, + const char* method_name, int protocol, string_array* param_strings); int osrfAppSessionMakeLocaleRequest( - osrf_app_session* session, jsonObject* params, - char* method_name, int protocol, string_array* param_strings, char* locale); + osrf_app_session* session, const jsonObject* params, const char* method_name, + int protocol, string_array* param_strings, char* locale); int osrf_app_session_make_locale_req( - osrf_app_session* session, jsonObject* params, - char* method_name, int protocol, string_array* param_strings, char* locale); + osrf_app_session* session, const jsonObject* params, const char* method_name, + int protocol, string_array* param_strings, char* locale); /** Sets the given request to complete state */ void osrf_app_session_set_complete( osrf_app_session* session, int request_id ); /** Returns true if the given request is complete */ -int osrf_app_session_request_complete( osrf_app_session* session, int request_id ); +int osrf_app_session_request_complete( const osrf_app_session* session, int request_id ); /** Does a recv call on the given request */ osrf_message* osrfAppSessionRequestRecv( @@ -150,7 +150,7 @@ int osrf_app_session_request_resend( osrf_app_session*, int request_id ); void osrf_app_session_reset_remote( osrf_app_session* ); /** Sets the remote target to 'remote_id' */ -void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ); +void osrf_app_session_set_remote( osrf_app_session* session, const char* remote_id ); /** pushes the given message into the result list of the app_request * whose request_id matches the messages thread_trace @@ -235,8 +235,9 @@ int _osrf_app_session_send( osrf_app_session*, osrf_message* msg ); int osrfAppSessionSendBatch( osrf_app_session*, osrf_message* msgs[], int size ); -int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data ); -int osrfAppRequestRespondComplete( osrfAppSession* ses, int requestId, jsonObject* data ); +int osrfAppRequestRespond( osrfAppSession* ses, int requestId, const jsonObject* data ); +int osrfAppRequestRespondComplete( + osrfAppSession* ses, int requestId, const jsonObject* data ); int osrfAppSessionStatus( osrfAppSession* ses, int type, char* name, int reqId, char* message ); diff --git a/include/opensrf/osrf_application.h b/include/opensrf/osrf_application.h index 20bb5e0..3b28f6b 100644 --- a/include/opensrf/osrf_application.h +++ b/include/opensrf/osrf_application.h @@ -120,7 +120,7 @@ typedef struct _osrfMethodContextStruct osrfMethodContext; @param soFile The library (.so) file that implements this application @return 0 on success, -1 on error */ -int osrfAppRegisterApplication( char* appName, char* soFile ); +int osrfAppRegisterApplication( const char* appName, const char* soFile ); /** Register a method @@ -134,22 +134,22 @@ int osrfAppRegisterApplication( char* appName, char* soFile ); @param streaming True if this is a streaming method that requires an atomic version @return 0 on success, -1 on error */ -int osrfAppRegisterMethod( char* appName, char* methodName, - char* symbolName, char* notes, int argc, int options ); +int osrfAppRegisterMethod( const char* appName, const char* methodName, + const char* symbolName, const char* notes, int argc, int options ); -int osrfAppRegisterExtendedMethod( char* appName, char* methodName, - char* symbolName, char* notes, int argc, int options, void* ); +int osrfAppRegisterExtendedMethod( const char* appName, const char* methodName, + const char* symbolName, const char* notes, int argc, int options, void* ); -osrfMethod* _osrfAppBuildMethod( char* methodName, - char* symbolName, char* notes, int argc, int options, void* ); +osrfMethod* _osrfAppBuildMethod( const char* methodName, const char* symbolName, + const char* notes, int argc, int options, void* ); /** Finds the given app in the list of apps @param name The name of the application @return The application pointer or NULL if there is no such application */ -osrfApplication* _osrfAppFindApplication( char* name ); +osrfApplication* _osrfAppFindApplication( const char* name ); /** Finds the given method for the given app @@ -158,7 +158,7 @@ osrfApplication* _osrfAppFindApplication( char* name ); @return A method pointer or NULL if no such method exists for the given application */ -osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ); +osrfMethod* _osrfAppFindMethod( const char* appName, const char* methodName ); /** Finds the given method for the given app @@ -167,7 +167,7 @@ osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ); @return A method pointer or NULL if no such method exists for the given application */ -osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ); +osrfMethod* __osrfAppFindMethod( osrfApplication* app, const char* methodName ); /** @@ -178,7 +178,7 @@ osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ); @params reqId The request id for this request @param params The method parameters */ -int osrfAppRunMethod( char* appName, char* methodName, +int osrfAppRunMethod( const char* appName, const char* methodName, osrfAppSession* ses, int reqId, jsonObject* params ); @@ -197,7 +197,7 @@ int __osrfAppRunSystemMethod(osrfMethodContext* context); /** Registers all of the system methods for this app so that they may be treated the same as other methods */ -int __osrfAppRegisterSysMethods( char* app ); +int __osrfAppRegisterSysMethods( const char* app ); @@ -208,14 +208,14 @@ int __osrfAppRegisterSysMethods( char* app ); @param msg The debug message to send to the client @return 0 on successfully sending of the message, -1 otherwise */ -int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, ... ); +int osrfAppRequestRespondException( osrfAppSession* ses, int request, const char* msg, ... ); int __osrfAppPostProcess( osrfMethodContext* context, int retcode ); -int osrfAppRespond( osrfMethodContext* context, jsonObject* data ); -int _osrfAppRespond( osrfMethodContext* context, jsonObject* data, int complete ); -int osrfAppRespondComplete( osrfMethodContext* context, jsonObject* data ); +int osrfAppRespond( osrfMethodContext* context, const jsonObject* data ); +int _osrfAppRespond( osrfMethodContext* context, const jsonObject* data, int complete ); +int osrfAppRespondComplete( osrfMethodContext* context, const jsonObject* data ); /* OSRF_METHOD_ATOMIC and/or OSRF_METHOD_CACHABLE and/or 0 for no special options */ //int osrfAppProcessMethodOptions( char* method ); @@ -227,8 +227,8 @@ int osrfAppEcho( osrfMethodContext* ctx ); /** * Tells the backend process to run its child init function */ -int osrfAppRunChildInit(char* appname); -void osrfAppSetOnExit(osrfApplication* app, char* appName); +int osrfAppRunChildInit(const char* appname); +void osrfAppSetOnExit(osrfApplication* app, const char* appName); void osrfAppRunExitCode(); diff --git a/include/opensrf/osrf_cache.h b/include/opensrf/osrf_cache.h index cf7238d..7545237 100644 --- a/include/opensrf/osrf_cache.h +++ b/include/opensrf/osrf_cache.h @@ -30,7 +30,7 @@ GNU General Public License for more details. @param maxCacheSeconds The maximum amount of time an object / string may be cached. Negative number means there is no limit */ -int osrfCacheInit( char* serverStrings[], int size, time_t maxCacheSeconds ); +int osrfCacheInit( const char* serverStrings[], int size, time_t maxCacheSeconds ); /** @@ -58,25 +58,25 @@ int osrfCachePutString( char* key, const char* value, time_t seconds); @param key The cache key @return The object (which must be freed) if it exists, otherwise returns NULL */ -jsonObject* osrfCacheGetObject( char* key, ... ); +jsonObject* osrfCacheGetObject( const char* key, ... ); /** Grabs a string from the cache. @param key The cache key @return The string (which must be freed) if it exists, otherwise returns NULL */ -char* osrfCacheGetString( char* key, ... ); +char* osrfCacheGetString( const char* key, ... ); /** Removes the item with the given key from the cache. @return 0 on success, -1 on error. */ -int osrfCacheRemove( char* key, ... ); +int osrfCacheRemove( const char* key, ... ); /** * Sets the expire time to 'seconds' for the given key */ -int osrfCacheSetExpire( time_t seconds, char* key, ... ); +int osrfCacheSetExpire( time_t seconds, const char* key, ... ); diff --git a/include/opensrf/transport_client.h b/include/opensrf/transport_client.h index e61f840..89a51e3 100644 --- a/include/opensrf/transport_client.h +++ b/include/opensrf/transport_client.h @@ -50,7 +50,7 @@ transport_client* client_init( const char* server, int port, const char* unix_pa // success, 0 otherwise. // --------------------------------------------------------------------------- int client_connect( transport_client* client, - char* username, char* password, char* resource, + const char* username, const char* password, const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ); @@ -71,7 +71,7 @@ int client_send_message( transport_client* client, transport_message* msg ); // --------------------------------------------------------------------------- // Returns 1 if this client is currently connected to the server, 0 otherwise // --------------------------------------------------------------------------- -int client_connected( transport_client* client ); +int client_connected( const transport_client* client ); // --------------------------------------------------------------------------- // This is the message handler required by transport_session. This handler diff --git a/include/opensrf/transport_session.h b/include/opensrf/transport_session.h index 5aa8597..a5420c6 100644 --- a/include/opensrf/transport_session.h +++ b/include/opensrf/transport_session.h @@ -189,14 +189,6 @@ typedef struct transport_session_struct transport_session; transport_session* init_transport( const char* server, int port, const char* unix_path, void* user_data, int component ); -// ------------------------------------------------------------------ -// Returns the value of the given XML attribute -// The xmlChar** construct is commonly returned from SAX event -// handlers. Pass that in with the name of the attribute you want -// to retrieve. -// ------------------------------------------------------------------ -char* get_xml_attr( const xmlChar** atts, char* attr_name ); - // ------------------------------------------------------------------ // Waits at most 'timeout' seconds for data to arrive from the // TCP handler. A timeout of -1 means to wait indefinitely. diff --git a/include/opensrf/utils.h b/include/opensrf/utils.h index ebf05a5..a42bc06 100644 --- a/include/opensrf/utils.h +++ b/include/opensrf/utils.h @@ -165,10 +165,10 @@ GNU General Public License for more details. truncation may occurr. */ int init_proc_title( int argc, char* argv[] ); -int set_proc_title( char* format, ... ); +int set_proc_title( const char* format, ... ); -int daemonize(); +int daemonize( void ); void* safe_malloc(int size); void* safe_calloc(int size); @@ -188,10 +188,10 @@ growing_buffer* buffer_init( int initial_num_bytes); // XXX This isn't defined in utils.c!! removing for now... //int buffer_addchar(growing_buffer* gb, char c); -int buffer_add(growing_buffer* gb, char* c); +int buffer_add(growing_buffer* gb, const char* c); int buffer_fadd(growing_buffer* gb, const char* format, ... ); int buffer_reset( growing_buffer* gb); -char* buffer_data( growing_buffer* gb); +char* buffer_data( const growing_buffer* gb); char* buffer_release( growing_buffer* gb ); int buffer_free( growing_buffer* gb ); int buffer_add_char(growing_buffer* gb, char c); @@ -226,11 +226,11 @@ int clr_fl( int fd, int flags ); // Utility method -double get_timestamp_millis(); +double get_timestamp_millis( void ); /* returns true if the whole string is a number */ -int stringisnum(char* s); +int stringisnum(const char* s); /* reads a file and returns the string version of the file user is responsible for freeing the returned char* @@ -243,7 +243,7 @@ char* file_to_string(const char* filename); Calculates the md5 of the text provided. The returned string must be freed by the caller. */ -char* md5sum( char* text, ... ); +char* md5sum( const char* text, ... ); /** diff --git a/src/c-apps/osrf_dbmath.c b/src/c-apps/osrf_dbmath.c index a88db87..41ace40 100644 --- a/src/c-apps/osrf_dbmath.c +++ b/src/c-apps/osrf_dbmath.c @@ -47,8 +47,8 @@ int osrfMathRun( osrfMethodContext* ctx ) { OSRF_METHOD_VERIFY_CONTEXT(ctx); - jsonObject* x = jsonObjectGetIndex(ctx->params, 0); - jsonObject* y = jsonObjectGetIndex(ctx->params, 1); + const jsonObject* x = jsonObjectGetIndex(ctx->params, 0); + const jsonObject* y = jsonObjectGetIndex(ctx->params, 1); if( x && y ) { diff --git a/src/c-apps/osrf_math.c b/src/c-apps/osrf_math.c index 7c3cf29..f2e614d 100644 --- a/src/c-apps/osrf_math.c +++ b/src/c-apps/osrf_math.c @@ -58,8 +58,8 @@ int osrfMathRun( osrfMethodContext* ctx ) { OSRF_METHOD_VERIFY_CONTEXT(ctx); /* see osrf_application.h */ /* collect the request params */ - jsonObject* x = jsonObjectGetIndex(ctx->params, 0); - jsonObject* y = jsonObjectGetIndex(ctx->params, 1); + const jsonObject* x = jsonObjectGetIndex(ctx->params, 0); + const jsonObject* y = jsonObjectGetIndex(ctx->params, 1); if( x && y ) { diff --git a/src/c-apps/osrf_version.c b/src/c-apps/osrf_version.c index 9d07874..dbe845b 100644 --- a/src/c-apps/osrf_version.c +++ b/src/c-apps/osrf_version.c @@ -49,10 +49,10 @@ int osrfVersion( osrfMethodContext* ctx ) { return 0; } - jsonObject* serv = jsonObjectGetIndex(ctx->params, 0); - jsonObject* meth = jsonObjectGetIndex(ctx->params, 1); - char* service = jsonObjectGetString(serv); - char* methd = jsonObjectGetString(meth); + const jsonObject* serv = jsonObjectGetIndex(ctx->params, 0); + const jsonObject* meth = jsonObjectGetIndex(ctx->params, 1); + const char* service = jsonObjectGetString(serv); + const char* methd = jsonObjectGetString(meth); if( service && methd ) { /* shove the additional params into an array */ diff --git a/src/libopensrf/osrf_app_session.c b/src/libopensrf/osrf_app_session.c index bf852b2..c1a3246 100644 --- a/src/libopensrf/osrf_app_session.c +++ b/src/libopensrf/osrf_app_session.c @@ -21,6 +21,7 @@ osrf_app_request* _osrf_app_request_init( req->complete = 0; req->payload = msg; req->result = NULL; + req->reset_timeout = 0; return req; @@ -182,7 +183,7 @@ char* osrf_app_session_set_locale( osrf_app_session* session, const char* locale } /** returns a session from the global session hash */ -osrf_app_session* osrf_app_session_find_session( char* session_id ) { +osrf_app_session* osrf_app_session_find_session( const char* session_id ) { if(session_id) return osrfHashGet(osrfAppSessionCache, session_id); return NULL; } @@ -196,13 +197,13 @@ void _osrf_app_session_push_session( osrf_app_session* session ) { osrfHashSet( osrfAppSessionCache, session, session->session_id ); } -/** Allocates a initializes a new app_session */ +/** Allocates and initializes a new app_session */ -osrf_app_session* osrfAppSessionClientInit( char* remote_service ) { +osrf_app_session* osrfAppSessionClientInit( const char* remote_service ) { return osrf_app_client_session_init( remote_service ); } -osrf_app_session* osrf_app_client_session_init( char* remote_service ) { +osrf_app_session* osrf_app_client_session_init( const char* remote_service ) { if (!remote_service) { osrfLogWarning( OSRF_LOG_MARK, "No remote service specified in osrf_app_client_session_init"); @@ -281,12 +282,16 @@ osrf_app_session* osrf_app_client_session_init( char* remote_service ) { session->state = OSRF_SESSION_DISCONNECTED; session->type = OSRF_SESSION_CLIENT; //session->next = NULL; + + session->userData = NULL; + session->userDataFree = NULL; + _osrf_app_session_push_session( session ); return session; } osrf_app_session* osrf_app_server_session_init( - char* session_id, char* our_app, char* remote_id ) { + const char* session_id, const char* our_app, const char* remote_id ) { osrfLogDebug( OSRF_LOG_MARK, "Initing server session with session id %s, service %s," " and remote_id %s", session_id, our_app, remote_id ); @@ -324,6 +329,9 @@ osrf_app_session* osrf_app_server_session_init( session->state = OSRF_SESSION_DISCONNECTED; session->type = OSRF_SESSION_SERVER; + session->userData = NULL; + session->userDataFree = NULL; + _osrf_app_session_push_session( session ); return session; @@ -351,32 +359,32 @@ void _osrf_app_session_free( osrf_app_session* session ){ } int osrfAppSessionMakeRequest( - osrf_app_session* session, jsonObject* params, - char* method_name, int protocol, string_array* param_strings ) { + osrf_app_session* session, const jsonObject* params, + const char* method_name, int protocol, string_array* param_strings ) { return osrf_app_session_make_locale_req( session, params, method_name, protocol, param_strings, NULL ); } int osrfAppSessionMakeLocaleRequest( - osrf_app_session* session, jsonObject* params, - char* method_name, int protocol, string_array* param_strings, char* locale ) { + osrf_app_session* session, const jsonObject* params, const char* method_name, + int protocol, string_array* param_strings, char* locale ) { return osrf_app_session_make_locale_req( session, params, method_name, protocol, param_strings, locale ); } int osrf_app_session_make_req( - osrf_app_session* session, jsonObject* params, - char* method_name, int protocol, string_array* param_strings) { + osrf_app_session* session, const jsonObject* params, + const char* method_name, int protocol, string_array* param_strings) { return osrf_app_session_make_locale_req(session, params, method_name, protocol, param_strings, NULL); } int osrf_app_session_make_locale_req( - osrf_app_session* session, jsonObject* params, - char* method_name, int protocol, string_array* param_strings, char* locale ) { + osrf_app_session* session, const jsonObject* params, const char* method_name, + int protocol, string_array* param_strings, char* locale ) { if(session == NULL) return -1; osrfLogMkXid(); @@ -424,7 +432,7 @@ void osrf_app_session_set_complete( osrf_app_session* session, int request_id ) if(req) req->complete = 1; } -int osrf_app_session_request_complete( osrf_app_session* session, int request_id ) { +int osrf_app_session_request_complete( const osrf_app_session* session, int request_id ) { if(session == NULL) return 0; osrf_app_request* req = OSRF_LIST_GET_INDEX( session->request_queue, request_id ); @@ -446,7 +454,7 @@ void osrf_app_session_reset_remote( osrf_app_session* session ){ session->remote_id = strdup(session->orig_remote_id); } -void osrf_app_session_set_remote( osrf_app_session* session, char* remote_id ) { +void osrf_app_session_set_remote( osrf_app_session* session, const char* remote_id ) { if(session == NULL) return; if( session->remote_id ) @@ -619,10 +627,8 @@ int _osrf_app_session_send( osrf_app_session* session, osrf_message* msg ){ */ int osrf_app_session_queue_wait( osrf_app_session* session, int timeout, int* recvd ){ if(session == NULL) return 0; - int ret_val = 0; osrfLogDebug(OSRF_LOG_MARK, "AppSession in queue_wait with timeout %d", timeout ); - ret_val = osrf_stack_entry_point(session->transport_handle, timeout, recvd); - return ret_val; + return osrf_stack_entry_point(session->transport_handle, timeout, recvd); } /** Disconnects (if client) and removes the given session from the global session cache @@ -663,7 +669,7 @@ osrf_message* osrf_app_session_request_recv( -int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data ) { +int osrfAppRequestRespond( osrfAppSession* ses, int requestId, const jsonObject* data ) { if(!ses || ! data ) return -1; osrf_message* msg = osrf_message_init( RESULT, requestId, 1 ); @@ -681,7 +687,7 @@ int osrfAppRequestRespond( osrfAppSession* ses, int requestId, jsonObject* data int osrfAppRequestRespondComplete( - osrfAppSession* ses, int requestId, jsonObject* data ) { + osrfAppSession* ses, int requestId, const jsonObject* data ) { osrf_message* payload = osrf_message_init( RESULT, requestId, 1 ); osrf_message_set_status_info( payload, NULL, "OK", OSRF_STATUS_OK ); diff --git a/src/libopensrf/osrf_application.c b/src/libopensrf/osrf_application.c index c70d446..127627d 100644 --- a/src/libopensrf/osrf_application.c +++ b/src/libopensrf/osrf_application.c @@ -1,12 +1,12 @@ #include -osrfHash* __osrfAppHash = NULL; +static osrfHash* _osrfAppHash = NULL; -int osrfAppRegisterApplication( char* appName, char* soFile ) { +int osrfAppRegisterApplication( const char* appName, const char* soFile ) { if(!appName || ! soFile) return -1; char* error; - if(!__osrfAppHash) __osrfAppHash = osrfNewHash(); + if(!_osrfAppHash) _osrfAppHash = osrfNewHash(); osrfLogInfo( OSRF_LOG_MARK, "Registering application %s with file %s", appName, soFile ); @@ -22,7 +22,7 @@ int osrfAppRegisterApplication( char* appName, char* soFile ) { } app->methods = osrfNewHash(); - osrfHashSet( __osrfAppHash, app, appName ); + osrfHashSet( _osrfAppHash, app, appName ); /* see if we can run the initialize method */ int (*init) (void); @@ -57,7 +57,7 @@ int osrfAppRegisterApplication( char* appName, char* soFile ) { } -void osrfAppSetOnExit(osrfApplication* app, char* appName) { +void osrfAppSetOnExit(osrfApplication* app, const char* appName) { if(!(app && appName)) return; /* see if we can run the initialize method */ @@ -75,7 +75,7 @@ void osrfAppSetOnExit(osrfApplication* app, char* appName) { } -int osrfAppRunChildInit(char* appname) { +int osrfAppRunChildInit(const char* appname) { osrfApplication* app = _osrfAppFindApplication(appname); if(!app) return -1; @@ -101,7 +101,7 @@ int osrfAppRunChildInit(char* appname) { void osrfAppRunExitCode() { - osrfHashIterator* itr = osrfNewHashIterator(__osrfAppHash); + osrfHashIterator* itr = osrfNewHashIterator(_osrfAppHash); osrfApplication* app; while( (app = osrfHashIteratorNext(itr)) ) { if( app->onExit ) { @@ -112,8 +112,8 @@ void osrfAppRunExitCode() { } -int osrfAppRegisterMethod( char* appName, char* methodName, - char* symbolName, char* notes, int argc, int options ) { +int osrfAppRegisterMethod( const char* appName, const char* methodName, + const char* symbolName, const char* notes, int argc, int options ) { return osrfAppRegisterExtendedMethod( appName, @@ -127,8 +127,8 @@ int osrfAppRegisterMethod( char* appName, char* methodName, } -int osrfAppRegisterExtendedMethod( char* appName, char* methodName, - char* symbolName, char* notes, int argc, int options, void * user_data ) { +int osrfAppRegisterExtendedMethod( const char* appName, const char* methodName, + const char* symbolName, const char* notes, int argc, int options, void * user_data ) { if( !appName || ! methodName ) return -1; @@ -160,8 +160,8 @@ int osrfAppRegisterExtendedMethod( char* appName, char* methodName, -osrfMethod* _osrfAppBuildMethod( char* methodName, - char* symbolName, char* notes, int argc, int options, void* user_data ) { +osrfMethod* _osrfAppBuildMethod( const char* methodName, const char* symbolName, + const char* notes, int argc, int options, void* user_data ) { osrfMethod* method = safe_malloc(sizeof(osrfMethod)); @@ -185,7 +185,7 @@ osrfMethod* _osrfAppBuildMethod( char* methodName, } -int __osrfAppRegisterSysMethods( char* app ) { +int __osrfAppRegisterSysMethods( const char* app ) { osrfAppRegisterMethod( app, OSRF_SYSMETHOD_INTROSPECT, NULL, @@ -206,23 +206,23 @@ int __osrfAppRegisterSysMethods( char* app ) { return 0; } -osrfApplication* _osrfAppFindApplication( char* name ) { +osrfApplication* _osrfAppFindApplication( const char* name ) { if(!name) return NULL; - return (osrfApplication*) osrfHashGet(__osrfAppHash, name); + return (osrfApplication*) osrfHashGet(_osrfAppHash, name); } -osrfMethod* __osrfAppFindMethod( osrfApplication* app, char* methodName ) { +osrfMethod* __osrfAppFindMethod( osrfApplication* app, const char* methodName ) { if(!app || ! methodName) return NULL; return (osrfMethod*) osrfHashGet( app->methods, methodName ); } -osrfMethod* _osrfAppFindMethod( char* appName, char* methodName ) { +osrfMethod* _osrfAppFindMethod( const char* appName, const char* methodName ) { if(!appName || ! methodName) return NULL; return __osrfAppFindMethod( _osrfAppFindApplication(appName), methodName ); } -int osrfAppRunMethod( char* appName, char* methodName, +int osrfAppRunMethod( const char* appName, const char* methodName, osrfAppSession* ses, int reqId, jsonObject* params ) { if( !(appName && methodName && ses) ) return -1; @@ -285,15 +285,15 @@ int osrfAppRunMethod( char* appName, char* methodName, } -int osrfAppRespond( osrfMethodContext* ctx, jsonObject* data ) { +int osrfAppRespond( osrfMethodContext* ctx, const jsonObject* data ) { return _osrfAppRespond( ctx, data, 0 ); } -int osrfAppRespondComplete( osrfMethodContext* context, jsonObject* data ) { +int osrfAppRespondComplete( osrfMethodContext* context, const jsonObject* data ) { return _osrfAppRespond( context, data, 1 ); } -int _osrfAppRespond( osrfMethodContext* ctx, jsonObject* data, int complete ) { +int _osrfAppRespond( osrfMethodContext* ctx, const jsonObject* data, int complete ) { if(!(ctx && ctx->method)) return -1; if( ctx->method->options & OSRF_METHOD_ATOMIC ) { @@ -346,7 +346,7 @@ int __osrfAppPostProcess( osrfMethodContext* ctx, int retcode ) { return 0; } -int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, ... ) { +int osrfAppRequestRespondException( osrfAppSession* ses, int request, const char* msg, ... ) { if(!ses) return -1; if(!msg) msg = ""; VA_LIST_TO_STRING(msg); @@ -356,11 +356,11 @@ int osrfAppRequestRespondException( osrfAppSession* ses, int request, char* msg, } -static void __osrfAppSetIntrospectMethod( osrfMethodContext* ctx, osrfMethod* method, jsonObject* resp ) { +static void _osrfAppSetIntrospectMethod( osrfMethodContext* ctx, const osrfMethod* method, jsonObject* resp ) { if(!(ctx && resp)) return; jsonObjectSetKey(resp, "api_name", jsonNewObject(method->name)); - jsonObjectSetKey(resp, "method", jsonNewObject(method->symbol)); + jsonObjectSetKey(resp, "method", jsonNewObject(method->symbol)); jsonObjectSetKey(resp, "service", jsonNewObject(ctx->session->remote_service)); jsonObjectSetKey(resp, "notes", jsonNewObject(method->notes)); jsonObjectSetKey(resp, "argc", jsonNewNumberObject(method->argc)); @@ -425,7 +425,7 @@ int osrfAppIntrospect( osrfMethodContext* ctx ) { if( (len = strlen(methodSubstring)) <= strlen(method->name) ) { if( !strncmp( method->name, methodSubstring, len) ) { resp = jsonNewObject(NULL); - __osrfAppSetIntrospectMethod( ctx, method, resp ); + _osrfAppSetIntrospectMethod( ctx, method, resp ); osrfAppRespond(ctx, resp); jsonObjectFree(resp); } @@ -449,7 +449,7 @@ int osrfAppIntrospectAll( osrfMethodContext* ctx ) { osrfMethod* method; while( (method = osrfHashIteratorNext(itr)) ) { resp = jsonNewObject(NULL); - __osrfAppSetIntrospectMethod( ctx, method, resp ); + _osrfAppSetIntrospectMethod( ctx, method, resp ); osrfAppRespond(ctx, resp); jsonObjectFree(resp); } @@ -464,7 +464,7 @@ int osrfAppEcho( osrfMethodContext* ctx ) { OSRF_METHOD_VERIFY_CONTEXT(ctx); int i; for( i = 0; i < ctx->params->size; i++ ) { - jsonObject* str = jsonObjectGetIndex(ctx->params,i); + const jsonObject* str = jsonObjectGetIndex(ctx->params,i); osrfAppRespond(ctx, str); } return 1; diff --git a/src/libopensrf/osrf_cache.c b/src/libopensrf/osrf_cache.c index 6c83633..2cd52b1 100644 --- a/src/libopensrf/osrf_cache.c +++ b/src/libopensrf/osrf_cache.c @@ -15,18 +15,18 @@ GNU General Public License for more details. #include -struct memcache* __osrfCache = NULL; -time_t __osrfCacheMaxSeconds = -1; +static struct memcache* _osrfCache = NULL; +static time_t _osrfCacheMaxSeconds = -1; -int osrfCacheInit( char* serverStrings[], int size, time_t maxCacheSeconds ) { +int osrfCacheInit( const char* serverStrings[], int size, time_t maxCacheSeconds ) { if( !(serverStrings && size > 0) ) return -1; int i; - __osrfCache = mc_new(); - __osrfCacheMaxSeconds = maxCacheSeconds; + _osrfCache = mc_new(); + _osrfCacheMaxSeconds = maxCacheSeconds; for( i = 0; i < size && serverStrings[i]; i++ ) - mc_server_add4( __osrfCache, serverStrings[i] ); + mc_server_add4( _osrfCache, serverStrings[i] ); return 0; } @@ -35,26 +35,26 @@ int osrfCachePutObject( char* key, const jsonObject* obj, time_t seconds ) { if( !(key && obj) ) return -1; char* s = jsonObjectToJSON( obj ); osrfLogInternal( OSRF_LOG_MARK, "osrfCachePut(): Putting object: %s", s); - if( seconds < 0 ) seconds = __osrfCacheMaxSeconds; + if( seconds < 0 ) seconds = _osrfCacheMaxSeconds; - mc_set(__osrfCache, key, strlen(key), s, strlen(s), seconds, 0); + mc_set(_osrfCache, key, strlen(key), s, strlen(s), seconds, 0); free(s); return 0; } int osrfCachePutString( char* key, const char* value, time_t seconds ) { if( !(key && value) ) return -1; - if( seconds < 0 ) seconds = __osrfCacheMaxSeconds; + if( seconds < 0 ) seconds = _osrfCacheMaxSeconds; osrfLogInternal( OSRF_LOG_MARK, "osrfCachePutString(): Putting string: %s", value); - mc_set(__osrfCache, key, strlen(key), value, strlen(value), seconds, 0); + mc_set(_osrfCache, key, strlen(key), value, strlen(value), seconds, 0); return 0; } -jsonObject* osrfCacheGetObject( char* key, ... ) { +jsonObject* osrfCacheGetObject( const char* key, ... ) { jsonObject* obj = NULL; if( key ) { VA_LIST_TO_STRING(key); - char* data = (char*) mc_aget( __osrfCache, VA_BUF, strlen(VA_BUF) ); + char* data = (char*) mc_aget( _osrfCache, VA_BUF, strlen(VA_BUF) ); if( data ) { osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data); obj = jsonParseString( data ); @@ -65,10 +65,10 @@ jsonObject* osrfCacheGetObject( char* key, ... ) { return NULL; } -char* osrfCacheGetString( char* key, ... ) { +char* osrfCacheGetString( const char* key, ... ) { if( key ) { VA_LIST_TO_STRING(key); - char* data = (char*) mc_aget(__osrfCache, VA_BUF, strlen(VA_BUF) ); + char* data = (char*) mc_aget(_osrfCache, VA_BUF, strlen(VA_BUF) ); osrfLogInternal( OSRF_LOG_MARK, "osrfCacheGetObject(): Returning object: %s", data); if(!data) osrfLogWarning(OSRF_LOG_MARK, "No cache data exists with key %s", VA_BUF); return data; @@ -77,16 +77,16 @@ char* osrfCacheGetString( char* key, ... ) { } -int osrfCacheRemove( char* key, ... ) { +int osrfCacheRemove( const char* key, ... ) { if( key ) { VA_LIST_TO_STRING(key); - return mc_delete(__osrfCache, VA_BUF, strlen(VA_BUF), 0 ); + return mc_delete(_osrfCache, VA_BUF, strlen(VA_BUF), 0 ); } return -1; } -int osrfCacheSetExpire( time_t seconds, char* key, ... ) { +int osrfCacheSetExpire( time_t seconds, const char* key, ... ) { if( key ) { VA_LIST_TO_STRING(key); jsonObject* o = osrfCacheGetObject( VA_BUF ); @@ -97,8 +97,8 @@ int osrfCacheSetExpire( time_t seconds, char* key, ... ) { } void osrfCacheCleanup() { - if(__osrfCache) - mc_free(__osrfCache); + if(_osrfCache) + mc_free(_osrfCache); } diff --git a/src/libopensrf/osrf_json_tools.c b/src/libopensrf/osrf_json_tools.c index aaf65a5..35ce3c9 100644 --- a/src/libopensrf/osrf_json_tools.c +++ b/src/libopensrf/osrf_json_tools.c @@ -14,16 +14,11 @@ GNU General Public License for more details. */ #include +#include -jsonObject* _jsonObjectEncodeClass( const jsonObject* obj, int ignoreClass ); +static jsonObject* _jsonObjectEncodeClass( const jsonObject* obj, int ignoreClass ); - -jsonObject* jsonObjectFindPath( const jsonObject* obj, char* path, ...); -jsonObject* _jsonObjectFindPathRecurse(const jsonObject* obj, char* root, char* path); -jsonObject* __jsonObjectFindPathRecurse(const jsonObject* obj, char* root); - - -static char* __tabs(int count) { +static char* _tabs(int count) { growing_buffer* buf = buffer_init(24); int i; for(i=0;i"); _recurse_jsonObjectToXML( obj, res_xml ); - output = buffer_data(res_xml); - - buffer_free(res_xml); - - return output; + return buffer_release(res_xml); } -int _recurse_jsonObjectToXML(jsonObject* obj, growing_buffer* res_xml) { +int _recurse_jsonObjectToXML(const jsonObject* obj, growing_buffer* res_xml) { char * hint = NULL; - char * bool_val = NULL; - int i = 0; if (obj->classname) hint = strdup(obj->classname); @@ -268,18 +261,17 @@ int _recurse_jsonObjectToXML(jsonObject* obj, growing_buffer* res_xml) { } else if(obj->type == JSON_BOOL) { + const char* bool_val; if (obj->value.b) - bool_val = strdup("true"); + bool_val = "true"; else - bool_val = strdup("false"); + bool_val = "false"; if (hint) buffer_fadd(res_xml, "", bool_val, hint); else buffer_fadd(res_xml, "", bool_val); - free(bool_val); - } else if (obj->type == JSON_STRING) { if (hint) { char * t = _escape_xml(jsonObjectGetString(obj)); @@ -312,7 +304,7 @@ int _recurse_jsonObjectToXML(jsonObject* obj, growing_buffer* res_xml) { else buffer_add(res_xml,""); - for ( i = 0; i!= obj->size; i++ ) + for ( int i = 0; i!= obj->size; i++ ) _recurse_jsonObjectToXML(jsonObjectGetIndex(obj,i), res_xml); buffer_add(res_xml,""); @@ -325,7 +317,7 @@ int _recurse_jsonObjectToXML(jsonObject* obj, growing_buffer* res_xml) { buffer_add(res_xml,""); jsonIterator* itr = jsonNewIterator(obj); - jsonObject* tmp; + const jsonObject* tmp; while( (tmp = jsonIteratorNext(itr)) ) { buffer_fadd(res_xml,"",itr->key); _recurse_jsonObjectToXML(tmp, res_xml); @@ -342,8 +334,7 @@ int _recurse_jsonObjectToXML(jsonObject* obj, growing_buffer* res_xml) { return 1; } -char* _escape_xml (char* text) { - char* out; +char* _escape_xml (const char* text) { growing_buffer* b = buffer_init(256); int len = strlen(text); int i; @@ -357,9 +348,7 @@ char* _escape_xml (char* text) { else buffer_add_char(b,text[i]); } - out = buffer_data(b); - buffer_free(b); - return out; + return buffer_release(b); } #endif diff --git a/src/libopensrf/osrf_system.c b/src/libopensrf/osrf_system.c index de25a29..f3807bf 100644 --- a/src/libopensrf/osrf_system.c +++ b/src/libopensrf/osrf_system.c @@ -56,7 +56,7 @@ static int _osrfSystemInitCache( void ) { if( cacheServers->type == JSON_ARRAY ) { int i; - char* servers[cacheServers->size]; + const char* servers[cacheServers->size]; for( i = 0; i != cacheServers->size; i++ ) { servers[i] = jsonObjectGetString( jsonObjectGetIndex(cacheServers, i) ); osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[i]); @@ -64,7 +64,7 @@ static int _osrfSystemInitCache( void ) { osrfCacheInit( servers, cacheServers->size, atoi(maxCache) ); } else { - char* servers[] = { jsonObjectGetString(cacheServers) }; + const char* servers[] = { jsonObjectGetString(cacheServers) }; osrfLogInfo( OSRF_LOG_MARK, "Adding cache server %s", servers[0]); osrfCacheInit( servers, 1, atoi(maxCache) ); } @@ -116,7 +116,7 @@ int osrfSystemBootstrap( char* hostname, char* configfile, char* contextNode ) { osrfStringArrayAdd(arr, jsonObjectGetString(apps)); } else { - jsonObject* app; + const jsonObject* app; while( (app = jsonObjectGetIndex(apps, i++)) ) osrfStringArrayAdd(arr, jsonObjectGetString(app)); } diff --git a/src/libopensrf/transport_client.c b/src/libopensrf/transport_client.c index 5676a43..d73d5fe 100644 --- a/src/libopensrf/transport_client.c +++ b/src/libopensrf/transport_client.c @@ -50,12 +50,10 @@ transport_client* client_init( const char* server, int port, const char* unix_pa if(server == NULL) return NULL; /* build and clear the client object */ - size_t c_size = sizeof( transport_client); - transport_client* client = safe_malloc( c_size ); + transport_client* client = safe_malloc( sizeof( transport_client) ); /* build and clear the message list */ - size_t l_size = sizeof( transport_message_list ); - client->m_list = safe_malloc( l_size ); + client->m_list = safe_malloc( sizeof( transport_message_list ) ); client->m_list->next = NULL; client->m_list->message = NULL; @@ -73,7 +71,7 @@ transport_client* client_init( const char* server, int port, const char* unix_pa int client_connect( transport_client* client, - char* username, char* password, char* resource, + const char* username, const char* password, const char* resource, int connect_timeout, enum TRANSPORT_AUTH_TYPE auth_type ) { if(client == NULL) return 0; return session_connect( client->session, username, @@ -86,7 +84,7 @@ int client_disconnect( transport_client* client ) { return session_disconnect( client->session ); } -int client_connected( transport_client* client ) { +int client_connected( const transport_client* client ) { if(client == NULL) return 0; return client->session->state_machine->connected; } @@ -184,9 +182,8 @@ void client_message_handler( void* client, transport_message* msg ){ transport_client* cli = (transport_client*) client; - size_t len = sizeof(transport_message_node); - transport_message_node* node = - (transport_message_node*) safe_malloc(len); + transport_message_node* node = safe_malloc( sizeof( transport_message_node) ); + node->next = NULL; node->type = MESSAGE_LIST_ITEM; node->message = msg; diff --git a/src/libopensrf/transport_session.c b/src/libopensrf/transport_session.c index 684cd11..6d67cec 100644 --- a/src/libopensrf/transport_session.c +++ b/src/libopensrf/transport_session.c @@ -1,6 +1,6 @@ #include - +static char* get_xml_attr( const xmlChar** atts, const char* attr_name ); // --------------------------------------------------------------------------------- // returns a built and allocated transport_session object. @@ -25,7 +25,9 @@ transport_session* init_transport( const char* server, session->status_buffer = buffer_init( JABBER_STATUS_BUFSIZE ); session->recipient_buffer = buffer_init( JABBER_JID_BUFSIZE ); session->message_error_type = buffer_init( JABBER_JID_BUFSIZE ); - session->session_id = buffer_init( 64 ); + session->session_id = buffer_init( 64 ); + + session->message_error_code = 0; /* for OpenSRF extensions */ session->router_to_buffer = buffer_init( JABBER_JID_BUFSIZE ); @@ -34,6 +36,7 @@ transport_session* init_transport( const char* server, session->router_class_buffer = buffer_init( JABBER_JID_BUFSIZE ); session->router_command_buffer = buffer_init( JABBER_JID_BUFSIZE ); + session->router_broadcast = 0; if( session->body_buffer == NULL || session->subject_buffer == NULL || session->thread_buffer == NULL || session->from_buffer == NULL || @@ -43,12 +46,35 @@ transport_session* init_transport( const char* server, session->session_id == NULL ) { osrfLogError(OSRF_LOG_MARK, "init_transport(): buffer_init returned NULL" ); + buffer_free( session->body_buffer ); + buffer_free( session->subject_buffer ); + buffer_free( session->thread_buffer ); + buffer_free( session->from_buffer ); + buffer_free( session->status_buffer ); + buffer_free( session->recipient_buffer ); + buffer_free( session->router_to_buffer ); + buffer_free( session->router_from_buffer ); + buffer_free( session->router_class_buffer ); + buffer_free( session->router_command_buffer ); + buffer_free( session->session_id ); + free( session ); return 0; } /* initialize the jabber state machine */ session->state_machine = (jabber_machine*) safe_malloc( sizeof(jabber_machine) ); + session->state_machine->connected = 0; + session->state_machine->connecting = 0; + session->state_machine->in_message = 0; + session->state_machine->in_message_body = 0; + session->state_machine->in_thread = 0; + session->state_machine->in_subject = 0; + session->state_machine->in_error = 0; + session->state_machine->in_message_error = 0; + session->state_machine->in_iq = 0; + session->state_machine->in_presence = 0; + session->state_machine->in_status = 0; /* initialize the sax push parser */ session->parser_ctxt = xmlCreatePushParserCtxt(SAXHandler, session, "", 0, NULL); @@ -57,6 +83,8 @@ transport_session* init_transport( const char* server, session->sock_mgr = (socket_manager*) safe_malloc( sizeof(socket_manager) ); session->sock_mgr->data_received = &grab_incoming; + session->sock_mgr->on_socket_closed = NULL; + session->sock_mgr->socket = NULL; session->sock_mgr->blob = session; session->port = port; @@ -66,6 +94,7 @@ transport_session* init_transport( const char* server, else session->unix_path = NULL; session->sock_id = 0; + session->message_callback = NULL; return session; } @@ -420,7 +449,13 @@ void startElementHandler( } } -char* get_xml_attr( const xmlChar** atts, char* attr_name ) { +// ------------------------------------------------------------------ +// Returns the value of the given XML attribute +// The xmlChar** construct is commonly returned from SAX event +// handlers. Pass that in with the name of the attribute you want +// to retrieve. +// ------------------------------------------------------------------ +static char* get_xml_attr( const xmlChar** atts, const char* attr_name ) { int i; if (atts != NULL) { for(i = 0;(atts[i] != NULL);i++) { @@ -480,22 +515,22 @@ void endElementHandler( void *session, const xmlChar *name) { return; } - if( strcmp( (char*) name, "body" ) == 0 ) { + if( strcmp( (const char*) name, "body" ) == 0 ) { ses->state_machine->in_message_body = 0; return; } - if( strcmp( (char*) name, "subject" ) == 0 ) { + if( strcmp( (const char*) name, "subject" ) == 0 ) { ses->state_machine->in_subject = 0; return; } - if( strcmp( (char*) name, "thread" ) == 0 ) { + if( strcmp( (const char*) name, "thread" ) == 0 ) { ses->state_machine->in_thread = 0; return; } - if( strcmp( (char*) name, "iq" ) == 0 ) { + if( strcmp( (const char*) name, "iq" ) == 0 ) { ses->state_machine->in_iq = 0; if( ses->message_error_code > 0 ) { osrfLogWarning( OSRF_LOG_MARK, "Error in IQ packet: code %d", ses->message_error_code ); @@ -505,7 +540,7 @@ void endElementHandler( void *session, const xmlChar *name) { return; } - if( strcmp( (char*) name, "presence" ) == 0 ) { + if( strcmp( (const char*) name, "presence" ) == 0 ) { ses->state_machine->in_presence = 0; /* if( ses->presence_callback ) { @@ -516,17 +551,17 @@ void endElementHandler( void *session, const xmlChar *name) { return; } - if( strcmp( (char*) name, "status" ) == 0 ) { + if( strcmp( (const char*) name, "status" ) == 0 ) { ses->state_machine->in_status = 0; return; } - if( strcmp( (char*) name, "error" ) == 0 ) { + if( strcmp( (const char*) name, "error" ) == 0 ) { ses->state_machine->in_message_error = 0; return; } - if( strcmp( (char*) name, "error:error" ) == 0 ) { + if( strcmp( (const char*) name, "error:error" ) == 0 ) { ses->state_machine->in_error = 0; return; } @@ -557,7 +592,7 @@ void characterHandler( void *session, const xmlChar *ch, int len) { char data[len+1]; - strncpy( data, (char*) ch, len ); + strncpy( data, (const char*) ch, len ); data[len] = 0; //printf( "Handling characters: %s\n", data ); diff --git a/src/libopensrf/utils.c b/src/libopensrf/utils.c index 0323fb7..54ba77b 100644 --- a/src/libopensrf/utils.c +++ b/src/libopensrf/utils.c @@ -64,7 +64,7 @@ int init_proc_title( int argc, char* argv[] ) { return 0; } -int set_proc_title( char* format, ... ) { +int set_proc_title( const char* format, ... ) { VA_LIST_TO_STRING(format); osrf_clearbuf( *(global_argv), global_argv_size); return snprintf( *(global_argv), global_argv_size, VA_BUF ); @@ -72,7 +72,7 @@ int set_proc_title( char* format, ... ) { /* utility method for profiling */ -double get_timestamp_millis() { +double get_timestamp_millis( void ) { struct timeval tv; gettimeofday(&tv, NULL); double time = (int)tv.tv_sec + ( ((double)tv.tv_usec / 1000000) ); @@ -186,7 +186,7 @@ int buffer_fadd(growing_buffer* gb, const char* format, ... ) { } -int buffer_add(growing_buffer* gb, char* data) { +int buffer_add(growing_buffer* gb, const char* data) { if(!(gb && data)) return 0; int data_len = strlen( data ); @@ -248,7 +248,7 @@ int buffer_free( growing_buffer* gb ) { return 1; } -char* buffer_data( growing_buffer *gb) { +char* buffer_data( const growing_buffer *gb) { return strdup( gb->buf ); } @@ -379,7 +379,7 @@ char* uescape( const char* string, int size, int full_escape ) { // A function to turn a process into a daemon -int daemonize() { +int daemonize( void ) { pid_t f = fork(); if (f == -1) { @@ -413,7 +413,7 @@ int daemonize() { /* Return 1 if the string represents an integer, */ /* as recognized by strtol(); Otherwise return 0. */ -int stringisnum(char* s) { +int stringisnum(const char* s) { char* w; strtol(s, &w, 10); return *w ? 0 : 1; @@ -449,7 +449,7 @@ char* file_to_string(const char* filename) { } -char* md5sum( char* text, ... ) { +char* md5sum( const char* text, ... ) { struct md5_ctx ctx; unsigned char digest[16]; diff --git a/src/router/osrf_router.c b/src/router/osrf_router.c index 3c12745..84744df 100644 --- a/src/router/osrf_router.c +++ b/src/router/osrf_router.c @@ -12,8 +12,8 @@ #define ROUTER_REQUEST_STATS_CLASS_SUMMARY "opensrf.router.info.stats.class.summary" osrfRouter* osrfNewRouter( - char* domain, char* name, - char* resource, char* password, int port, + const char* domain, const char* name, + const char* resource, const char* password, int port, osrfStringArray* trustedClients, osrfStringArray* trustedServers ) { if(!( domain && name && resource && password && port && trustedClients && trustedServers )) return NULL; @@ -132,7 +132,8 @@ void osrfRouterHandleIncoming( osrfRouter* router ) { } } -int osrfRouterClassHandleIncoming( osrfRouter* router, char* classname, osrfRouterClass* class ) { +int osrfRouterClassHandleIncoming( osrfRouter* router, const char* classname, + osrfRouterClass* class ) { if(!(router && class)) return -1; transport_message* msg; @@ -220,7 +221,7 @@ int osrfRouterHandleMessage( osrfRouter* router, transport_message* msg ) { -osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname ) { +osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* classname ) { if(!(router && router->classes && classname)) return NULL; osrfRouterClass* class = safe_malloc(sizeof(osrfRouterClass)); @@ -231,9 +232,14 @@ osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname ) { class->connection = client_init( router->domain, router->port, NULL, 0 ); - if(!client_connect( class->connection, router->name, + if(!client_connect( class->connection, router->name, router->password, classname, 10, AUTH_DIGEST ) ) { - osrfRouterClassFree( classname, class ); + // We cast away the constness of classname. Though ugly, this + // cast is benign because osrfRouterClassFree doesn't actually + // write through the pointer. We can't readily change its + // signature because it is used for a function pointer, and + // we would have to change other signatures the same way. + osrfRouterClassFree( (char *) classname, class ); return NULL; } @@ -242,7 +248,7 @@ osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname ) { } -int osrfRouterClassAddNode( osrfRouterClass* rclass, char* remoteId ) { +int osrfRouterClassAddNode( osrfRouterClass* rclass, const char* remoteId ) { if(!(rclass && rclass->nodes && remoteId)) return -1; osrfLogInfo( OSRF_LOG_MARK, "Adding router node for remote id %s", remoteId ); @@ -260,8 +266,8 @@ int osrfRouterClassAddNode( osrfRouterClass* rclass, char* remoteId ) { ? return NULL if it's the last node ? */ -transport_message* osrfRouterClassHandleBounce( - osrfRouter* router, char* classname, osrfRouterClass* rclass, transport_message* msg ) { +transport_message* osrfRouterClassHandleBounce( osrfRouter* router, + const char* classname, osrfRouterClass* rclass, transport_message* msg ) { osrfLogDebug( OSRF_LOG_MARK, "osrfRouterClassHandleBounce()"); @@ -356,7 +362,7 @@ int osrfRouterClassHandleMessage( } -int osrfRouterRemoveClass( osrfRouter* router, char* classname ) { +int osrfRouterRemoveClass( osrfRouter* router, const char* classname ) { if(!(router && router->classes && classname)) return -1; osrfLogInfo( OSRF_LOG_MARK, "Removing router class %s", classname ); osrfHashRemove( router->classes, classname ); @@ -365,7 +371,7 @@ int osrfRouterRemoveClass( osrfRouter* router, char* classname ) { int osrfRouterClassRemoveNode( - osrfRouter* router, char* classname, char* remoteId ) { + osrfRouter* router, const char* classname, const char* remoteId ) { if(!(router && router->classes && classname && remoteId)) return 0; @@ -433,13 +439,13 @@ void osrfRouterFree( osrfRouter* router ) { -osrfRouterClass* osrfRouterFindClass( osrfRouter* router, char* classname ) { +osrfRouterClass* osrfRouterFindClass( osrfRouter* router, const char* classname ) { if(!( router && router->classes && classname )) return NULL; return (osrfRouterClass*) osrfHashGet( router->classes, classname ); } -osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, char* remoteId ) { +osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, const char* remoteId ) { if(!(rclass && remoteId)) return NULL; return (osrfRouterNode*) osrfHashGet( rclass->nodes, remoteId ); } @@ -689,7 +695,7 @@ int osrfRouterHandleMethodNFound( int osrfRouterHandleAppResponse( osrfRouter* router, - transport_message* msg, osrfMessage* omsg, jsonObject* response ) { + transport_message* msg, osrfMessage* omsg, const jsonObject* response ) { if( response ) { /* send the response message */ diff --git a/src/router/osrf_router.h b/src/router/osrf_router.h index 1122a08..7b3b639 100644 --- a/src/router/osrf_router.h +++ b/src/router/osrf_router.h @@ -62,8 +62,9 @@ typedef struct __osrfRouterNodeStruct osrfRouterNode; @param trustedServers The array of server domains that we allow to register, etc. with ust. @return The allocated router or NULL on memory error */ -osrfRouter* osrfNewRouter( char* domain, char* name, char* resource, - char* password, int port, osrfStringArray* trustedClients, osrfStringArray* trustedServers ); +osrfRouter* osrfNewRouter( const char* domain, const char* name, const char* resource, + const char* password, int port, osrfStringArray* trustedClients, + osrfStringArray* trustedServers ); /** Connects the given router to the network @@ -85,7 +86,7 @@ void osrfRouterRun( osrfRouter* router ); @param classname The name of the class this node handles. @return 0 on success, -1 on connection error. */ -osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname ); +osrfRouterClass* osrfRouterAddClass( osrfRouter* router, const char* classname ); /** Adds a new server node to the given class. @@ -93,7 +94,7 @@ osrfRouterClass* osrfRouterAddClass( osrfRouter* router, char* classname ); @param remoteId The remote login of this node @return 0 on success, -1 on generic error */ -int osrfRouterClassAddNode( osrfRouterClass* rclass, char* remoteId ); +int osrfRouterClassAddNode( osrfRouterClass* rclass, const char* remoteId ); /** @@ -113,7 +114,7 @@ int osrfRouterClassHandleMessage( osrfRouter* router, /** Removes a given class from the router, freeing as it goes */ -int osrfRouterRemoveClass( osrfRouter* router, char* classname ); +int osrfRouterRemoveClass( osrfRouter* router, const char* classname ); /** Removes the given node from the class. Also, if this is that last node in the set, @@ -122,7 +123,8 @@ int osrfRouterRemoveClass( osrfRouter* router, char* classname ); @return 1 on successful remove with class removal @return -1 error on removal */ -int osrfRouterClassRemoveNode( osrfRouter* router, char* classname, char* remoteId ); +int osrfRouterClassRemoveNode( osrfRouter* router, const char* classname, + const char* remoteId ); /** Frees a router class object @@ -145,12 +147,12 @@ void osrfRouterFree( osrfRouter* router ); /** Finds the class associated with the given class name in the list of classes */ -osrfRouterClass* osrfRouterFindClass( osrfRouter* router, char* classname ); +osrfRouterClass* osrfRouterFindClass( osrfRouter* router, const char* classname ); /** Finds the router node within this class with the given remote id */ -osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, char* remoteId ); +osrfRouterNode* osrfRouterClassFindNode( osrfRouterClass* rclass, const char* remoteId ); /** @@ -173,15 +175,16 @@ void osrfRouterHandleIncoming( osrfRouter* router ); Utility method for handling incoming requests to a router class, makes sure sender is a trusted client */ -int osrfRouterClassHandleIncoming( osrfRouter* router, char* classname, osrfRouterClass* class ); +int osrfRouterClassHandleIncoming( osrfRouter* router, + const char* classname, osrfRouterClass* class ); /* handles case where router node is not longer reachable. copies over the data from the last sent message and returns a newly crafted suitable for treating as a newly inconing message. Removes the dead node and If there are no more nodes to send the new message to, returns NULL. */ -transport_message* osrfRouterClassHandleBounce( - osrfRouter* router, char* classname, osrfRouterClass* rclass, transport_message* msg ); +transport_message* osrfRouterClassHandleBounce( osrfRouter* router, + const char* classname, osrfRouterClass* rclass, transport_message* msg ); @@ -215,7 +218,7 @@ int osrfRouterRespondConnect( osrfRouter* router, transport_message* msg, osrfMe int osrfRouterProcessAppRequest( osrfRouter* router, transport_message* msg, osrfMessage* omsg ); int osrfRouterHandleAppResponse( osrfRouter* router, - transport_message* msg, osrfMessage* omsg, jsonObject* response ); + transport_message* msg, osrfMessage* omsg, const jsonObject* response ); int osrfRouterHandleMethodNFound( osrfRouter* router, transport_message* msg, osrfMessage* omsg ); -- 2.43.2