EmuProxyZA / emuProxyZA.c
From TivoZA
Back to emuProxyZA.
Download source (You will need to rename it to: emuProxyZA.c)
To update the below source file, select Upload file and upload your new emuProxyZA.c file (there is no need to upload it as a .txt). If you do upload a new version, please add a brief description to the change log at the bottom of this page indicating what changes you made and why.
[edit]
Source
/* emuProxyZA is based on emuProxy2 (by Tim Kleingeld) and the tivodns library (by Christopher R. Wingert). The reason for the name change is I have made significant changes to emuProxy2. emuProxy2 (and emuProxy3) does not correctly send through the requests as it relies on their being a Content-length field which is only present in the mlog.cgi request, this has now been fixed as well as a couple of major features added as listed below. Features: * Use DNS addresses as well as static IP addresses * Allows limiting the number of guide days to download, nice for those on dial-up (only possible if your provider/emulator supports it). The TivoZA emulator supplies 30 days of guide data by default which even gzipped can be 2MB for the full download. * Fix mlog.cgi request, mlog.cgi requests previously failed when the svclog was too large (this also prevented guide downloads being available for long periods, such as a month) * Fix Series 1 LocationID problem (i.e. how to use shorter LocationID's) * Accessing the server/emulator through transparent proxies (part of emuProxy2) * HTTP header fix (added in emuProxy3 by Warren Toomey) * Allow full traces of the requests sent and the responses received, great for debugging This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For the latest version and more information please see: http://tivoza.nanfo.com Enjoy, regards TivoZA */ #include <stdio.h> #include <time.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <sys/signal.h> #include <sys/errno.h> #include <string.h> #include "tivodns.h" #include "base64.c" #include <fcntl.h> #ifdef TIVO # include "bootpage.c" # include "mfs-utils/mfs.h" #endif #define VERSION "4.1.40" #define BUFFER_SIZE 1024 #define RESOLVCONF_FILE "/etc/resolv.conf" #define PORT 8000 #define SERVERIP "204.176.49.2" #define SERVERPORT 80 #define NTPIP "132.163.4.102" //"204.176.49.2" #define NTPPORT 37 #define LISTENTO "0.0.0.0" // Useful for testing //#define LISTENTO "127.0.0.1" // Potentially more secure #define DEBUGPATH "/tmp/" #define CONFIG_FILE "/hack/etc/emuProxyZA.conf" static char * argv_init[8+2];// = { NULL, }; static char * envp_init[8+2];// = { NULL, }; char *emuconf = CONFIG_FILE; char *resolvconf = RESOLVCONF_FILE; int localport = PORT; char *serverip = SERVERIP; int serverport = SERVERPORT; char *ntpip; char *listento = LISTENTO; char *proxyip; char *proxyport; char *proxyAuth = NULL; char myHeaders[BUFFER_SIZE]; char outBuffer[BUFFER_SIZE] = ""; char debugClientPath[100] = DEBUGPATH; char debugServerPath[100] = DEBUGPATH; int debugClient; int debugServer; int debug; int outBufferLength = 0; int inited = 0; int gotAlarm; int count = 0; int rawSend = 0; int onScreenDisplay = 0; int tcInfo = 0; int timeOffset = 0; int forceUpdate = 2; int guideDays = 0; char *weekDays = ""; int urlbase = 0; char locationID[50] = ""; time_t timeWeekdayOverride = 0; int bGuidedSetup = -1; int altNTP = 0; //Having flags to check if the following has been completed will save string comparison time int fixMlog; int fixHServer; int bodylength; int post; int addedHeaders; int headerComplete; int contentStarted; int totalRead; /****************************************************************************************/ void alarmHandler() { gotAlarm = 1; } /****************************************************************************************/ char *ltrim(char *buffer) { char *cp = buffer; if (cp && *cp) { // find first non-whitespace character //while (_istspace(*cp)) while (cp[0] == ' ') cp++; if (cp != buffer) memcpy(buffer, cp, (strlen(cp)+1)*sizeof(char)); } return buffer; } /****************************************************************************************/ char *rtrim(char *buffer) { char *cp = buffer; if (cp && *cp) { int bNonSpaceSeen = 0; // check if string is blank while (*cp) { if (!(cp[0] == ' ')) bNonSpaceSeen = 1; cp++; } if (bNonSpaceSeen) { cp--; // find last non-whitespace character while ( (cp >= buffer) && (cp[0] == ' ') ) *cp-- = 0; //_T('\0'); } else { // string contains only whitespace characters *buffer = 0; //_T('\0'); } } return buffer; } /****************************************************************************************/ char *trim(char *buffer) { return ltrim(rtrim(buffer)); } /****************************************************************************************/ char *substr(const char* buffer, int count) { char *outS = strdup(buffer); outS[count] = 0; return outS; } /****************************************************************************************/ int chrpos(const char* buffer, const char find) { char *pos = strchr(buffer, find); if (pos == NULL) return -1; else return pos - buffer; } /****************************************************************************************/ int strpos(const char* buffer, const char* find) { char *pos = strstr(buffer, find); if (pos == NULL) return -1; else return pos - buffer; } /****************************************************************************************/ unsigned long getIPAddress(const char* serverAddr) { struct in4_addr *ipAddr; int fd; int b; /* Convert a DNS address to an IP address */ if ( sscanf( serverAddr , "%d.%d.%d.%d", &b, &b, &b, &b ) != 4 ) { if (debug == 2) fprintf(stderr, "DNS address detected (%s), attempting to resolve to an IP address\n", serverAddr); /* Load resolv.conf to enable DNS lookups*/ if (inited == 0) { fd = open( resolvconf, O_RDONLY ); if ( fd < 0 ) { perror( "Failed to open resolv.conf" ); return INADDR_NONE; } close(fd); tivodns_init(resolvconf); inited = 1; } /* Convert the DNS address to an IP address */ ipAddr = tivodns_resolveip4(serverAddr); if (!ipAddr) { perror("Failed to find server IP for given DNS!\n"); return INADDR_NONE; } serverAddr = tivodns_ntoa4(ipAddr); } else if (debug == 2) fprintf(stderr, "IP address detected (%s), no DNS resolution required\n", serverAddr); return inet_addr(serverAddr); /* Fill in the server address */ } /****************************************************************************************/ void outSocket(int socket, char buffer[BUFFER_SIZE], int length, FILE *ofile) { //A similar function should be created for client out as this function relies on the specific //debugServer, outBufferLength & outBuffer variables if ((outBufferLength > 0) && ((length == -1) || (outBufferLength + length > BUFFER_SIZE)) ) { write(socket, outBuffer, outBufferLength); if (ofile) fwrite(outBuffer, outBufferLength, 1, ofile); outBufferLength = 0; } if (length > 0) { memcpy(outBuffer + outBufferLength, buffer, length); outBufferLength += length; } /*if (length != -1) { write(socket, buffer, length); if (debugServer) fwrite(buffer, length, 1, ofile); } */ } /****************************************************************************************/ void fixLocationID(int server, char buffer[BUFFER_SIZE], int length, FILE *ofile) { char newLocationID[100] = "IDB_LOCATIONID: "; char locID[50] = ""; char *pos; char *posEnd; int onlySatellite = 0; #ifdef TIVO //Load the current value stored in the MFS and replace it if necessary strcpy(locID, mfs_attr_get_str("/State/LocationConfig", "PostalCode")); if ( (strcmp(locationID, "0")) && (strcmp(locationID, locID)) ) { if (debug == 2) fprintf(stderr, "Update PostalCode to: %s\n", locationID); mfs_attr_set("/State/LocationConfig", "PostalCode", locationID); } #endif pos = strchr(buffer, ':') + 2; onlySatellite = !strncmp(pos, "DBS", 3); if (!strcmp(locationID, "0")) { //Only determine the locationID if a fixed one was not specified if (onlySatellite) { fprintf(stderr,"EmuProxyZA Error: When only receiving satellite data you need to supply a LocationID, run emuProxyZA -h for more help!\n"); exit(1); } strncpy(locID, pos, strchr(buffer, '-') - pos); } else strcpy(locID, locationID); if (debug == 2) fprintf(stderr, "LocationID: %s\n", locID); posEnd = strchr(pos, '~'); if (onlySatellite) { //Handle just Satellite postal codes if (posEnd != NULL) { strncat(newLocationID, pos, posEnd - pos + 1); strncat(newLocationID, locID, 3); pos = strchr(pos, '-'); } } else { //Handle Cable & Satellite postal codes strncat(newLocationID, locID, 4); pos = strchr(pos, '-'); if (posEnd != NULL) { //Handle Satellite part of request strncat(newLocationID, pos, posEnd - pos + 1); strncat(newLocationID, locID, 3); pos = strchr(pos + 1, '-'); } } posEnd = strchr(pos, '\n'); strncat(newLocationID, pos, posEnd - pos); strcat(newLocationID, "\n"); outSocket(server, newLocationID, strlen(newLocationID), ofile); if (debug == 2) fprintf(stderr, "Location ID changed to: %s", newLocationID); } /****************************************************************************************/ void fixTcInfo(int server, char buffer[BUFFER_SIZE], int length, FILE *ofile) { //Only the ip address and port numbers will be changed the rest will remain as is char newTcInfo[100]; char *pos; char *posEnd; sprintf(newTcInfo, "%s:%d", serverip, serverport); pos = strchr(buffer, ':') + 1; //Find the end of the IDB_TCINFO field pos = strchr(pos, ':') + 1; //Find the second colon just before the ip address is passed outSocket(server, buffer, pos - buffer, ofile); outSocket(server, newTcInfo, strlen(newTcInfo), ofile); pos = strchr(pos, ':'); //Find the colon between the ip adrress and port number pos = strchr(pos + 1, ':'); //Find the colon after port number posEnd = strchr(pos, '\n'); //Find the end of the line //Output the remiander of the IDB_TCINFO field outSocket(server, pos, posEnd - pos + 1, ofile); if (debug == 2) fprintf(stderr, "IDB_TCINFO has been corrected"); } /****************************************************************************************/ char *extractFileName(const char* buffer) { char *fileName = NULL; fileName = strchr(buffer, ' ') + 1; fileName = substr(fileName, strpos(fileName, " ")); fileName = strrchr(fileName, '/') + 1; if (debug == 2) fprintf(stderr, "\nFileName: %s\n", fileName); return fileName; } /****************************************************************************************/ int GuidedSetup() { if (bGuidedSetup < 0) { //Check if TiVo is in Guided Setup mode #ifdef TIVO char *bootpg = bootpage("/dev/hda"); bGuidedSetup = (bootpg != NULL) && (strstr(bootpg, "GS=1") != NULL); //Check if in Maintenance Mode if (!bGuidedSetup) bGuidedSetup = mfs_attr_get_int("/State/GeneralConfig", "Complete") != 7; //Check if Guided Setup has been completed #endif if (debug) fprintf(stderr, "Guided Setup: %d\n", bGuidedSetup); } return bGuidedSetup; } /****************************************************************************************/ int GuideDataTo() { u32 fsid, count, i, ver, idx, sliceID; u32 maxVer = 0; char name[20]; int arrOffset = time(NULL) / 86400 - 10; //Allows days in array to be populated from today - 10, no find required int days[100]; for(i=0; i < 100; i++) days[i] = 0; fsid = mfs_fsid( "/Schedule" ); if (fsid > 0) { struct mfs_dirent *dir = mfs_dir(fsid,&count); for(i=0; i < count; i++) { strcpy(name, strrchr(dir[i].name, ':') + 1); sliceID = atoi(name); //Get date strcpy(name, strchr(dir[i].name, ':') + 1); strchr(name, ':')[0] = 0; idx = atoi(name) - arrOffset; if (days[idx] != 1) { days[idx] = 1; ver = query_int(sliceID, "ServerVersion"); if (ver > maxVer) maxVer = ver; //printf("%s: %d\n", name, ver); } } if (dir) mfs_dir_free(dir); } return maxVer; } /****************************************************************************************/ void requestNTP(int server) { if (proxyip) { char strReq[100]; FILE *ofile = NULL; if (debug == 2) fprintf(stderr, "Performing NTP request via proxy.\n"); if (debugServer) { char filename[100]; sprintf(filename, "%sep%dRequest.log", debugServerPath, count); ofile = fopen(filename, "w"); } sprintf(strReq, "GET http://%s:%d HTTP/1.0\r\n", ntpip, NTPPORT); outSocket(server, strReq, strlen(strReq), ofile); if (proxyAuth) { char extraInfo[BUFFER_SIZE]; sprintf(extraInfo, "Proxy-authorization: Basic %s\r\n", proxyAuth); outSocket(server, extraInfo, strlen(extraInfo), ofile); } //Send end of request outSocket(server, "\r\n\r\n", 4, ofile); outSocket(server, "", -1, ofile); if (debugServer) fclose(ofile); } } /****************************************************************************************/ void fixEpoch(int socket, char buffer[BUFFER_SIZE], int length, FILE *ofile) { int i; unsigned char val; unsigned long long int epoch = 0; unsigned long long int oldepoch = 0; char epochStr[10] = ""; char currentStr[10]; if (debug == 2) fprintf(stderr, "\nNTP epoch time: %s\n", buffer); for (i=0; i < length; i++) { val = buffer[i]; epoch = epoch + val; if (i + 1 < length) epoch = epoch << 8; fprintf(stderr, "loop %d: %c = %d\n", i, val, val); } epoch = epoch + (timeOffset * 60); //timeOffset need to be in minutes while (epoch > 0) { oldepoch = epoch; epoch = epoch >> 8; val = oldepoch - (epoch << 8); strcpy(currentStr, epochStr); sprintf(epochStr, "%c%s", val, currentStr); fprintf(stderr, "loop time: %c = %d, %s\n", val, val, epochStr); } fprintf(stderr, "\nnew time: %s\n", epochStr); outSocket(socket, epochStr, strlen(epochStr), ofile); } /****************************************************************************************/ void checkRequest(int socket, char buf[BUFFER_SIZE], int length, FILE *ofile) { char buffer[BUFFER_SIZE]; char *http10; /* Pointer to occurrence of "HTTP/1.0 CR" */ strncpy(buffer, buf, length); buffer[length] = 0; if ( (bodylength < 0) && strncasecmp(buffer, "Content-length:", 15) == 0) { if (!fixMlog) { bodylength = atoi(buffer+15); outSocket(socket, buffer, length, ofile); } } else if (fixMlog && headerComplete && !rawSend) { //No mlog content will be sent to the socket } else if (fixHServer && !rawSend && (strncasecmp(buffer, "IDB_SWDESC:", 11) == 0)) { if (debug == 2) fprintf(stderr, "Line removed: IDB_SWDESC\n"); } else if (fixHServer && strcmp(locationID, "") && (strncasecmp(buffer, "IDB_LOCATIONID:", 15) == 0)) { fixLocationID(socket, buffer, length, ofile); } else if (fixHServer && forceUpdate && (strncasecmp(buffer, "IDB_ST_HIST:", 12) == 0)) { char strHist[BUFFER_SIZE] = "IDB_ST_HIST:\n"; //Write the histry field #ifdef TIVO if ( (forceUpdate == 2) && (!GuidedSetup()) ) { if (debug == 2) fprintf(stderr, "Replacing IDB_ST_HIST with correct GuideDataTo\n"); strncpy(strHist, buffer, strrchr(buffer, ':') - buffer + 1); sprintf(strHist, "%s%d-1\n", strHist, GuideDataTo() ); } else #endif if (debug == 2) fprintf(stderr, "Removing known slices from IDB_ST_HIST\n"); outSocket(socket, strHist, strlen(strHist), ofile); } else if (fixHServer && tcInfo && (strncasecmp(buffer, "IDB_TCINFO:", 11) == 0)) { fixTcInfo(socket, buffer, length, ofile); } else if (!addedHeaders && strstr(buffer, "HTTP/1") != NULL) { if (onScreenDisplay) osd_write( extractFileName(buffer) ); post = (!strncasecmp("POST", buffer, 4)); fixMlog = ((strstr(buffer, "mlog.cgi") != NULL) && !rawSend); fixHServer = (strstr(buffer, "HServer.cgi") != NULL); /*None of this first condition should be here, the problem is the mlog.cgi has param gzip which causes it to fail. When using a proxy to proxy the line get duplicated due to the way in which each line is sent one by one, so this should be fixed!!!! Should probably buffer the data and only send it when the TiVo normally would send it, i.e. after the full read has completed??? Add a param -S2 for series 2 mode, this would seperate each instruction by the & symbol rather than by a new line!*/ //Below test is needed for Series 2 units /* if (forceUpdate && fixMlog) { outSocket(socket, "POST /tivo-service/mlog.cgi HTTP/1.0\n", 37, ofile); fixMlog = 0; addedHeaders = 1; } else*/ if (rawSend == 2) { outSocket(socket, buffer, length, ofile); } else { //Write the corrected http request to the socket, Replace any "HTTP/1.0 CR" with " HTTP/1.0CR if ((http10 = strstr(buffer, "HTTP/1.0 \015")) != NULL) memcpy(http10, " HTTP/1.0\015", 10); if (proxyip) { char *strReq; int idx = strchr(buffer, '/') - buffer; strncpy(strReq, buffer, idx); strReq[idx] = 0; sprintf(strReq, "%shttp://%s:%d%s", strReq, serverip, serverport, buffer + idx); outSocket(socket, strReq, strlen(strReq), ofile); if (proxyAuth) { char extraInfo[BUFFER_SIZE]; sprintf(extraInfo, "Proxy-authorization: Basic %s\r\n", proxyAuth); outSocket(socket, extraInfo, strlen(extraInfo), ofile); } } else outSocket(socket, buffer, length, ofile); //Add guide days, if set to the HServer.cgi request if (fixHServer && guideDays) sprintf(myHeaders, strcat(myHeaders, "GuideDays: %d\n"), guideDays); // Append our headers after the HTTP/1.x line outSocket(socket, myHeaders, strlen(myHeaders), ofile); if (fixHServer && !rawSend) { char extraInfo[BUFFER_SIZE]; sprintf(extraInfo, "emuProxyZA: %s\n", VERSION); outSocket(socket, extraInfo, strlen(extraInfo), ofile); } addedHeaders = 1; if (debug == 2) fprintf(stderr, "Send my headers\n"); } } else { //Check if we are finished reading the header if (!headerComplete && ( (buffer[0] == '\n') || (buffer[0] == '\r') )) { headerComplete = 1; if (debug == 2) { fprintf(stderr, ", header complete: "); if (buffer[0] == '\r') fprintf(stderr, "Blank line found"); else if (buffer[0] == '\n') fprintf(stderr, "Blank DOS line found"); fprintf(stderr, "\n"); } contentStarted = 1; //Add Content-length to the HServer.cgi request if (post && (bodylength < 0) ) { bodylength = 0; outSocket(socket, "Content-length: 0\r\n\r\n", 21, ofile); } else outSocket(socket, buffer, length, ofile); } else { //Write the buffer content to the socket outSocket(socket, buffer, length, ofile); } } } /****************************************************************************************/ void checkResponse(int socket, char buf[BUFFER_SIZE], int length, FILE *ofile) { char buffer[BUFFER_SIZE]; strncpy(buffer, buf, length); buffer[length] = 0; if (fixHServer && altNTP && (strncasecmp(buffer, "TIME_SVC=", 9) == 0)) { char ntpInfo[BUFFER_SIZE]; //Get NTP server address if (!ntpip) { char strPad[100]; char strNTP[100]; if ( sscanf( buffer , "%s -b %s ", &strPad, &strNTP) == 2) ntpip = strdup(strNTP); } if (debug == 2) fprintf(stderr, "NTP server: %s", ntpip); //If no ntpip is found, set it to the default if ((!ntpip) || (ntpip == "")) ntpip = NTPIP; //Update sent NTP address sprintf(ntpInfo, "TIME_SVC=/bin/ntpdate -b %s\n", listento); outSocket(socket, ntpInfo, strlen(ntpInfo), ofile); } else { //Write the buffer content to the socket outSocket(socket, buffer, length, ofile); } } /****************************************************************************************/ int ProcessBuffer(int socket, char buffer[BUFFER_SIZE], int length, int readComplete, object_fn fnCheck, FILE *ofile) { int end = 0; int start = 0; do { int newline = chrpos(buffer+start, '\n'); if (newline < 0) { end = length - start; if (debug == 2) fprintf(stderr,"New line is NULL, Read: %d, Start: %d, End: %d\n", length, start, end); if (readComplete || (end + 1 >= BUFFER_SIZE)) { fnCheck(socket, buffer+start, end, ofile); return 0; } else { memcpy(buffer, buffer+start, end); return end; } } else { end = newline + 1; if (debug == 2) fprintf(stderr,"Start: %d, End: %d", start, end); fnCheck(socket, buffer+start, end, ofile); if (debug == 2) fprintf(stderr,", written\n"); start += end; } } while (start < length); return 0; } /****************************************************************************************/ void request(int server, int client) { char buffer[BUFFER_SIZE]; int r = 0; int end = 0; int readComplete = 0; FILE *ofile = NULL; char filename[100]; if (debugServer) { sprintf(filename, "%sep%dRequest.log", debugServerPath, count); ofile = fopen(filename, "w"); } //Reset the flags addedHeaders = 0; headerComplete = 0; contentStarted = 0; bodylength = -1; post = 0; totalRead = 0; // Pass out post data do { if (!readComplete) { //alarm(15); // Just in case r = read(client, buffer+end, (sizeof buffer) - end - 1); totalRead += r; readComplete = (r < (sizeof buffer) - end - 1); r += end; if (gotAlarm) { break; if (debug) fprintf(stderr,"Client read failed, got alarm!\n"); } } if (debug == 2) fprintf(stderr,"Request read count: %d, read complete: %d\n", r, readComplete); if (r > 0) { buffer[r] = 0; end = 0; if (contentStarted && !fixMlog && !fixHServer) { if (debug == 2) fprintf(stderr,"Direct stream output\n"); outSocket(server, buffer, r, ofile); } else end = ProcessBuffer(server, buffer, r, readComplete, checkRequest, ofile); } if (debug == 2) fprintf(stderr,"ReadComplete %d, HeaderComplete: %d, ContentStarted: %d\n", readComplete, headerComplete, contentStarted); readComplete = readComplete && headerComplete && contentStarted; readComplete = readComplete && (bodylength < 0 || totalRead >= (bodylength)); if (debug == 2) fprintf(stderr, "Bodylength: %d, TotalRead: %d\n", bodylength, totalRead); if (!readComplete) { if (debug == 2) fprintf(stderr, "ReadComplete not true so not cheking for file end!\n"); } else if (bodylength < 0 || totalRead >= bodylength) { if (debug == 2) fprintf(stderr, "Bodylength read complete!\n"); } else if (strncasecmp(buffer + r - 2, "\n\n", 2) == 0) { if (debug == 2) fprintf(stderr, "End of transmission, double blank unix line found!\n"); } else if (strncasecmp(buffer + r - 4, "\r\n\r\n", 4) == 0) { if (debug == 2) fprintf(stderr, "End of transmission, double blank dos line found!\n"); } else readComplete = 0; } while (!readComplete || !headerComplete || !contentStarted); if (fixMlog && !rawSend) outSocket(server, "\nemuProxyZA\n", 12, ofile); outSocket(server, "", -1, ofile); if (debugServer) fclose(ofile); if (debug) fprintf(stderr,"Request finished!\n"); } /****************************************************************************************/ void response(int server, int client, int port) { char buffer[BUFFER_SIZE]; int r; FILE *ofile = NULL; char filename[100]; int readComplete = 0; int end = 0; //Reset the flags totalRead = 0; if (debug) fprintf(stderr,"Response started:\n"); if (debugClient) { sprintf(filename, "%sep%dResponse.log", debugClientPath, count); ofile = fopen(filename, "w"); } if (debug) fprintf(stderr,"Response file created.\n"); // Pass back the reply do { if (!readComplete) { alarm(15); // Just in case r = read(server, buffer+end, (sizeof buffer) - end - 1); totalRead += r; readComplete = (r <= 0); //(r < (sizeof buffer) - end - 1); r += end; if (gotAlarm) { break; if (debug) fprintf(stderr,"Server read failed, got alarm!\n"); } } if (debug == 2) fprintf(stderr,"Request read count: %d, read complete: %d\n", r, readComplete); if (r > 0) { buffer[r] = 0; end = 0; if ((timeOffset != 0) && (port == NTPPORT)) fixEpoch(client, buffer, r, ofile); else if (fixHServer && altNTP) end = ProcessBuffer(client, buffer, r, readComplete, checkResponse, ofile); else outSocket(client, buffer, r, ofile); if (debug == 2) fprintf(stderr,", written"); } if (debug == 2) fprintf(stderr,", done\n"); } while (!readComplete); outSocket(client, "", -1, ofile); if (debugClient) fclose(ofile); if (debug) fprintf(stderr,"Response finished!\n"); } /****************************************************************************************/ int handleConnection(int listener, const char* serverAddr, int port) { int server = -1; int client; struct sockaddr_in addr; time_t t0 = time(NULL); struct tm *tTm = localtime ( &t0 ); char dayOfWeek[1]; char *pos; client = accept(listener, 0, 0); if (client < 0) return 1; if (debug) fprintf(stderr, "\nAccepted connection on port: %d\nDate/Time: %s", port, asctime(tTm)); count++; if (serverAddr == "") { //Need to be done before weekday check so weekday can be overriden close(client); return 0; } if (debug) fprintf(stderr, "timeWeekdayOverride: %d, Difference: %d\n", timeWeekdayOverride, t0 - timeWeekdayOverride); if ((timeWeekdayOverride > 0) && (t0 - timeWeekdayOverride > 3600)) // 3600 = 60minutes x 60seconds timeWeekdayOverride = 0; if ( (!timeWeekdayOverride) && (weekDays != "") && (!GuidedSetup()) ) { //Check to see if a call is allowed to be made today sprintf(dayOfWeek, "%ld", tTm->tm_wday); pos = strstr(weekDays, dayOfWeek); if (debug) fprintf(stderr, "Weekdays: %s, DayOfWeek: %s, pos: %d\n", weekDays, dayOfWeek, pos); if (pos == NULL) { if (debug) fprintf(stderr, "You have specified not to allow guide downloads on this day of the week.\n"); close(client); return 0; } } //Process request-------------------------------------------- if (onScreenDisplay) osd_init(); server = socket(PF_INET, SOCK_STREAM, 0); if (server < 0) { perror("Could not create socket"); close(client); return 0; } else if (debug == 2) fprintf(stderr, "Server socket created\n"); //Fill in the server address /* Moved the ip address assignment down here as this now allows for DNS addresses as well as dynamic DNS addresses which could have changed IP address between calls to the emulator */ addr.sin_family = AF_INET; if (proxyip) { addr.sin_addr.s_addr = getIPAddress(proxyip); addr.sin_port = htons(proxyport); } else { addr.sin_addr.s_addr = getIPAddress(serverAddr); //serverip); addr.sin_port = htons(serverport); } if (addr.sin_addr.s_addr == INADDR_NONE)