From 480000c33a915bd264e8233a6ed9c4e5adda3e29 Mon Sep 17 00:00:00 2001 From: oddsock Date: Wed, 14 Dec 2005 21:13:25 +0000 Subject: [PATCH] added some reconnect logic fixed a buffer overflow on large id3tags use pclose for popen'd file handles git-svn-id: https://svn.xiph.org/trunk/ezstream@10589 0101bb08-14d6-0310-b084-bc0e0c8e3800 --- conf/Makefile.am | 2 +- configure.in | 2 +- src/ezstream.c | 122 +++++++++++++++++++++++++++-------------------- 3 files changed, 72 insertions(+), 54 deletions(-) diff --git a/conf/Makefile.am b/conf/Makefile.am index 91a44cb..8ae5254 100644 --- a/conf/Makefile.am +++ b/conf/Makefile.am @@ -2,4 +2,4 @@ AUTOMAKE_OPTIONS = foreign -EXTRA_DIST = ezstream_m3u.xml ezstream_mp3.xml ezstream_vorbis.xml ezstream_reencoding_example.xml +EXTRA_DIST = ezstream_mp3.xml ezstream_reencoding_example_mp3.xml ezstream_reencoding_example_theora.xml ezstream_reencoding_example_vorbis.xml ezstream_vorbis.xml diff --git a/configure.in b/configure.in index 16dc0b0..9ad964f 100644 --- a/configure.in +++ b/configure.in @@ -1,4 +1,4 @@ -AC_INIT([ezstream], [0.2.0], [oddsock@xiph.org]) +AC_INIT([ezstream], [0.2.1], [oddsock@xiph.org]) AC_PREREQ(2.54) AC_CONFIG_SRCDIR(src/ezstream.c) diff --git a/src/ezstream.c b/src/ezstream.c index d0c6189..20599a5 100644 --- a/src/ezstream.c +++ b/src/ezstream.c @@ -5,14 +5,15 @@ #include #ifdef WIN32 #include -#include -#include +#include +#include #endif #include #include #include "configfile.h" -#ifndef WIN32 -#include +#ifndef WIN32 +#include +#include #endif #include @@ -31,7 +32,9 @@ void hup_handler(int sig) #endif #ifdef WIN32 #define STRNCASECMP strnicmp -#define popen _popen +#define popen _popen +#define pclose _pclose +#define snprintf _snprintf #else #define STRNCASECMP strncasecmp #endif @@ -181,15 +184,15 @@ char* buildCommandString(char *extension, char *fileName, char *metadata) printf("Going to execute (%s)\n", commandString); return(commandString); } - -#ifdef WIN32 -char *basename(char *fileName) { - char *pLast = strrchr(fileName, '\\'); - if (pLast) { - return pLast+1; - } - return NULL; -} + +#ifdef WIN32 +char *basename(char *fileName) { + char *pLast = strrchr(fileName, '\\'); + if (pLast) { + return pLast+1; + } + return NULL; +} #endif char * processMetadata(shout_t *shout, char *extension, char *fileName) { FILE *filepstream = NULL; @@ -198,6 +201,8 @@ char * processMetadata(shout_t *shout, char *extension, char *fileName) { char *songInfo = NULL; int songLen = 0; ID3Tag id3tag; + char temptrackName[31]; + char tempartistName[31]; filepstream = fopen(fileName, "rb"); if (filepstream == NULL) { @@ -213,11 +218,16 @@ char * processMetadata(shout_t *shout, char *extension, char *fileName) { fread(&id3tag, 1, 127, filepstream); if (!strncmp(id3tag.tag, "TAG", strlen("TAG"))) { /* We have an Id3 tag */ - songLen = strlen(id3tag.artistName) + strlen(" - ") + strlen(id3tag.trackName); + memset(temptrackName, '\000', sizeof(temptrackName)); + memset(tempartistName, '\000', sizeof(tempartistName)); + snprintf(temptrackName, sizeof(temptrackName)-1, "%s", id3tag.trackName); + snprintf(tempartistName, sizeof(tempartistName)-1, "%s", id3tag.artistName); + + songLen = sizeof(tempartistName) + strlen(" - ") + sizeof(temptrackName) + 1; songInfo = (char *)malloc(songLen); memset(songInfo, '\000', songLen); - sprintf(songInfo, "%s - %s", id3tag.artistName, id3tag.trackName); + snprintf(songInfo, songLen-1, "%s - %s", tempartistName, temptrackName); } } } @@ -263,14 +273,14 @@ char * processMetadata(shout_t *shout, char *extension, char *fileName) { if (!songInfo) { /* If we didn't get any song info via tags or comments, then lets just use the filename */ - char *p1 = NULL; - char *p2 = basename(fileName); + char *p1 = NULL; + char *p2 = basename(fileName); if (p2) { songInfo = strdup(p2); p1 = strrchr(songInfo, '.'); if (p1) { *p1 = '\000'; - } + } } } @@ -290,11 +300,11 @@ char * processMetadata(shout_t *shout, char *extension, char *fileName) { return songInfo; } -FILE *openResource(shout_t *shout, char *fileName) +FILE *openResource(shout_t *shout, char *fileName, int *popenFlag) { FILE *filep = NULL; - - printf("Opening file (%s)\n", fileName); + + printf("Opening file (%s)\n", fileName); if (!strcmp(fileName, "stdin")) { #ifdef WIN32 _setmode(_fileno(stdin), _O_BINARY); @@ -314,15 +324,17 @@ FILE *openResource(shout_t *shout, char *fileName) } pMetadata = processMetadata(shout, extension, fileName); + *popenFlag = 0; if (pezConfig->reencode) { /* Lets set the metadata first */ if (strlen(extension) > 0) { pCommandString = buildCommandString(extension, fileName, pMetadata); /* Open up the decode/encode loop using popen() */ - filep = popen(pCommandString, "r"); -#ifdef WIN32 - _setmode(_fileno(filep), _O_BINARY ); -#endif + filep = popen(pCommandString, "r"); + *popenFlag = 1; +#ifdef WIN32 + _setmode(_fileno(filep), _O_BINARY ); +#endif free(pMetadata); free(pCommandString); return filep; @@ -348,12 +360,13 @@ FILE *openResource(shout_t *shout, char *fileName) int streamFile(shout_t *shout, char *fileName) { FILE *filepstream = NULL; char buff[4096]; - long read, ret, total; + long read, ret = 0, total; + int popenFlag = 0; printf("Streaming %s\n", fileName); - filepstream = openResource(shout, fileName); + filepstream = openResource(shout, fileName, &popenFlag); if (!filepstream) { printf("Cannot open %s\n", fileName); return 0; @@ -365,27 +378,27 @@ int streamFile(shout_t *shout, char *fileName) { if (read > 0) { ret = shout_send(shout, buff, read); - if (ret != SHOUTERR_SUCCESS) { + if (ret != SHOUTERR_SUCCESS) { int loop = 1; - printf("DEBUG: Send error: %s\n", shout_get_error(shout)); - - while (loop) { - printf("Disconnected from server, reconnecting....\n"); - shout_close(shout); - if (shout_open(shout) == SHOUTERR_SUCCESS) { - printf("Successful reconnection....\n"); - ret = shout_send(shout, buff, read); - loop = 0; - } - else { - printf("Reconnect failed..waiting 5 seconds.\n"); -#ifdef WIN32 - Sleep(5000); -#else - sleep(5); -#endif - } - } + printf("DEBUG: Send error: %s\n", shout_get_error(shout)); + + while (loop) { + printf("Disconnected from server, reconnecting....\n"); + shout_close(shout); + if (shout_open(shout) == SHOUTERR_SUCCESS) { + printf("Successful reconnection....\n"); + ret = shout_send(shout, buff, read); + loop = 0; + } + else { + printf("Reconnect failed..waiting 5 seconds.\n"); +#ifdef WIN32 + Sleep(5000); +#else + sleep(5); +#endif + } + } } shout_delay(shout); } else { @@ -394,7 +407,13 @@ int streamFile(shout_t *shout, char *fileName) { shout_sync(shout); } - fclose(filepstream); + if (popenFlag) { + printf("Closing via pclose\n"); + pclose(filepstream); + } + else { + fclose(filepstream); + } filepstream = NULL; return ret; } @@ -402,8 +421,7 @@ int streamPlaylist(shout_t *shout, char *fileName) { FILE *filep = NULL; char streamFileName[8096] = ""; char lastStreamFileName[8096] = ""; - int loop = 1; - int ret = 1; + int loop = 1; filep = fopen(fileName, "r"); if (filep == 0) { @@ -420,7 +438,7 @@ int streamPlaylist(shout_t *shout, char *fileName) { strcpy(lastStreamFileName, streamFileName); /* Skip entries that begin with a # */ if (strncmp(streamFileName, "#", 1)) { - streamFile(shout, streamFileName); + streamFile(shout, streamFileName); } } if (rereadPlaylist) {