Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

FIx missing ACK issue with webrtc gateway #554

Open
devopsec opened this issue Jan 23, 2024 · 3 comments
Open

FIx missing ACK issue with webrtc gateway #554

devopsec opened this issue Jan 23, 2024 · 3 comments

Comments

@devopsec
Copy link
Member

webrtc to SIP, and vice versa translation

@devopsec devopsec converted this from a draft issue Jan 23, 2024
@mackhendricks mackhendricks moved this from Killer Features to In Progress in v0.75 Release Jan 23, 2024
@devopsec
Copy link
Member Author

currently the hurdles to tackle are:

  • asterisk SIP phone to external webrtc client the ACK is not forwarded to the SIP phone

@mackhendricks mackhendricks changed the title support for acting as an webrtc gateway FIx missing ACK issue with webrtc gateway Jan 23, 2024
@devopsec devopsec removed this from v0.75 Release Jun 14, 2024
@fatburger25
Copy link

fatburger25 commented Oct 21, 2024

Here is the fix,

@devopsec @mackhendricks

Please make changes to the existing kamailio config for the asterisk to accept the contact header format

Add the following code or replace the if (isbflagset(FLB_WS_DEVICE)) condition under INVITE route

			# Manipulate the WS Device header and send it to the PBX
                        $var(aor) = $fU;
                        $var(original_user) = $(ct{re.subst,/^<sip:([^@]+)@.*/\1/});
                        $var(host) = $(ct{re.subst,/^<*sip:.*@([^:;>]+).*$/\1/});

                        sql_query("kam", "SELECT received FROM location WHERE username='$var(aor)'", "ra");

                        if ($dbr(ra=>rows) > 0) {
                            $var(received) = $dbr(ra=>[0,0]);
                            xlog("L_WARN", "host ip is $var(host) Received value for $var(aor): $var(received)\n");
                        } else {
                            xlog("L_WARN", "No record found for $var(aor)\n");
                        }
                        sql_result_free("ra");

                        if (is_method("REGISTER")) {
                                if ($var(received) =~ "^sip:") {
                                    $var(received) = $(var(received){s.strip,4});
                                }
                                # Capture the original Contact header
                                $sht(original_contact=>$ci) = $ct;

                                $var(original_contact) = $ct;
                                append_hf("X-Original-Contact: $var(original_contact)\r\n");
                                xlog("L_WARN", "Original Contact: $ct for $ci \n");     

                                # Extract the domain and parameters from the original Contact
                                $var(domain_and_params) = $(ct{re.subst,/^<sip:[^@]+@([^>]+)>/\1/});

                                # Construct the new Contact header
                                $var(new_contact) = "<sip:" + $fU + "@" + $var(received) + ";x-ast-orig-host=" + $var(host) + ":0>";
                 
                                # Replace the Contact header
                                remove_hf("Contact");
                                append_hf("Contact: $var(new_contact)\r\n");
                                # Log the new Contact header
                                $var(via) = "SIP/2.0/WSS " + $var(host) + ";rport=" + $sp + ";received=" + $si + ";branch=" + $ci;
                                xlog("L_INFO", "New Contact: $var(new_contact)\n");
                        }
                        insert_hf("Koretel-VoIP: $si\r\n");
                        xlog("L_WARN", "Hitting Isravel Testing - This is a WS Device Orig Contact $var(orig) and source URI is $su and original user is $var(original_user) and Destination URI is $du Contact $ct and $fU $si:$sp\n");
                        add_path();
		}
		else {
			#Add the Path header for SIP UAC's - so that we know how to route back
			add_path_received($fU);
		}

Update the MANAGE_REPLY Route like below

onreply_route[MANAGE_REPLY] {
        # Rewrite the SDP on incoming replies of Websockets
        if (t_check_status("100|183|180|200") && has_body("application/sdp")) {
                $var(ru) = $sht(original_contact=>$ci);
                xlog("L_WARN", "new ru $var(ru) tu=$tu tU=$tU ru $ru du $du ct $ct Contact header transport=wss");
                if ($ct =~ ".*;transport=wss.*") {

                        $var(user) = $(ct{re.subst,/^<*sip:([^@]+)@.*$/\1/});
                        xlog("L_INFO", "User from Contact: $var(user)\n");

                        $var(aor) = $tU;
                        sql_query("kam", "SELECT received FROM location WHERE username='$var(aor)'", "ra");

                        if ($dbr(ra=>rows) > 0) {
                            $var(received) = $dbr(ra=>[0,0]);
                            xlog("L_WARN", "Received value for $var(aor): $var(received)\n");
                        } else {
                            xlog("L_WARN", "No record found for $var(aor)\n");
                        }
                        sql_result_free("ra");
                        if ($var(received) =~ "^sip:") {
                            $var(received) = $(var(received){s.strip,4});
                        }

                        # Capture the original Contact header
                        append_hf("X-Original-Contact: $var(ru)\r\n");
                        xlog("L_WARN", "Original Contact: $var(received) \n");

                        $var(new_contact) = "<sip:" + $var(user) + "@" + $var(received) + ">";

                        # Replace the Contact header
                        remove_hf("Contact");
                        append_hf("Contact: $var(new_contact)\r\n");

                        xlog("L_INFO", "New Contact: $var(new_contact) and Via $var(via)\n");
                        xlog("L_WARN", "Contact header contains transport=wss\n");
                } else {
                        xlog("L_WARN", "$ct Contact header does not contains transport=wss\n");
                }
	}

	xlog("L_DBG", "incoming reply from source address $si:$sp\n");

	# Rewrite the SDP on incoming replies
	if (t_check_status("183|180|200") && has_body("application/sdp")) {
		route(RTPENGINEANSWER);
		if ($avp(sdp_media_direction) != $null) {
			if (!msg_apply_changes()) {
				xlog("L_ERR", "could not update sdp\n");
			}
			if (!subst("/^a=(sendrecv|recvonly|sendonly|inactive).*/a=$avp(sdp_media_direction)/")) {
				#search_append_body("^a=.+", "a=$avp(sdp_media_direction)");
				xlog("L_ERR", "could not update sdp\n");
			}
			if (!msg_apply_changes()) {
				xlog("L_ERR", "could not update sdp\n");
			}
		}
		# TODO: why are we dropping 183 replies to MSTEAMS? - marked for review/validation
		if (t_check_status("183") && isbflagset(FLB_DST_MSTEAMS)) {
			drop();
		}
	}

Update the WS_REPLY route below

onreply_route[WS_REPLY] {

    # Reply to the WS REGISTER response - Send back the old Contact header back to the Server
    if (is_method("REGISTER") && status == 200) {
        # Retrieve the original Contact from the custom header
        $var(orig_contact) = $sht(original_contact=>$ci);

        if ($var(orig_contact) != $null) {
            # Modify the username in the original Contact
            $var(modified_contact) = $(var(orig_contact){re.subst,/^<sip:[^@]+/sip:$fU/});

            # Replace the Contact header in the response
            remove_hf("Contact");
            append_hf("Contact: $var(orig_contact)\r\n");

            xlog("L_INFO", "Modified 200 OK - Old Contact $var(orig_contact) and New Contact: $var(modified_contact)\n");

        } else {
            xlog("L_WARN", "X-Original-Contact header not found in response\n");
        }
    }

    if (nat_uac_test("64")) {
		# Do NAT traversal stuff for replies to a WebSocket connection
		# - even if it is not behind a NAT!
		# This won't be needed in the future if Kamailio and the
		# WebSocket client support Outbound and Path.
		add_contact_alias();
    }
}

@mackhendricks
Copy link
Member

Thank you....we are reviewing this. We really appreicate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: No status
Development

No branches or pull requests

3 participants