stream.c 18 KB

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