stream.c 18 KB

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