comparison test-voice/sip_udp.c @ 0:35c0d9f03c0a

beginning with sipout-test-voice, a copy of sip-manual-out from themwi-system-sw
author Mychaela Falconia <falcon@freecalypso.org>
date Sun, 03 Mar 2024 23:20:19 -0800
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:35c0d9f03c0a
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 <unistd.h>
14 #include "../libsip/parse.h"
15 #include "../libsip/out_msg.h"
16
17 extern struct in_addr sip_bind_ip;
18 extern unsigned sip_bind_port;
19
20 int sip_socket;
21
22 open_sip_udp_socket()
23 {
24 struct sockaddr_in sin;
25 int rc;
26
27 sip_socket = socket(AF_INET, SOCK_DGRAM, 0);
28 if (sip_socket < 0) {
29 perror("socket(AF_INET, SOCK_DGRAM, 0)");
30 exit(1);
31 }
32 sin.sin_family = AF_INET;
33 sin.sin_addr = sip_bind_ip;
34 sin.sin_port = htons(sip_bind_port);
35 rc = bind(sip_socket, (struct sockaddr *) &sin, sizeof sin);
36 if (rc < 0) {
37 perror("bind of SIP UDP socket");
38 exit(1);
39 }
40 return(0);
41 }
42
43 void
44 sip_socket_select()
45 {
46 struct sip_pkt_rx pkt;
47 struct sockaddr_in sin;
48 socklen_t addrlen;
49 int rc;
50
51 addrlen = sizeof sin;
52 rc = recvfrom(sip_socket, pkt.pkt_buffer, MAX_SIP_RX_PACKET, 0,
53 (struct sockaddr *) &sin, &addrlen);
54 if (rc <= 0) {
55 perror("recvfrom");
56 return;
57 }
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 printf("Incoming SIP UDP message parse error %d\n", rc);
63 return;
64 }
65 /* dispatch good-so-far SIP message */
66 if (pkt.parse_msgtype == SIP_MSG_TYPE_REQ)
67 process_sip_request(&pkt, &sin);
68 else if (pkt.parse_msgtype == SIP_MSG_TYPE_RESP)
69 process_sip_response(&pkt, &sin);
70 }
71
72 void
73 sip_tx_packet(msg, sin)
74 struct sip_msg_out *msg;
75 struct sockaddr_in *sin;
76 {
77 socklen_t addrlen;
78
79 addrlen = sizeof(struct sockaddr_in);
80 sendto(sip_socket, msg->buf, msg->msg_len, 0, (struct sockaddr *) sin,
81 addrlen);
82 log_sip_msg_tx(msg->buf, msg->msg_len, sin);
83 }