Initially implement the new osmo-mgw and libosmo-mgcp

Leave the old osmo-bsc_mgcp and libosmo-legacy-mgcp as it is; on a copy thereof
(added by a previous commit), apply changes to initially implement the new
osmo-mgw.

Adjust build system and debian packaging to accomodate the new libosmo-mgcp and
osmo-mgw.

The main differences:

*) use a list to manage rtp connections.

Aggregate all rtp related information inside a single struct.

Use a linked list to manage the both connections (net and bts).
The idea behind using a list is that we might support conference
calls at some later point.

Store the linked list in struct mgcp_endpoint, have a private linked
list for each endpoint. The list contains connection items which are
implemented in struct mgcp_conn. A connection is allocated and freed
using the functions in mgcp_conn.c. A connection is allocated on the
reception of a CRCX command and freed with the reception of a DLCX
command.

*) remove external transcoder feature

Fortunatelly the external transcoder feature is not needed
anymore. This patch removes the related code.

*) vty: get rid of CONN_BTS and CONN_NET

Since the new connection model does not make a difference
between BTS and NET connections the VTY should not use
the fixed CONN_BTS and CONN_NET constants.

- Handle the conns list inside the endpoint directly
- introduce function to dump basic rtp connection info
- introduce human readable names for connections

Parts of the code adjusted to use generalized connections instead of explicit
BTS/NET ones:

- teach mgcp_send_dummy() to send dummy packets to any RTP connection
- network: generalize mgcp_bind_net/bts_rtp_port()
- network: generalize mgcp_send()
- tap: generalize call tapping feature
- stat: generalize statistics
- Replace rtp_data_net() and rtp_data_bts() with generalized rtp_data_rx()

*) mgcp_protocol.c fixes:

- check ci string before it is converted:
  In case of missing ci, a nullpointer is delivered to strtoul().
  Add a function that takes ci, checks it and converts it to an
  uint32_t. Use the return code to react on missing ci.
- output error message on missing CI.
- when parsing the mode, print log message when mode is missing.
- use mode_orig when mode is missing.
- fix ptime formatstring to use %u rather than %d.
- cosmetic: log when connection is deleted on DLCX.
- change loglevels of CRCX, MDCX, DLCX events from DEBUG to NOTICE.

*) mgcp_test

- apply rename of strline_r() to mgcp_strline().
- MGCP command macros:
  - Add 'I: 1' parameters.
  - Use proper port numbers:
    from m=audio 0 RTP/AVP 126
    to   m=audio 16002 RTP/AVP 128
  - Change ptime to 'a=ptime:40' because this is what the MGW currently
    returns.  CRCX generally feed a ptime:40 and this is expected to be
    returned.
- struct mgcp_test: Use only one ptype, there are no explicit BTS and NET
  endpoints anymore.
  Hence remove one column from tests[].
- test_messages():
  - Enable: remove '#if 0'
  - Remove concept of BTS and NET endpoints: test only one conn, as they are
    now interchangeable anyway.
  - remove endpoint init, now done internally.
  - add false asserts in error cases.
- test_retransmission():
  - remove endpoint init, now done internally.
  - add false asserts in error cases.
- test_packet_error_detection():
  - Remove concept of BTS and NET endpoints: test only one conn, as they are
    now interchangeable anyway. Use arbitrary conn ids (e.g. 4711).
  - remove endpoint init, now done internally.
  - add false assert in error case.
  - Assert that a conn really vanishes on DLCX, previously the conn would
    remain and just be unused, now it is actually discarded.
- test_no_cycle()
  - Remove concept of BTS and NET endpoints: test only one conn, as they are
    now interchangeable anyway. Use arbitrary conn ids (e.g. 4711).
- test_no_name()
  - Enable: remove '#if 0'.
  - remove endpoint init, now done internally.
  - add false assert in error case.
- mgcp_test.ok: adjust expected results to status quo:
  - We now see two dummy packets instead of one, now sent to both sides because
    we don't know of BTS or NET side. (maybe drop dummy packets later...)
  - packet duration, conn mode: now sane defaults show instead of unset.
- various whitespace and formatting changes from lindent.

Change-Id: Ie008599136c7ed8a0dfbb0cf803188975a499fc5
diff --git a/include/osmocom/mgcp/mgcp.h b/include/osmocom/mgcp/mgcp.h
index c017faf..67c499b 100644
--- a/include/osmocom/mgcp/mgcp.h
+++ b/include/osmocom/mgcp/mgcp.h
@@ -20,21 +20,21 @@
  *
  */
 
-#ifndef OPENBSC_MGCP_H
-#define OPENBSC_MGCP_H
+#pragma once
 
 #include <osmocom/core/msgb.h>
 #include <osmocom/core/write_queue.h>
 #include <osmocom/core/timer.h>
 #include <osmocom/core/logging.h>
+#include <osmocom/mgcp/mgcp_ep.h>
 
 #include <arpa/inet.h>
 #include <sys/types.h>
 #include <sys/socket.h>
 #include <netinet/in.h>
 
-#define RTP_PORT_DEFAULT 4000
-#define RTP_PORT_NET_DEFAULT 16000
+#define RTP_PORT_DEFAULT_RANGE_START 16002
+#define RTP_PORT_DEFAULT_RANGE_END RTP_PORT_DEFAULT_RANGE_START + 64
 
 /**
  * Calculate the RTP audio port for the given multiplex
@@ -102,26 +102,20 @@
 				     struct mgcp_rtp_end *dst_end,
 				     struct mgcp_rtp_end *src_end);
 
+struct mgcp_conn_rtp;
 typedef void (*mgcp_get_format)(struct mgcp_endpoint *endp,
 				int *payload_type,
 				const char**subtype_name,
-				const char**fmtp_extra);
-
-#define PORT_ALLOC_STATIC	0
-#define PORT_ALLOC_DYNAMIC	1
+				const char**fmtp_extra,
+				struct mgcp_conn_rtp *conn);
 
 /**
  * This holds information on how to allocate ports
  */
 struct mgcp_port_range {
-	int mode;
-
 	/* addr or NULL to fall-back to default */
 	char *bind_addr;
 
-	/* pre-allocated from a base? */
-	int base_port;
-
 	/* dynamically allocated */
 	int range_start;
 	int range_end;
@@ -160,6 +154,10 @@
 	/* timer */
 	struct osmo_timer_list keepalive_timer;
 
+	/* When set, incoming RTP packets are not filtered
+	 * when ports and ip-address do not match (debug) */
+	int rtp_accept_all;
+
 	unsigned int number_endpoints;
 	struct mgcp_endpoint *endpoints;
 };
@@ -188,16 +186,8 @@
 	int source_port;
 	char *local_ip;
 	char *source_addr;
-	char *bts_ip;
 	char *call_agent_addr;
 
-	struct in_addr bts_in;
-
-	/* transcoder handling */
-	char *transcoder_ip;
-	struct in_addr transcoder_in;
-	int transcoder_remote_base;
-
 	/* RTP processing */
 	mgcp_processing rtp_processing_cb;
 	mgcp_processing_setup setup_rtp_processing_cb;
@@ -206,12 +196,10 @@
 
 	struct osmo_wqueue gw_fd;
 
-	struct mgcp_port_range bts_ports;
 	struct mgcp_port_range net_ports;
-	struct mgcp_port_range transcoder_ports;
 	int endp_dscp;
 
-	int bts_force_ptime;
+	int force_ptime;
 
 	mgcp_change change_cb;
 	mgcp_policy policy_cb;
@@ -226,10 +214,6 @@
 	struct mgcp_trunk_config trunk;
 	struct llist_head trunks;
 
-	/* only used for start with a static configuration */
-	int last_net_port;
-	int last_bts_port;
-
 	enum mgcp_role role;
 
 	/* osmux translator: 0 means disabled, 1 means enabled */
@@ -259,11 +243,6 @@
 int mgcp_vty_init(void);
 int mgcp_endpoints_allocate(struct mgcp_trunk_config *cfg);
 void mgcp_release_endp(struct mgcp_endpoint *endp);
-void mgcp_initialize_endp(struct mgcp_endpoint *endp);
-int mgcp_reset_transcoder(struct mgcp_config *cfg);
-void mgcp_format_stats(struct mgcp_endpoint *endp, char *stats, size_t size);
-int mgcp_parse_stats(struct msgb *msg, uint32_t *ps, uint32_t *os, uint32_t *pr, uint32_t *_or, int *loss, uint32_t *jitter);
-
 void mgcp_trunk_set_keepalive(struct mgcp_trunk_config *tcfg, int interval);
 
 /*
@@ -293,7 +272,4 @@
 
 
 int mgcp_create_bind(const char *source_addr, struct osmo_fd *fd, int port);
-int mgcp_send(struct mgcp_endpoint *endp, int dest, int is_rtp, struct sockaddr_in *addr, char *buf, int rc);
 int mgcp_udp_send(int fd, struct in_addr *addr, int port, char *buf, int len);
-
-#endif