fixup
[lcr.git] / sip.cpp
diff --git a/sip.cpp b/sip.cpp
index aa28f59..c3a4f00 100644 (file)
--- a/sip.cpp
+++ b/sip.cpp
@@ -103,7 +103,6 @@ Psip::Psip(int type, char *portname, struct port_settings *settings, struct inte
        add_work(&p_s_delete, delete_event, this, 0);
        p_s_handle = 0;
        p_s_magic = 0;
-       memset(&p_s_auth_nonce, 0, sizeof(p_s_auth_nonce));
        memset(&p_s_rtp_fd, 0, sizeof(p_s_rtp_fd));
        memset(&p_s_rtcp_fd, 0, sizeof(p_s_rtcp_fd));
        memset(&p_s_rtp_sin_local, 0, sizeof(p_s_rtp_sin_local));
@@ -1736,7 +1735,8 @@ static void i_register(struct sip_inst *inst, int status, char const *phrase, nu
                authorization = sip->sip_authorization;
                status = check_authorization(authorization, "REGISTER", inst->auth_user, inst->auth_password, inst->auth_realm, inst->auth_nonce, &auth_text);
                if (status == 401) {
-                       generate_nonce(inst->auth_nonce);
+                       if (!inst->auth_nonce[0])
+                               generate_nonce(inst->auth_nonce);
                        SPRINT(auth_str, "Digest realm=\"%s\", nonce=\"%s\", algorithm=MD5, qop=\"auth\"", inst->auth_realm, inst->auth_nonce);
                }
        } else {
@@ -1748,7 +1748,6 @@ static void i_register(struct sip_inst *inst, int status, char const *phrase, nu
 
        if (status == 200) {
                SCPY(inst->remote_peer, uri);
-               inst->auth_nonce[0] = '\0';
        }
 
        sip_trace_header(NULL, inst->interface_name, "RESPOND", DIRECTION_OUT);
@@ -1852,12 +1851,14 @@ void Psip::i_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *mag
        PDEBUG(DEBUG_SIP, "invite received (%s->%s)\n", from, to);
 
        sip_trace_header(this, inst->interface_name, "Authorization", DIRECTION_IN);
-       if (inst->auth_realm[0]) {
+       if (inst->auth_realm[0] || p_state != PORT_STATE_IDLE) {
+               /* only authenticate remote, if we have a realm set and we don't have re-invite */
                authorization = sip->sip_proxy_authorization;
-               status = check_authorization(authorization, "INVITE", inst->auth_user, inst->auth_password, inst->auth_realm, p_s_auth_nonce, &auth_text);
-               if (status == 401) {
-                       generate_nonce(p_s_auth_nonce);
-                       SPRINT(auth_str, "Digest realm=\"%s\", nonce=\"%s\", algorithm=MD5, qop=\"auth\"", inst->auth_realm, p_s_auth_nonce);
+               status = check_authorization(authorization, "INVITE", inst->auth_user, inst->auth_password, inst->auth_realm, inst->auth_nonce, &auth_text);
+               if (status == 407) {
+                       if (!inst->auth_nonce[0])
+                               generate_nonce(inst->auth_nonce);
+                       SPRINT(auth_str, "Digest realm=\"%s\", nonce=\"%s\", algorithm=MD5, qop=\"auth\"", inst->auth_realm, inst->auth_nonce);
                }
        } else {
                status = 200;
@@ -1867,7 +1868,19 @@ void Psip::i_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *mag
        end_trace();
 
        if (status == 200) {
-               p_s_auth_nonce[0] = '\0';
+       } else {
+               sip_trace_header(this, inst->interface_name, "INVITE", DIRECTION_IN);
+               end_trace();
+
+               sip_trace_header(this, inst->interface_name, "RESPOND", DIRECTION_OUT);
+               add_trace("respond", "value", "%d", status);
+               add_trace("reason", NULL, "peer invited");
+               end_trace();
+
+               nua_respond(nh, status, auth_text, SIPTAG_CONTACT(sip->sip_contact), TAG_IF(auth_str[0], SIPTAG_PROXY_AUTHENTICATE_STR(auth_str)), TAG_END());
+               new_state(PORT_STATE_RELEASE);
+               trigger_work(&p_s_delete);
+               return;
        }
 
        sip_trace_header(this, inst->interface_name, "Payload received", DIRECTION_NONE);
@@ -2217,6 +2230,12 @@ void Psip::r_invite(int status, char const *phrase, nua_t *nua, nua_magic_t *mag
        add_trace("respond", "value", "%d", status);
        end_trace();
 
+       if (status == 401 || status == 407) {
+               PDEBUG(DEBUG_SIP, "Invite challenge received\n");
+               challenge(inst, this, status, phrase, nua, magic, nh, hmagic, sip, tags);
+               return;
+       }
+
        /* connect audio */
        if (status == 183 || (status >= 200 && status <= 299)) {
                int ret;
@@ -2391,7 +2410,6 @@ static void sip_callback(nua_event_t event, int status, char const *phrase, nua_
        struct sip_inst *inst = (struct sip_inst *) magic;
        class Port *port;
        class Psip *psip = NULL;
-       int rc;
 
        PDEBUG(DEBUG_SIP, "Event %d from SIP stack received (handle=%p)\n", event, nh);
        if (!nh)
@@ -2480,14 +2498,6 @@ static void sip_callback(nua_event_t event, int status, char const *phrase, nua_
                return;
        }
 
-       switch (status) {
-       case 401:
-       case 407:
-               rc = challenge(inst, psip, status, phrase, nua, magic, nh, hmagic, sip, tags);
-               if (rc >= 0)
-                       return;
-       }
-
        switch (event) {
        case nua_r_set_params:
                PDEBUG(DEBUG_SIP, "setparam response\n");