annotate libsip/primary_parse.c @ 42:891ebfb55e6b

sip-rx-test program written, compiles
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 04 Sep 2022 17:55:40 -0800
parents 77d980126efd
children 30572642e853
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
40
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we are going to implement the first stage of
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * parsing for incoming SIP UDP packets, using struct sip_pkt_rx
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 * defined in parse.h.
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5 */
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <ctype.h>
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <string.h>
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <strings.h>
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include "parse.h"
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 static
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 find_crlf(ptr, msg_end, endp, nextp)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 char *ptr, *msg_end, **endp, **nextp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16 {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 for (;;) {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 if (ptr >= msg_end)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 switch (*ptr) {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21 case '\0':
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 case '\n':
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 *endp = ptr;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 *nextp = ptr + 1;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26 return(1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 case '\r':
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 *endp = ptr;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
29 ptr++;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
30 if (ptr >= msg_end)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 if (*ptr != '\n')
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 *nextp = ptr + 1;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 return(1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 }
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
37 ptr++;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
38 }
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 static
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42 try_status_line(msg)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 struct sip_pkt_rx *msg;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 if (strncmp(msg->pkt_buffer, "SIP/2.0 ", 8))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 if (!isdigit(msg->pkt_buffer[8]))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 if (!isdigit(msg->pkt_buffer[9]))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 if (!isdigit(msg->pkt_buffer[10]))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 if (msg->pkt_buffer[11] != ' ')
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
54 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
55 msg->status_code = atoi(msg->pkt_buffer + 8);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 msg->status_str = msg->pkt_buffer + 8;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
57 return(1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 }
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 static
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 try_request_line(msg)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
62 struct sip_pkt_rx *msg;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 char *cp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 cp = msg->pkt_buffer;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 if (!isupper(*cp))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
68 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
69 while (isalnum(*cp))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 cp++;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71 if (*cp != ' ')
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 msg->req_method = msg->pkt_buffer;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 *cp++ = '\0';
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 msg->req_uri = cp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 while (*cp && !isspace(*cp))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
77 cp++;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
78 if (*cp != ' ')
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
79 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
80 *cp++ = '\0';
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
81 if (strcmp(cp, "SIP/2.0"))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 else
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
84 return(1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
85 }
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
86
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
87 parse_incoming_sip_msg(msg)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
88 struct sip_pkt_rx *msg;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
89 {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
90 char *msg_end = msg->pkt_buffer + msg->pkt_length;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
91 char *cp, *endp, *nextp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
92 unsigned hdr_cnt;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
93 int rc;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
94
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
95 /* begin by isolating the Request-Line or Status-Line */
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
96 rc = find_crlf(msg->pkt_buffer, msg_end, &endp, &nextp);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
97 if (!rc)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
98 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
99 *endp = '\0';
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
100 if (try_status_line(msg))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
101 msg->parse_msgtype = SIP_MSG_TYPE_RESP;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
102 else if (try_request_line(msg))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
103 msg->parse_msgtype = SIP_MSG_TYPE_REQ;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
104 else
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
105 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
106 /* now preparse header fields */
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
107 cp = nextp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
108 for (hdr_cnt = 0; ; ) {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
109 rc = find_crlf(cp, msg_end, &endp, &nextp);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
110 if (!rc)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
111 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
112 if (endp == cp) /* final CRLF? */
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
113 break;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
114 if (!isalpha(*cp))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
115 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
116 if (hdr_cnt >= MAX_HEADER_FIELDS)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
117 return(-2);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
118 msg->hdr_fields[hdr_cnt].field_name = cp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
119 while (cp < endp && (isalnum(*cp) || *cp == '-'))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
120 cp++;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
121 if (cp >= endp)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
122 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
123 if (*cp == ':')
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
124 *cp++ = '\0';
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
125 else if (isspace(*cp)) {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
126 *cp++ = '\0';
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
127 while (cp < endp && isspace(*cp))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
128 cp++;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
129 if (cp >= endp)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
130 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
131 if (*cp++ != ':')
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
132 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
133 } else
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
134 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
135 while (cp < endp && isspace(*cp))
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
136 cp++;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
137 msg->hdr_fields[hdr_cnt++].field_value = cp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
138 cp = nextp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
139 while (cp < msg_end && (*cp == ' ' || *cp == '\t')) {
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
140 rc = find_crlf(cp, msg_end, &endp, &nextp);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
141 if (!rc)
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
142 return(-1);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
143 cp = nextp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
144 }
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
145 *endp = '\0';
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
146 }
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
147 msg->num_hdr_fields = hdr_cnt;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
148 msg->msg_body = nextp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
149 msg->msg_body_len = msg_end - nextp;
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
150 return(0);
77d980126efd libsip started with primary parsing function
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
151 }