Ejemplo n.º 1
0
void
sendServerStatus(const char* modem, char* tag)
{
    DIR* dirp;
    struct dirent* dentp;
    config deflt;
    char fifomatch[80];
    int fifomatchlen;

    if (!(dirp = opendir("."))) {
	syslog(LOG_ERR, "%s: opendir: %m", SPOOLDIR);
	sendError("Problem accessing spool directory.");
	done(-1, "EXIT");
    }
    /*
     * Setup a prefix for matching potential FIFO files.
     * We do this carefully and in a way that insures we
     * use only the definitions in config.h.
     */
    if (strcmp(modem, MODEM_ANY) == 0)
	modem = "";
    sprintf(fifomatch, "%s.%.*s", FAX_FIFO,
	sizeof (fifomatch) - (sizeof (FAX_FIFO)+2), modem);
    fifomatchlen = strlen(fifomatch);
    while ((dentp = readdir(dirp)) != 0) {
	int fifo;

	if (strncmp(dentp->d_name, fifomatch, fifomatchlen) != 0)
	    continue;
	fifo = open(dentp->d_name, O_WRONLY|O_NDELAY);
	if (fifo != -1) {
	    config configuration;
	    char fileName[1024];
	    char* cp;

	    (void) close(fifo);
	    cp = strchr(dentp->d_name, '.') + 1;
	    sprintf(fileName, "%s.%s", FAX_CONFIG, cp);
	    getConfig(fileName, &configuration, &deflt);
	    if (version > 0) {
		char serverStatus[1024];
		char* tp;

		sprintf(fileName, "%s/%s", FAX_STATUSDIR, cp);
		getServerStatus(fileName, serverStatus);
		/*
		 * Convert fifo name from canonical format back
		 * to a pathname by replacing '_'s with '/'s.
		 */
		for (tp = cp; tp = strchr(tp, '_'); *tp = '/')
		    ;
		sendClient("server", "%s:%s:%s",
		    configuration.faxNumber, cp, serverStatus);
	    } else
		sendClient("server", "%s", configuration.faxNumber);
	}
    }
    (void) closedir(dirp);
}
Ejemplo n.º 2
0
/* *************************
 * GET
 * Mongoose http will call this function for each request.
 * May run in separate thread, will be multithreaded.
 */
void get(struct mg_connection *conn, const struct mg_request_info *ri, void *user_data) 
{
	size_t isize;
	size_t size;
	FILE *fp;
	char *data;
	int res;
    int SN;
    int modulus=1;
	
	printf("Request: %s\n",ri->query_string);
	printf("Uri: -->%s<--\n",ri->uri);

    if ( str_eq(ri->uri, "/status.html") ) {
        mg_printf(conn, "HTTP/1.1 200 OK\r\n");
		mg_printf(conn, "Content-Type: text/html\r\n\r\n");
		mg_printf(conn, STATUS_PAGE, SN1_par, SN2_par, SN3_par );
        mg_printf(conn, FORM_PAGE);
        
        return;
    }
    
    if ( str_eq(ri->uri, "/SET_STATUS") ) {
        
        getServerStatus( ri->query_string );
        
        mg_printf(conn, "HTTP/1.1 200 OK\r\n");
		mg_printf(conn, "Content-Type: text/html\r\n\r\n");
		mg_printf(conn, STATUS_PAGE, SN1_par, SN2_par, SN3_par );
        mg_printf(conn, FORM_PAGE);
        
        return;
        
    }
    
	if ( str_eq( ri->uri, "/favicon.ico") ) {
		data  = (char *) malloc( MAXDATASIZE );
		
		struct stat st;
		stat("favicon.ico", &st);
		size = st.st_size;	
		
		fp = fopen("favicon.ico","rb" );
		if (fp == NULL) {
			isize = 0;
			goto finis;			
		} 		
		isize = fread( data, sizeof( char ), size, fp ); 
		
		fclose( fp );
		
	finis:		
		// printf("Sending data if size %d as image \n", (int)size );
		
		mg_printf(conn, "HTTP/1.1 200 OK\r\n");
		mg_printf(conn, "Content-Type: image/png\r\n");
		mg_printf(conn, "Content-Length: %d\r\n\r\n",(int)size );
		
		res = mg_write(conn, data, (int)size);
		return;
		
	}

	if (ri->query_string == NULL) {
		mg_printf(conn, "HTTP/1.1 400 NO DATA RETURNED\r\n");
		mg_printf(conn, "Content-Type: text/plain\r\n\r\n");
		mg_printf(conn, "NULL Query. No answer\r\n");
	    return;
	}
	
	// Else return YES or NO
	//
	
	mg_printf(conn, "HTTP/1.1 200 OK\r\n");
	mg_printf(conn, "Content-Type: text/plain\r\n\r\n");
	helper_num++;
    
    SN = getServerNr(ri->query_string);
    
    SN = SN % 3;  // if someone does not read the assignment correctly
    
    if ( SN == 0 ){
        helper1++;
        if (SN1_par == 0 ) {
            mg_printf(conn, "NO\r\n");				            
        } else if (SN1_par == 100 ) {
            mg_printf(conn, "YES\r\n");				
        } else {
            modulus = 10;
            if ( (helper1 % modulus) <= (SN1_par/10) ) {
                mg_printf(conn, "YES\r\n");				
            } else {
                mg_printf(conn, "NO\r\n");				
            }

        }
    }else if ( SN == 1 ) {
        helper2++;
        if (SN2_par == 0 ) {
            mg_printf(conn, "NO\r\n");				            
        } else if (SN2_par == 100 ) {
            mg_printf(conn, "YES\r\n");				
        } else {
            modulus = 10;
            if ( (helper2 % modulus) <= (SN2_par/10) ) {
                mg_printf(conn, "YES\r\n");				
            } else {
                mg_printf(conn, "NO\r\n");				
            }
            
        }
        
    } else {
        helper3++;
        if (SN3_par == 0 ) {
            mg_printf(conn, "NO\r\n");				            
        } else if (SN3_par == 100 ) {
            mg_printf(conn, "YES\r\n");				
        } else {
            modulus = 10;
            if ( (helper3 % modulus) <= (SN3_par/10) ) {
                mg_printf(conn, "YES\r\n");				
            } else {
                mg_printf(conn, "NO\r\n");				
            }
            
        }
        
    }

	return;
}