stream.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747
  1. /*
  2. * Squeezelite - lightweight headless squeezebox emulator
  3. *
  4. * (c) Adrian Smith 2012-2015, triode1@btinternet.com
  5. * Ralph Irving 2015-2017, ralph_irving@hotmail.com
  6. *
  7. * This program is free software: you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation, either version 3 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program. If not, see <http://www.gnu.org/licenses/>.
  19. *
  20. */
  21. // stream thread
  22. #ifndef _GNU_SOURCE
  23. #define _GNU_SOURCE
  24. #endif
  25. #include "squeezelite.h"
  26. #include <fcntl.h>
  27. #if USE_SSL
  28. #include "openssl/ssl.h"
  29. #include "openssl/err.h"
  30. #endif
  31. #if SUN
  32. #include <signal.h>
  33. #endif
  34. static log_level loglevel;
  35. static struct buffer buf;
  36. struct buffer *streambuf = &buf;
  37. #define LOCK mutex_lock(streambuf->mutex)
  38. #define UNLOCK mutex_unlock(streambuf->mutex)
  39. /*
  40. When LMS sends a close/open sequence very quickly, the stream thread might
  41. still be waiting in the poll() on the closed socket. It is never recommended
  42. to have a thread closing a socket used by another thread but it works, as
  43. opposed to an infinite select().
  44. In stream_sock() a new socket is created and full OS will allocate a different
  45. one but on RTOS and simple IP stack, the same might be re-used and that causes
  46. an exception as a thread is already waiting on a newly allocated socket
  47. A simple variable that forces stream_sock() to wait until we are out of poll()
  48. is enough and much faster than a mutex
  49. */
  50. static bool polling;
  51. static sockfd fd;
  52. struct EXT_RAM_ATTR streamstate stream;
  53. static EXT_RAM_ATTR struct {
  54. enum { OGG_OFF, OGG_SYNC, OGG_HEADER, OGG_SEGMENTS, OGG_PAGE } state;
  55. u32_t want, miss, match;
  56. u8_t* data, segments[255];
  57. #pragma pack(push, 1)
  58. struct {
  59. char pattern[4];
  60. u8_t version, type;
  61. u64_t granule;
  62. u32_t serial, page, checksum;
  63. u8_t count;
  64. } header;
  65. #pragma pack(pop)
  66. } ogg;
  67. #if USE_SSL
  68. static SSL_CTX *SSLctx;
  69. SSL *ssl;
  70. #endif
  71. #if !USE_SSL
  72. #define _recv(ssl, fc, buf, n, opt) recv(fd, buf, n, opt)
  73. #define _send(ssl, fd, buf, n, opt) send(fd, buf, n, opt)
  74. #define _poll(ssl, pollinfo, timeout) poll(pollinfo, 1, timeout)
  75. #define _last_error() last_error()
  76. #else
  77. #define _last_error() ERROR_WOULDBLOCK
  78. static int _recv(SSL *ssl, int fd, void *buffer, size_t bytes, int options) {
  79. int n;
  80. if (!ssl) return recv(fd, buffer, bytes, options);
  81. n = SSL_read(ssl, (u8_t*) buffer, bytes);
  82. if (n <= 0 && SSL_get_error(ssl, n) == SSL_ERROR_ZERO_RETURN) return 0;
  83. return n;
  84. }
  85. static int _send(SSL *ssl, int fd, void *buffer, size_t bytes, int options) {
  86. int n;
  87. if (!ssl) return send(fd, buffer, bytes, options);
  88. while (1) {
  89. int err;
  90. ERR_clear_error();
  91. if ((n = SSL_write(ssl, (u8_t*) buffer, bytes)) >= 0) return n;
  92. err = SSL_get_error(ssl, n);
  93. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) continue;
  94. LOG_INFO("SSL write error %d", err );
  95. return n;
  96. }
  97. }
  98. /*
  99. can't mimic exactly poll as SSL is a real pain. Even if SSL_pending returns
  100. 0, there might be bytes to read but when select (poll) return > 0, there might
  101. be no frame available. As well select (poll) < 0 does not mean that there is
  102. no data pending
  103. */
  104. static int _poll(SSL *ssl, struct pollfd *pollinfo, int timeout) {
  105. if (!ssl) return poll(pollinfo, 1, timeout);
  106. if (pollinfo->events & POLLIN && SSL_pending(ssl)) {
  107. if (pollinfo->events & POLLOUT) poll(pollinfo, 1, 0);
  108. pollinfo->revents = POLLIN;
  109. return 1;
  110. }
  111. return poll(pollinfo, 1, timeout);
  112. }
  113. #endif
  114. static bool send_header(void) {
  115. char *ptr = stream.header;
  116. int len = stream.header_len;
  117. unsigned try = 0;
  118. ssize_t n;
  119. while (len) {
  120. n = _send(ssl, fd, ptr, len, MSG_NOSIGNAL);
  121. if (n <= 0) {
  122. if (n < 0 && _last_error() == ERROR_WOULDBLOCK && try < 10) {
  123. LOG_SDEBUG("retrying (%d) writing to socket", ++try);
  124. usleep(1000);
  125. continue;
  126. }
  127. LOG_INFO("failed writing to socket: %s", strerror(last_error()));
  128. stream.disconnect = LOCAL_DISCONNECT;
  129. stream.state = DISCONNECT;
  130. wake_controller();
  131. return false;
  132. }
  133. LOG_SDEBUG("wrote %d bytes to socket", n);
  134. ptr += n;
  135. len -= n;
  136. }
  137. LOG_SDEBUG("wrote header");
  138. return true;
  139. }
  140. static bool running = true;
  141. static void _disconnect(stream_state state, disconnect_code disconnect) {
  142. stream.state = state;
  143. stream.disconnect = disconnect;
  144. if (ogg.state == OGG_PAGE && ogg.data) free(ogg.data);
  145. ogg.data = NULL;
  146. #if USE_SSL
  147. if (ssl) {
  148. SSL_shutdown(ssl);
  149. SSL_free(ssl);
  150. ssl = NULL;
  151. }
  152. #endif
  153. closesocket(fd);
  154. fd = -1;
  155. wake_controller();
  156. }
  157. static u32_t memfind(const u8_t* haystack, u32_t n, const char* needle, u32_t len, u32_t *offset) {
  158. int i;
  159. for (i = 0; i < n && *offset != len; i++) *offset = (haystack[i] == needle[*offset]) ? *offset + 1 : 0;
  160. return i;
  161. }
  162. static void stream_ogg(size_t n) {
  163. if (ogg.state == OGG_OFF) return;
  164. u8_t* p = streambuf->writep;
  165. while (n) {
  166. size_t consumed = min(ogg.miss, n);
  167. // copy as many bytes as possible and come back later if we do'nt have enough
  168. if (ogg.data) {
  169. memcpy(ogg.data + ogg.want - ogg.miss, p, consumed);
  170. ogg.miss -= consumed;
  171. if (ogg.miss) return;
  172. }
  173. // we have what we want, let's parse
  174. switch (ogg.state) {
  175. case OGG_SYNC: {
  176. ogg.miss -= consumed;
  177. if (consumed) break;
  178. // we have to memorize position in case any of last 3 bytes match...
  179. int pos = memfind(p, n, "OggS", 4, &ogg.match);
  180. if (ogg.match == 4) {
  181. consumed = pos - ogg.match;
  182. ogg.state = OGG_HEADER;
  183. ogg.miss = ogg.want = sizeof(ogg.header);
  184. ogg.data = (u8_t*) &ogg.header;
  185. ogg.match = 0;
  186. } else {
  187. LOG_INFO("OggS not at expected position");
  188. return;
  189. }
  190. break;
  191. }
  192. case OGG_HEADER:
  193. if (!memcmp(ogg.header.pattern, "OggS", 4)) {
  194. ogg.miss = ogg.want = ogg.header.count;
  195. ogg.data = ogg.segments;
  196. ogg.state = OGG_SEGMENTS;
  197. } else {
  198. ogg.state = OGG_SYNC;
  199. ogg.data = NULL;
  200. }
  201. break;
  202. case OGG_SEGMENTS:
  203. // calculate size of page using lacing values
  204. for (int i = 0; i < ogg.want; i++) ogg.miss += ogg.data[i];
  205. ogg.want = ogg.miss;
  206. if (ogg.header.granule == 0) {
  207. // granule 0 means a new stream, so let's look into it
  208. ogg.state = OGG_PAGE;
  209. ogg.data = malloc(ogg.want);
  210. } else {
  211. // otherwise, jump over data
  212. ogg.state = OGG_SYNC;
  213. ogg.data = NULL;
  214. }
  215. break;
  216. case OGG_PAGE: {
  217. u32_t offset = 0;
  218. // try to find one of valid Ogg pattern (vorbis, opus)
  219. for (char** tag = (char*[]) { "\x3vorbis", "OpusTags", NULL }; *tag; tag++, offset = 0) {
  220. u32_t pos = memfind(ogg.data, ogg.want, *tag, strlen(*tag), &offset);
  221. if (offset != strlen(*tag)) continue;
  222. // u32:len,char[]:vendorId, u32:N, N x (u32:len,char[]:comment)
  223. char* p = (char*) ogg.data + pos;
  224. p += *p + 4;
  225. u32_t count = *p;
  226. p += 4;
  227. // LMS metadata format for Ogg is "Ogg", N x (u16:len,char[]:comment)
  228. memcpy(stream.header, "Ogg", 3);
  229. stream.header_len = 3;
  230. for (u32_t len; count--; p += len) {
  231. len = *p;
  232. p += 4;
  233. // only report what we use and don't overflow (network byte order)
  234. if (!strncasecmp(p, "TITLE=", 6) || !strncasecmp(p, "ARTIST=", 7) || !strncasecmp(p, "ALBUM=", 6)) {
  235. if (stream.header_len + len > MAX_HEADER) break;
  236. stream.header[stream.header_len++] = len >> 8;
  237. stream.header[stream.header_len++] = len;
  238. memcpy(stream.header + stream.header_len, p, len);
  239. stream.header_len += len;
  240. }
  241. }
  242. stream.meta_send = true;
  243. wake_controller();
  244. LOG_INFO("Ogg metadata length: %u", stream.header_len - 3);
  245. break;
  246. }
  247. free(ogg.data);
  248. ogg.state = OGG_SYNC;
  249. break;
  250. }
  251. default:
  252. break;
  253. }
  254. p += consumed;
  255. n -= consumed;
  256. }
  257. }
  258. static void *stream_thread() {
  259. while (running) {
  260. struct pollfd pollinfo;
  261. size_t space;
  262. LOCK;
  263. space = min(_buf_space(streambuf), _buf_cont_write(streambuf));
  264. if (fd < 0 || !space || stream.state <= STREAMING_WAIT) {
  265. UNLOCK;
  266. usleep(space ? 100000 : 25000);
  267. continue;
  268. }
  269. if (stream.state == STREAMING_FILE) {
  270. int n = read(fd, streambuf->writep, space);
  271. if (n == 0) {
  272. LOG_INFO("end of stream");
  273. _disconnect(DISCONNECT, DISCONNECT_OK);
  274. }
  275. if (n > 0) {
  276. _buf_inc_writep(streambuf, n);
  277. stream.bytes += n;
  278. LOG_SDEBUG("streambuf read %d bytes", n);
  279. }
  280. if (n < 0) {
  281. LOG_WARN("error reading: %s", strerror(last_error()));
  282. _disconnect(DISCONNECT, REMOTE_DISCONNECT);
  283. }
  284. UNLOCK;
  285. continue;
  286. } else {
  287. pollinfo.fd = fd;
  288. pollinfo.events = POLLIN;
  289. if (stream.state == SEND_HEADERS) {
  290. pollinfo.events |= POLLOUT;
  291. }
  292. }
  293. UNLOCK;
  294. // no mutex needed - we just want to know if we are inside poll()
  295. polling = true;
  296. if (_poll(ssl, &pollinfo, 100)) {
  297. polling = false;
  298. LOCK;
  299. // check socket has not been closed while in poll
  300. if (fd < 0) {
  301. UNLOCK;
  302. continue;
  303. }
  304. if ((pollinfo.revents & POLLOUT) && stream.state == SEND_HEADERS) {
  305. if (send_header()) stream.state = RECV_HEADERS;
  306. stream.header_len = 0;
  307. UNLOCK;
  308. continue;
  309. }
  310. if (pollinfo.revents & (POLLIN | POLLHUP)) {
  311. // get response headers
  312. if (stream.state == RECV_HEADERS) {
  313. // read one byte at a time to catch end of header
  314. char c;
  315. static int endtok;
  316. int n = _recv(ssl, fd, &c, 1, 0);
  317. if (n <= 0) {
  318. if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
  319. UNLOCK;
  320. continue;
  321. }
  322. LOG_INFO("error reading headers: %s", n ? strerror(last_error()) : "closed");
  323. _disconnect(STOPPED, LOCAL_DISCONNECT);
  324. UNLOCK;
  325. continue;
  326. }
  327. *(stream.header + stream.header_len) = c;
  328. stream.header_len++;
  329. if (stream.header_len > MAX_HEADER - 1) {
  330. LOG_ERROR("received headers too long: %u", stream.header_len);
  331. _disconnect(DISCONNECT, LOCAL_DISCONNECT);
  332. }
  333. if (stream.header_len > 1 && (c == '\r' || c == '\n')) {
  334. endtok++;
  335. if (endtok == 4) {
  336. *(stream.header + stream.header_len) = '\0';
  337. LOG_INFO("headers: len: %d\n%s", stream.header_len, stream.header);
  338. stream.state = stream.cont_wait ? STREAMING_WAIT : STREAMING_BUFFERING;
  339. wake_controller();
  340. }
  341. } else {
  342. endtok = 0;
  343. }
  344. UNLOCK;
  345. continue;
  346. }
  347. // receive icy meta data
  348. if (stream.meta_interval && stream.meta_next == 0) {
  349. if (stream.meta_left == 0) {
  350. // read meta length
  351. u8_t c;
  352. int n = _recv(ssl, fd, &c, 1, 0);
  353. if (n <= 0) {
  354. if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
  355. UNLOCK;
  356. continue;
  357. }
  358. LOG_INFO("error reading icy meta: %s", n ? strerror(last_error()) : "closed");
  359. _disconnect(STOPPED, LOCAL_DISCONNECT);
  360. UNLOCK;
  361. continue;
  362. }
  363. stream.meta_left = 16 * c;
  364. stream.header_len = 0; // amount of received meta data
  365. // MAX_HEADER must be more than meta max of 16 * 255
  366. }
  367. if (stream.meta_left) {
  368. int n = _recv(ssl, fd, stream.header + stream.header_len, stream.meta_left, 0);
  369. if (n <= 0) {
  370. if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
  371. UNLOCK;
  372. continue;
  373. }
  374. LOG_INFO("error reading icy meta: %s", n ? strerror(last_error()) : "closed");
  375. _disconnect(STOPPED, LOCAL_DISCONNECT);
  376. UNLOCK;
  377. continue;
  378. }
  379. stream.meta_left -= n;
  380. stream.header_len += n;
  381. }
  382. if (stream.meta_left == 0) {
  383. if (stream.header_len) {
  384. *(stream.header + stream.header_len) = '\0';
  385. LOG_INFO("icy meta: len: %u\n%s", stream.header_len, stream.header);
  386. stream.meta_send = true;
  387. wake_controller();
  388. }
  389. stream.meta_next = stream.meta_interval;
  390. UNLOCK;
  391. continue;
  392. }
  393. // stream body into streambuf
  394. } else {
  395. int n;
  396. space = min(_buf_space(streambuf), _buf_cont_write(streambuf));
  397. if (stream.meta_interval) {
  398. space = min(space, stream.meta_next);
  399. }
  400. n = _recv(ssl, fd, streambuf->writep, space, 0);
  401. if (n == 0) {
  402. LOG_INFO("end of stream (%u bytes)", stream.bytes);
  403. _disconnect(DISCONNECT, DISCONNECT_OK);
  404. }
  405. if (n < 0 && _last_error() != ERROR_WOULDBLOCK) {
  406. LOG_INFO("error reading: %s", strerror(last_error()));
  407. _disconnect(DISCONNECT, REMOTE_DISCONNECT);
  408. }
  409. if (n > 0) {
  410. stream_ogg(n);
  411. _buf_inc_writep(streambuf, n);
  412. stream.bytes += n;
  413. if (stream.meta_interval) {
  414. stream.meta_next -= n;
  415. }
  416. } else {
  417. UNLOCK;
  418. continue;
  419. }
  420. if (stream.state == STREAMING_BUFFERING && stream.bytes > stream.threshold) {
  421. stream.state = STREAMING_HTTP;
  422. wake_controller();
  423. }
  424. LOG_SDEBUG("streambuf read %d bytes", n);
  425. }
  426. }
  427. UNLOCK;
  428. } else {
  429. polling = false;
  430. LOG_SDEBUG("poll timeout");
  431. }
  432. }
  433. #if USE_SSL
  434. if (SSLctx) {
  435. SSL_CTX_free(SSLctx);
  436. }
  437. #endif
  438. return 0;
  439. }
  440. static thread_type thread;
  441. void stream_init(log_level level, unsigned stream_buf_size) {
  442. loglevel = level;
  443. LOG_INFO("init stream");
  444. LOG_DEBUG("streambuf size: %u", stream_buf_size);
  445. buf_init(streambuf, stream_buf_size);
  446. if (streambuf->buf == NULL) {
  447. LOG_ERROR("unable to malloc buffer");
  448. exit(2);
  449. }
  450. #if USE_SSL
  451. #if !LINKALL && !NO_SSLSYM
  452. if (ssl_loaded) {
  453. #endif
  454. SSL_library_init();
  455. SSLctx = SSL_CTX_new(SSLv23_client_method());
  456. if (SSLctx == NULL) {
  457. LOG_ERROR("unable to allocate SSL context");
  458. exit(3);
  459. }
  460. SSL_CTX_set_options(SSLctx, SSL_OP_NO_SSLv2);
  461. #if !LINKALL && !NO_SSLSYM
  462. }
  463. #endif
  464. ssl = NULL;
  465. #endif
  466. #if SUN
  467. signal(SIGPIPE, SIG_IGN); /* Force sockets to return -1 with EPIPE on pipe signal */
  468. #endif
  469. stream.state = STOPPED;
  470. stream.header = malloc(MAX_HEADER);
  471. *stream.header = '\0';
  472. fd = -1;
  473. #if LINUX || FREEBSD
  474. touch_memory(streambuf->buf, streambuf->size);
  475. #endif
  476. #if LINUX || OSX || FREEBSD || EMBEDDED
  477. pthread_attr_t attr;
  478. pthread_attr_init(&attr);
  479. #ifdef PTHREAD_STACK_MIN
  480. pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + STREAM_THREAD_STACK_SIZE);
  481. #endif
  482. pthread_create_name(&thread, &attr, stream_thread, NULL, "stream");
  483. pthread_attr_destroy(&attr);
  484. #endif
  485. #if WIN
  486. thread = CreateThread(NULL, STREAM_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&stream_thread, NULL, 0, NULL);
  487. #endif
  488. }
  489. void stream_close(void) {
  490. LOG_INFO("close stream");
  491. LOCK;
  492. running = false;
  493. UNLOCK;
  494. #if LINUX || OSX || FREEBSD || EMBEDDED
  495. pthread_join(thread, NULL);
  496. #endif
  497. free(stream.header);
  498. buf_destroy(streambuf);
  499. }
  500. void stream_file(const char *header, size_t header_len, unsigned threshold) {
  501. buf_flush(streambuf);
  502. LOCK;
  503. stream.header_len = header_len;
  504. memcpy(stream.header, header, header_len);
  505. *(stream.header+header_len) = '\0';
  506. LOG_INFO("opening local file: %s", stream.header);
  507. #if WIN
  508. fd = open(stream.header, O_RDONLY | O_BINARY);
  509. #else
  510. fd = open(stream.header, O_RDONLY);
  511. #endif
  512. stream.state = STREAMING_FILE;
  513. if (fd < 0) {
  514. LOG_INFO("can't open file: %s", stream.header);
  515. stream.state = DISCONNECT;
  516. }
  517. wake_controller();
  518. stream.cont_wait = false;
  519. stream.meta_interval = 0;
  520. stream.meta_next = 0;
  521. stream.meta_left = 0;
  522. stream.meta_send = false;
  523. stream.sent_headers = false;
  524. stream.bytes = 0;
  525. stream.threshold = threshold;
  526. UNLOCK;
  527. }
  528. void stream_sock(u32_t ip, u16_t port, char codec, const char *header, size_t header_len, unsigned threshold, bool cont_wait) {
  529. struct sockaddr_in addr;
  530. #if EMBEDDED
  531. // wait till we are not polling anymore
  532. while (polling && running) { usleep(10000); }
  533. #endif
  534. int sock = socket(AF_INET, SOCK_STREAM, 0);
  535. if (sock < 0) {
  536. LOG_ERROR("failed to create socket");
  537. return;
  538. }
  539. memset(&addr, 0, sizeof(addr));
  540. addr.sin_family = AF_INET;
  541. addr.sin_addr.s_addr = ip;
  542. addr.sin_port = port;
  543. LOG_INFO("connecting to %s:%d", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
  544. set_nonblock(sock);
  545. set_nosigpipe(sock);
  546. if (connect_timeout(sock, (struct sockaddr *) &addr, sizeof(addr), 10) < 0) {
  547. LOG_INFO("unable to connect to server");
  548. LOCK;
  549. stream.state = DISCONNECT;
  550. stream.disconnect = UNREACHABLE;
  551. UNLOCK;
  552. return;
  553. }
  554. #if USE_SSL
  555. if (ntohs(port) == 443) {
  556. char server[256], *p;
  557. ssl = SSL_new(SSLctx);
  558. SSL_set_fd(ssl, sock);
  559. // add SNI
  560. sscanf(header, "Host:%255s", server);
  561. if (server) {
  562. if ((p = strchr(server, ':')) != NULL) *p = '\0';
  563. SSL_set_tlsext_host_name(ssl, server);
  564. }
  565. while (1) {
  566. int status, err = 0;
  567. ERR_clear_error();
  568. status = SSL_connect(ssl);
  569. // successful negotiation
  570. if (status == 1) break;
  571. // error or non-blocking requires more time
  572. if (status < 0) {
  573. err = SSL_get_error(ssl, status);
  574. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) continue;
  575. }
  576. LOG_WARN("unable to open SSL socket %d (%d)", status, err);
  577. closesocket(sock);
  578. SSL_free(ssl);
  579. ssl = NULL;
  580. LOCK;
  581. stream.state = DISCONNECT;
  582. stream.disconnect = UNREACHABLE;
  583. UNLOCK;
  584. return;
  585. }
  586. } else {
  587. ssl = NULL;
  588. }
  589. #endif
  590. buf_flush(streambuf);
  591. LOCK;
  592. fd = sock;
  593. stream.state = SEND_HEADERS;
  594. stream.cont_wait = cont_wait;
  595. stream.meta_interval = 0;
  596. stream.meta_next = 0;
  597. stream.meta_left = 0;
  598. stream.meta_send = false;
  599. stream.header_len = header_len;
  600. memcpy(stream.header, header, header_len);
  601. *(stream.header+header_len) = '\0';
  602. LOG_INFO("header: %s", stream.header);
  603. stream.sent_headers = false;
  604. stream.bytes = 0;
  605. stream.threshold = threshold;
  606. ogg.miss = ogg.match = 0;
  607. ogg.state = (codec == 'o' || codec == 'p') ? OGG_SYNC : OGG_OFF;
  608. UNLOCK;
  609. }
  610. bool stream_disconnect(void) {
  611. bool disc = false;
  612. LOCK;
  613. #if USE_SSL
  614. if (ssl) {
  615. SSL_shutdown(ssl);
  616. SSL_free(ssl);
  617. ssl = NULL;
  618. }
  619. #endif
  620. if (fd != -1) {
  621. closesocket(fd);
  622. fd = -1;
  623. disc = true;
  624. }
  625. stream.state = STOPPED;
  626. if (ogg.state == OGG_PAGE && ogg.data) free(ogg.data);
  627. ogg.data = NULL;
  628. UNLOCK;
  629. return disc;
  630. }