Example #1
0
int main(int argc, char *argv[])
{
    int                 sockfd , clisockfd;
    unsigned int        clilen;
    int                 childpid;
    struct sockaddr_in  serv_addr,cli_addr;
    int err_select;//JY1113

#ifdef LPR_with_ASUS//JY1112
    int 		LPRflag = 0;
    fd_set		rfds, afds;
    int			nfds;
    int			sockfd_ASUS;
    unsigned int        clilen_ASUS;
    int                 childpid_ASUS;
    struct sockaddr_in  serv_addr_ASUS,cli_addr_ASUS;
#endif
#ifdef Raw_Printing_with_ASUS  //Lisa
	int		netfd, fd, clientlen, one = 1;
	struct sockaddr_in	netaddr, client;
#endif
	int lock;
	int pid = 0;
	FILE *fp;

	fp = fopen("/var/run/lpdparent.pid", "r");
	if (fp) {
		fscanf(fp, "%d", &pid);
		fclose(fp);
	}

	if ((pid > 1) && (kill(pid, 0) == 0 || errno != ESRCH)) {
		syslog(LOGOPTS, "another lpd daemon exists!!\n");
		exit(0);
	}

	update_pidfile();

    //Initial the server the not busy
    lptstatus.busy = FALSE;

    sigset_t sigs_to_catch;
    sigemptyset(&sigs_to_catch);
    sigaddset(&sigs_to_catch, SIGCLD);
    sigaddset(&sigs_to_catch, SIGINT);
    sigaddset(&sigs_to_catch, SIGQUIT);
    sigaddset(&sigs_to_catch, SIGKILL);
    sigaddset(&sigs_to_catch, SIGUSR2);
    sigprocmask(SIG_UNBLOCK, &sigs_to_catch, NULL);

    //Setup the signal handler
    signal(SIGCLD, sig_child); 
    signal(SIGINT, sig_cleanup); 
    signal(SIGQUIT, sig_cleanup); 
    signal(SIGKILL, sig_cleanup);
    signal(SIGUSR2, sig_remove);//JY1110 
    
    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 )
    {
        syslog(LOGOPTS, "can't open stream socket: %m");
        exit(0);
    }
    
    bzero((char *)&serv_addr , sizeof(serv_addr));
    serv_addr.sin_family        = AF_INET;
    serv_addr.sin_addr.s_addr   = htonl(INADDR_ANY);
    serv_addr.sin_port          = htons(PNT_SVR_PORT_LPR);

    
    if(bind(sockfd,(struct sockaddr *)&serv_addr , sizeof(serv_addr)) < 0 )
    {
        syslog(LOGOPTS, "can't bind socket with port %d: %m", PNT_SVR_PORT_LPR);
        exit(0);
    }
    /*JY1111*/
    int windowsize=2920;
    setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize));
    int no_delay=1;
    setsockopt(sockfd, IPPROTO_TCP, TCP_NODELAY, &no_delay, sizeof(no_delay));	// by Jiahao. 20080808.

    listen(sockfd , 15);

#ifdef Raw_Printing_with_ASUS //Lisa
	if ((netfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)
	{
		syslog(LOGOPTS, "cannot open stream socket for raw printing: %m\n");
		exit(1);
	}
	if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
	{
		syslog(LOGOPTS, "cannot setsocketopt for raw printing: %m\n");
		exit(1);
	}
	netaddr.sin_family = AF_INET;
	netaddr.sin_port = htons(BASEPORT);
	netaddr.sin_addr.s_addr = htonl(INADDR_ANY);
	memset(netaddr.sin_zero, 0, sizeof(netaddr.sin_zero));
	if (bind(netfd, (struct sockaddr*) &netaddr, sizeof(netaddr)) < 0)
	{
		syslog(LOGOPTS, "cannot bind socket with port %d for raw printing: %m\n", BASEPORT);
		exit(1);
	}
	if (listen(netfd, 5) < 0)
	{
		syslog(LOGOPTS, "cannot listen socket for raw printing: %m\n");
		exit(1);
	}
	//clientlen = sizeof(client);
	//memset(&client, 0, sizeof(client));
#endif

#ifdef LPR_with_ASUS//JY1112
	if((sockfd_ASUS = socket(AF_INET,SOCK_STREAM,0)) < 0 )
	{
	        syslog(LOG_ERR, "can't open stream socket for LPR: %m");
	        exit(0);
	}
    	bzero((char *)&serv_addr_ASUS , sizeof(serv_addr_ASUS));
    	serv_addr_ASUS.sin_family        = AF_INET;
    	serv_addr_ASUS.sin_addr.s_addr   = htonl(INADDR_ANY);
    	serv_addr_ASUS.sin_port          = htons(PNT_SVR_PORT_ASUS);

    	if(bind(sockfd_ASUS,(struct sockaddr *)&serv_addr_ASUS , sizeof(serv_addr_ASUS)) < 0 )
    	{
		syslog(LOG_ERR, "can't bind socket for LPR: %m");
		exit(0);
   	}

    	setsockopt(sockfd_ASUS, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize));

    	listen(sockfd_ASUS , 15);
    
    	/*set the fds*/
    	nfds=MAX(sockfd, sockfd_ASUS);
    	FD_ZERO(&afds);
#endif
    
    while(TRUE)
    {
	//if (busy) syslog(LOG_NOTICE, "busying %d %d\n", lptstatus.busy, busy);

	lock = file_lock("printer");				// by Jiahao for U2EC. 20080808.
	if (lptstatus.busy==FALSE && nvram_invmatch("MFP_busy", "2"))
	{
		busy=FALSE;
		nvram_set_int_temp("MFP_busy", 0);

		nvram_set_temp("u2ec_busyip", "");
	}
	file_unlock(lock);

#ifdef Raw_Printing_with_ASUS //Lisa
	FD_SET(netfd, &afds);
#endif
#ifdef LPR_with_ASUS//JY1112
	FD_SET(sockfd, &afds);
	FD_SET(sockfd_ASUS, &afds);
	memcpy(&rfds, &afds, sizeof(rfds));

	if((err_select=select(nfds+1, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0 )) < 0) 
	{
//JY1120	printf("select error on sockfd: error=%d\n", errno);
		/**/
//		printf("sockfd_FD_ISSET=%d\n", FD_ISSET(sockfd, &rfds));
//JY1120		printf("sockfd_ASUS FD_ISSET=%d\n", FD_ISSET(sockfd_ASUS, &rfds));
		/**/
//		if(errno != 4)//JY1113: delete
		//syslog(LOG_NOTICE, "select error %d\n", err_select);
		continue;
	}
#endif
        clilen = sizeof(cli_addr);

	if(FD_ISSET(sockfd_ASUS, &rfds))
	{
		LPRflag = 0;
		clisockfd   = accept(sockfd_ASUS,(struct sockaddr *)&cli_addr, &clilen);
	}
#ifdef LPR_with_ASUS//JY1112 
	else if(FD_ISSET(sockfd, &rfds))
	{
		LPRflag = 1;
		clisockfd   = accept(sockfd,(struct sockaddr *)&cli_addr, &clilen);
	}
#endif
#ifdef Raw_Printing_with_ASUS //Lisa
//	else if(FD_ISSET(netfd, &rfds) && busy==FALSE)
	else if(FD_ISSET(netfd, &rfds))
	{
		lock = file_lock("printer");	// by Jiahao for U2EC. 20080808.
		if (nvram_match("MFP_busy", "0"))
		{
			file_unlock(lock);
			LPRflag = 2;
			clisockfd = accept(netfd, (struct sockaddr*) &cli_addr, &clilen);
		}
		else
		{
			file_unlock(lock);
			sleep(2);
			continue;
		}
	}
#endif
	else
        {
		//syslog(LOG_NOTICE, "No select\n");
		sleep(2);
		continue;
	}

	strcpy(clientaddr , inet_ntoa(cli_addr.sin_addr));
	
	if(clisockfd < 0)
        {
	     //syslog(LOG_NOTICE, "LPD error: No clisockfd %d\n", LPRflag);
             continue;
        }

	lock = file_lock("printer");
//	if (busy!=FALSE)	/* 2004/09/10 by Joey, process nack in parent for LPR and Remote Prot */
	if (nvram_invmatch("MFP_busy", "0"))		// by Jiahao for U2EC. 20080808.
	{
		file_unlock(lock);
		//syslog(LOG_NOTICE, "Printing others 1 %d %d\n", LPRflag, clisockfd);
		if (LPRflag==0) processReq(clisockfd);
		else if (LPRflag==1) processReq_LPR(clisockfd, 0);
		//syslog(LOG_NOTICE, "Printing others %d %d\n", LPRflag, clisockfd);
		close(clisockfd);
		// For Raw printing, don't resonse to client while busy
		sleep(5);
		continue;
	}

	nvram_set_int_temp("MFP_busy", 1);

	if (nvram_match("lan_ipaddr_t", ""))
		nvram_set_temp("u2ec_busyip", nvram_safe_get("lan_ipaddr"));
	else
		nvram_set_temp("u2ec_busyip", nvram_safe_get("lan_ipaddr_t"));

	file_unlock(lock);

        if( (childpid = fork() ) < 0)
        {
        }
        else if(childpid == 0) 
        {
		//syslog(LOG_NOTICE, "Printing %d\n", LPRflag);

		if(LPRflag==0) processReq(clisockfd); 
#ifdef LPR_with_ASUS//JY1114 
		else if(LPRflag==1) processReq_LPR(clisockfd, 1);
#endif
#ifdef Raw_Printing_with_ASUS //Lisa
		else if(LPRflag == 2) processReq_Raw(clisockfd);
#endif
		close(sockfd);
		close(sockfd_ASUS);
#ifdef Raw_Printing_with_ASUS
		close(netfd);
#endif
        	exit(0);
        }
              
	//syslog(0, "Printing Process %d %d %d\n", busy, lptstatus.busy, childpid);

        //parents process goes on here
        //remark PRINT("fork -- childpid %d\n",childpid);

	if(lptstatus.busy == FALSE)
	{
		lptstatus.busy = TRUE;
//		busy = TRUE;				// remarked by Jiahao for U2EC. 20080808.
		strcpy(lptstatus.addr , inet_ntoa(cli_addr.sin_addr));
		lptstatus.pid = childpid;
	}

        close(clisockfd);
    }

}
Example #2
0
main()
{
    int                 sockfd , clisockfd;
    unsigned int        clilen;
    int                 childpid;
    struct sockaddr_in  serv_addr,cli_addr;
#ifdef LPR_with_ASUS//JY1112
    int 		LPRflag = 0;
    fd_set		rfds, afds;
    int			nfds,nfds1;
    int			sockfd_ASUS;
    unsigned int        clilen_ASUS;
    int                 childpid_ASUS;
    struct sockaddr_in  serv_addr_ASUS,cli_addr_ASUS;
#endif
#ifdef Raw_Printing_with_ASUS  //Lisa
    int		netfd, fd, clientlen, one = 1;
    struct sockaddr_in	netaddr, client;
#endif

    //Initial the server the not busy
    lptstatus.busy = FALSE;

    //Setup the signal handler
    signal(SIGCLD, sig_child);
    signal(SIGINT, sig_cleanup);
    signal(SIGQUIT, sig_cleanup);
    signal(SIGKILL, sig_cleanup);
    signal(SIGUSR2, sig_remove);//JY1110

    if((sockfd = socket(AF_INET,SOCK_STREAM,0)) < 0 )
    {
        perror("can't open stream socket:");
        exit(0);
    }

    bzero((char *)&serv_addr , sizeof(serv_addr));
    serv_addr.sin_family        = AF_INET;
    serv_addr.sin_addr.s_addr   = htonl(INADDR_ANY);
    serv_addr.sin_port          = htons(PNT_SVR_PORT_LPR);


    if(bind(sockfd,(struct sockaddr *)&serv_addr , sizeof(serv_addr)) < 0 )
    {
        perror("can't bind:");
        exit(0);
    }
    /*JY1111*/
    int windowsize=2920;
    setsockopt(sockfd, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize));

#if 1
    int currentpid=getpid();
    FILE *pidfileread;

    if((pidfileread=fopen("/var/run/lpdparent.pid", "r")) == NULL) {
        pidfileread=fopen("/var/run/lpdparent.pid", "w");
        fprintf(pidfileread, "%d", currentpid);
        fclose(pidfileread);
    }
    else {
        printf("another lpd daemon exists!!\n");
        fclose(pidfileread);
        exit(0);
    }
#endif
    /*JY1110
    	int testusb = 0;
    	testusb = check_par_usb_prn();
    	if(testusb){
    		printf("USB\n");//JY1112delete
    		fd_print=open("/dev/usb/lp0", O_RDWR);
    	}
    	else{
    		printf("PARALLEL\n");//JY1112delete
    		fd_print=open("/dev/lp0", O_RDWR);
    	}
    	checkstatus_usb_par();
    	close(fd_print);
    111111*/

    listen(sockfd , 15);

#ifdef Raw_Printing_with_ASUS //Lisa
    if ((netfd = socket(AF_INET, SOCK_STREAM, IPPROTO_IP)) < 0)
    {
//		syslog(LOGOPTS, "socket: %m\n");
        exit(1);
    }
    if (setsockopt(netfd, SOL_SOCKET, SO_REUSEADDR, &one, sizeof(one)) < 0)
    {
//		syslog(LOGOPTS, "setsocketopt: %m\n");
        exit(1);
    }
    netaddr.sin_port = htons(BASEPORT);
    netaddr.sin_addr.s_addr = htonl(INADDR_ANY);
    memset(netaddr.sin_zero, 0, sizeof(netaddr.sin_zero));
    if (bind(netfd, (struct sockaddr*) &netaddr, sizeof(netaddr)) < 0)
    {
//		syslog(LOGOPTS, "bind: %m\n");
        exit(1);
    }
    if (listen(netfd, 5) < 0)
    {
//		syslog(LOGOPTS, "listen: %m\n");
        exit(1);
    }
    //clientlen = sizeof(client);
    //memset(&client, 0, sizeof(client));
#endif


#ifdef LPR_with_ASUS//JY1112
    if((sockfd_ASUS = socket(AF_INET,SOCK_STREAM,0)) < 0 )
    {
        perror("can't open stream socket:");
        exit(0);
    }
    bzero((char *)&serv_addr_ASUS , sizeof(serv_addr_ASUS));
    serv_addr_ASUS.sin_family        = AF_INET;
    serv_addr_ASUS.sin_addr.s_addr   = htonl(INADDR_ANY);
    serv_addr_ASUS.sin_port          = htons(PNT_SVR_PORT_ASUS);

    if(bind(sockfd_ASUS,(struct sockaddr *)&serv_addr_ASUS , sizeof(serv_addr_ASUS)) < 0 )
    {
        perror("can't bind:");
        exit(0);
    }

    setsockopt(sockfd_ASUS, SOL_SOCKET, SO_RCVBUF, (char *)&windowsize, sizeof(windowsize));

    listen(sockfd_ASUS , 15);

    /*set the fds*/
    nfds1=MAX(sockfd, sockfd_ASUS);
#ifdef Raw_Printing_with_ASUS //Lisa
    nfds=MAX(nfds1, netfd);
#endif
    FD_ZERO(&afds);
#endif



    while(TRUE)
    {
#ifdef Raw_Printing_with_ASUS //Lisa
        FD_SET(netfd, &afds);
#endif
#ifdef LPR_with_ASUS//JY1112
        FD_SET(sockfd, &afds);
        FD_SET(sockfd_ASUS, &afds);

        int err_select;//JY1113
        memcpy(&rfds, &afds, sizeof(rfds));
        if((err_select=select(nfds+1, &rfds, (fd_set *)0, (fd_set *)0, (struct timeval *)0 )) < 0) {
//JY1120		printf("select error on sockfd: error=%d\n", errno);
            /**/
//		printf("sockfd_FD_ISSET=%d\n", FD_ISSET(sockfd, &rfds));
//JY1120		printf("sockfd_ASUS FD_ISSET=%d\n", FD_ISSET(sockfd_ASUS, &rfds));
            /**/
//		if(errno != 4)//JY1113: delete
            continue;
        }
#endif
        clilen      = sizeof(cli_addr);

        if (busy == FALSE) //Add by Lisa
        {
            busy = TRUE; // Add by Lisa
#ifdef LPR_with_ASUS//JY1112 
            if(FD_ISSET(sockfd, &rfds))
            {
                LPRflag = 1;
                clisockfd   = accept(sockfd,(struct sockaddr *)&cli_addr, &clilen);
//JY1120
                printf("LPR sock received...\n");//JY1114: delete
            }
            else if(FD_ISSET(sockfd_ASUS, &rfds))
            {
                LPRflag = 0;
                clisockfd   = accept(sockfd_ASUS,(struct sockaddr *)&cli_addr, &clilen);
//JY1120
                printf("ASUSRemote port sock receved...\n");//JY1114: delete
            }
#ifdef Raw_Printing_with_ASUS //Lisa
            else if(FD_ISSET(netfd, &rfds))
            {
                LPRflag = 2;
                clisockfd = accept(netfd, (struct sockaddr*) &cli_addr, &clilen);
                printf("Raw Printing sock received...\n");
            }
#endif
            else {
                perror("lpr: no data received...");
                continue;
            }

#else
            clisockfd   = accept(sockfd,(struct sockaddr *)&cli_addr, &clilen);
#endif
            strcpy(clientaddr , inet_ntoa(cli_addr.sin_addr));
            if(clisockfd < 0)
            {
                perror("accept error:");
                continue;
            }

            //remark PRINT("Accept OK ,client address -- %s\n",inet_ntoa(cli_addr.sin_addr));

            if( (childpid = fork() ) < 0)
            {
                perror("fork error:");
            }
            else if(childpid == 0)
            {
                printf("child start\n");
                //child process starts here
//#ifdef LPR_SUPPORT
//JY1120		printf("LPR process....\n");
#ifdef LPR_with_ASUS//JY1114 
//JY1120		printf("lptstatus.busy=%s\n", lptstatus.busy);//JY1114
//JY1120		printf("lptstatus.pid=%d\n", lptstatus.pid);//JY1114
                if(LPRflag == 1)
                {
                    printf("LPRflag_remote=%d \n",LPRflag);
                    processReq_LPR(clisockfd);
                }
#ifdef Raw_Printing_with_ASUS //Lisa
                else if (LPRflag == 2)
                {
                    printf("LPRflag_raw=%d \n",LPRflag);
                    processReq_Raw(clisockfd);
                }
#endif
                else
#endif
                {
                    printf("LPRflag_LPR=%d \n",LPRflag);
                    processReq(clisockfd);
                }

//#else
//            processReq(clisockfd);
//#endif
                //PRINT("client exit\n");
                if(LPRflag == 1)
                    printf("LPR client exit\n");//JY1113delete
                else if (LPRflag == 2)
                    printf("Raw Printing client exit\n");
                else
                    printf("ASUS Remote client exit\n");//JY1113delete
//		check_prn_status(ONLINE, "");
                close(sockfd);//
                close(sockfd_ASUS);//
                exit(0);
            }


            //parents process goes on here
            //remark PRINT("fork -- childpid %d\n",childpid);

            if(lptstatus.busy == FALSE)
            {
                //someone is using the printer now
                //lptstatus.busy = TRUE;

                //record the infomation of the client process
                strcpy(lptstatus.addr , inet_ntoa(cli_addr.sin_addr));
                lptstatus.pid = childpid;
            }
        }
        //PRINT("lptstatus.addr %s\n",lptstatus.addr);

        close(clisockfd);
    }

}