Source

ocaml-sip / src / Sip_types.ml

Full commit
(*********************************************************************************************************************
 * Copyrights (C) 2012 by Pawel Wieczorek <wieczyk at gmail>
 *  http://bitbucket.org/wieczyk/ocaml-sip
 *)

open Batteries
open Sip_utils

(***************************************************************************************************)
(* DATA TYPE *)

type sip_request_method
    = REGISTER
    | INVITE
    | CANCEL
    | ACK
    | PRACK
    | BYE
    | OPTIONS
    | SUBSCRIBE
    | NOTIFY
    | PUBLISH
    | INFO
    | REFER
    | MESSAGE
    | UPDATE
    | UnknownRequestMethod of string


type sip_response_code
    = RESP_100_TRYING
    | RESP_180_RINGING
    | RESP_181_CALL_IS_BEING_FORWARDED
    | RESP_182_QUEUED
    | RESP_183_SESSION_IN_PROGRESS

    | RESP_200_OK
    | RESP_202_ACCEPTED
    | RESP_204_NO_NOTIFICATION

    | RESP_300_MULTIPLE_CHOICES
    | RESP_301_MOVED_PERMANENTLY
    | RESP_302_MOVED_TEMPORILY
    | RESP_305_USE_PROXY
    | RESP_380_ALTERNATIVE_SERVICE

    | RESP_400_BAD_REQUEST
    | RESP_401_UNAUTHORIZED
    | RESP_402_PAYMENT_REQUIRED
    | RESP_403_FORBIDDEN
    | RESP_404_NOT_FOUND
    | RESP_405_METHOD_NOT_ALLOWED

    | RESP_500_SERVER_INTERNAL_ERROR
    | RESP_501_NOT_IMPLEMENTED
    | RESP_502_BAD_GATEWAY
    | RESP_503_SERVICE_UNAVAILABLE

    | RESP_600_BUSY_EVERYWHERE

    | OtherResponse of int


type sip_login =
    SipLogin of string

type sip_host = 
    SipHost of string

type sip_uri =
    SipUri of string

type sip_address =
    SipAddress of string

type sip_call_id =
    SipCallId of string

type sip_cseq =
    SipCseq of string

type sip_contact =
    SipContact of string

type sip_version =
    | SIP_1_0
    | SIP_2_0
    | UnknownSipVersion of string


type sip_via =
    | SipVia of string

class type sip_request =
    object

        method sip_method       : sip_request_method

        method sip_version      : sip_version

        method uri              : sip_uri

        method content_length   : int

        method addr_to          : sip_address

        method addr_from        : sip_address

        method vias             : sip_via list

        method call_id          : sip_call_id

        method cseq             : sip_cseq

        method max_forwards     : int

(*
        method contact          : sip_contact
*)

        method header           : string -> string

        method header_all       : string -> string list

    end

(*********************************************************************************************************************
 * Coercions
 *)

let sip_request_method_of_string = function
    | "REGISTER" -> REGISTER
    | "INVITE" -> INVITE
    | "CANCEL" -> CANCEL
    | "ACK" -> ACK
    | "PRACK" -> PRACK
    | "BYE" -> BYE
    | "OPTIONS" -> OPTIONS
    | "SUBSCRIBE" -> SUBSCRIBE
    | "NOTIFY" -> NOTIFY
    | "PUBLISH" -> PUBLISH
    | "INFO" -> INFO
    | "REFER" -> REFER
    | "MESSAGE" -> MESSAGE
    | "UPDATE" -> UPDATE
    | s -> UnknownRequestMethod s

let string_of_sip_request_method = function
    | REGISTER -> "REGISTER"
    | INVITE -> "INVITE"
    | CANCEL -> "CANCEL"
    | ACK -> "ACK"
    | PRACK -> "PRACK"
    | BYE -> "BYE"
    | OPTIONS -> "OPTIONS"
    | SUBSCRIBE -> "SUBSCRIBE"
    | NOTIFY -> "NOTIFY"
    | PUBLISH -> "PUBLISH"
    | INFO -> "INFO"
    | REFER -> "REFER"
    | MESSAGE -> "MESSAGE"
    | UPDATE -> "UPDATE"
    | UnknownRequestMethod s -> s

let int_of_response_code = function
    | RESP_100_TRYING -> 100
    | RESP_180_RINGING -> 180
    | RESP_181_CALL_IS_BEING_FORWARDED -> 181
    | RESP_182_QUEUED -> 182
    | RESP_183_SESSION_IN_PROGRESS -> 183

    | RESP_200_OK -> 200
    | RESP_202_ACCEPTED -> 202
    | RESP_204_NO_NOTIFICATION -> 204

    | RESP_300_MULTIPLE_CHOICES -> 300
    | RESP_301_MOVED_PERMANENTLY -> 301
    | RESP_302_MOVED_TEMPORILY -> 302
    | RESP_305_USE_PROXY -> 305
    | RESP_380_ALTERNATIVE_SERVICE -> 380

    | RESP_400_BAD_REQUEST -> 400
    | RESP_401_UNAUTHORIZED -> 401
    | RESP_402_PAYMENT_REQUIRED -> 402
    | RESP_403_FORBIDDEN -> 403
    | RESP_404_NOT_FOUND -> 404
    | RESP_405_METHOD_NOT_ALLOWED -> 405

    | RESP_500_SERVER_INTERNAL_ERROR -> 500
    | RESP_501_NOT_IMPLEMENTED -> 501
    | RESP_502_BAD_GATEWAY -> 502
    | RESP_503_SERVICE_UNAVAILABLE -> 503

    | RESP_600_BUSY_EVERYWHERE -> 600

    | OtherResponse i -> i

let sip_version_of_string = function
    | "SIP/1.0" ->
        SIP_1_0

    | "SIP/2.0" ->
        SIP_2_0

    | sipver when String.starts_with sipver "SIP/" ->
        UnknownSipVersion (String.slice ~first:4 sipver)

    | _ -> raise (Failure "sip_version_of_string")

let string_of_sip_version = function
    | SIP_1_0 -> "SIP/1.0"
    | SIP_2_0 -> "SIP/2.0"
    | UnknownSipVersion s -> "SIP/"^ s

let string_of_sip_uri = function
    | SipUri s -> s

let sip_uri_of_string = function
    | s -> SipUri s

let string_of_sip_cseq = function
    | SipCseq s -> s

let sip_cseq_of_string = function
    | s -> SipCseq s

let sip_call_id_of_string = function
    | s -> SipCallId s

let string_of_sip_call_id = function
    | SipCallId s -> s

let sip_via_of_string = function
    | s -> SipVia s

let string_of_sip_via = function
    | SipVia s -> s

let sip_address_of_string = function
    | s -> SipAddress s

let string_of_sip_address = function
    | SipAddress s -> s