1
0
mirror of https://gitlab.xiph.org/xiph/ezstream.git synced 2024-09-15 04:08:07 -04:00

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
This commit is contained in:
oddsock 2005-12-14 21:13:25 +00:00
parent e48b3b61aa
commit 480000c33a
3 changed files with 72 additions and 54 deletions

View File

@ -2,4 +2,4 @@
AUTOMAKE_OPTIONS = foreign 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

View File

@ -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_PREREQ(2.54)
AC_CONFIG_SRCDIR(src/ezstream.c) AC_CONFIG_SRCDIR(src/ezstream.c)

View File

@ -5,14 +5,15 @@
#include <string.h> #include <string.h>
#ifdef WIN32 #ifdef WIN32
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#include <windows.h> #include <windows.h>
#endif #endif
#include <shout/shout.h> #include <shout/shout.h>
#include <getopt.h> #include <getopt.h>
#include "configfile.h" #include "configfile.h"
#ifndef WIN32 #ifndef WIN32
#include <libgen.h> #include <libgen.h>
#include <unistd.h>
#endif #endif
#include <vorbis/vorbisfile.h> #include <vorbis/vorbisfile.h>
@ -31,7 +32,9 @@ void hup_handler(int sig)
#endif #endif
#ifdef WIN32 #ifdef WIN32
#define STRNCASECMP strnicmp #define STRNCASECMP strnicmp
#define popen _popen #define popen _popen
#define pclose _pclose
#define snprintf _snprintf
#else #else
#define STRNCASECMP strncasecmp #define STRNCASECMP strncasecmp
#endif #endif
@ -181,15 +184,15 @@ char* buildCommandString(char *extension, char *fileName, char *metadata)
printf("Going to execute (%s)\n", commandString); printf("Going to execute (%s)\n", commandString);
return(commandString); return(commandString);
} }
#ifdef WIN32 #ifdef WIN32
char *basename(char *fileName) { char *basename(char *fileName) {
char *pLast = strrchr(fileName, '\\'); char *pLast = strrchr(fileName, '\\');
if (pLast) { if (pLast) {
return pLast+1; return pLast+1;
} }
return NULL; return NULL;
} }
#endif #endif
char * processMetadata(shout_t *shout, char *extension, char *fileName) { char * processMetadata(shout_t *shout, char *extension, char *fileName) {
FILE *filepstream = NULL; FILE *filepstream = NULL;
@ -198,6 +201,8 @@ char * processMetadata(shout_t *shout, char *extension, char *fileName) {
char *songInfo = NULL; char *songInfo = NULL;
int songLen = 0; int songLen = 0;
ID3Tag id3tag; ID3Tag id3tag;
char temptrackName[31];
char tempartistName[31];
filepstream = fopen(fileName, "rb"); filepstream = fopen(fileName, "rb");
if (filepstream == NULL) { if (filepstream == NULL) {
@ -213,11 +218,16 @@ char * processMetadata(shout_t *shout, char *extension, char *fileName) {
fread(&id3tag, 1, 127, filepstream); fread(&id3tag, 1, 127, filepstream);
if (!strncmp(id3tag.tag, "TAG", strlen("TAG"))) { if (!strncmp(id3tag.tag, "TAG", strlen("TAG"))) {
/* We have an Id3 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); songInfo = (char *)malloc(songLen);
memset(songInfo, '\000', 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 (!songInfo) {
/* If we didn't get any song info via tags or comments, /* If we didn't get any song info via tags or comments,
then lets just use the filename */ then lets just use the filename */
char *p1 = NULL; char *p1 = NULL;
char *p2 = basename(fileName); char *p2 = basename(fileName);
if (p2) { if (p2) {
songInfo = strdup(p2); songInfo = strdup(p2);
p1 = strrchr(songInfo, '.'); p1 = strrchr(songInfo, '.');
if (p1) { if (p1) {
*p1 = '\000'; *p1 = '\000';
} }
} }
} }
@ -290,11 +300,11 @@ char * processMetadata(shout_t *shout, char *extension, char *fileName) {
return songInfo; return songInfo;
} }
FILE *openResource(shout_t *shout, char *fileName) FILE *openResource(shout_t *shout, char *fileName, int *popenFlag)
{ {
FILE *filep = NULL; FILE *filep = NULL;
printf("Opening file (%s)\n", fileName); printf("Opening file (%s)\n", fileName);
if (!strcmp(fileName, "stdin")) { if (!strcmp(fileName, "stdin")) {
#ifdef WIN32 #ifdef WIN32
_setmode(_fileno(stdin), _O_BINARY); _setmode(_fileno(stdin), _O_BINARY);
@ -314,15 +324,17 @@ FILE *openResource(shout_t *shout, char *fileName)
} }
pMetadata = processMetadata(shout, extension, fileName); pMetadata = processMetadata(shout, extension, fileName);
*popenFlag = 0;
if (pezConfig->reencode) { if (pezConfig->reencode) {
/* Lets set the metadata first */ /* Lets set the metadata first */
if (strlen(extension) > 0) { if (strlen(extension) > 0) {
pCommandString = buildCommandString(extension, fileName, pMetadata); pCommandString = buildCommandString(extension, fileName, pMetadata);
/* Open up the decode/encode loop using popen() */ /* Open up the decode/encode loop using popen() */
filep = popen(pCommandString, "r"); filep = popen(pCommandString, "r");
#ifdef WIN32 *popenFlag = 1;
_setmode(_fileno(filep), _O_BINARY ); #ifdef WIN32
#endif _setmode(_fileno(filep), _O_BINARY );
#endif
free(pMetadata); free(pMetadata);
free(pCommandString); free(pCommandString);
return filep; return filep;
@ -348,12 +360,13 @@ FILE *openResource(shout_t *shout, char *fileName)
int streamFile(shout_t *shout, char *fileName) { int streamFile(shout_t *shout, char *fileName) {
FILE *filepstream = NULL; FILE *filepstream = NULL;
char buff[4096]; char buff[4096];
long read, ret, total; long read, ret = 0, total;
int popenFlag = 0;
printf("Streaming %s\n", fileName); printf("Streaming %s\n", fileName);
filepstream = openResource(shout, fileName); filepstream = openResource(shout, fileName, &popenFlag);
if (!filepstream) { if (!filepstream) {
printf("Cannot open %s\n", fileName); printf("Cannot open %s\n", fileName);
return 0; return 0;
@ -365,27 +378,27 @@ int streamFile(shout_t *shout, char *fileName) {
if (read > 0) { if (read > 0) {
ret = shout_send(shout, buff, read); ret = shout_send(shout, buff, read);
if (ret != SHOUTERR_SUCCESS) { if (ret != SHOUTERR_SUCCESS) {
int loop = 1; int loop = 1;
printf("DEBUG: Send error: %s\n", shout_get_error(shout)); printf("DEBUG: Send error: %s\n", shout_get_error(shout));
while (loop) { while (loop) {
printf("Disconnected from server, reconnecting....\n"); printf("Disconnected from server, reconnecting....\n");
shout_close(shout); shout_close(shout);
if (shout_open(shout) == SHOUTERR_SUCCESS) { if (shout_open(shout) == SHOUTERR_SUCCESS) {
printf("Successful reconnection....\n"); printf("Successful reconnection....\n");
ret = shout_send(shout, buff, read); ret = shout_send(shout, buff, read);
loop = 0; loop = 0;
} }
else { else {
printf("Reconnect failed..waiting 5 seconds.\n"); printf("Reconnect failed..waiting 5 seconds.\n");
#ifdef WIN32 #ifdef WIN32
Sleep(5000); Sleep(5000);
#else #else
sleep(5); sleep(5);
#endif #endif
} }
} }
} }
shout_delay(shout); shout_delay(shout);
} else { } else {
@ -394,7 +407,13 @@ int streamFile(shout_t *shout, char *fileName) {
shout_sync(shout); shout_sync(shout);
} }
fclose(filepstream); if (popenFlag) {
printf("Closing via pclose\n");
pclose(filepstream);
}
else {
fclose(filepstream);
}
filepstream = NULL; filepstream = NULL;
return ret; return ret;
} }
@ -402,8 +421,7 @@ int streamPlaylist(shout_t *shout, char *fileName) {
FILE *filep = NULL; FILE *filep = NULL;
char streamFileName[8096] = ""; char streamFileName[8096] = "";
char lastStreamFileName[8096] = ""; char lastStreamFileName[8096] = "";
int loop = 1; int loop = 1;
int ret = 1;
filep = fopen(fileName, "r"); filep = fopen(fileName, "r");
if (filep == 0) { if (filep == 0) {
@ -420,7 +438,7 @@ int streamPlaylist(shout_t *shout, char *fileName) {
strcpy(lastStreamFileName, streamFileName); strcpy(lastStreamFileName, streamFileName);
/* Skip entries that begin with a # */ /* Skip entries that begin with a # */
if (strncmp(streamFileName, "#", 1)) { if (strncmp(streamFileName, "#", 1)) {
streamFile(shout, streamFileName); streamFile(shout, streamFileName);
} }
} }
if (rereadPlaylist) { if (rereadPlaylist) {