< Previous by Date Date Index Next by Date >
< Previous in Thread Thread Index Next in Thread >

Re: [reSIProcate-users] How to code this simple SIP session?


Seems like a good read through of RFC3261 would be good to get a better understanding how to form valid SIP messages and responses.  There are also code samples throughout resip that will help - see dum/Helper.cxx and stack/DeprecatedDialog.cxx.  

A couple of things I see quickly:
1.  The CSEQ in the ACK should match the invite.
2.  The ACK/200 message should be routed to the Contact header received in the 200.  If the destination is not receiving the ACK or it is malformed,  they will retransmit the 200.
3.  You don't need to populate the via header - the stack will do this for you as long as you add an empty via header:  see Helper::makeRequest for a good sample.  Similar rules apply to the Contact header - you only need to add a empty contact header with the user field only populated - the stack will fill in the rest.

Scott

On Fri, Apr 9, 2010 at 8:50 AM, Vijay Mathew <vijay.the.schemer@xxxxxxxxx> wrote:
Hi all,

I want to create a simple SIP client that will do the following
interactions with an existing SIP server:

client                                    server

INVITE   ============>
            <===========      200 OK
ACK      ============>
            <============   SUBSCRIBE
200 OK  =============>
NOTIFY  =============>
           <=============   200 OK
BYE     ==============>
          <==============  200 OK

I tried the following code to start with the INVITE and the ACK. I get
"200 OK" for the INVITE but for the ACK, I am again getting the same
"200 OK" that I got for the INVITE. What did I do wrong?

static SipMessage* receive(SipStack& tu)
{
       while (true)
       {
               Sleep(100);
               SipMessage *msg = stack.receive();
               if (msg)
               {
                       std::cout << *msg << '\n';
                       return msg;
               }
       }
       return 0;
}

int
main ()
{
   NameAddr target;
   target.uri().scheme() = "sip";
   target.uri().host() = "192.168.114.211";
   target.uri().port() = 5060;
   target.uri().param(p_transport) = "tcp";

   NameAddr from = target;
   from.uri().user() = "me";
   from.uri().port() = 5070;

   std::auto_ptr<SipMessage> reg(Helper::makeInvite(target, from));

   reg->header(h_Contacts).clear();
   reg->header(h_Contacts).push_back(from);
   reg->header(h_Vias).front().transport() = Data("tcp");
   reg->header(h_Vias).front().sentHost() = "192.168.114.211";
   reg->header(h_Vias).front().sentPort() = 5070;
       Tokens tokens;
       tokens.push_back(Token(Data("x-com-nice-imm-call-state")));
       reg->header(h_AllowEvents) = tokens;

       std::cout << *reg << '\n';

       SipStack stack;
       stack.addTransport(TCP, 5070);
       StackThread stackThread(stack);
       stackThread.run();
       stack.send(*reg);

       SipMessage* msg = receive(stack);
       if (msg)
       {
               if (msg->isResponse())
               {
                       std::cout << "REASON=" << msg->getReason() << "\n";
                       std::auto_ptr<SipMessage> reg(Helper::makeRequest(target, from, ACK));
                       reg->header(h_Contacts).clear();
                       reg->header(h_Contacts).push_back(from);
                       reg->header(h_Vias).front().transport() = Data("tcp");
                       reg->header(h_Vias).front().sentHost() = "192.168.114.211";
                       reg->header(h_Vias).front().sentPort() = 5070;
                       reg->header(h_CallID) = msg->header(h_CallId);
                       std::cout << *reg << '\n';
                       delete msg;
                       msg = 0;
                       stack.send(*reg);
               }
               else
               {
                       std::cout << "MSG is not a response\n";
               }
       }

       msg = receive(stack);
       if (msg)
       {
               if (msg->isResponse())
               {
                       std::cout << "REASON=" << msg->getReason() << "\n";
                       delete msg;
                       // std::auto_ptr<SipMessage> reg(Helper::makeRequest(target, from, ACK));
               }
               else
               {
                       std::cout << "MSG is not a response\n";
               }
       }

       return 0;
}

Thanks in advance,

-- Vijay
_______________________________________________
resiprocate-users mailing list
resiprocate-users@xxxxxxxxxxxxxxx
List Archive: http://list.resiprocate.org/archive/resiprocate-users/