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 0.13.7 broken CANCEL #679

Merged
merged 3 commits into from
Apr 22, 2019
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 39 additions & 24 deletions src/SIPMessage.ts
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ import { UA } from "./UA";
import { URI } from "./URI";
import { Utils } from "./Utils";

const getSupportedHeader: ((request: OutgoingRequest | IncomingRequest) => string) = (request) => {
export const getSupportedHeader: ((request: OutgoingRequest | IncomingRequest) => string) = (request) => {
let optionTags: Array<string> = [];

if (request.method === C.REGISTER) {
Expand Down Expand Up @@ -70,7 +70,9 @@ export class OutgoingRequest {
public cseq: number;
public body: string | { body: string, contentType: string } | undefined;
public to: NameAddrHeader | undefined;
public toTag: string | undefined;
public from: NameAddrHeader | undefined;
public fromTag: string;
public extraHeaders: Array<string>;
public callId: string;

Expand Down Expand Up @@ -124,15 +126,17 @@ export class OutgoingRequest {

// To
const toUri: URI | string = params.toUri || ruri;
this.toTag = params.toTag;
let to: string = (params.toDisplayName || params.toDisplayName === 0) ? '"' + params.toDisplayName + '" ' : "";
to += "<" + ((toUri as URI).type === TypeStrings.URI ? (toUri as URI).toRaw() : toUri) + ">";
to += params.toTag ? ";tag=" + params.toTag : "";
to += this.toTag ? ";tag=" + this.toTag : "";

this.to = Grammar.nameAddrHeaderParse(to);
this.setHeader("to", to);

// From
const fromUri: URI | string = params.fromUri || ua.configuration.uri || "";
this.fromTag = params.fromTag || Utils.newTag();
let from: string;
if (params.fromDisplayName || params.fromDisplayName === 0) {
from = '"' + params.fromDisplayName + '" ';
Expand All @@ -142,7 +146,7 @@ export class OutgoingRequest {
from = "";
}
from += "<" + ((fromUri as URI).type === TypeStrings.URI ? (fromUri as URI).toRaw() : fromUri) + ">;tag=";
from += params.fromTag || Utils.newTag();
from += this.fromTag;

this.from = Grammar.nameAddrHeaderParse(from);
this.setHeader("from", from);
Expand All @@ -152,6 +156,7 @@ export class OutgoingRequest {
this.setHeader("call-id", this.callId);

// CSeq
// Why not make this a "1" if not provided? See: https://tools.ietf.org/html/rfc3261#section-8.1.1.5
this.cseq = params.cseq || Math.floor(Math.random() * 10000);
this.setHeader("cseq", this.cseq + " " + method);
}
Expand Down Expand Up @@ -292,32 +297,33 @@ export class OutgoingRequest {
if (!this.from) {
throw new Error("From undefined.");
}
const toHeader = this.getHeader("To");
if (!toHeader) {
throw new Error("To header undefined.");
}
const fromHeader = this.getHeader("From");
if (!fromHeader) {
throw new Error("From header undefined.");
}

// The following procedures are used to construct a CANCEL request. The
// Request-URI, Call-ID, To, the numeric part of CSeq, and From header
// fields in the CANCEL request MUST be identical to those in the
// request being cancelled, including tags. A CANCEL constructed by a
// client MUST have only a single Via header field value matching the
// top Via value in the request being cancelled. Using the same values
// for these header fields allows the CANCEL to be matched with the
// request it cancels (Section 9.2 indicates how such matching occurs).
// However, the method part of the CSeq header field MUST have a value
// of CANCEL. This allows it to be identified and processed as a
// transaction in its own right (See Section 17).
// https://tools.ietf.org/html/rfc3261#section-9.1
const cancel = new OutgoingRequest(
C.CANCEL,
this.ruri,
this.ua,
{
toUri: this.to.uri,
toTag: this.toTag,
fromUri: this.from.uri,
fromTag: this.fromTag,
callId: this.callId,
cseq: this.cseq
},
extraHeaders
);
this.setHeader("To", toHeader);
this.setHeader("From", fromHeader);
cancel.callId = this.callId;
this.setHeader("Call-ID", this.callId);
cancel.cseq = this.cseq;
this.setHeader("CSeq", this.cseq + " " + cancel.method);

// TODO: Revisit this.
// The CANCEL needs to use the same branch parameter so that
Expand All @@ -327,6 +333,9 @@ export class OutgoingRequest {
// the transaction will make a new one.
cancel.branch = this.branch;

// If the request being cancelled contains a Route header field, the
// CANCEL request MUST include that Route header field's values.
// https://tools.ietf.org/html/rfc3261#section-9.1
if (this.headers.Route) {
cancel.headers.Route = this.headers.Route;
}
Expand Down Expand Up @@ -421,7 +430,7 @@ export class OutgoingRequest {
* @class Class for incoming SIP message.
*/
// tslint:disable-next-line:max-classes-per-file
class IncomingMessage {
export class IncomingMessage {
public type: TypeStrings = TypeStrings.IncomingMessage;
public viaBranch!: string;
public method!: string;
Expand All @@ -433,7 +442,7 @@ class IncomingMessage {
public callId!: string;
public cseq!: number;
public via!: {host: string, port: number};
public headers: {[name: string]: any} = {};
public headers: {[name: string]: Array<{ parsed?: any, raw: string }>} = {};
public referTo: string | undefined;
public data!: string;

Expand All @@ -444,7 +453,7 @@ class IncomingMessage {
* @param {String} value header value
*/
public addHeader(name: string, value: string): void {
const header: any = { raw: value };
const header = { raw: value };
name = Utils.headerize(name);

if (this.headers[name]) {
Expand All @@ -460,7 +469,7 @@ class IncomingMessage {
* @returns {String|undefined} Returns the specified header, undefined if header doesn't exist.
*/
public getHeader(name: string): string | undefined {
const header: Array<any> = this.headers[Utils.headerize(name)];
const header = this.headers[Utils.headerize(name)];

if (header) {
if (header[0]) {
Expand Down Expand Up @@ -516,8 +525,8 @@ class IncomingMessage {
return;
}

const header: any = this.headers[name][idx];
const value: string = header.raw;
const header = this.headers[name][idx];
const value = header.raw;

if (header.parsed) {
return header.parsed;
Expand Down Expand Up @@ -677,6 +686,12 @@ export class IncomingRequest extends IncomingMessage {
let to: string = this.getHeader("To") || "";

if (!this.toTag && code > 100) {
// FIXME: This is a MUST, but we are generating a random tag each response
// o To header tags MUST be generated for responses in a stateless
// manner - in a manner that will generate the same tag for the
// same request consistently. For information on tag construction
// see Section 19.3.
// https://tools.ietf.org/html/rfc3261#section-8.2.7
to += ";tag=" + Utils.newTag();
} else if (this.toTag && !this.s("to").hasParam("tag")) {
to += ";tag=" + this.toTag;
Expand Down Expand Up @@ -845,7 +860,7 @@ export class IncomingResponse extends IncomingMessage {
options ? options.body : undefined
);

this.transaction.ackResponse(this, request);
this.transaction.ackResponse(request);

return request;
}
Expand Down
2 changes: 1 addition & 1 deletion src/Subscription.ts
Original file line number Diff line number Diff line change
Expand Up @@ -293,7 +293,7 @@ export class Subscription extends ClientContext {
}

public on(name: "accepted", callback: (response: any, cause: C.causes) => void): this;
public on(name: "notify", callback: (notification: Notification) => void): this;
public on(name: "notify", callback: (notification: { request: IncomingRequest }) => void): this;
public on(
name: "failed" | "rejected" | "terminated",
callback: (messageOrResponse?: any, cause?: C.causes) => void
Expand Down
9 changes: 6 additions & 3 deletions src/Transactions.ts
Original file line number Diff line number Diff line change
Expand Up @@ -339,13 +339,16 @@ export class InviteClientTransaction extends ClientTransaction {
* the transport directly. Herein the transaction layer manages sending ACKs to 2xx responses
* and any retransmissions of those ACKs as needed.
*
* @param response The incoming 2xx final response.
* @param ack The outgoing ACK request.
*/
public ackResponse(response: IncomingResponse, ack: OutgoingRequest): void {
public ackResponse(ack: OutgoingRequest): void {
const toTag = ack.toTag;
if (!toTag) {
throw new Error("To tag undefined.");
}
const id = "z9hG4bK" + Math.floor(Math.random() * 10000000);
ack.setViaHeader(id, this.transport);
this.ackRetransmissionCache.set(response.toTag, ack); // Add to ACK retransmission cache
this.ackRetransmissionCache.set(toTag, ack); // Add to ACK retransmission cache
this.send(ack.toString()).catch((error: Exceptions.TransportError) => {
this.logTransportError(error, "Failed to send ACK to 2xx response.");
});
Expand Down