comparison sip-out/sip_udp.c @ 154:e54b0a9e322f

beginning of themwi-sip-out
author Mychaela Falconia <falcon@freecalypso.org>
date Tue, 11 Oct 2022 23:04:01 -0800
parents sip-in/sip_udp.c@ff4b76a107a1
children
comparison
equal deleted inserted replaced
153:99fd4ae573ae 154:e54b0a9e322f
1 /*
2 * In this module we implement our UDP socket for SIP,
3 * and the associated lowest-level protocol handling.
4 */
5
6 #include <sys/types.h>
7 #include <sys/socket.h>
8 #include <netinet/in.h>
9 #include <stdio.h>
10 #include <stdlib.h>
11 #include <string.h>
12 #include <strings.h>
13 #include <syslog.h>
14 #include <unistd.h>
15 #include "../libsip/parse.h"
16 #include "../libsip/out_msg.h"
17
18 extern struct in_addr sip_bind_ip;
19 extern unsigned sip_bind_port;
20
21 int sip_socket;
22
23 open_sip_udp_socket()
24 {
25 struct sockaddr_in sin;
26 int rc;
27
28 sip_socket = socket(AF_INET, SOCK_DGRAM, 0);
29 if (sip_socket < 0) {
30 syslog(LOG_CRIT, "socket(AF_INET, SOCK_DGRAM, 0): %m");
31 return(-1);
32 }
33 sin.sin_family = AF_INET;
34 sin.sin_addr = sip_bind_ip;
35 sin.sin_port = htons(sip_bind_port);
36 rc = bind(sip_socket, (struct sockaddr *) &sin, sizeof sin);
37 if (rc < 0) {
38 syslog(LOG_CRIT, "bind of SIP UDP socket: %m");
39 return(-1);
40 }
41 update_max_fd(sip_socket);
42 return(0);
43 }
44
45 void
46 sip_socket_select()
47 {
48 struct sip_pkt_rx pkt;
49 struct sockaddr_in sin;
50 socklen_t addrlen;
51 int rc;
52
53 addrlen = sizeof sin;
54 rc = recvfrom(sip_socket, pkt.pkt_buffer, MAX_SIP_RX_PACKET, 0,
55 (struct sockaddr *) &sin, &addrlen);
56 if (rc <= 0)
57 return;
58 pkt.pkt_length = rc;
59 log_sip_msg_rx(pkt.pkt_buffer, pkt.pkt_length, &sin);
60 rc = parse_incoming_sip_msg(&pkt);
61 if (rc < 0) {
62 /* parse errors */
63 if (rc == -2 && pkt.parse_msgtype == SIP_MSG_TYPE_REQ)
64 syslog(LOG_ERR,
65 "SIP %.16s msg exceeds MAX_HEADER_FIELDS",
66 pkt.req_method);
67 else if (rc == -2 && pkt.parse_msgtype == SIP_MSG_TYPE_RESP)
68 syslog(LOG_ERR,
69 "SIP response msg exceeds MAX_HEADER_FIELDS");
70 /* in any case, silently discard */
71 return;
72 }
73 /* dispatch good-so-far SIP message */
74 if (pkt.parse_msgtype == SIP_MSG_TYPE_REQ)
75 process_sip_request(&pkt, &sin);
76 else if (pkt.parse_msgtype == SIP_MSG_TYPE_RESP)
77 process_sip_response(&pkt, &sin);
78 }
79
80 void
81 sip_tx_packet(msg, sin)
82 struct sip_msg_out *msg;
83 struct sockaddr_in *sin;
84 {
85 socklen_t addrlen;
86
87 addrlen = sizeof(struct sockaddr_in);
88 sendto(sip_socket, msg->buf, msg->msg_len, 0, (struct sockaddr *) sin,
89 addrlen);
90 log_sip_msg_tx(msg->buf, msg->msg_len, sin);
91 }