# HG changeset patch # User Mychaela Falconia # Date 1679039177 28800 # Node ID 258932879f8b8677234047811d15027e47788061 # Parent 068fce34e5652a419a8be5891a106a1a4963f08b sip-manual-out: rework for internal RTP handling, using themwi-rtp-mgr diff -r 068fce34e565 -r 258932879f8b Makefile --- a/Makefile Sun Mar 12 08:41:10 2023 -0800 +++ b/Makefile Thu Mar 16 23:46:17 2023 -0800 @@ -12,7 +12,7 @@ mtctest: libnumdb libutil rtp-mgr: libutil sip-in: libnumdb libsip libutil -sip-manual-out: libsip libutil +sip-manual-out: librtpalloc libsip libutil sip-out: liboutrt libsip libutil utils: libnumdb librtpalloc libsip libutil diff -r 068fce34e565 -r 258932879f8b sip-manual-out/Makefile --- a/sip-manual-out/Makefile Sun Mar 12 08:41:10 2023 -0800 +++ b/sip-manual-out/Makefile Thu Mar 16 23:46:17 2023 -0800 @@ -1,9 +1,9 @@ CC= gcc CFLAGS= -O2 PROG= sip-manual-out -OBJS= bye_in.o disc_cmd.o dummy_rtp.o main.o readconf.o reinvite.o sip_log.o \ +OBJS= bye_in.o disc_cmd.o main.o readconf.o reinvite.o rtp.o sip_log.o \ sip_udp.o uac.o uas.o -LIBS= ../libsip/libsip.a ../libutil/libutil.a +LIBS= ../libsip/libsip.a ../librtpalloc/librtpalloc.a ../libutil/libutil.a INSTBIN=/usr/local/bin all: ${PROG} diff -r 068fce34e565 -r 258932879f8b sip-manual-out/dummy_rtp.c --- a/sip-manual-out/dummy_rtp.c Sun Mar 12 08:41:10 2023 -0800 +++ /dev/null Thu Jan 01 00:00:00 1970 +0000 @@ -1,66 +0,0 @@ -/* - * In this module we implement the code that connects to themwi-mgw - * and obtains a dummy PSTN-side RTP endpoint. - */ - -#include -#include -#include -#include -#include -#include -#include -#include -#include -#include "../include/tmgw_ctrl.h" -#include "../include/tmgw_const.h" - -struct sockaddr_in dummy_rtp_endp; - -static char tmgw_socket_pathname[] = "/var/gsm/tmgw_socket"; - -obtain_dummy_rtp() -{ - struct sockaddr_un sa; - unsigned sa_len; - int fd, rc; - struct tmgw_ctrl_req req; - struct tmgw_ctrl_resp resp; - - fd = socket(AF_UNIX, SOCK_SEQPACKET, 0); - if (fd < 0) { - perror("socket(AF_UNIX, SOCK_SEQPACKET, 0)"); - exit(1); - } - fill_sockaddr_un(tmgw_socket_pathname, &sa, &sa_len); - rc = connect(fd, (struct sockaddr *) &sa, sa_len); - if (rc < 0) { - perror(tmgw_socket_pathname); - exit(1); - } - bzero(&req, sizeof req); - req.opcode = TMGW_CTRL_OP_CRCX; - req.ep_id = TMGW_EP_TYPE_DUMMY_PSTN; - rc = send(fd, &req, sizeof req, 0); - if (rc < 0) { - perror("send to TMGW socket"); - exit(1); - } - rc = recv(fd, &resp, sizeof resp, 0); - if (rc < 0) { - perror("recv from TMGW socket"); - exit(1); - } - if (rc != sizeof resp) { - fprintf(stderr, - "error: response packet from TMGW has wrong length (%d bytes)\n", - rc); - exit(1); - } - if (resp.res != TMGW_RESP_OK) { - fprintf(stderr, "TMGW CRCX returned error %u\n", resp.res); - exit(1); - } - bcopy(&resp.pstn_addr, &dummy_rtp_endp, sizeof(struct sockaddr_in)); - return(0); -} diff -r 068fce34e565 -r 258932879f8b sip-manual-out/main.c --- a/sip-manual-out/main.c Sun Mar 12 08:41:10 2023 -0800 +++ b/sip-manual-out/main.c Thu Mar 16 23:46:17 2023 -0800 @@ -20,7 +20,8 @@ extern struct in_addr sip_bind_ip, sip_dest_ip; extern unsigned sip_bind_port, sip_dest_port; extern char sip_dest_domain[]; -extern struct sockaddr_in dummy_rtp_endp; +extern struct sockaddr_in rtp_local_addr; +extern int rtp_udp_fd, rtcp_udp_fd; struct sockaddr_in sip_dest_sin; char from_uri[128], to_uri[128], call_id[128]; @@ -55,8 +56,8 @@ if (rc < 0) goto msg_size_err; bzero(&sdp, sizeof sdp); - sdp.conn_ip = dummy_rtp_endp.sin_addr; - sdp.conn_port = ntohs(dummy_rtp_endp.sin_port); + sdp.conn_ip = rtp_local_addr.sin_addr; + sdp.conn_port = ntohs(rtp_local_addr.sin_port); if (pcma_codec_force) sdp.codec_mask = SDP_CODEC_MASK_PCMA; else { @@ -111,12 +112,12 @@ goto usage; read_config_file(argv[optind]); open_sip_udp_socket(); - obtain_dummy_rtp(); + obtain_rtp_endp(); sip_dest_sin.sin_family = AF_INET; sip_dest_sin.sin_addr = sip_dest_ip; sip_dest_sin.sin_port = htons(sip_dest_port); sprintf(from_uri, ";tag=out%u", argv[optind+1], - inet_ntoa(sip_bind_ip), ntohs(dummy_rtp_endp.sin_port)); + inet_ntoa(sip_bind_ip), ntohs(rtp_local_addr.sin_port)); sprintf(to_uri, "sip:%s@%s", argv[optind+2], sip_dest_domain); if (logfile) { rc = open_sip_log_file(logfile); @@ -129,20 +130,27 @@ char **argv; { fd_set fds; - int rc; + int rc, max_fd; preliminary_proc(argc, argv); gettimeofday(&cur_event_time, 0); sprintf(call_id, "%08u_%u@%s", (unsigned)(cur_event_time.tv_sec % 100000000), - ntohs(dummy_rtp_endp.sin_port), inet_ntoa(sip_bind_ip)); + ntohs(rtp_local_addr.sin_port), inet_ntoa(sip_bind_ip)); send_invite_req(); /* main select loop */ + max_fd = sip_socket; + if (rtp_udp_fd > max_fd) + max_fd = rtp_udp_fd; + if (rtcp_udp_fd > max_fd) + max_fd = rtcp_udp_fd; for (;;) { FD_ZERO(&fds); FD_SET(0, &fds); FD_SET(sip_socket, &fds); - rc = select(sip_socket+1, &fds, 0, 0, 0); + FD_SET(rtp_udp_fd, &fds); + FD_SET(rtcp_udp_fd, &fds); + rc = select(max_fd+1, &fds, 0, 0, 0); if (rc < 0) { if (errno == EINTR) continue; @@ -154,5 +162,9 @@ select_stdin(); if (FD_ISSET(sip_socket, &fds)) sip_socket_select(); + if (FD_ISSET(rtp_udp_fd, &fds)) + rtp_rx_select(); + if (FD_ISSET(rtcp_udp_fd, &fds)) + rtcp_rx_select(); } } diff -r 068fce34e565 -r 258932879f8b sip-manual-out/old/dummy_rtp.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sip-manual-out/old/dummy_rtp.c Thu Mar 16 23:46:17 2023 -0800 @@ -0,0 +1,66 @@ +/* + * In this module we implement the code that connects to themwi-mgw + * and obtains a dummy PSTN-side RTP endpoint. + */ + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include "../include/tmgw_ctrl.h" +#include "../include/tmgw_const.h" + +struct sockaddr_in dummy_rtp_endp; + +static char tmgw_socket_pathname[] = "/var/gsm/tmgw_socket"; + +obtain_dummy_rtp() +{ + struct sockaddr_un sa; + unsigned sa_len; + int fd, rc; + struct tmgw_ctrl_req req; + struct tmgw_ctrl_resp resp; + + fd = socket(AF_UNIX, SOCK_SEQPACKET, 0); + if (fd < 0) { + perror("socket(AF_UNIX, SOCK_SEQPACKET, 0)"); + exit(1); + } + fill_sockaddr_un(tmgw_socket_pathname, &sa, &sa_len); + rc = connect(fd, (struct sockaddr *) &sa, sa_len); + if (rc < 0) { + perror(tmgw_socket_pathname); + exit(1); + } + bzero(&req, sizeof req); + req.opcode = TMGW_CTRL_OP_CRCX; + req.ep_id = TMGW_EP_TYPE_DUMMY_PSTN; + rc = send(fd, &req, sizeof req, 0); + if (rc < 0) { + perror("send to TMGW socket"); + exit(1); + } + rc = recv(fd, &resp, sizeof resp, 0); + if (rc < 0) { + perror("recv from TMGW socket"); + exit(1); + } + if (rc != sizeof resp) { + fprintf(stderr, + "error: response packet from TMGW has wrong length (%d bytes)\n", + rc); + exit(1); + } + if (resp.res != TMGW_RESP_OK) { + fprintf(stderr, "TMGW CRCX returned error %u\n", resp.res); + exit(1); + } + bcopy(&resp.pstn_addr, &dummy_rtp_endp, sizeof(struct sockaddr_in)); + return(0); +} diff -r 068fce34e565 -r 258932879f8b sip-manual-out/rtp.c --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/sip-manual-out/rtp.c Thu Mar 16 23:46:17 2023 -0800 @@ -0,0 +1,58 @@ +/* + * In this module we implement our RTP handling: obtaining a PSTN-side + * RTP endpoint from themwi-rtp-mgr, then handling read select on RTP + * and RTCP UDP sockets. + */ + +#include +#include +#include +#include +#include +#include +#include +#include "../include/tmgw_const.h" +#include "../librtpalloc/rtp_alloc_simple.h" + +struct sockaddr_in rtp_local_addr; +int rtp_udp_fd, rtcp_udp_fd; + +static int got_some_rtp, got_some_rtcp; + +void +obtain_rtp_endp() +{ + int rc; + struct rtp_alloc_simple res; + + rc = rtp_alloc_simple(TMGW_EP_TYPE_PSTN_ONLY, &res); + if (rc < 0) + exit(1); /* error msg already printed */ + bcopy(&res.pstn_addr, &rtp_local_addr, sizeof(struct sockaddr_in)); + rtp_udp_fd = res.pstn_rtp_fd; + rtcp_udp_fd = res.pstn_rtcp_fd; +} + +void +rtp_rx_select() +{ + u_char buf[512]; + + recv(rtp_udp_fd, buf, sizeof buf, 0); + if (!got_some_rtp) { + printf("Got some RTP\n"); + got_some_rtp = 1; + } +} + +void +rtcp_rx_select() +{ + u_char buf[512]; + + recv(rtcp_udp_fd, buf, sizeof buf, 0); + if (!got_some_rtcp) { + printf("Got some RTCP\n"); + got_some_rtcp = 1; + } +}