Revert "Revert "and added files""
[bcm963xx.git] / userapps / opensource / libosip2 / src / osip2 / osip_event.c
1 /*
2   The oSIP library implements the Session Initiation Protocol (SIP -rfc3261-)
3   Copyright (C) 2001,2002,2003  Aymeric MOIZARD jack@atosc.org
4   
5   This library is free software; you can redistribute it and/or
6   modify it under the terms of the GNU Lesser General Public
7   License as published by the Free Software Foundation; either
8   version 2.1 of the License, or (at your option) any later version.
9   
10   This library is distributed in the hope that it will be useful,
11   but WITHOUT ANY WARRANTY; without even the implied warranty of
12   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13   Lesser General Public License for more details.
14   
15   You should have received a copy of the GNU Lesser General Public
16   License along with this library; if not, write to the Free Software
17   Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
18 */
19
20 #include <osip2/internal.h>
21 #include <osip2/osip.h>
22
23 #include "fsm.h"
24
25
26 /* Create a sipevent according to the SIP message buf. */
27 /* INPUT : char *buf | message as a string.            */
28 /* return NULL  if message cannot be parsed            */
29 osip_event_t *
30 osip_parse (char *buf)
31 {
32   osip_event_t *se = __osip_event_new (UNKNOWN_EVT, 0);
33   int i;
34
35 #ifdef TEST_PARSER_SPEED
36   {
37     int kk;
38     int pstime1, pstime;
39     struct timespec tv1;
40
41     clock_get_time (CLOCK_REALTIME, &tv1);
42     pstime = ((tv1.tv_sec * 1000) + (tv1.tv_nsec / 1000000));
43     for (kk = 0; kk < 10000; kk++)
44       {
45
46         i = osip_message_init (&(se->sip));
47
48         if (osip_message_parse (se->sip, buf) == -1)
49           {
50             fprintf (stdout, "osip_message_parse retrun -1\n");
51             osip_message_free (se->sip);
52           }
53         else
54           {                     /* msg is parsed */
55             osip_message_free (se->sip);
56           }
57       }
58     clock_get_time (CLOCK_REALTIME, &tv1);
59     pstime1 = ((tv1.tv_sec * 1000) + (tv1.tv_nsec / 1000000));
60     fprintf (stdout, "CPU clock ticks for 10000 messages - T1: %i - T2: %i\n",
61              pstime1, pstime);
62     fprintf (stdout, "CPU time for 10000 messages - %d\n",
63              (pstime1 - pstime));
64   }
65   osip_free (se);
66   return NULL;
67 #endif
68   /* parse message and set up an event */
69   i = osip_message_init (&(se->sip));
70   if (osip_message_parse (se->sip, buf) == -1)
71     {
72       OSIP_TRACE (osip_trace
73                   (__FILE__, __LINE__, OSIP_ERROR, NULL,
74                    "could not parse message\n"));
75       osip_message_free (se->sip);
76       osip_free (se);
77       return NULL;
78     }
79   else
80     {
81       if (se->sip->call_id != NULL && se->sip->call_id->number != NULL)
82         {
83           OSIP_TRACE (osip_trace
84                       (__FILE__, __LINE__, OSIP_INFO3, NULL,
85                        "MESSAGE REC. CALLID:%s\n", se->sip->call_id->number));
86         }
87
88       if (MSG_IS_REQUEST (se->sip))
89         {
90           if (se->sip->sip_method == NULL || se->sip->req_uri == NULL)
91             {
92               osip_message_free (se->sip);
93               osip_free (se);
94               return NULL;
95             }
96         }
97
98       se->type = evt_set_type_incoming_sipmessage (se->sip);
99       return se;
100     }
101 }
102
103 /* allocates an event from retransmitter.             */
104 /* USED ONLY BY THE STACK.                            */
105 /* INPUT : int transactionid | id of the transaction. */
106 /* INPUT : type_t type | type of event.               */
107 /* returns null on error. */
108 osip_event_t *
109 __osip_event_new (type_t type, int transactionid)
110 {
111   osip_event_t *sipevent;
112
113   sipevent = (osip_event_t *) osip_malloc (sizeof (osip_event_t));
114   if (sipevent == NULL)
115     return NULL;
116   sipevent->type = type;
117   sipevent->sip = NULL;
118   sipevent->transactionid = transactionid;
119   return sipevent;
120 }
121
122 /* allocates an event from user.                      */
123 /* USED ONLY BY THE USER.                             */
124 /* INPUT : osip_message_t *sip | sip message for transaction.  */
125 /* returns null on error. */
126 osip_event_t *
127 osip_new_outgoing_sipmessage (osip_message_t * sip)
128 {
129   osip_event_t *sipevent;
130
131   if (sip == NULL)
132     return NULL;
133   if (MSG_IS_REQUEST (sip))
134     {
135       if (sip->sip_method == NULL)
136         return NULL;
137       if (sip->req_uri == NULL)
138         return NULL;
139     }
140   sipevent = (osip_event_t *) osip_malloc (sizeof (osip_event_t));
141   if (sipevent == NULL)
142     return NULL;
143
144   sipevent->sip = sip;
145   sipevent->type = evt_set_type_outgoing_sipmessage (sip);
146   sipevent->transactionid = 0;
147   return sipevent;
148 }
149
150 type_t
151 evt_set_type_incoming_sipmessage (osip_message_t * sip)
152 {
153   if (MSG_IS_REQUEST (sip))
154     {
155       if (MSG_IS_INVITE (sip))
156         return RCV_REQINVITE;
157       else if (MSG_IS_ACK (sip))
158         return RCV_REQACK;
159       return RCV_REQUEST;
160     }
161   else
162     {
163       if (MSG_IS_STATUS_1XX (sip))
164         return RCV_STATUS_1XX;
165       else if (MSG_IS_STATUS_2XX (sip))
166         return RCV_STATUS_2XX;
167       return RCV_STATUS_3456XX;
168     }
169 }
170
171 type_t
172 evt_set_type_outgoing_sipmessage (osip_message_t * sip)
173 {
174
175   if (MSG_IS_REQUEST (sip))
176     {
177       if (MSG_IS_INVITE (sip))
178         return SND_REQINVITE;
179       if (MSG_IS_ACK (sip))
180         return SND_REQACK;
181       return SND_REQUEST;
182     }
183   else
184     {
185       if (MSG_IS_STATUS_1XX (sip))
186         return SND_STATUS_1XX;
187       else if (MSG_IS_STATUS_2XX (sip))
188         return SND_STATUS_2XX;
189       return SND_STATUS_3456XX;
190     }
191 }
192
193 void
194 osip_event_free (osip_event_t * event)
195 {
196   if (event != NULL)
197     {
198       osip_message_free (event->sip);
199       osip_free (event);
200     }
201 }