2019-06-13 10:02:06 +00:00
|
|
|
#pragma once
|
|
|
|
|
2021-06-01 07:54:23 +00:00
|
|
|
#include "util.hh"
|
|
|
|
|
2019-06-13 10:02:06 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
#include <winsock2.h>
|
2019-11-13 06:08:52 +00:00
|
|
|
#include <mswsock.h>
|
2019-07-15 08:49:24 +00:00
|
|
|
#include <inaddr.h>
|
2019-06-13 10:02:06 +00:00
|
|
|
#else
|
|
|
|
#include <netinet/ip.h>
|
|
|
|
#include <arpa/inet.h>
|
2019-07-18 08:55:21 +00:00
|
|
|
#include <sys/uio.h>
|
2019-06-13 10:02:06 +00:00
|
|
|
#endif
|
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
#include <vector>
|
2019-06-20 06:25:01 +00:00
|
|
|
#include <string>
|
2019-06-18 08:18:33 +00:00
|
|
|
|
2019-06-13 10:02:06 +00:00
|
|
|
|
2021-02-19 01:13:43 +00:00
|
|
|
namespace uvgrtp {
|
2019-06-13 10:02:06 +00:00
|
|
|
|
2020-09-17 09:45:20 +00:00
|
|
|
#ifdef _WIN32
|
|
|
|
typedef unsigned int socklen_t;
|
|
|
|
#endif
|
|
|
|
|
2022-07-19 02:04:32 +00:00
|
|
|
#if defined(UVGRTP_HAVE_SENDMSG) && !defined(UVGRTP_HAVE_SENDMMSG)
|
2022-07-13 23:38:31 +00:00
|
|
|
struct mmsghdr {
|
|
|
|
struct msghdr msg_hdr;
|
|
|
|
unsigned int msg_len;
|
|
|
|
};
|
|
|
|
static inline
|
|
|
|
int sendmmsg(int sockfd, struct mmsghdr *msgvec, unsigned int vlen,
|
|
|
|
int flags)
|
|
|
|
{
|
|
|
|
ssize_t n = 0;
|
|
|
|
for (unsigned int i = 0; i < vlen; i++) {
|
|
|
|
ssize_t ret = sendmsg(sockfd, &msgvec[i].msg_hdr, flags);
|
|
|
|
if (ret < 0)
|
|
|
|
break;
|
|
|
|
n += ret;
|
|
|
|
}
|
|
|
|
if (n == 0)
|
|
|
|
return -1;
|
|
|
|
return n;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2020-08-18 06:13:28 +00:00
|
|
|
const int MAX_BUFFER_COUNT = 256;
|
|
|
|
|
2020-08-17 09:51:23 +00:00
|
|
|
/* Vector of buffers that contain a full RTP frame */
|
|
|
|
typedef std::vector<std::pair<size_t, uint8_t *>> buf_vec;
|
|
|
|
|
|
|
|
/* Vector of RTP frames constructed from buf_vec entries */
|
|
|
|
typedef std::vector<std::vector<std::pair<size_t, uint8_t *>>> pkt_vec;
|
2019-06-13 10:02:06 +00:00
|
|
|
|
2020-08-18 06:13:28 +00:00
|
|
|
typedef rtp_error_t (*packet_handler_vec)(void *, buf_vec&);
|
2020-08-12 08:31:30 +00:00
|
|
|
|
|
|
|
struct socket_packet_handler {
|
2021-06-03 07:38:49 +00:00
|
|
|
void *arg = nullptr;
|
|
|
|
packet_handler_vec handler = nullptr;
|
2020-08-12 08:31:30 +00:00
|
|
|
};
|
|
|
|
|
2019-06-13 10:02:06 +00:00
|
|
|
class socket {
|
|
|
|
public:
|
2020-05-28 09:24:18 +00:00
|
|
|
socket(int flags);
|
2019-06-13 10:02:06 +00:00
|
|
|
~socket();
|
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
/* Create socket using "family", "type" and "protocol"
|
|
|
|
*
|
|
|
|
* NOTE: Only family AF_INET (ie. IPv4) is supported
|
|
|
|
*
|
|
|
|
* Return RTP_OK on success
|
|
|
|
* return RTP_SOCKET_ERROR if creating the socket failed */
|
2019-06-13 10:02:06 +00:00
|
|
|
rtp_error_t init(short family, int type, int protocol);
|
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
/* Same as bind(2), assigns an address for the underlying socket object
|
|
|
|
*
|
|
|
|
* Return RTP_OK on success
|
|
|
|
* Return RTP_BIND_ERROR if the bind failed */
|
2019-06-13 10:02:06 +00:00
|
|
|
rtp_error_t bind(short family, unsigned host, short port);
|
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
/* Same as setsockopt(2), used to manipulate the underlying socket object
|
|
|
|
*
|
|
|
|
* Return RTP_OK on success
|
|
|
|
* Return RTP_GENERIC_ERROR if setsockopt failed */
|
2019-06-13 10:02:06 +00:00
|
|
|
rtp_error_t setsockopt(int level, int optname, const void *optval, socklen_t optlen);
|
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
/* Same as send(2), send message to remote using "flags"
|
|
|
|
* This function uses the internal addr_ object as remote address so it MUST be set
|
|
|
|
*
|
2019-07-18 08:55:21 +00:00
|
|
|
* It is possible to combine multiple buffers and send them as one RTP frame by calling
|
|
|
|
* the sendto() with a vector containing the buffers and their lengths
|
|
|
|
*
|
2019-06-19 06:26:04 +00:00
|
|
|
* Write the amount of bytes sent to "bytes_sent" if it's not NULL
|
|
|
|
*
|
2019-06-18 08:18:33 +00:00
|
|
|
* Return RTP_OK on success and write the amount of bytes sent to "bytes_sent"
|
|
|
|
* Return RTP_SEND_ERROR on error and set "bytes_sent" to -1 */
|
2019-06-19 06:16:57 +00:00
|
|
|
rtp_error_t sendto(uint8_t *buf, size_t buf_len, int flags);
|
2019-06-13 10:02:06 +00:00
|
|
|
rtp_error_t sendto(uint8_t *buf, size_t buf_len, int flags, int *bytes_sent);
|
2020-08-17 09:51:23 +00:00
|
|
|
rtp_error_t sendto(buf_vec& buffers, int flags);
|
|
|
|
rtp_error_t sendto(buf_vec& buffers, int flags, int *bytes_sent);
|
|
|
|
rtp_error_t sendto(pkt_vec& buffers, int flags);
|
|
|
|
rtp_error_t sendto(pkt_vec& buffers, int flags, int *bytes_sent);
|
2019-06-13 10:02:06 +00:00
|
|
|
|
2019-06-19 06:16:57 +00:00
|
|
|
/* Same as sendto() but the remote address given as parameter */
|
|
|
|
rtp_error_t sendto(sockaddr_in& addr, uint8_t *buf, size_t buf_len, int flags);
|
2019-06-18 08:26:46 +00:00
|
|
|
rtp_error_t sendto(sockaddr_in& addr, uint8_t *buf, size_t buf_len, int flags, int *bytes_sent);
|
2020-08-17 09:51:23 +00:00
|
|
|
rtp_error_t sendto(sockaddr_in& addr, buf_vec& buffers, int flags);
|
|
|
|
rtp_error_t sendto(sockaddr_in& addr, buf_vec& buffers, int flags, int *bytes_sent);
|
|
|
|
rtp_error_t sendto(sockaddr_in& addr, pkt_vec& buffers, int flags);
|
|
|
|
rtp_error_t sendto(sockaddr_in& addr, pkt_vec& buffers, int flags, int *bytes_sent);
|
2020-01-10 06:21:25 +00:00
|
|
|
|
|
|
|
/* Same as recv(2), receives a message from socket (remote address not known)
|
|
|
|
*
|
|
|
|
* Write the amount of bytes read to "bytes_read" if it's not NULL
|
|
|
|
*
|
|
|
|
* Return RTP_OK on success and write the amount of bytes received to "bytes_read"
|
|
|
|
* Return RTP_INTERRUPTED if the call was interrupted due to timeout and set "bytes_sent" to 0
|
|
|
|
* Return RTP_GENERIC_ERROR on error and set "bytes_sent" to -1 */
|
|
|
|
rtp_error_t recv(uint8_t *buf, size_t buf_len, int flags);
|
|
|
|
rtp_error_t recv(uint8_t *buf, size_t buf_len, int flags, int *bytes_read);
|
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
/* Same as recvfrom(2), receives a message from remote
|
|
|
|
*
|
2019-06-19 06:26:04 +00:00
|
|
|
* Write the sender address to "sender" if it's not NULL
|
|
|
|
* Write the amount of bytes read to "bytes_read" if it's not NULL
|
2019-06-18 08:18:33 +00:00
|
|
|
*
|
|
|
|
* Return RTP_OK on success and write the amount of bytes sent to "bytes_sent"
|
|
|
|
* Return RTP_INTERRUPTED if the call was interrupted due to timeout and set "bytes_sent" to 0
|
|
|
|
* Return RTP_GENERIC_ERROR on error and set "bytes_sent" to -1 */
|
2019-06-19 06:26:04 +00:00
|
|
|
rtp_error_t recvfrom(uint8_t *buf, size_t buf_len, int flags, sockaddr_in *sender, int *bytes_read);
|
|
|
|
rtp_error_t recvfrom(uint8_t *buf, size_t buf_len, int flags, sockaddr_in *sender);
|
2019-06-13 10:02:06 +00:00
|
|
|
rtp_error_t recvfrom(uint8_t *buf, size_t buf_len, int flags, int *bytes_read);
|
2019-06-19 06:26:04 +00:00
|
|
|
rtp_error_t recvfrom(uint8_t *buf, size_t buf_len, int flags);
|
2019-06-13 10:02:06 +00:00
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
/* Create sockaddr_in object using the provided information
|
|
|
|
* NOTE: "family" must be AF_INET */
|
|
|
|
sockaddr_in create_sockaddr(short family, unsigned host, short port);
|
2019-06-13 10:02:06 +00:00
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
/* Create sockaddr_in object using the provided information
|
|
|
|
* NOTE: "family" must be AF_INET */
|
2019-06-13 10:02:06 +00:00
|
|
|
sockaddr_in create_sockaddr(short family, std::string host, short port);
|
|
|
|
|
2019-07-15 05:04:24 +00:00
|
|
|
/* Get reference to the actual socket object */
|
|
|
|
socket_t& get_raw_socket();
|
2019-06-13 10:02:06 +00:00
|
|
|
|
2019-06-18 08:18:33 +00:00
|
|
|
/* Initialize the private "addr_" object with "addr"
|
|
|
|
* This is used when calling send() */
|
2019-06-13 10:02:06 +00:00
|
|
|
void set_sockaddr(sockaddr_in addr);
|
|
|
|
|
2020-02-07 10:22:58 +00:00
|
|
|
/* Get the out address for the socket if it exists */
|
|
|
|
sockaddr_in& get_out_address();
|
|
|
|
|
2020-08-18 06:13:28 +00:00
|
|
|
/* Install a packet handler for vector-based send operations.
|
2020-08-12 08:31:30 +00:00
|
|
|
*
|
2020-08-18 06:13:28 +00:00
|
|
|
* This handler allows the caller to inject extra functionality to the send operation
|
2020-08-12 08:31:30 +00:00
|
|
|
* without polluting src/socket.cc with unrelated code
|
|
|
|
* (such as collecting RTCP session statistics info or encrypting a packet)
|
|
|
|
*
|
|
|
|
* "arg" is an optional parameter that can be passed to the handler when it's called */
|
|
|
|
rtp_error_t install_handler(void *arg, packet_handler_vec handler);
|
|
|
|
|
2019-06-13 10:02:06 +00:00
|
|
|
private:
|
2019-06-18 08:26:46 +00:00
|
|
|
/* helper function for sending UPD packets, see documentation for sendto() above */
|
|
|
|
rtp_error_t __sendto(sockaddr_in& addr, uint8_t *buf, size_t buf_len, int flags, int *bytes_sent);
|
2020-01-10 06:21:25 +00:00
|
|
|
rtp_error_t __recv(uint8_t *buf, size_t buf_len, int flags, int *bytes_read);
|
2019-06-19 06:26:04 +00:00
|
|
|
rtp_error_t __recvfrom(uint8_t *buf, size_t buf_len, int flags, sockaddr_in *sender, int *bytes_read);
|
2019-06-18 08:26:46 +00:00
|
|
|
|
2019-07-18 08:55:21 +00:00
|
|
|
/* __sendtov() does the same as __sendto but it combines multiple buffers into one frame and sends them */
|
2020-08-17 09:51:23 +00:00
|
|
|
rtp_error_t __sendtov(sockaddr_in& addr, buf_vec& buffers, int flags, int *bytes_sent);
|
2021-02-19 01:13:43 +00:00
|
|
|
rtp_error_t __sendtov(sockaddr_in& addr, uvgrtp::pkt_vec& buffers, int flags, int *bytes_sent);
|
2019-07-18 08:55:21 +00:00
|
|
|
|
2019-06-13 10:02:06 +00:00
|
|
|
socket_t socket_;
|
|
|
|
sockaddr_in addr_;
|
2020-05-28 09:24:18 +00:00
|
|
|
int flags_;
|
2019-07-18 08:55:21 +00:00
|
|
|
|
2020-08-26 10:48:22 +00:00
|
|
|
/* __sendto() calls these handlers in order before sending the packet */
|
2020-08-12 08:31:30 +00:00
|
|
|
std::vector<socket_packet_handler> buf_handlers_;
|
|
|
|
|
|
|
|
/* __sendtov() calls these handlers in order before sending the packet */
|
|
|
|
std::vector<socket_packet_handler> vec_handlers_;
|
|
|
|
|
2022-03-01 08:22:14 +00:00
|
|
|
#ifndef NDEBUG
|
|
|
|
uint64_t sent_packets_ = 0;
|
|
|
|
uint64_t received_packets_ = 0;
|
|
|
|
#endif // !NDEBUG
|
|
|
|
|
2019-07-18 08:55:21 +00:00
|
|
|
#ifdef _WIN32
|
2019-08-12 07:03:16 +00:00
|
|
|
WSABUF buffers_[MAX_BUFFER_COUNT];
|
2019-07-18 08:55:21 +00:00
|
|
|
#else
|
2019-08-12 07:03:16 +00:00
|
|
|
struct mmsghdr header_;
|
|
|
|
struct iovec chunks_[MAX_BUFFER_COUNT];
|
2019-07-18 08:55:21 +00:00
|
|
|
#endif
|
2019-06-13 10:02:06 +00:00
|
|
|
};
|
2022-02-28 06:46:04 +00:00
|
|
|
}
|
2021-02-19 01:13:43 +00:00
|
|
|
|
|
|
|
namespace uvg_rtp = uvgrtp;
|