mirror of
https://github.com/curl/curl.git
synced 2026-05-16 02:56:20 +03:00
Before this patch curl used the C preprocessor to override standard memory allocation symbols: malloc, calloc, strdup, realloc, free. The goal of these is to replace them with curl's debug wrappers in `CURLDEBUG` builds, another was to replace them with the wrappers calling user-defined allocators in libcurl. This solution needed a bunch of workarounds to avoid breaking external headers: it relied on include order to do the overriding last. For "unity" builds it needed to reset overrides before external includes. Also in test apps, which are always built as single source files. It also needed the `(symbol)` trick to avoid overrides in some places. This would still not fix cases where the standard symbols were macros. It was also fragile and difficult to figure out which was the actual function behind an alloc or free call in a specific piece of code. This in turn caused bugs where the wrong allocator was accidentally called. To avoid these problems, this patch replaces this solution with `curlx_`-prefixed allocator macros, and mapping them _once_ to either the libcurl wrappers, the debug wrappers or the standard ones, matching the rest of the code in libtests. This concludes the long journey to avoid redefining standard functions in the curl codebase. Note: I did not update `packages/OS400/*.c` sources. They did not `#include` `curl_setup.h`, `curl_memory.h` or `memdebug.h`, meaning the overrides were never applied to them. This may or may not have been correct. For now I suppressed the direct use of standard allocators via a local `.checksrc`. Probably they (except for `curlcl.c`) should be updated to include `curl_setup.h` and use the `curlx_` macros. This patch changes mappings in two places: - `lib/curl_threads.c` in libtests: Before this patch it mapped to libcurl allocators. After, it maps to standard allocators, like the rest of libtests code. - `units`: before this patch it mapped to standard allocators. After, it maps to libcurl allocators. Also: - drop all position-dependent `curl_memory.h` and `memdebug.h` includes, and delete the now unnecessary headers. - rename `Curl_tcsdup` macro to `curlx_tcsdup` and define like the other allocators. - map `curlx_strdup()` to `_strdup()` on Windows (was: `strdup()`). To fix warnings silenced via `_CRT_NONSTDC_NO_DEPRECATE`. - multibyte: map `curlx_convert_*()` to `_strdup()` on Windows (was: `strdup()`). - src: do not reuse the `strdup` name for the local replacement. - lib509: call `_strdup()` on Windows (was: `strdup()`). - test1132: delete test obsoleted by this patch. - CHECKSRC.md: update text for `SNPRINTF`. - checksrc: ban standard allocator symbols. Follow-up tob12da22db1#18866 Follow-up todb98daab05#18844 Follow-up to4deea9396b#18814 Follow-up to9678ff5b1b#18776 Follow-up to10bac43b87#18774 Follow-up to20142f5d06#18634 Follow-up tobf7375ecc5#18503 Follow-up to9863599d69#18502 Follow-up to3bb5e58c10#17827 Closes #19626
390 lines
14 KiB
C
390 lines
14 KiB
C
/***************************************************************************
|
|
* _ _ ____ _
|
|
* Project ___| | | | _ \| |
|
|
* / __| | | | |_) | |
|
|
* | (__| |_| | _ <| |___
|
|
* \___|\___/|_| \_\_____|
|
|
*
|
|
* Copyright (C) Daniel Stenberg, <daniel@haxx.se>, et al.
|
|
* Copyright (C) Howard Chu, <hyc@highlandsun.com>
|
|
*
|
|
* This software is licensed as described in the file COPYING, which
|
|
* you should have received as part of this distribution. The terms
|
|
* are also available at https://curl.se/docs/copyright.html.
|
|
*
|
|
* You may opt to use, copy, modify, merge, publish, distribute and/or sell
|
|
* copies of the Software, and permit persons to whom the Software is
|
|
* furnished to do so, under the terms of the COPYING file.
|
|
*
|
|
* This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
|
|
* KIND, either express or implied.
|
|
*
|
|
* SPDX-License-Identifier: curl
|
|
*
|
|
***************************************************************************/
|
|
|
|
#include "curl_setup.h"
|
|
|
|
#ifdef USE_LIBRTMP
|
|
|
|
#include "curl_rtmp.h"
|
|
#include "urldata.h"
|
|
#include "url.h"
|
|
#include "curlx/nonblock.h" /* for curlx_nonblock */
|
|
#include "progress.h" /* for Curl_pgrsSetUploadSize */
|
|
#include "transfer.h"
|
|
#include "curlx/warnless.h"
|
|
#include <curl/curl.h>
|
|
#include <librtmp/rtmp.h>
|
|
|
|
#if defined(_WIN32) && !defined(USE_LWIPSOCK)
|
|
#define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
|
|
#define SET_RCVTIMEO(tv,s) int tv = s*1000
|
|
#elif defined(LWIP_SO_SNDRCVTIMEO_NONSTANDARD)
|
|
#define SET_RCVTIMEO(tv,s) int tv = s*1000
|
|
#else
|
|
#define SET_RCVTIMEO(tv,s) struct timeval tv = {s,0}
|
|
#endif
|
|
|
|
#define DEF_BUFTIME (2*60*60*1000) /* 2 hours */
|
|
|
|
/* meta key for storing RTMP* at connection */
|
|
#define CURL_META_RTMP_CONN "meta:proto:rtmp:conn"
|
|
|
|
|
|
static CURLcode rtmp_setup_connection(struct Curl_easy *data,
|
|
struct connectdata *conn);
|
|
static CURLcode rtmp_do(struct Curl_easy *data, bool *done);
|
|
static CURLcode rtmp_done(struct Curl_easy *data, CURLcode, bool premature);
|
|
static CURLcode rtmp_connect(struct Curl_easy *data, bool *done);
|
|
static CURLcode rtmp_disconnect(struct Curl_easy *data,
|
|
struct connectdata *conn, bool dead);
|
|
|
|
static Curl_recv rtmp_recv;
|
|
static Curl_send rtmp_send;
|
|
|
|
/*
|
|
* RTMP protocol handler.h, based on https://rtmpdump.mplayerhq.hu
|
|
*/
|
|
|
|
const struct Curl_handler Curl_handler_rtmp = {
|
|
"rtmp", /* scheme */
|
|
rtmp_setup_connection, /* setup_connection */
|
|
rtmp_do, /* do_it */
|
|
rtmp_done, /* done */
|
|
ZERO_NULL, /* do_more */
|
|
rtmp_connect, /* connect_it */
|
|
ZERO_NULL, /* connecting */
|
|
ZERO_NULL, /* doing */
|
|
ZERO_NULL, /* proto_pollset */
|
|
ZERO_NULL, /* doing_pollset */
|
|
ZERO_NULL, /* domore_pollset */
|
|
ZERO_NULL, /* perform_pollset */
|
|
rtmp_disconnect, /* disconnect */
|
|
ZERO_NULL, /* write_resp */
|
|
ZERO_NULL, /* write_resp_hd */
|
|
ZERO_NULL, /* connection_check */
|
|
ZERO_NULL, /* attach connection */
|
|
ZERO_NULL, /* follow */
|
|
PORT_RTMP, /* defport */
|
|
CURLPROTO_RTMP, /* protocol */
|
|
CURLPROTO_RTMP, /* family */
|
|
PROTOPT_NONE /* flags */
|
|
};
|
|
|
|
const struct Curl_handler Curl_handler_rtmpt = {
|
|
"rtmpt", /* scheme */
|
|
rtmp_setup_connection, /* setup_connection */
|
|
rtmp_do, /* do_it */
|
|
rtmp_done, /* done */
|
|
ZERO_NULL, /* do_more */
|
|
rtmp_connect, /* connect_it */
|
|
ZERO_NULL, /* connecting */
|
|
ZERO_NULL, /* doing */
|
|
ZERO_NULL, /* proto_pollset */
|
|
ZERO_NULL, /* doing_pollset */
|
|
ZERO_NULL, /* domore_pollset */
|
|
ZERO_NULL, /* perform_pollset */
|
|
rtmp_disconnect, /* disconnect */
|
|
ZERO_NULL, /* write_resp */
|
|
ZERO_NULL, /* write_resp_hd */
|
|
ZERO_NULL, /* connection_check */
|
|
ZERO_NULL, /* attach connection */
|
|
ZERO_NULL, /* follow */
|
|
PORT_RTMPT, /* defport */
|
|
CURLPROTO_RTMPT, /* protocol */
|
|
CURLPROTO_RTMPT, /* family */
|
|
PROTOPT_NONE /* flags */
|
|
};
|
|
|
|
const struct Curl_handler Curl_handler_rtmpe = {
|
|
"rtmpe", /* scheme */
|
|
rtmp_setup_connection, /* setup_connection */
|
|
rtmp_do, /* do_it */
|
|
rtmp_done, /* done */
|
|
ZERO_NULL, /* do_more */
|
|
rtmp_connect, /* connect_it */
|
|
ZERO_NULL, /* connecting */
|
|
ZERO_NULL, /* doing */
|
|
ZERO_NULL, /* proto_pollset */
|
|
ZERO_NULL, /* doing_pollset */
|
|
ZERO_NULL, /* domore_pollset */
|
|
ZERO_NULL, /* perform_pollset */
|
|
rtmp_disconnect, /* disconnect */
|
|
ZERO_NULL, /* write_resp */
|
|
ZERO_NULL, /* write_resp_hd */
|
|
ZERO_NULL, /* connection_check */
|
|
ZERO_NULL, /* attach connection */
|
|
ZERO_NULL, /* follow */
|
|
PORT_RTMP, /* defport */
|
|
CURLPROTO_RTMPE, /* protocol */
|
|
CURLPROTO_RTMPE, /* family */
|
|
PROTOPT_NONE /* flags */
|
|
};
|
|
|
|
const struct Curl_handler Curl_handler_rtmpte = {
|
|
"rtmpte", /* scheme */
|
|
rtmp_setup_connection, /* setup_connection */
|
|
rtmp_do, /* do_it */
|
|
rtmp_done, /* done */
|
|
ZERO_NULL, /* do_more */
|
|
rtmp_connect, /* connect_it */
|
|
ZERO_NULL, /* connecting */
|
|
ZERO_NULL, /* doing */
|
|
ZERO_NULL, /* proto_pollset */
|
|
ZERO_NULL, /* doing_pollset */
|
|
ZERO_NULL, /* domore_pollset */
|
|
ZERO_NULL, /* perform_pollset */
|
|
rtmp_disconnect, /* disconnect */
|
|
ZERO_NULL, /* write_resp */
|
|
ZERO_NULL, /* write_resp_hd */
|
|
ZERO_NULL, /* connection_check */
|
|
ZERO_NULL, /* attach connection */
|
|
ZERO_NULL, /* follow */
|
|
PORT_RTMPT, /* defport */
|
|
CURLPROTO_RTMPTE, /* protocol */
|
|
CURLPROTO_RTMPTE, /* family */
|
|
PROTOPT_NONE /* flags */
|
|
};
|
|
|
|
const struct Curl_handler Curl_handler_rtmps = {
|
|
"rtmps", /* scheme */
|
|
rtmp_setup_connection, /* setup_connection */
|
|
rtmp_do, /* do_it */
|
|
rtmp_done, /* done */
|
|
ZERO_NULL, /* do_more */
|
|
rtmp_connect, /* connect_it */
|
|
ZERO_NULL, /* connecting */
|
|
ZERO_NULL, /* doing */
|
|
ZERO_NULL, /* proto_pollset */
|
|
ZERO_NULL, /* doing_pollset */
|
|
ZERO_NULL, /* domore_pollset */
|
|
ZERO_NULL, /* perform_pollset */
|
|
rtmp_disconnect, /* disconnect */
|
|
ZERO_NULL, /* write_resp */
|
|
ZERO_NULL, /* write_resp_hd */
|
|
ZERO_NULL, /* connection_check */
|
|
ZERO_NULL, /* attach connection */
|
|
ZERO_NULL, /* follow */
|
|
PORT_RTMPS, /* defport */
|
|
CURLPROTO_RTMPS, /* protocol */
|
|
CURLPROTO_RTMP, /* family */
|
|
PROTOPT_NONE /* flags */
|
|
};
|
|
|
|
const struct Curl_handler Curl_handler_rtmpts = {
|
|
"rtmpts", /* scheme */
|
|
rtmp_setup_connection, /* setup_connection */
|
|
rtmp_do, /* do_it */
|
|
rtmp_done, /* done */
|
|
ZERO_NULL, /* do_more */
|
|
rtmp_connect, /* connect_it */
|
|
ZERO_NULL, /* connecting */
|
|
ZERO_NULL, /* doing */
|
|
ZERO_NULL, /* proto_pollset */
|
|
ZERO_NULL, /* doing_pollset */
|
|
ZERO_NULL, /* domore_pollset */
|
|
ZERO_NULL, /* perform_pollset */
|
|
rtmp_disconnect, /* disconnect */
|
|
ZERO_NULL, /* write_resp */
|
|
ZERO_NULL, /* write_resp_hd */
|
|
ZERO_NULL, /* connection_check */
|
|
ZERO_NULL, /* attach connection */
|
|
ZERO_NULL, /* follow */
|
|
PORT_RTMPS, /* defport */
|
|
CURLPROTO_RTMPTS, /* protocol */
|
|
CURLPROTO_RTMPT, /* family */
|
|
PROTOPT_NONE /* flags */
|
|
};
|
|
|
|
static void rtmp_conn_dtor(void *key, size_t klen, void *entry)
|
|
{
|
|
RTMP *r = entry;
|
|
(void)key;
|
|
(void)klen;
|
|
RTMP_Close(r);
|
|
RTMP_Free(r);
|
|
}
|
|
|
|
static CURLcode rtmp_setup_connection(struct Curl_easy *data,
|
|
struct connectdata *conn)
|
|
{
|
|
RTMP *r = RTMP_Alloc();
|
|
if(!r ||
|
|
Curl_conn_meta_set(conn, CURL_META_RTMP_CONN, r, rtmp_conn_dtor))
|
|
return CURLE_OUT_OF_MEMORY;
|
|
|
|
RTMP_Init(r);
|
|
RTMP_SetBufferMS(r, DEF_BUFTIME);
|
|
if(!RTMP_SetupURL(r, data->state.url))
|
|
/* rtmp_conn_dtor() performs the cleanup */
|
|
return CURLE_URL_MALFORMAT;
|
|
return CURLE_OK;
|
|
}
|
|
|
|
static CURLcode rtmp_connect(struct Curl_easy *data, bool *done)
|
|
{
|
|
struct connectdata *conn = data->conn;
|
|
RTMP *r = Curl_conn_meta_get(conn, CURL_META_RTMP_CONN);
|
|
SET_RCVTIMEO(tv, 10);
|
|
|
|
if(!r)
|
|
return CURLE_FAILED_INIT;
|
|
|
|
if(conn->sock[FIRSTSOCKET] > INT_MAX) {
|
|
/* The socket value is invalid for rtmp. */
|
|
return CURLE_FAILED_INIT;
|
|
}
|
|
|
|
r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
|
|
|
|
/* We have to know if it is a write before we send the
|
|
* connect request packet
|
|
*/
|
|
if(data->state.upload)
|
|
r->Link.protocol |= RTMP_FEATURE_WRITE;
|
|
|
|
/* For plain streams, use the buffer toggle trick to keep data flowing */
|
|
if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
|
|
!(r->Link.protocol & RTMP_FEATURE_HTTP))
|
|
r->Link.lFlags |= RTMP_LF_BUFX;
|
|
|
|
(void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
|
|
setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
|
|
(char *)&tv, sizeof(tv));
|
|
|
|
if(!RTMP_Connect1(r, NULL))
|
|
return CURLE_FAILED_INIT;
|
|
|
|
/* Clients must send a periodic BytesReceived report to the server */
|
|
r->m_bSendCounter = TRUE;
|
|
|
|
*done = TRUE;
|
|
conn->recv[FIRSTSOCKET] = rtmp_recv;
|
|
conn->send[FIRSTSOCKET] = rtmp_send;
|
|
return CURLE_OK;
|
|
}
|
|
|
|
static CURLcode rtmp_do(struct Curl_easy *data, bool *done)
|
|
{
|
|
struct connectdata *conn = data->conn;
|
|
RTMP *r = Curl_conn_meta_get(conn, CURL_META_RTMP_CONN);
|
|
|
|
if(!r || !RTMP_ConnectStream(r, 0))
|
|
return CURLE_FAILED_INIT;
|
|
|
|
if(data->state.upload) {
|
|
Curl_pgrsSetUploadSize(data, data->state.infilesize);
|
|
Curl_xfer_setup_send(data, FIRSTSOCKET);
|
|
}
|
|
else
|
|
Curl_xfer_setup_recv(data, FIRSTSOCKET, -1);
|
|
*done = TRUE;
|
|
return CURLE_OK;
|
|
}
|
|
|
|
static CURLcode rtmp_done(struct Curl_easy *data, CURLcode status,
|
|
bool premature)
|
|
{
|
|
(void)data;
|
|
(void)status;
|
|
(void)premature;
|
|
|
|
return CURLE_OK;
|
|
}
|
|
|
|
static CURLcode rtmp_disconnect(struct Curl_easy *data,
|
|
struct connectdata *conn,
|
|
bool dead_connection)
|
|
{
|
|
RTMP *r = Curl_conn_meta_get(conn, CURL_META_RTMP_CONN);
|
|
(void)data;
|
|
(void)dead_connection;
|
|
if(r)
|
|
Curl_conn_meta_remove(conn, CURL_META_RTMP_CONN);
|
|
return CURLE_OK;
|
|
}
|
|
|
|
static CURLcode rtmp_recv(struct Curl_easy *data, int sockindex, char *buf,
|
|
size_t len, size_t *pnread)
|
|
{
|
|
struct connectdata *conn = data->conn;
|
|
RTMP *r = Curl_conn_meta_get(conn, CURL_META_RTMP_CONN);
|
|
CURLcode result = CURLE_OK;
|
|
ssize_t rv;
|
|
|
|
(void)sockindex;
|
|
*pnread = 0;
|
|
if(!r)
|
|
return CURLE_FAILED_INIT;
|
|
|
|
rv = RTMP_Read(r, buf, curlx_uztosi(len));
|
|
if(!curlx_sztouz(rv, pnread)) {
|
|
if(r->m_read.status == RTMP_READ_COMPLETE ||
|
|
r->m_read.status == RTMP_READ_EOF) {
|
|
data->req.size = data->req.bytecount;
|
|
}
|
|
else
|
|
result = CURLE_RECV_ERROR;
|
|
}
|
|
|
|
return result;
|
|
}
|
|
|
|
static CURLcode rtmp_send(struct Curl_easy *data, int sockindex,
|
|
const void *buf, size_t len, bool eos,
|
|
size_t *pnwritten)
|
|
{
|
|
struct connectdata *conn = data->conn;
|
|
RTMP *r = Curl_conn_meta_get(conn, CURL_META_RTMP_CONN);
|
|
ssize_t rv;
|
|
|
|
(void)sockindex;
|
|
(void)eos;
|
|
*pnwritten = 0;
|
|
if(!r)
|
|
return CURLE_FAILED_INIT;
|
|
|
|
rv = RTMP_Write(r, (const char *)buf, curlx_uztosi(len));
|
|
if(!curlx_sztouz(rv, pnwritten))
|
|
return CURLE_SEND_ERROR;
|
|
|
|
return CURLE_OK;
|
|
}
|
|
|
|
void Curl_rtmp_version(char *version, size_t len)
|
|
{
|
|
char suff[2];
|
|
if(RTMP_LIB_VERSION & 0xff) {
|
|
suff[0] = (RTMP_LIB_VERSION & 0xff) + 'a' - 1;
|
|
suff[1] = '\0';
|
|
}
|
|
else
|
|
suff[0] = '\0';
|
|
|
|
curl_msnprintf(version, len, "librtmp/%d.%d%s",
|
|
RTMP_LIB_VERSION >> 16, (RTMP_LIB_VERSION >> 8) & 0xff,
|
|
suff);
|
|
}
|
|
|
|
#endif /* USE_LIBRTMP */
|