libssh2 - Agent Forwarding not working

I am using libssh2 library to ssh connections in my mobile application. Here I want to use Agent Forwarding support.

I have followed same procedure as they have provided in example here LibSSH2 Agent Forwarding.

I am able to create agent successfully but when I try to connect it with libssh2_agent_connect(agent) It gives me error -39 LIBSSH2_ERROR_BAD_USE.

Well I am checking same thing using MAC OSX terminal and it's working fine. Please suggest if anything wrong,

This is how I am trying once do successful connection

struct libssh2_agent_publickey *identity, *prev_identity = NULL;
int rc;
agent = libssh2_agent_init(session);

if (!agent)
{
    fprintf(stderr, "Failure initializing ssh-agent support\n");
    rc = 1;
}

int temp=libssh2_agent_connect(agent);
if (temp)
{
    fprintf(stderr, "Failure connecting to ssh-agent\n");
    rc = 1;
}

Is there anything wrong here? please suggest.


From the code at https://github.com/libssh2/libssh2/blob/master/src/agent.c

path = getenv("SSH_AUTH_SOCK");
if (!path)
    return _libssh2_error(agent->session, LIBSSH2_ERROR_BAD_USE,
                          "no auth sock variable");

So obviously, this error is happening because you don't have the environment variable pointing to the agent socket set correctly. Have your code print the value of getenv("SSH_AUTH_SOCK") in case of an error to confirm this.