annotate sip-manual-out/sip_udp.c @ 156:0bacca1f2f7b

sip-out: handle all INVITE responses, except errors
author Mychaela Falconia <falcon@freecalypso.org>
date Wed, 12 Oct 2022 07:13:55 -0800
parents d74b545a3c2a
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
1 /*
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
2 * In this module we implement our UDP socket for SIP,
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
3 * and the associated lowest-level protocol handling.
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
4 */
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
5
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
6 #include <sys/types.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
7 #include <sys/socket.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
8 #include <netinet/in.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
9 #include <stdio.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
10 #include <stdlib.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
11 #include <string.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
12 #include <strings.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
13 #include <unistd.h>
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
14 #include "../libsip/parse.h"
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
15 #include "../libsip/out_msg.h"
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
16
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
17 extern struct in_addr sip_bind_ip;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
18 extern unsigned sip_bind_port;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
19
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
20 int sip_socket;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
21
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
22 open_sip_udp_socket()
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
23 {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
24 struct sockaddr_in sin;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
25 int rc;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
26
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
27 sip_socket = socket(AF_INET, SOCK_DGRAM, 0);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
28 if (sip_socket < 0) {
71
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
29 perror("socket(AF_INET, SOCK_DGRAM, 0)");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
30 exit(1);
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
31 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
32 sin.sin_family = AF_INET;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
33 sin.sin_addr = sip_bind_ip;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
34 sin.sin_port = htons(sip_bind_port);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
35 rc = bind(sip_socket, (struct sockaddr *) &sin, sizeof sin);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
36 if (rc < 0) {
71
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
37 perror("bind of SIP UDP socket");
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
38 exit(1);
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
39 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
40 return(0);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
41 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
42
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
43 void
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
44 sip_socket_select()
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
45 {
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
46 struct sip_pkt_rx pkt;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
47 struct sockaddr_in sin;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
48 socklen_t addrlen;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
49 int rc;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
50
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
51 addrlen = sizeof sin;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
52 rc = recvfrom(sip_socket, pkt.pkt_buffer, MAX_SIP_RX_PACKET, 0,
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
53 (struct sockaddr *) &sin, &addrlen);
71
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
54 if (rc <= 0) {
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
55 perror("recvfrom");
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
56 return;
71
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
57 }
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
58 pkt.pkt_length = rc;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
59 log_sip_msg_rx(pkt.pkt_buffer, pkt.pkt_length, &sin);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
60 rc = parse_incoming_sip_msg(&pkt);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
61 if (rc < 0) {
71
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
62 printf("Incoming SIP UDP message parse error %d\n", rc);
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
63 return;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
64 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
65 /* dispatch good-so-far SIP message */
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
66 if (pkt.parse_msgtype == SIP_MSG_TYPE_REQ)
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
67 process_sip_request(&pkt, &sin);
71
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
68 else if (pkt.parse_msgtype == SIP_MSG_TYPE_RESP)
d74b545a3c2a sip-manual-out: new test program
Mychaela Falconia <falcon@freecalypso.org>
parents: 67
diff changeset
69 process_sip_response(&pkt, &sin);
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
70 }
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
71
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
72 void
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
73 sip_tx_packet(msg, sin)
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
74 struct sip_msg_out *msg;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
75 struct sockaddr_in *sin;
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
76 {
67
15c9e1f8f756 sip-in: enable SIP UDP output
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
77 socklen_t addrlen;
15c9e1f8f756 sip-in: enable SIP UDP output
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
78
15c9e1f8f756 sip-in: enable SIP UDP output
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
79 addrlen = sizeof(struct sockaddr_in);
15c9e1f8f756 sip-in: enable SIP UDP output
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
80 sendto(sip_socket, msg->buf, msg->msg_len, 0, (struct sockaddr *) sin,
15c9e1f8f756 sip-in: enable SIP UDP output
Mychaela Falconia <falcon@freecalypso.org>
parents: 47
diff changeset
81 addrlen);
47
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
82 log_sip_msg_tx(msg->buf, msg->msg_len, sin);
62f39c7cee15 themwi-sip-in skeleton started
Mychaela Falconia <falcon@freecalypso.org>
parents:
diff changeset
83 }