cURL / Mailing Lists / curl-library / Single Mail

curl-library

Re: Issues in using 64 bit curl library on a RHEL 64 bit machine

From: Chitra Nanda <chitrananda9_at_gmail.com>
Date: Tue, 15 Jun 2010 01:29:45 -0700

Hi Daniel,

Thanks for pointing out the mistakes.

I do understand that function pointer usage is not the best way, but that
would be the only way if I am trying to link the library dynamically at
runtime right. If there is any better alternative please suggest the same.
It will be very helpful.

Also I am compiling the code with -g option( *g++ -g curlsample.cpp -ldl* )
to get the symbolic information. Where would I get the debug version of the
library. Do you mean to say that I need to build the curl library from the
curl source for the same.

I am adding the code again with the changes to call curl_easy_perform.
Irrespective of that it just fails at the very first call to the curl
library. It doesn't reach the point to execute the file transfer which would
need the call to curl_easy_perform. The code is given below.

If you need more info, please let me know about the same. Thanks for your
time.

===================================================================================================================
#include <stdio.h>
#include <dlfcn.h>
#include <curl/curl.h>
#include <curl/types.h>
#include <curl/easy.h>
#include <string>

int main()
{

    CURLcode res;

    void* sdl_library = dlopen( "/lib64/libcurl.so.4", RTLD_GLOBAL |
RTLD_NOW );

    if ( sdl_library == 0 )
    {
        printf("Checking with the /usr/lib location for libcurl\n");
        sdl_library = dlopen( "/usr/lib64/libcurl.so.4", RTLD_GLOBAL |
RTLD_NOW );
        if ( sdl_library == 0 )
        {
            printf("Checking with the /usr/lib without version location for
libcurl \n");
             sdl_library = dlopen( "/usr/lib64/libcurl.so", RTLD_GLOBAL |
RTLD_NOW );
        }
    }

    if ( sdl_library != 0 )
    {
      FILE *fp;
      char *errstr;
      const char * pch = strrchr( "/tmp/test.txt", '/' );
      if ( pch )
      {
         filename = pch + 1;
         fp = fopen( "test.txt", "wb" );
         if ( fp )
         {
           CURL *curl;
           CURL* ( *fptr )();
           *( void ** )( &fptr ) = dlsym( sdl_library, "curl_easy_init" );
           if ( fptr == 0 )
           {
              printf("unable to locate function curl_easy_init in curl so
\n" );
              return -1;
           }
           else
           {
               curl = ( *fptr )();
               if ( curl )
               {
                 CURLcode ( *fptr1 ) ( CURL*, int, char* );
                 *( void ** )( &fptr1 ) = dlsym( sdl_library,
"curl_easy_setopt" );
                 if ( fptr1 == 0 )
                 {
                    printf("unable to locate function curl_easy_setopt in
curl so \n");
                    return -1;
                 }
                 else
                 {
                   //Sample URL just for testing, whatever is the URL it
fails on a 64 bit machine
                   res = ( *fptr1 ) ( curl, CURLOPT_URL, const_cast<char*>(
"http://10.12.13.14//test.txt" ) );
                   if(res != 0)
                     printf("Error accessing curl url %d \n", res);
                     //////////////////////////////
////////////////////////////////
                    CURLcode ( *fptr3 ) ( CURL*, int, FILE* );
                    *( void ** )( &fptr3 ) = dlsym( sdl_library,
"curl_easy_setopt" );
                    //CURL_ERR_CHECK( errstr );
                    if ( fptr3 == 0 )
                    {
                        printf("unable to locate function curl_easy_setopt
in curl so \n" );
                        return -1;
                    }
                    else
                    {
                       res = ( *fptr3 ) ( curl, CURLOPT_FILE, fp );
                       if(res != 0)
                           printf("Error accessing curl url %d \n", res);

////////////////////////////////////////////////////////////
                       CURLcode ( *fptr5 ) ( CURL*, int, char* );
                       *( void ** )( &fptr5 ) = dlsym( sdl_library,
"curl_easy_setopt" );
                       //CURL_ERR_CHECK( errstr );
                       if ( fptr5 == 0 )
                       {
                           printf("unable to locate function
curl_easy_setopt in curl so \n" );
                           return -1;
                       }
                       else
                       {
                           char curlerr[512];
                           res = ( *fptr5 ) ( curl, CURLOPT_ERRORBUFFER,
curlerr );
                           if(res != 0)
                           {
                              printf("Error accessing curl url %d \n", res);
                              //printf("Error string %s \n",
curl_easy_strerror(res));
                           }

/////////////////////////////////////////////////////////////
                           CURLcode ( *fptr2 )( CURL* );
                           *( void ** )( &fptr2 ) = dlsym( sdl_library,
"curl_easy_perform" );
                           if ( fptr2 == 0 )
                           {
                              printf("unable to locate function
curl_easy_perform in curl so \n" );
                              return -1;
                           }
                           *else
                           {
                             res = ( *fptr2 )( curl );
                                 if ( res != 0 )
                                  {
                                             //always cleanup
                                            void ( *fptr4 ) ( CURL* );
                                            *( void ** )( &fptr4 ) = dlsym(
sdl_library, "curl_easy_cleanup" );
                                            if ( fptr4 == 0 )
                                            {
                                                printf("unable to locate
function curl_easy_cleanup in curl so" );
                                                return( -1 );
                                            }
                                            else
                                            {
                                                ( *fptr4 ) ( curl );
                                                int j;
                                                j = dlclose( sdl_library );
                                                printf("Successfully closed
curl so" ) ;
                                                if ( j != 0
) {
                                                    printf("UNABLE TO CLOSE
DYNAMIC LIB" );
                                             }
                                    }
                            } *
                       }//End of fptr5
                    }//End of fptr3
               }//End of Ptr1
           }//End of Curl
        fclose( fp );
    }//End of fptr
     }//End of fp
   } //End of pch
  } //End of sld_library
}//End of Main

==================================================================================================================

Thanks and Regards,
Chitra

On Mon, Jun 14, 2010 at 3:53 AM, Daniel Stenberg <daniel_at_haxx.se> wrote:

> On Mon, 14 Jun 2010, Chitra Nanda wrote:
>
> If I try to link the library dynamically at compile time then I don't run
>> into any issues. But if I try to link it at runtime I run into issues.
>>
>
> I strongly advice against doing that. dlopening a versioned library such as
> libcurl will lead to breakage sooner or later. And it is rarely any benefit.
>
>
> CURLcode ( *fptr2 )( CURL* );
>> *( void ** )( &fptr2 ) = dlsym( sdl_library,
>> "curl_easy_perform" );
>>
>
> I'd say that's one of the most ugly forms of typecast you can do. But
> forgive me if I misunderstood something here, that line doesn't _call_
> curl_easy_perform() it just looks it up, right?
>
> So where is the call to curl_easy_perform() ?
>
> And besides that, you should consider using a debug version of the library
> so that you get full symbols and everything for when you try to debug it.
>
> --
>
> / daniel.haxx.se
> -------------------------------------------------------------------
> List admin: http://cool.haxx.se/list/listinfo/curl-library
> Etiquette: http://curl.haxx.se/mail/etiquette.html
>

-------------------------------------------------------------------
List admin: http://cool.haxx.se/list/listinfo/curl-library
Etiquette: http://curl.haxx.se/mail/etiquette.html
Received on 2010-06-15