stream.c 18 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733
  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 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_HEADER && 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 = malloc(stream.ogg.miss);
  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 = realloc(stream.ogg.data, stream.ogg.want);
  196. } else {
  197. // otherwise, jump over data
  198. stream.ogg.state = STREAM_OGG_SYNC;
  199. free(stream.ogg.data);
  200. stream.ogg.data = NULL;
  201. }
  202. break;
  203. case STREAM_OGG_PAGE: {
  204. u32_t offset = 0;
  205. // try to find one of valid Ogg pattern (vorbis, opus)
  206. for (char** tag = (char*[]) { "\x3vorbis", "OpusTags", NULL }; *tag; tag++, offset = 0) {
  207. u32_t pos = memfind(stream.ogg.data, stream.ogg.want, *tag, strlen(*tag), &offset);
  208. if (offset != strlen(*tag)) continue;
  209. // u32:len,char[]:vendorId, u32:N, N x (u32:len,char[]:comment)
  210. char* p = (char*) stream.ogg.data + pos;
  211. p += *p + 4;
  212. u32_t count = *p;
  213. p += 4;
  214. // LMS metadata format for Ogg is "Ogg", N x (u16:len,char[]:comment)
  215. memcpy(stream.header, "Ogg", 3);
  216. stream.header_len = 3;
  217. for (u32_t len; count--; p += len) {
  218. len = *p;
  219. p += 4;
  220. // only report what we use and don't overflow (network byte order)
  221. if (!strncasecmp(p, "TITLE=", 6) || !strncasecmp(p, "ARTIST=", 7) || !strncasecmp(p, "ALBUM=", 6)) {
  222. if (stream.header_len + len > MAX_HEADER) break;
  223. stream.header[stream.header_len++] = len >> 8;
  224. stream.header[stream.header_len++] = len;
  225. memcpy(stream.header + stream.header_len, p, len);
  226. stream.header_len += len;
  227. }
  228. }
  229. stream.meta_send = true;
  230. wake_controller();
  231. LOG_INFO("Ogg meta len: %u", stream.header_len);
  232. break;
  233. }
  234. free(stream.ogg.data);
  235. stream.ogg.state = STREAM_OGG_SYNC;
  236. break;
  237. }
  238. default:
  239. break;
  240. }
  241. p += consumed;
  242. n -= consumed;
  243. }
  244. }
  245. static void *stream_thread() {
  246. while (running) {
  247. struct pollfd pollinfo;
  248. size_t space;
  249. LOCK;
  250. space = min(_buf_space(streambuf), _buf_cont_write(streambuf));
  251. if (fd < 0 || !space || stream.state <= STREAMING_WAIT) {
  252. UNLOCK;
  253. usleep(space ? 100000 : 25000);
  254. continue;
  255. }
  256. if (stream.state == STREAMING_FILE) {
  257. int n = read(fd, streambuf->writep, space);
  258. if (n == 0) {
  259. LOG_INFO("end of stream");
  260. _disconnect(DISCONNECT, DISCONNECT_OK);
  261. }
  262. if (n > 0) {
  263. _buf_inc_writep(streambuf, n);
  264. stream.bytes += n;
  265. LOG_SDEBUG("streambuf read %d bytes", n);
  266. }
  267. if (n < 0) {
  268. LOG_WARN("error reading: %s", strerror(last_error()));
  269. _disconnect(DISCONNECT, REMOTE_DISCONNECT);
  270. }
  271. UNLOCK;
  272. continue;
  273. } else {
  274. pollinfo.fd = fd;
  275. pollinfo.events = POLLIN;
  276. if (stream.state == SEND_HEADERS) {
  277. pollinfo.events |= POLLOUT;
  278. }
  279. }
  280. UNLOCK;
  281. // no mutex needed - we just want to know if we are inside poll()
  282. polling = true;
  283. if (_poll(ssl, &pollinfo, 100)) {
  284. polling = false;
  285. LOCK;
  286. // check socket has not been closed while in poll
  287. if (fd < 0) {
  288. UNLOCK;
  289. continue;
  290. }
  291. if ((pollinfo.revents & POLLOUT) && stream.state == SEND_HEADERS) {
  292. if (send_header()) stream.state = RECV_HEADERS;
  293. stream.header_len = 0;
  294. UNLOCK;
  295. continue;
  296. }
  297. if (pollinfo.revents & (POLLIN | POLLHUP)) {
  298. // get response headers
  299. if (stream.state == RECV_HEADERS) {
  300. // read one byte at a time to catch end of header
  301. char c;
  302. static int endtok;
  303. int n = _recv(ssl, fd, &c, 1, 0);
  304. if (n <= 0) {
  305. if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
  306. UNLOCK;
  307. continue;
  308. }
  309. LOG_INFO("error reading headers: %s", n ? strerror(last_error()) : "closed");
  310. _disconnect(STOPPED, LOCAL_DISCONNECT);
  311. UNLOCK;
  312. continue;
  313. }
  314. *(stream.header + stream.header_len) = c;
  315. stream.header_len++;
  316. if (stream.header_len > MAX_HEADER - 1) {
  317. LOG_ERROR("received headers too long: %u", stream.header_len);
  318. _disconnect(DISCONNECT, LOCAL_DISCONNECT);
  319. }
  320. if (stream.header_len > 1 && (c == '\r' || c == '\n')) {
  321. endtok++;
  322. if (endtok == 4) {
  323. *(stream.header + stream.header_len) = '\0';
  324. LOG_INFO("headers: len: %d\n%s", stream.header_len, stream.header);
  325. stream.state = stream.cont_wait ? STREAMING_WAIT : STREAMING_BUFFERING;
  326. wake_controller();
  327. }
  328. } else {
  329. endtok = 0;
  330. }
  331. UNLOCK;
  332. continue;
  333. }
  334. // receive icy meta data
  335. if (stream.meta_interval && stream.meta_next == 0) {
  336. if (stream.meta_left == 0) {
  337. // read meta length
  338. u8_t c;
  339. int n = _recv(ssl, fd, &c, 1, 0);
  340. if (n <= 0) {
  341. if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
  342. UNLOCK;
  343. continue;
  344. }
  345. LOG_INFO("error reading icy meta: %s", n ? strerror(last_error()) : "closed");
  346. _disconnect(STOPPED, LOCAL_DISCONNECT);
  347. UNLOCK;
  348. continue;
  349. }
  350. stream.meta_left = 16 * c;
  351. stream.header_len = 0; // amount of received meta data
  352. // MAX_HEADER must be more than meta max of 16 * 255
  353. }
  354. if (stream.meta_left) {
  355. int n = _recv(ssl, fd, stream.header + stream.header_len, stream.meta_left, 0);
  356. if (n <= 0) {
  357. if (n < 0 && _last_error() == ERROR_WOULDBLOCK) {
  358. UNLOCK;
  359. continue;
  360. }
  361. LOG_INFO("error reading icy meta: %s", n ? strerror(last_error()) : "closed");
  362. _disconnect(STOPPED, LOCAL_DISCONNECT);
  363. UNLOCK;
  364. continue;
  365. }
  366. stream.meta_left -= n;
  367. stream.header_len += n;
  368. }
  369. if (stream.meta_left == 0) {
  370. if (stream.header_len) {
  371. *(stream.header + stream.header_len) = '\0';
  372. LOG_INFO("icy meta: len: %u\n%s", stream.header_len, stream.header);
  373. stream.meta_send = true;
  374. wake_controller();
  375. }
  376. stream.meta_next = stream.meta_interval;
  377. UNLOCK;
  378. continue;
  379. }
  380. // stream body into streambuf
  381. } else {
  382. int n;
  383. space = min(_buf_space(streambuf), _buf_cont_write(streambuf));
  384. if (stream.meta_interval) {
  385. space = min(space, stream.meta_next);
  386. }
  387. n = _recv(ssl, fd, streambuf->writep, space, 0);
  388. if (n == 0) {
  389. LOG_INFO("end of stream (%u bytes)", stream.bytes);
  390. _disconnect(DISCONNECT, DISCONNECT_OK);
  391. }
  392. if (n < 0 && _last_error() != ERROR_WOULDBLOCK) {
  393. LOG_INFO("error reading: %s", strerror(last_error()));
  394. _disconnect(DISCONNECT, REMOTE_DISCONNECT);
  395. }
  396. if (n > 0) {
  397. stream_ogg(n);
  398. _buf_inc_writep(streambuf, n);
  399. stream.bytes += n;
  400. if (stream.meta_interval) {
  401. stream.meta_next -= n;
  402. }
  403. } else {
  404. UNLOCK;
  405. continue;
  406. }
  407. if (stream.state == STREAMING_BUFFERING && stream.bytes > stream.threshold) {
  408. stream.state = STREAMING_HTTP;
  409. wake_controller();
  410. }
  411. LOG_SDEBUG("streambuf read %d bytes", n);
  412. }
  413. }
  414. UNLOCK;
  415. } else {
  416. polling = false;
  417. LOG_SDEBUG("poll timeout");
  418. }
  419. }
  420. #if USE_SSL
  421. if (SSLctx) {
  422. SSL_CTX_free(SSLctx);
  423. }
  424. #endif
  425. return 0;
  426. }
  427. static thread_type thread;
  428. void stream_init(log_level level, unsigned stream_buf_size) {
  429. loglevel = level;
  430. LOG_INFO("init stream");
  431. LOG_DEBUG("streambuf size: %u", stream_buf_size);
  432. buf_init(streambuf, stream_buf_size);
  433. if (streambuf->buf == NULL) {
  434. LOG_ERROR("unable to malloc buffer");
  435. exit(2);
  436. }
  437. #if USE_SSL
  438. #if !LINKALL && !NO_SSLSYM
  439. if (ssl_loaded) {
  440. #endif
  441. SSL_library_init();
  442. SSLctx = SSL_CTX_new(SSLv23_client_method());
  443. if (SSLctx == NULL) {
  444. LOG_ERROR("unable to allocate SSL context");
  445. exit(3);
  446. }
  447. SSL_CTX_set_options(SSLctx, SSL_OP_NO_SSLv2);
  448. #if !LINKALL && !NO_SSLSYM
  449. }
  450. #endif
  451. ssl = NULL;
  452. #endif
  453. #if SUN
  454. signal(SIGPIPE, SIG_IGN); /* Force sockets to return -1 with EPIPE on pipe signal */
  455. #endif
  456. stream.state = STOPPED;
  457. stream.header = malloc(MAX_HEADER);
  458. *stream.header = '\0';
  459. fd = -1;
  460. #if LINUX || FREEBSD
  461. touch_memory(streambuf->buf, streambuf->size);
  462. #endif
  463. #if LINUX || OSX || FREEBSD || EMBEDDED
  464. pthread_attr_t attr;
  465. pthread_attr_init(&attr);
  466. #ifdef PTHREAD_STACK_MIN
  467. pthread_attr_setstacksize(&attr, PTHREAD_STACK_MIN + STREAM_THREAD_STACK_SIZE);
  468. #endif
  469. pthread_create_name(&thread, &attr, stream_thread, NULL, "stream");
  470. pthread_attr_destroy(&attr);
  471. #endif
  472. #if WIN
  473. thread = CreateThread(NULL, STREAM_THREAD_STACK_SIZE, (LPTHREAD_START_ROUTINE)&stream_thread, NULL, 0, NULL);
  474. #endif
  475. }
  476. void stream_close(void) {
  477. LOG_INFO("close stream");
  478. LOCK;
  479. running = false;
  480. UNLOCK;
  481. #if LINUX || OSX || FREEBSD || EMBEDDED
  482. pthread_join(thread, NULL);
  483. #endif
  484. free(stream.header);
  485. buf_destroy(streambuf);
  486. }
  487. void stream_file(const char *header, size_t header_len, unsigned threshold) {
  488. buf_flush(streambuf);
  489. LOCK;
  490. stream.header_len = header_len;
  491. memcpy(stream.header, header, header_len);
  492. *(stream.header+header_len) = '\0';
  493. LOG_INFO("opening local file: %s", stream.header);
  494. #if WIN
  495. fd = open(stream.header, O_RDONLY | O_BINARY);
  496. #else
  497. fd = open(stream.header, O_RDONLY);
  498. #endif
  499. stream.state = STREAMING_FILE;
  500. if (fd < 0) {
  501. LOG_INFO("can't open file: %s", stream.header);
  502. stream.state = DISCONNECT;
  503. }
  504. wake_controller();
  505. stream.cont_wait = false;
  506. stream.meta_interval = 0;
  507. stream.meta_next = 0;
  508. stream.meta_left = 0;
  509. stream.meta_send = false;
  510. stream.sent_headers = false;
  511. stream.bytes = 0;
  512. stream.threshold = threshold;
  513. UNLOCK;
  514. }
  515. void stream_sock(u32_t ip, u16_t port, char codec, const char *header, size_t header_len, unsigned threshold, bool cont_wait) {
  516. struct sockaddr_in addr;
  517. #if EMBEDDED
  518. // wait till we are not polling anymore
  519. while (polling && running) { usleep(10000); }
  520. #endif
  521. int sock = socket(AF_INET, SOCK_STREAM, 0);
  522. if (sock < 0) {
  523. LOG_ERROR("failed to create socket");
  524. return;
  525. }
  526. memset(&addr, 0, sizeof(addr));
  527. addr.sin_family = AF_INET;
  528. addr.sin_addr.s_addr = ip;
  529. addr.sin_port = port;
  530. LOG_INFO("connecting to %s:%d", inet_ntoa(addr.sin_addr), ntohs(addr.sin_port));
  531. set_nonblock(sock);
  532. set_nosigpipe(sock);
  533. if (connect_timeout(sock, (struct sockaddr *) &addr, sizeof(addr), 10) < 0) {
  534. LOG_INFO("unable to connect to server");
  535. LOCK;
  536. stream.state = DISCONNECT;
  537. stream.disconnect = UNREACHABLE;
  538. UNLOCK;
  539. return;
  540. }
  541. #if USE_SSL
  542. if (ntohs(port) == 443) {
  543. char server[256], *p;
  544. ssl = SSL_new(SSLctx);
  545. SSL_set_fd(ssl, sock);
  546. // add SNI
  547. sscanf(header, "Host:%255s", server);
  548. if (server) {
  549. if ((p = strchr(server, ':')) != NULL) *p = '\0';
  550. SSL_set_tlsext_host_name(ssl, server);
  551. }
  552. while (1) {
  553. int status, err = 0;
  554. ERR_clear_error();
  555. status = SSL_connect(ssl);
  556. // successful negotiation
  557. if (status == 1) break;
  558. // error or non-blocking requires more time
  559. if (status < 0) {
  560. err = SSL_get_error(ssl, status);
  561. if (err == SSL_ERROR_WANT_READ || err == SSL_ERROR_WANT_WRITE) continue;
  562. }
  563. LOG_WARN("unable to open SSL socket %d (%d)", status, err);
  564. closesocket(sock);
  565. SSL_free(ssl);
  566. ssl = NULL;
  567. LOCK;
  568. stream.state = DISCONNECT;
  569. stream.disconnect = UNREACHABLE;
  570. UNLOCK;
  571. return;
  572. }
  573. } else {
  574. ssl = NULL;
  575. }
  576. #endif
  577. buf_flush(streambuf);
  578. LOCK;
  579. fd = sock;
  580. stream.state = SEND_HEADERS;
  581. stream.cont_wait = cont_wait;
  582. stream.meta_interval = 0;
  583. stream.meta_next = 0;
  584. stream.meta_left = 0;
  585. stream.meta_send = false;
  586. stream.header_len = header_len;
  587. memcpy(stream.header, header, header_len);
  588. *(stream.header+header_len) = '\0';
  589. LOG_INFO("header: %s", stream.header);
  590. stream.sent_headers = false;
  591. stream.bytes = 0;
  592. stream.threshold = threshold;
  593. stream.ogg.miss = stream.ogg.match = 0;
  594. stream.ogg.state = (codec == 'o' || codec == 'p') ? STREAM_OGG_SYNC : STREAM_OGG_OFF;
  595. UNLOCK;
  596. }
  597. bool stream_disconnect(void) {
  598. bool disc = false;
  599. LOCK;
  600. #if USE_SSL
  601. if (ssl) {
  602. SSL_shutdown(ssl);
  603. SSL_free(ssl);
  604. ssl = NULL;
  605. }
  606. #endif
  607. if (fd != -1) {
  608. closesocket(fd);
  609. fd = -1;
  610. disc = true;
  611. }
  612. stream.state = STOPPED;
  613. if (stream.ogg.state == STREAM_OGG_HEADER && stream.ogg.data) free(stream.ogg.data);
  614. stream.ogg.data = NULL;
  615. UNLOCK;
  616. return disc;
  617. }