Search code examples
c++authenticationsipexosip

eXosip: login always fails on first try


I am developing a SIP-Application using eXosip. When I try to login to the server the first attempt always fails.

eXosip_lock(ctx);
eXosip_add_authentication_info(ctx, username, login, passwd, NULL, domain);

osip_message_t *reg;
int rid = eXosip_register_build_initial_register(ctx, account, server, NULL, 3600, &reg);

// Not sure what they do, but they seem to be necessary
osip_message_set_supported (reg, "100rel");
osip_message_set_supported (reg, "path");

eXosip_register_send_register(ctx, rid, reg);
eXosip_unlock(ctx);

(I've removed error checking to make it more readable.) The above code results in an EXOSIP_REGISTER_FAILED event. Reacting to this event by executing the following code results in a successful registration.

eXosip_lock(ctx);

osip_message_t *reg;
eXosip_register_build_register(ctx, rid, 3600, &reg);
eXosip_register_send_register(ctx, rid, reg);

eXosip_unlock(ctx);

I tried different accounts at different providers and it's always the same. Granted, it works, but it makes the program flow confusing and error handling harder (e.g. for erroneous login credentials), which I'd like to avoid if possible.

Now I don't know if this behavior is to be expected (I couldn't find anything about it), but... can anybody help? Thanks in advance!


Solution

  • Ok, this was a bit stupid. For anyone who wonders or encounters the same, most servers will reply to the initial register with a 401 (unauthorized) or 407 (proxy authentication required). Knowing this I was able to successfully register using the following code:

    eXosip_automatic_action(ctx);
    
    if (evt->type == EXOSIP_REGISTER_FAILED)
    {
       if (evt->response != NULL &&
              (evt->response->status_code == 401 || evt->response->status_code == 407))
       {
           eXosip_default_action(ctx, evt);
       }
       else
           // login really failed
    }
    

    eXosip_automatic_action will handle 401, 407, 422, 3xx and reregister before the registration expires. eXosip_default_action will handle some more things regarding 401/407. Quite handy!