openssl: guard against OOM on context creation
[platform/upstream/curl.git] / lib / curl_rtmp.c
1 /***************************************************************************
2  *                      _   _ ____  _
3  *  Project         ___| | | |  _ \| |
4  *                 / __| | | | |_) | |
5  *                | (__| |_| |  _ <| |___
6  *                 \___|\___/|_| \_\_____|
7  *
8  * Copyright (C) 2012 - 2020, Daniel Stenberg, <daniel@haxx.se>, et al.
9  * Copyright (C) 2010, Howard Chu, <hyc@highlandsun.com>
10  *
11  * This software is licensed as described in the file COPYING, which
12  * you should have received as part of this distribution. The terms
13  * are also available at https://curl.haxx.se/docs/copyright.html.
14  *
15  * You may opt to use, copy, modify, merge, publish, distribute and/or sell
16  * copies of the Software, and permit persons to whom the Software is
17  * furnished to do so, under the terms of the COPYING file.
18  *
19  * This software is distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY
20  * KIND, either express or implied.
21  *
22  ***************************************************************************/
23
24 #include "curl_setup.h"
25
26 #ifdef USE_LIBRTMP
27
28 #include "curl_rtmp.h"
29 #include "urldata.h"
30 #include "nonblock.h" /* for curlx_nonblock */
31 #include "progress.h" /* for Curl_pgrsSetUploadSize */
32 #include "transfer.h"
33 #include "warnless.h"
34 #include <curl/curl.h>
35 #include <librtmp/rtmp.h>
36 #include "curl_memory.h"
37 /* The last #include file should be: */
38 #include "memdebug.h"
39
40 #if defined(WIN32) && !defined(USE_LWIPSOCK)
41 #define setsockopt(a,b,c,d,e) (setsockopt)(a,b,c,(const char *)d,(int)e)
42 #define SET_RCVTIMEO(tv,s)   int tv = s*1000
43 #elif defined(LWIP_SO_SNDRCVTIMEO_NONSTANDARD)
44 #define SET_RCVTIMEO(tv,s)   int tv = s*1000
45 #else
46 #define SET_RCVTIMEO(tv,s)   struct timeval tv = {s,0}
47 #endif
48
49 #define DEF_BUFTIME    (2*60*60*1000)    /* 2 hours */
50
51 static CURLcode rtmp_setup_connection(struct connectdata *conn);
52 static CURLcode rtmp_do(struct connectdata *conn, bool *done);
53 static CURLcode rtmp_done(struct connectdata *conn, CURLcode, bool premature);
54 static CURLcode rtmp_connect(struct connectdata *conn, bool *done);
55 static CURLcode rtmp_disconnect(struct connectdata *conn, bool dead);
56
57 static Curl_recv rtmp_recv;
58 static Curl_send rtmp_send;
59
60 /*
61  * RTMP protocol handler.h, based on https://rtmpdump.mplayerhq.hu
62  */
63
64 const struct Curl_handler Curl_handler_rtmp = {
65   "RTMP",                               /* scheme */
66   rtmp_setup_connection,                /* setup_connection */
67   rtmp_do,                              /* do_it */
68   rtmp_done,                            /* done */
69   ZERO_NULL,                            /* do_more */
70   rtmp_connect,                         /* connect_it */
71   ZERO_NULL,                            /* connecting */
72   ZERO_NULL,                            /* doing */
73   ZERO_NULL,                            /* proto_getsock */
74   ZERO_NULL,                            /* doing_getsock */
75   ZERO_NULL,                            /* domore_getsock */
76   ZERO_NULL,                            /* perform_getsock */
77   rtmp_disconnect,                      /* disconnect */
78   ZERO_NULL,                            /* readwrite */
79   ZERO_NULL,                            /* connection_check */
80   PORT_RTMP,                            /* defport */
81   CURLPROTO_RTMP,                       /* protocol */
82   CURLPROTO_RTMP,                       /* family */
83   PROTOPT_NONE                          /* flags*/
84 };
85
86 const struct Curl_handler Curl_handler_rtmpt = {
87   "RTMPT",                              /* scheme */
88   rtmp_setup_connection,                /* setup_connection */
89   rtmp_do,                              /* do_it */
90   rtmp_done,                            /* done */
91   ZERO_NULL,                            /* do_more */
92   rtmp_connect,                         /* connect_it */
93   ZERO_NULL,                            /* connecting */
94   ZERO_NULL,                            /* doing */
95   ZERO_NULL,                            /* proto_getsock */
96   ZERO_NULL,                            /* doing_getsock */
97   ZERO_NULL,                            /* domore_getsock */
98   ZERO_NULL,                            /* perform_getsock */
99   rtmp_disconnect,                      /* disconnect */
100   ZERO_NULL,                            /* readwrite */
101   ZERO_NULL,                            /* connection_check */
102   PORT_RTMPT,                           /* defport */
103   CURLPROTO_RTMPT,                      /* protocol */
104   CURLPROTO_RTMPT,                      /* family */
105   PROTOPT_NONE                          /* flags*/
106 };
107
108 const struct Curl_handler Curl_handler_rtmpe = {
109   "RTMPE",                              /* scheme */
110   rtmp_setup_connection,                /* setup_connection */
111   rtmp_do,                              /* do_it */
112   rtmp_done,                            /* done */
113   ZERO_NULL,                            /* do_more */
114   rtmp_connect,                         /* connect_it */
115   ZERO_NULL,                            /* connecting */
116   ZERO_NULL,                            /* doing */
117   ZERO_NULL,                            /* proto_getsock */
118   ZERO_NULL,                            /* doing_getsock */
119   ZERO_NULL,                            /* domore_getsock */
120   ZERO_NULL,                            /* perform_getsock */
121   rtmp_disconnect,                      /* disconnect */
122   ZERO_NULL,                            /* readwrite */
123   ZERO_NULL,                            /* connection_check */
124   PORT_RTMP,                            /* defport */
125   CURLPROTO_RTMPE,                      /* protocol */
126   CURLPROTO_RTMPE,                      /* family */
127   PROTOPT_NONE                          /* flags*/
128 };
129
130 const struct Curl_handler Curl_handler_rtmpte = {
131   "RTMPTE",                             /* scheme */
132   rtmp_setup_connection,                /* setup_connection */
133   rtmp_do,                              /* do_it */
134   rtmp_done,                            /* done */
135   ZERO_NULL,                            /* do_more */
136   rtmp_connect,                         /* connect_it */
137   ZERO_NULL,                            /* connecting */
138   ZERO_NULL,                            /* doing */
139   ZERO_NULL,                            /* proto_getsock */
140   ZERO_NULL,                            /* doing_getsock */
141   ZERO_NULL,                            /* domore_getsock */
142   ZERO_NULL,                            /* perform_getsock */
143   rtmp_disconnect,                      /* disconnect */
144   ZERO_NULL,                            /* readwrite */
145   ZERO_NULL,                            /* connection_check */
146   PORT_RTMPT,                           /* defport */
147   CURLPROTO_RTMPTE,                     /* protocol */
148   CURLPROTO_RTMPTE,                     /* family */
149   PROTOPT_NONE                          /* flags*/
150 };
151
152 const struct Curl_handler Curl_handler_rtmps = {
153   "RTMPS",                              /* scheme */
154   rtmp_setup_connection,                /* setup_connection */
155   rtmp_do,                              /* do_it */
156   rtmp_done,                            /* done */
157   ZERO_NULL,                            /* do_more */
158   rtmp_connect,                         /* connect_it */
159   ZERO_NULL,                            /* connecting */
160   ZERO_NULL,                            /* doing */
161   ZERO_NULL,                            /* proto_getsock */
162   ZERO_NULL,                            /* doing_getsock */
163   ZERO_NULL,                            /* domore_getsock */
164   ZERO_NULL,                            /* perform_getsock */
165   rtmp_disconnect,                      /* disconnect */
166   ZERO_NULL,                            /* readwrite */
167   ZERO_NULL,                            /* connection_check */
168   PORT_RTMPS,                           /* defport */
169   CURLPROTO_RTMPS,                      /* protocol */
170   CURLPROTO_RTMP,                       /* family */
171   PROTOPT_NONE                          /* flags*/
172 };
173
174 const struct Curl_handler Curl_handler_rtmpts = {
175   "RTMPTS",                             /* scheme */
176   rtmp_setup_connection,                /* setup_connection */
177   rtmp_do,                              /* do_it */
178   rtmp_done,                            /* done */
179   ZERO_NULL,                            /* do_more */
180   rtmp_connect,                         /* connect_it */
181   ZERO_NULL,                            /* connecting */
182   ZERO_NULL,                            /* doing */
183   ZERO_NULL,                            /* proto_getsock */
184   ZERO_NULL,                            /* doing_getsock */
185   ZERO_NULL,                            /* domore_getsock */
186   ZERO_NULL,                            /* perform_getsock */
187   rtmp_disconnect,                      /* disconnect */
188   ZERO_NULL,                            /* readwrite */
189   ZERO_NULL,                            /* connection_check */
190   PORT_RTMPS,                           /* defport */
191   CURLPROTO_RTMPTS,                     /* protocol */
192   CURLPROTO_RTMPT,                      /* family */
193   PROTOPT_NONE                          /* flags*/
194 };
195
196 static CURLcode rtmp_setup_connection(struct connectdata *conn)
197 {
198   RTMP *r = RTMP_Alloc();
199   if(!r)
200     return CURLE_OUT_OF_MEMORY;
201
202   RTMP_Init(r);
203   RTMP_SetBufferMS(r, DEF_BUFTIME);
204   if(!RTMP_SetupURL(r, conn->data->change.url)) {
205     RTMP_Free(r);
206     return CURLE_URL_MALFORMAT;
207   }
208   conn->proto.rtmp = r;
209   return CURLE_OK;
210 }
211
212 static CURLcode rtmp_connect(struct connectdata *conn, bool *done)
213 {
214   RTMP *r = conn->proto.rtmp;
215   SET_RCVTIMEO(tv, 10);
216
217   r->m_sb.sb_socket = (int)conn->sock[FIRSTSOCKET];
218
219   /* We have to know if it's a write before we send the
220    * connect request packet
221    */
222   if(conn->data->set.upload)
223     r->Link.protocol |= RTMP_FEATURE_WRITE;
224
225   /* For plain streams, use the buffer toggle trick to keep data flowing */
226   if(!(r->Link.lFlags & RTMP_LF_LIVE) &&
227      !(r->Link.protocol & RTMP_FEATURE_HTTP))
228     r->Link.lFlags |= RTMP_LF_BUFX;
229
230   (void)curlx_nonblock(r->m_sb.sb_socket, FALSE);
231   setsockopt(r->m_sb.sb_socket, SOL_SOCKET, SO_RCVTIMEO,
232              (char *)&tv, sizeof(tv));
233
234   if(!RTMP_Connect1(r, NULL))
235     return CURLE_FAILED_INIT;
236
237   /* Clients must send a periodic BytesReceived report to the server */
238   r->m_bSendCounter = true;
239
240   *done = TRUE;
241   conn->recv[FIRSTSOCKET] = rtmp_recv;
242   conn->send[FIRSTSOCKET] = rtmp_send;
243   return CURLE_OK;
244 }
245
246 static CURLcode rtmp_do(struct connectdata *conn, bool *done)
247 {
248   struct Curl_easy *data = conn->data;
249   RTMP *r = conn->proto.rtmp;
250
251   if(!RTMP_ConnectStream(r, 0))
252     return CURLE_FAILED_INIT;
253
254   if(conn->data->set.upload) {
255     Curl_pgrsSetUploadSize(data, data->state.infilesize);
256     Curl_setup_transfer(data, -1, -1, FALSE, FIRSTSOCKET);
257   }
258   else
259     Curl_setup_transfer(data, FIRSTSOCKET, -1, FALSE, -1);
260   *done = TRUE;
261   return CURLE_OK;
262 }
263
264 static CURLcode rtmp_done(struct connectdata *conn, CURLcode status,
265                           bool premature)
266 {
267   (void)conn; /* unused */
268   (void)status; /* unused */
269   (void)premature; /* unused */
270
271   return CURLE_OK;
272 }
273
274 static CURLcode rtmp_disconnect(struct connectdata *conn,
275                                 bool dead_connection)
276 {
277   RTMP *r = conn->proto.rtmp;
278   (void)dead_connection;
279   if(r) {
280     conn->proto.rtmp = NULL;
281     RTMP_Close(r);
282     RTMP_Free(r);
283   }
284   return CURLE_OK;
285 }
286
287 static ssize_t rtmp_recv(struct connectdata *conn, int sockindex, char *buf,
288                          size_t len, CURLcode *err)
289 {
290   RTMP *r = conn->proto.rtmp;
291   ssize_t nread;
292
293   (void)sockindex; /* unused */
294
295   nread = RTMP_Read(r, buf, curlx_uztosi(len));
296   if(nread < 0) {
297     if(r->m_read.status == RTMP_READ_COMPLETE ||
298         r->m_read.status == RTMP_READ_EOF) {
299       conn->data->req.size = conn->data->req.bytecount;
300       nread = 0;
301     }
302     else
303       *err = CURLE_RECV_ERROR;
304   }
305   return nread;
306 }
307
308 static ssize_t rtmp_send(struct connectdata *conn, int sockindex,
309                          const void *buf, size_t len, CURLcode *err)
310 {
311   RTMP *r = conn->proto.rtmp;
312   ssize_t num;
313
314   (void)sockindex; /* unused */
315
316   num = RTMP_Write(r, (char *)buf, curlx_uztosi(len));
317   if(num < 0)
318     *err = CURLE_SEND_ERROR;
319
320   return num;
321 }
322 #endif  /* USE_LIBRTMP */