예제 #1
0
//---------------------------------------------------------------------------
// mode & 1 = test only, don't create dirs
QString TSatScript::get_rx_command(const QString& satname, const double frequency, bool *error, int mode)
{
    QString rc;

    _satname = satname;
    _satname.replace(" ", "-");
    _frequency = frequency;

    *error = true;

    if(!rx_srcrip_enable())
        return "Error: RX Script is not enabled!";

    if(satname.isEmpty())
        return "Error: No satellite name!";
    if(frequency <= 0)
        return "Error: Undefined frequency!";

    now = QDateTime::currentDateTime();

    _frames_filename   = "";
    _baseband_filename = "";

    rc = getcommand(_rx_script, _rx_script_args, error, mode);

    if(!*error) {
        if(postproc_srcrip_enable() && _baseband_filename.isEmpty()) {
            *error = true;
            return "\nBaseband file must be defined when post RX processing script is enabled!";
        }
    }

    return rc;
}
예제 #2
0
파일: nandsim.c 프로젝트: AhmadTux/freebsd
int
main(int argc, char **argv)
{
	struct nandsim_command *cmdopts;
	int retcode = 0;

	if (argc < 2) {
		cmdhelp(argc, argv);
		retcode = EX_USAGE;
	} else {
		cmdopts = getcommand(argv[1]);
		if (cmdopts != NULL && cmdopts->commandfunc != NULL) {
			if (checkusage(argc, cmdopts->req_argc, argv) == 1) {
				/* Print command specific usage */
				printf("nandsim %s", cmdopts->usagestring);
				return (EX_USAGE);
			}
			retcode = cmdopts->commandfunc(argc, argv);

			if (retcode == EX_USAGE) {
				/* Print command-specific usage */
				printf("nandsim %s", cmdopts->usagestring);
			} else if (retcode == EX_OSFILE) {
				error("Could not open device file");
			}

		} else {
			error("Unknown command!");
			retcode = EX_USAGE;
		}
	}
	return (retcode);
}
예제 #3
0
파일: ui.cpp 프로젝트: amagnasco/scrap
Command Interface::detailspage(Being & b) {
    Command c;
    bool viewingplayer = false;

    do {
        Being & viewed = (viewingplayer ? GAME.being[0] : b);
        displaydetails(viewed);
        if (viewed.isplayer) {
            if (b.isplayer)
                msg("Viewing your system details. Press any key...");
            else {
                char msgbuf[128];

                strcpy(msgbuf, "\
Viewing your system details. Press d to view ");
                strcat(msgbuf, b.getname());
                strcat(msgbuf, "'s systems.");
                msg(msgbuf);
            }
        }
        else {
            char msgbuf[128];

            strcpy(msgbuf, "Viewing ");
            strcat(msgbuf, viewed.getname());
            strcat(msgbuf, "'s system details. Press d to view your systems.");
            msg(msgbuf);
        }
        c = getcommand();
        if (c == COMMAND_DETAILS)
            if (b.isplayer)
                c = COMMAND_UNDEFINED;
            else
                viewingplayer = !viewingplayer;
    } while (c == COMMAND_DETAILS);
예제 #4
0
파일: control.c 프로젝트: imr/ngspice
/* va: TODO: free control structure(s) before overwriting (memory leakage) */
int
cp_evloop(char *string)
{
    wordlist *wlist, *ww, *freewl;
    struct control *x;
    char *i;
    int nn;

#define newblock                                                \
    do {                                                        \
        cend[stackp]->co_children = TMALLOC(struct control, 1); \
        ZERO(cend[stackp]->co_children, struct control);        \
        cend[stackp]->co_children->co_parent = cend[stackp];    \
        cend[stackp] = cend[stackp]->co_children;               \
        cend[stackp]->co_type = CO_UNFILLED;                    \
    } while(0)

    for (;;) {
        freewl = wlist = getcommand(string);
        if (wlist == NULL) {    /* End of file or end of user input. */
            if (cend[stackp] && cend[stackp]->co_parent && !string) {
                cp_resetcontrol();
                continue;
            } else {
                return (0);
            }
        }
        if ((wlist->wl_word == NULL) || (*wlist->wl_word == '\0')) {
            /* User just typed return. */
            wl_free(wlist); /* va, avoid memory leak */
            if (string) {
                return (1);
            } else {
                cp_event--;
                continue;
            }
        }

        /* Just a check... */
        for (ww = wlist; ww; ww = ww->wl_next)
            if (!ww->wl_word) {
                fprintf(cp_err,
                        "cp_evloop: Internal Error: NULL word pointer\n");
                continue;
            }


        /* Add this to the control structure list. If cend->co_type is
         * CO_UNFILLED, the last line was the beginning of a block,
         * and this is the unfilled first statement.
         */
        /* va: TODO: free old structure and its content, before overwriting */
        if (cend[stackp] && (cend[stackp]->co_type != CO_UNFILLED)) {
            cend[stackp]->co_next = TMALLOC(struct control, 1);
            ZERO(cend[stackp]->co_next, struct control);
            cend[stackp]->co_next->co_prev = cend[stackp];
            cend[stackp]->co_next->co_parent = cend[stackp]->co_parent;
            cend[stackp] = cend[stackp]->co_next;
        } else if (!cend[stackp]) {
예제 #5
0
파일: TCOMMAND.C 프로젝트: EstebanAO/Subir
void rmenu(void)
/* Executes the commands in the row menu */
{
 switch(getcommand(RMENU, RCOMMAND))
 {
  case 0 :
   insertrow(currow);
   break;
  case 1 :
   deleterow(currow);
   break;
 } /* switch */
} /* rmenu */
예제 #6
0
파일: TCOMMAND.C 프로젝트: EstebanAO/Subir
void umenu(void)
/* Executes the commands in the utility menu */
{
 switch(getcommand(UMENU, UCOMMAND))
 {
  case 0 :
   recalc();
   break;
  case 1 :
   changeformdisplay(!formdisplay);
   displayscreen(UPDATE);
   break;
 } /* switch */
} /* umenu */
예제 #7
0
파일: TCOMMAND.C 프로젝트: EstebanAO/Subir
void cmenu(void)
/* Executes the commands in the column menu */
{
 switch(getcommand(CMENU, CCOMMAND))
 {
  case 0 :
   insertcol(curcol);
   break;
  case 1 :
   deletecol(curcol);
   break;
  case 2 :
   setcolwidth(curcol);
   break;
 } /* switch */
} /* cmenu */
예제 #8
0
파일: ui.cpp 프로젝트: amagnasco/scrap
int Interface::selectitem(char * question, Being & viewed) {
    int ret;
    bool done = false;
    bool readcommand = true;
    Command c;

    while (!done) {
        if (readcommand) {
            query(question);
            c = getcommand();
        }
        else
            readcommand = true;
        switch (c) {
        case COMMAND_CANCEL:
            ret = -1; done = true; break;
        case COMMAND_1:
            ret = 0; done = true; break;
        case COMMAND_2:
            ret = 1; done = true; break;
        case COMMAND_3:
            ret = 2; done = true; break;
        case COMMAND_4:
            ret = 3; done = true; break;
        case COMMAND_5:
            ret = 4; done = true; break;
        case COMMAND_6:
            ret = 5; done = true; break;
        case COMMAND_7:
            ret = 6; done = true; break;
        case COMMAND_8:
            ret = 7; done = true; break;
        case COMMAND_DETAILS:
            c = detailspage(viewed);
            GAME.normaldisplay();
            viewed.displaystatus();
            readcommand = false;
            break;;
        default:
            break;
        }
    }

    return ret;
}
예제 #9
0
//---------------------------------------------------------------------------
// mode & 1 = test only, don't create dirs
QString TSatScript::get_postproc_command(bool *error, int mode)
{
    QString rc;

    *error = true;

    if(!postproc_srcrip_enable())
        return "Error: Post RX Script is not enabled!";

    if(!rx_srcrip_enable())
        return "Error: RX Script must also be enabled!";

    if(_baseband_filename.isEmpty())
        return "Error: Baseband file is empty or not defined in the RX script! Run the RX script first!";

    rc = getcommand(_postproc_script, _postproc_script_args, error, mode);

    return rc;
}
예제 #10
0
파일: TCOMMAND.C 프로젝트: EstebanAO/Subir
void mainmenu(void)
/* Executes the commands in the main menu */
{
 switch(getcommand(MENU, COMMAND))
 {
  case 0 :
   smenu();
   break;
  case 1 :
   formatcells();
   break;
  case 2 :
   deletecell(curcol, currow, UPDATE);
   printfreemem();
   if (autocalc)
    recalc();
   break;
  case 3 :
   gotocell();
   break;
  case 4 :
   cmenu();
   break;
  case 5 :
   rmenu();
   break;
  case 6 :
   editcell(curcell);
   break;
  case 7 :
   umenu();
   break;
  case 8 :
   changeautocalc(!autocalc);
   break;
  case 9 :
   checkforsave();
   stop = TRUE;
   break;
 } /* switch */
} /* mainmenu */
예제 #11
0
int main(int argc, char **argv)
{
		int retcode;

		/** Init **/
		if (lirc_init("lircE161",1)==-1) exit(EXIT_FAILURE); /* Init lirc_client */
		if (lirc_readconfig( lirc_configfile, &lirc_config, NULL) !=0 ){
				lirc_deinit();
				return EXIT_FAILURE;
		}

		/* Get the command to run on command_buf */
		strcpy(command_buf, getcommand(lirc_config));

		/* build up the arguments in arf_buf at every key press */
		while(1){
				retcode = getnextlircinput();
				if (retcode == LIRCE161_ERROR)
						goto out;
				if (retcode == LIRCE161_DONE) /* done, let get out and run */
						break;
		}

		/* Put the command together */
		sprintf(composite_buf, (const char *)command_buf, arg_buf);
#ifdef DEBUG
		printf("Command is %s\n", command_buf);
		printf("Arguments are %s\n", arg_buf);
		printf("Composite Command is %s\n", composite_buf);
#endif
		system(composite_buf);

		/* finish up */
out:
		lirc_freeconfig(lirc_config);
		lirc_deinit();

		return EXIT_SUCCESS;
}
예제 #12
0
파일: TCOMMAND.C 프로젝트: EstebanAO/Subir
void smenu(void)
/* Executes the commands in the spreadsheet menu */
{
 char filename[MAXINPUT + 1];

 filename[0] = 0;
 switch(getcommand(SMENU, SCOMMAND))
 {
  case 0 :
   checkforsave();
   loadsheet(filename);
   break;
  case 1 :
   savesheet();
   break;
  case 2 :
   printsheet();
   break;
  case 3 :
   checkforsave();
   clearsheet();
   break;
 } /* switch */
} /* smenu */
예제 #13
0
파일: dhtnode.c 프로젝트: hanzki/ccourse13
int main(int argc, char *argv[]) {
    if (argc < 3) { // Checking for correct arquments
        printf("Useage: dhtnode own_hostname own_port\n");
        exit(0);
    }
    char* my_url = argv[1];
    char* my_port = argv[2];
    
    sha1_t my_hash; // Create hash for own address
    hash_url(my_url, my_port, my_hash);
    fd_set master;
    fd_set rfds;
    int retval;
    int running = 1;
    int listensock = create_listen_socket(my_port);
    con_t server;
    memset(&server,0,sizeof server);
    con_t left;
    memset(&left,0,sizeof left);
    con_t right;
    memset(&right,0,sizeof right);
    FD_SET(STDIN_FILENO, &master);
    FD_SET(listensock, &master);
    
    int fdmax = listensock;
    struct timeval looptime;

    while (running) {
        looptime.tv_sec = 1;
        looptime.tv_usec = 0;
        rfds = master;
        retval = select(fdmax + 1, &rfds, NULL, NULL, &looptime);

        if (retval == -1)
            die("select failed");
        else if (retval) {
            if (FD_ISSET(STDIN_FILENO, &rfds)) {
                int argcount;
                char** arguments = NULL;
                int cmd = getcommand(STDIN_FILENO, &argcount, &arguments);
                printf("got command\n");
                if (cmd < 0) {
                    //TODO handle different errors differently
                    printf("error: %d\n", cmd);
                    exit(-1);
                }
                switch (cmd) {
                    case USER_EXIT:
                        if(server.state != ST_EMPTY && (argcount == 0 || strcmp(arguments[0],"force"))){//remember strcmp returns 0 on match
                            printf("trying to exit\n");
                            send_dht_pckt(server.socket,DHT_DEREGISTER_BEGIN,my_hash,my_hash,0,NULL);
                        }
                        else{
                            printf("exiting\n");
                            running = 0;
                        }
                        freeargs(argcount, arguments);
                        break;
                    case USER_CONNECT:
                        if(argcount != 2){
                            printf("usage: connect url port\n");
                            freeargs(argcount,arguments);
                            break;
                        }
                        if(server.state != ST_EMPTY){
                            printf("Already connected to the server. Use exit to disconnect.\n");
                            freeargs(argcount,arguments);
                            break;
                        }
                        printf("connecting to %s:%s\n", arguments[0], arguments[1]);
                        int sock = create_connection(arguments[0], arguments[1]);
                        printf("connection made\n");
                        FD_SET(sock, &master);
                        if(sock > fdmax) fdmax = sock;
                        server.socket = sock;
                        server.state = ST_WF_SER_SHAKE;
                        
                        freeargs(argcount, arguments);
                        break;
                    case USER_PRINT:
                        printf("printing...\n");
                        for(int i = 0; i < argcount; i++){
                            printf("%s:\n",arguments[i]);
                            if(!strcmp(arguments[i],"sockets")){
                               printf("server, %d %s\n",server.socket,(server.socket != 0 && FD_ISSET(server.socket,&master))? "is set":"not set"); 
                               printf("right, %d %s\n",right.socket,(right.socket != 0 && FD_ISSET(right.socket,&master))? "is set":"not set"); 
                               printf("left, %d %s\n",left.socket,(left.socket != 0 && FD_ISSET(left.socket,&master))? "is set":"not set"); 
                            }
                            else if(!strcmp(arguments[i],"states")){
                                printf("server, %d\n",server.state);
                                printf("right, %d\n",right.state);
                                printf("left, %d\n",left.state);
                            }
                        }
                        freeargs(argcount,arguments);
                        break;
                    case USER_UNKNW_CMD:
                        printf("unknown command, line was:");
                        for (int i = 0; i < argcount; i++) {
                            printf(" %s", arguments[i]);
                        }
                        printf("\n");
                        freeargs(argcount, arguments);
                        break;
                }
            }
            //check for new connections
            if (FD_ISSET(listensock, &rfds)) {
                printf("someone is connecting\n");
                if(right.state == ST_EMPTY){
                    right.socket = accept_connection(listensock);
                    if(right.socket < 0){
                        die(strerror(errno));
                    }
                    FD_SET(right.socket, &master);
                    if(right.socket > fdmax) fdmax = right.socket;
                    right.state = ST_WF_CLI_SHAKE;
                } else if (left.state == ST_EMPTY) {
                    left.socket = accept_connection(listensock);
                    if(left.socket < 0){
                        die(strerror(errno));
                    }
                    FD_SET(left.socket, &master);
                    if(left.socket > fdmax) fdmax = left.socket;
                    left.state = ST_WF_CLI_SHAKE;
                }
            }
            //check for msg from server
            if(server.state != ST_EMPTY && FD_ISSET(server.socket, &rfds)){
                int status;
                if(server.state == ST_WF_SER_SHAKE){
                    printf("getting shake\n");
                    status = recvdata(server.socket, NULL);
                    if(status == NTW_SERVER_SHAKE){
                        printf("shaking back\n");
                        send_shake(server.socket, NTW_CLIENT_SHAKE);
                        unsigned char formated_url[strlen(my_url)+3];
                        format_url(my_url, my_port, formated_url);
                        printf("sending DHT_REGISTER_BEGIN\n");
                        send_dht_pckt(server.socket,DHT_REGISTER_BEGIN,my_hash,my_hash,strlen(my_url)+3,formated_url);
                        server.state = ST_ONLINE;
                    }
                    //TODO error handling
                    /*On correct shake shake back and send DHT_REGISTER_BEGIN
                     with our sha1 as sender and target and
                     our port+url as payload
                     switch state to ST_ONLINE*/
                } else{
                    DHT_t packet;
                    memset(&packet, 0, sizeof packet);
                    status = recvdata(server.socket, &packet);
                    if(status != NTW_PACKET_OK){
                        switch(status){
                            case NTW_ERR_SERIOUS:
                                die(strerror(errno));
                                break;
                            case NTW_DISCONNECT:
                                die("server disconnected");
                                break;
                            default:
                                printf("getting packet failed somehow\n");
                                memset(&packet, 0, sizeof packet);
                                break;
                        }
                    }
                    printf("received from server:\n");
                    print_dht_packet(&packet);
                    //TODO error handling
                    uint16_t reqt = ntohs(packet.req_type);
                    if(server.state == ST_ONLINE && reqt == DHT_REGISTER_FAKE_ACK){
                        printf("fake ack received\n");
                        /*send DHT_REGISTER_DONE to server
                         target and sender are both our hash
                         state to ST_IDLING*/
                        int a = send_dht_pckt(server.socket, DHT_REGISTER_DONE, my_hash, my_hash, 0, NULL);
                        if(a == 0) server.state = ST_IDLING;
                        else exit(1); // or error handling
                        
                    } else if(server.state == ST_IDLING && reqt == DHT_REGISTER_BEGIN){
                        printf("dht_register_begin received\n");
                        /*open connection to the new node
                         which port+url is in packet and
                         send data + DHT_REGISTER_ACK
                         our hash sender and target
                         state to ST_WF_REG_DONE*/
                        char his_url[packet.pl_length-2];
                        strcpy(his_url,(char*)packet.payload+2);
                        uint16_t* port = (uint16_t*)packet.payload;
                        *port = ntohs(*port);
                        char his_port[10];
                        sprintf(his_port,"%u",*port);
                        int sock = create_connection(his_url,his_port);
                        FD_SET(sock, &master);
                        if(sock > fdmax) fdmax = sock;
                        con_t* pal;
                        pal = (right.state == ST_EMPTY) ? &right : &left;
                        memcpy(pal->hash,packet.sender,20);
                        pal->socket = sock;
                        pal->state = ST_WF_SER_SHAKE;
                        //server.state = ST_WF_REG_DONE;
                        
                    } else if(server.state == ST_IDLING && reqt == DHT_REGISTER_DONE){
                        printf("received reg done\n");
                        //server.state = ST_IDLING;
                        /*dump old data and return to ST_IDLING*/
                    } else if(server.state == ST_IDLING && reqt == DHT_DEREGISTER_ACK){
                        printf("dereg ack received\n");
                        /*open connection to neighbours their urls are as
                         payload of the package and send data and
                         DHT_DEREGISTER_BEGIN (our hash as sender and target)
                         change to ST_WF_DEREG_DONE*/
                        uint16_t port_buf = ((uint16_t*)packet.payload)[0];
                        port_buf = ntohs(port_buf);
                        char port1[10];
                        sprintf(port1,"%u",port_buf);
                        char url1[30];
                        strcpy(url1,(char*)(packet.payload+2));
                        
                        port_buf = ((uint16_t*)(packet.payload+3+strlen(url1)))[0];
                        port_buf = ntohs(port_buf);
                        char port2[10];
                        sprintf(port2,"%u",port_buf);
                        char url2[30];
                        strcpy(url2,(char*)(packet.payload+5+strlen(url1)));
                        printf("parsing got:\nurl1 = %s:%s\nurl2 = %s:%s\n", url1, port1, url2, port2);
                        
                        //connect to right neighbour
                        right.socket = create_connection(url1,port1);
                        FD_SET(right.socket, &master);
                        if(right.socket > fdmax) fdmax = right.socket;
                        hash_url(url1,port1,right.hash);
                        right.state = ST_WF_SER_SHAKE;
                        //connect to left neighbour
                        left.socket = create_connection(url2,port2);
                        FD_SET(left.socket, &master);
                        if(left.socket > fdmax) fdmax = left.socket;
                        hash_url(url2,port2,left.hash);
                        left.state = ST_WF_SER_SHAKE;
                        
                        server.state = ST_WF_DEREG_DONE;
                    } else if(server.state == ST_IDLING && reqt == DHT_DEREGISTER_DENY){
                        printf("dereg deny received\n");
                        /*tell user that leaving is impossible
                         return to ST_IDLING*/
                        printf("Unable to exit. You are the last node. To over-ride this use exit force command.\n");
                        server.state = ST_IDLING;
                    } else if(server.state == ST_WF_DEREG_DONE && reqt == DHT_DEREGISTER_DONE){
                        if(right.state == ST_DEREG_DATA_SENT && !memcmp(right.hash, packet.sender, 20)){
                            FD_CLR(right.socket, &master);
                            close(right.socket);
                            memset(&right,0,sizeof right);
                        }
                        else if(left.state == ST_DEREG_DATA_SENT && !memcmp(left.hash, packet.sender, 20)){
                            FD_CLR(left.socket, &master);
                            close(left.socket);
                            memset(&left,0,sizeof left);
                        }
                        if(left.state == ST_EMPTY && right.state == ST_EMPTY){
                            printf("leaving the network\n");
                            close(server.socket);
                            FD_CLR(server.socket, &master);
                            memset(&server,0,sizeof server);
                            printf("left the network\n");
                        }
                        /*close the connection to server
                         state to ST_EMPTY*/
                        }
                    }
                }
            }
            for(int i = 0; i<2;i++){
                con_t* neighbour = (i == 0) ? &right : &left;
            //check for msg from right and left neighbour
            if(neighbour->state != ST_EMPTY && FD_ISSET(neighbour->socket, &rfds)){
                int status;
                if(neighbour->state == ST_WF_CLI_SHAKE){
                    /* prepare to receive data
                     switch state to ST_RCV_DATA_REG or
                     ST_RCV_DATA_DEREG depending on server state*/
                    status = recvdata(neighbour->socket, NULL);
                    if(status == NTW_CLIENT_SHAKE){
                        if(server.state == ST_ONLINE){
                            neighbour->state = ST_RCV_DATA_REG;
                        }
                        if(server.state == ST_IDLING){ 
                            neighbour->state = ST_RCV_DATA_DEREG;
                        }
                    } else {
                        switch(status){
                            case NTW_ERR_SERIOUS:
                                die(strerror(errno));
                                break;
                            case NTW_DISCONNECT:
                                printf("%s disconnected\n",(i)?"left":"right");
                                FD_CLR(neighbour->socket,&master);
                                memset(neighbour,0,sizeof *neighbour);
                                break;
                            default:
                                printf("getting shake failed somehow\n");
                                break;
                        }
                    }
                } else if (neighbour->state == ST_WF_SER_SHAKE){
                    /* answer with client shake and pump your data to the neighbour
                     after that send DHT_REGISTER_ACK or DHT_DEREG_BEGIN
                     either way after this the connection to neighbour can be disconnected*/
                    status = recvdata(neighbour->socket, NULL);
                    if(status == NTW_SERVER_SHAKE){
                        printf("shaking back\n");
                        send_shake(neighbour->socket, NTW_CLIENT_SHAKE);
                        if(server.state == ST_IDLING){ //we should end with DHT_REGISTER_ACK
                            send_dht_pckt(neighbour->socket,DHT_REGISTER_ACK,my_hash,my_hash,0,NULL);
                            FD_CLR(neighbour->socket, &master);
                            close(neighbour->socket);
                            memset(neighbour,0,sizeof *neighbour);
                        }
                        if(server.state == ST_WF_DEREG_DONE){ //we should end with DHT_DEREGISTER_BEGIN
                            send_dht_pckt(neighbour->socket,DHT_DEREGISTER_BEGIN,my_hash,my_hash,0,NULL);
                            FD_CLR(neighbour->socket, &master);
                            close(neighbour->socket);
                            neighbour->state = ST_DEREG_DATA_SENT;
                        }
                    } else {
                        switch(status){
                            case NTW_ERR_SERIOUS:
                                die(strerror(errno));
                                break;
                            case NTW_DISCONNECT:
                                die("neighbour disconnected");
                                break;
                            default:
                                printf("getting shake failed somehow\n");
                                break;
                        }
                    }
                    
                } else{
                    DHT_t packet;
                    memset(&packet, 0, sizeof packet);
                    status = recvdata(neighbour->socket, &packet);
                    printf("received package from %s:\n",(i)?"left":"right");
                    print_dht_packet(&packet);
                    if(status != NTW_PACKET_OK){
                        switch(status){
                            case NTW_ERR_SERIOUS:
                                die(strerror(errno));
                                break;
                            case NTW_DISCONNECT:
                                printf("%s neighbour disconnected.\n",(i)?"left":"right");
                                FD_CLR(neighbour->socket,&master);
                                close(neighbour->socket);
                                memset(neighbour,0,sizeof * neighbour);
                                break;
                            case NTW_ERR_TIMEOUT:
                                printf("package from %s timed out!\n",(i)?"left":"right");
                                memset(&packet,0,sizeof packet);
                                break;
                        }
                    }
                    uint16_t reqt = ntohs(packet.req_type);
                    if(neighbour->state == ST_RCV_DATA_REG && reqt == DHT_REGISTER_ACK){
                        /*all data is now received */
                        FD_CLR(neighbour->socket, &master);
                        close(neighbour->socket);
                        memset(neighbour,0,sizeof *neighbour);
                        neighbour->state = ST_REG_DATA_RCVD;
                        if(right.state == ST_REG_DATA_RCVD && left.state == ST_REG_DATA_RCVD){
                            send_dht_pckt(server.socket, DHT_REGISTER_DONE, my_hash, my_hash, 0, NULL);
                            right.state = ST_EMPTY;
                            left.state = ST_EMPTY;
                            server.state = ST_IDLING;
                        }
                    } else if(neighbour->state == ST_RCV_DATA_DEREG && reqt == DHT_DEREGISTER_BEGIN){
                        send_dht_pckt(server.socket, DHT_DEREGISTER_DONE, packet.sender, my_hash, 0, NULL);
                        FD_CLR(neighbour->socket, &master);
                        close(neighbour->socket);
                        memset(neighbour,0,sizeof *neighbour);
                    }
                }
            }
        }
    }

    close(listensock);

    return 0;
}
예제 #14
0
파일: control.c 프로젝트: each/bind9-collab
/*%
 * This function is called to process the incoming command
 * when a control channel message is received.
 */
isc_result_t
named_control_docommand(isccc_sexpr_t *message, isc_boolean_t readonly,
			isc_buffer_t **text)
{
	isccc_sexpr_t *data;
	char *cmdline = NULL;
	char *command = NULL;
	isc_result_t result;
	int log_level;
	isc_buffer_t src;
	isc_lex_t *lex = NULL;
#ifdef HAVE_LIBSCF
	named_smf_want_disable = 0;
#endif

	data = isccc_alist_lookup(message, "_data");
	if (!isccc_alist_alistp(data)) {
		/*
		 * No data section.
		 */
		return (ISC_R_FAILURE);
	}

	result = isccc_cc_lookupstring(data, "type", &cmdline);
	if (result != ISC_R_SUCCESS) {
		/*
		 * We have no idea what this is.
		 */
		return (result);
	}

	result = isc_lex_create(named_g_mctx, strlen(cmdline), &lex);
	if (result != ISC_R_SUCCESS)
		return (result);

	isc_buffer_init(&src, cmdline, strlen(cmdline));
	isc_buffer_add(&src, strlen(cmdline));
	result = isc_lex_openbuffer(lex, &src);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	result = getcommand(lex, &command);
	if (result != ISC_R_SUCCESS)
		goto cleanup;

	/*
	 * Compare the 'command' parameter against all known control commands.
	 */
	if (command_compare(command, NAMED_COMMAND_NULL) ||
	    command_compare(command, NAMED_COMMAND_STATUS))
	{
		log_level = ISC_LOG_DEBUG(1);
	} else {
		log_level = ISC_LOG_INFO;
	}

	/*
	 * If this listener should have read-only access, reject
	 * restricted commands here. rndc nta is handled specially
	 * below.
	 */
	if (readonly &&
	    !command_compare(command, NAMED_COMMAND_NTA) &&
	    !command_compare(command, NAMED_COMMAND_NULL) &&
	    !command_compare(command, NAMED_COMMAND_STATUS) &&
	    !command_compare(command, NAMED_COMMAND_SHOWZONE) &&
	    !command_compare(command, NAMED_COMMAND_TESTGEN) &&
	    !command_compare(command, NAMED_COMMAND_ZONESTATUS))
	{
		isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
			      NAMED_LOGMODULE_CONTROL, log_level,
			      "rejecting restricted control channel "
			      "command '%s'", cmdline);
		result = ISC_R_FAILURE;
		goto cleanup;
	}

	isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
		      NAMED_LOGMODULE_CONTROL, log_level,
		      "received control channel command '%s'",
		      cmdline);

	if (command_compare(command, NAMED_COMMAND_RELOAD)) {
		result = named_server_reloadcommand(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_RECONFIG)) {
		result = named_server_reconfigcommand(named_g_server);
	} else if (command_compare(command, NAMED_COMMAND_REFRESH)) {
		result = named_server_refreshcommand(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_RETRANSFER)) {
		result = named_server_retransfercommand(named_g_server,
							lex, text);
	} else if (command_compare(command, NAMED_COMMAND_HALT)) {
#ifdef HAVE_LIBSCF
		/*
		 * If we are managed by smf(5), AND in chroot, then
		 * we cannot connect to the smf repository, so just
		 * return with an appropriate message back to rndc.
		 */
		if (named_smf_got_instance == 1 && named_smf_chroot == 1) {
			result = named_smf_add_message(text);
			goto cleanup;
		}
		/*
		 * If we are managed by smf(5) but not in chroot,
		 * try to disable ourselves the smf way.
		 */
		if (named_smf_got_instance == 1 && named_smf_chroot == 0)
			named_smf_want_disable = 1;
		/*
		 * If named_smf_got_instance = 0, named_smf_chroot
		 * is not relevant and we fall through to
		 * isc_app_shutdown below.
		 */
#endif
		/* Do not flush master files */
		named_server_flushonshutdown(named_g_server, ISC_FALSE);
		named_os_shutdownmsg(cmdline, *text);
		isc_app_shutdown();
		result = ISC_R_SUCCESS;
	} else if (command_compare(command, NAMED_COMMAND_STOP)) {
		/*
		 * "stop" is the same as "halt" except it does
		 * flush master files.
		 */
#ifdef HAVE_LIBSCF
		if (named_smf_got_instance == 1 && named_smf_chroot == 1) {
			result = named_smf_add_message(text);
			goto cleanup;
		}
		if (named_smf_got_instance == 1 && named_smf_chroot == 0)
			named_smf_want_disable = 1;
#endif
		named_server_flushonshutdown(named_g_server, ISC_TRUE);
		named_os_shutdownmsg(cmdline, *text);
		isc_app_shutdown();
		result = ISC_R_SUCCESS;
	} else if (command_compare(command, NAMED_COMMAND_DUMPSTATS)) {
		result = named_server_dumpstats(named_g_server);
	} else if (command_compare(command, NAMED_COMMAND_QUERYLOG)) {
		result = named_server_togglequerylog(named_g_server, lex);
	} else if (command_compare(command, NAMED_COMMAND_DUMPDB)) {
		named_server_dumpdb(named_g_server, lex, text);
		result = ISC_R_SUCCESS;
	} else if (command_compare(command, NAMED_COMMAND_SECROOTS)) {
		result = named_server_dumpsecroots(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_TRACE)) {
		result = named_server_setdebuglevel(named_g_server, lex);
	} else if (command_compare(command, NAMED_COMMAND_NOTRACE)) {
		named_g_debuglevel = 0;
		isc_log_setdebuglevel(named_g_lctx, named_g_debuglevel);
		result = ISC_R_SUCCESS;
	} else if (command_compare(command, NAMED_COMMAND_FLUSH)) {
		result = named_server_flushcache(named_g_server, lex);
	} else if (command_compare(command, NAMED_COMMAND_FLUSHNAME)) {
		result = named_server_flushnode(named_g_server, lex, ISC_FALSE);
	} else if (command_compare(command, NAMED_COMMAND_FLUSHTREE)) {
		result = named_server_flushnode(named_g_server, lex, ISC_TRUE);
	} else if (command_compare(command, NAMED_COMMAND_STATUS)) {
		result = named_server_status(named_g_server, text);
	} else if (command_compare(command, NAMED_COMMAND_TSIGLIST)) {
		result = named_server_tsiglist(named_g_server, text);
	} else if (command_compare(command, NAMED_COMMAND_TSIGDELETE)) {
		result = named_server_tsigdelete(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_FREEZE)) {
		result = named_server_freeze(named_g_server, ISC_TRUE, lex,
					     text);
	} else if (command_compare(command, NAMED_COMMAND_UNFREEZE) ||
		   command_compare(command, NAMED_COMMAND_THAW)) {
		result = named_server_freeze(named_g_server, ISC_FALSE, lex,
					     text);
	} else if (command_compare(command, NAMED_COMMAND_SCAN)) {
		result = ISC_R_SUCCESS;
		named_server_scan_interfaces(named_g_server);
	} else if (command_compare(command, NAMED_COMMAND_SYNC)) {
		result = named_server_sync(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_RECURSING)) {
		result = named_server_dumprecursing(named_g_server);
	} else if (command_compare(command, NAMED_COMMAND_TIMERPOKE)) {
		result = ISC_R_SUCCESS;
		isc_timermgr_poke(named_g_timermgr);
	} else if (command_compare(command, NAMED_COMMAND_NULL)) {
		result = ISC_R_SUCCESS;
	} else if (command_compare(command, NAMED_COMMAND_NOTIFY)) {
		result = named_server_notifycommand(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_VALIDATION)) {
		result = named_server_validation(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_SIGN) ||
		   command_compare(command, NAMED_COMMAND_LOADKEYS)) {
		result = named_server_rekey(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_ADDZONE) ||
		   command_compare(command, NAMED_COMMAND_MODZONE)) {
		result = named_server_changezone(named_g_server, cmdline, text);
	} else if (command_compare(command, NAMED_COMMAND_DELZONE)) {
		result = named_server_delzone(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_SHOWZONE)) {
		result = named_server_showzone(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_SIGNING)) {
		result = named_server_signing(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_ZONESTATUS)) {
		result = named_server_zonestatus(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_NTA)) {
		result = named_server_nta(named_g_server, lex, readonly, text);
	} else if (command_compare(command, NAMED_COMMAND_TESTGEN)) {
		result = named_server_testgen(lex, text);
	} else if (command_compare(command, NAMED_COMMAND_MKEYS)) {
		result = named_server_mkeys(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_DNSTAP) ||
		   command_compare(command, NAMED_COMMAND_DNSTAPREOPEN)) {
		result = named_server_dnstap(named_g_server, lex, text);
	} else if (command_compare(command, NAMED_COMMAND_TCPTIMEOUTS)) {
		result = named_server_tcptimeouts(lex, text);
	} else if (command_compare(command, NAMED_COMMAND_SERVESTALE)) {
		result = named_server_servestale(named_g_server, lex, text);
	} else {
		isc_log_write(named_g_lctx, NAMED_LOGCATEGORY_GENERAL,
			      NAMED_LOGMODULE_CONTROL, ISC_LOG_WARNING,
			      "unknown control channel command '%s'",
			      command);
		result = DNS_R_UNKNOWNCOMMAND;
	}

 cleanup:
	if (lex != NULL)
		isc_lex_destroy(&lex);

	return (result);
}
예제 #15
0
파일: main.c 프로젝트: jyin0813/OpenBSD-src
int
main(int ac, char *av[])
{
	int			f_usage = 0, f_list = 0, f_showscore = 0;
	int			f_printpath = 0;
	const char		*file = NULL;
	char			*name, *ptr, *seed;
	struct sigaction	sa;
	gid_t			gid;
#ifdef BSD
	struct itimerval	itv;
#endif

	open_score_file();

	/* revoke privs */
	gid = getgid();
	setresgid(gid, gid, gid);

	start_time = time(0);
	makenoise = 1;
	seed = NULL;

	name = *av++;
	while (*av) {
#ifndef SAVEDASH
		if (**av == '-') 
			++*av;
		else
			break;
#endif
		ptr = *av++;
		while (*ptr) {
			switch (*ptr) {
			case '?':
			case 'u':
				f_usage++;
				break;
			case 'l':
				f_list++;
				break;
			case 's':
			case 't':
				f_showscore++;
				break;
			case 'p':
				f_printpath++;
				break;
			case 'q':
				makenoise = 0;
				break;
			case 'r':
				seed = *av;
				av++;
				break;
			case 'f':
			case 'g':
				file = *av;
				av++;
				break;
			default: 
				warnx("unknown option '%c'", *ptr);
				f_usage++;
				break;
			}
			ptr++;
		}
	}
	if (seed != NULL)
		srandom(atol(seed));
	else
		srandomdev();

	if (f_usage)
		fprintf(stderr, 
		    "usage: %s [-lpqstu?] [-f game] [-g game] [-r seed]\n",
		    name);
	if (f_showscore)
		log_score(1);
	if (f_list)
		list_games();
	if (f_printpath) {
		size_t	len;
		char	buf[256];

		strlcpy(buf, _PATH_GAMES, sizeof buf);
		len = strlen(buf);
		if (len != 0 && buf[len - 1] == '/')
			buf[len - 1] = '\0';
		puts(buf);
	}
		
	if (f_usage || f_showscore || f_list || f_printpath)
		exit(0);

	if (file == NULL)
		file = default_game();
	else
		file = okay_game(file);

	if (file == NULL || read_file(file) < 0)
		exit(1);

	setup_screen(sp);

	addplane();

	signal(SIGINT, quit);
	signal(SIGQUIT, quit);
#ifdef BSD
	signal(SIGTSTP, SIG_IGN);
	signal(SIGSTOP, SIG_IGN);
#endif
	signal(SIGHUP, log_score_quit);
	signal(SIGTERM, log_score_quit);

	tcgetattr(fileno(stdin), &tty_start);
	tty_new = tty_start;
	tty_new.c_lflag &= ~(ICANON|ECHO);
	tty_new.c_iflag |= ICRNL;
	tty_new.c_cc[VMIN] = 1;
	tty_new.c_cc[VTIME] = 0;
	tcsetattr(fileno(stdin), TCSADRAIN, &tty_new);

	memset(&sa, 0, sizeof sa);
	sa.sa_handler = update;
	sigemptyset(&sa.sa_mask);
	sigaddset(&sa.sa_mask, SIGALRM);
	sigaddset(&sa.sa_mask, SIGINT);
	sa.sa_flags = 0;
	sigaction(SIGALRM, &sa, (struct sigaction *)0);

#ifdef BSD
	itv.it_value.tv_sec = 0;
	itv.it_value.tv_usec = 1;
	itv.it_interval.tv_sec = sp->update_secs;
	itv.it_interval.tv_usec = 0;
	setitimer(ITIMER_REAL, &itv, NULL);
#endif
#ifdef SYSV
	alarm(sp->update_secs);
#endif

	for (;;) {
		if (getcommand() != 1)
			planewin();
		else {
#ifdef BSD
			itv.it_value.tv_sec = 0;
			itv.it_value.tv_usec = 0;
			setitimer(ITIMER_REAL, &itv, NULL);
#endif
#ifdef SYSV
			alarm(0);
#endif

			update(0);

#ifdef BSD
			itv.it_value.tv_sec = sp->update_secs;
			itv.it_value.tv_usec = 0;
			itv.it_interval.tv_sec = sp->update_secs;
			itv.it_interval.tv_usec = 0;
			setitimer(ITIMER_REAL, &itv, NULL);
#endif
#ifdef SYSV
			alarm(sp->update_secs);
#endif
		}
	}
}
예제 #16
0
파일: entity.c 프로젝트: beoran/kq-fork
/*! \brief Run script
 *
 * This executes script commands.  This is from Verge1.
 *
 * \param   target_entity Entity to process
 */
static void entscript(t_entity target_entity)
{
    if (g_ent[target_entity].active == 0)
    {
        return;
    }
    if (g_ent[target_entity].cmd == 0)
    {
        getcommand(target_entity);
    }
    switch (g_ent[target_entity].cmd)
    {
        case COMMAND_MOVE_UP:
            if (move(target_entity, 0, -1))
            {
                g_ent[target_entity].cmdnum--;
            }
            break;
        case COMMAND_MOVE_DOWN:
            if (move(target_entity, 0, 1))
            {
                g_ent[target_entity].cmdnum--;
            }
            break;
        case COMMAND_MOVE_LEFT:
            if (move(target_entity, -1, 0))
            {
                g_ent[target_entity].cmdnum--;
            }
            break;
        case COMMAND_MOVE_RIGHT:
            if (move(target_entity, 1, 0))
            {
                g_ent[target_entity].cmdnum--;
            }
            break;
        case COMMAND_WAIT:
            g_ent[target_entity].cmdnum--;
            break;
        case COMMAND_FINISH_COMMANDS:
            return;
        case COMMAND_REPEAT:
            g_ent[target_entity].sidx = 0;
            g_ent[target_entity].cmdnum = 0;
            break;
        case COMMAND_MOVETO_X:
            if (g_ent[target_entity].tilex < g_ent[target_entity].cmdnum)
            {
                move(target_entity, 1, 0);
            }
            if (g_ent[target_entity].tilex > g_ent[target_entity].cmdnum)
            {
                move(target_entity, -1, 0);
            }
            if (g_ent[target_entity].tilex == g_ent[target_entity].cmdnum)
            {
                g_ent[target_entity].cmdnum = 0;
            }
            break;
        case COMMAND_MOVETO_Y:
            if (g_ent[target_entity].tiley < g_ent[target_entity].cmdnum)
            {
                move(target_entity, 0, 1);
            }
            if (g_ent[target_entity].tiley > g_ent[target_entity].cmdnum)
            {
                move(target_entity, 0, -1);
            }
            if (g_ent[target_entity].tiley == g_ent[target_entity].cmdnum)
            {
                g_ent[target_entity].cmdnum = 0;
            }
            break;
        case COMMAND_FACE:
            g_ent[target_entity].facing = g_ent[target_entity].cmdnum;
            g_ent[target_entity].cmdnum = 0;
            break;
    }
    if (g_ent[target_entity].cmdnum == 0)
    {
        g_ent[target_entity].cmd = 0;
    }
}
예제 #17
0
파일: main.c 프로젝트: Gwenio/DragonFlyBSD
int
main(int argc __unused, char *argv[])
{
	int                     seed = 0;
	int			f_usage = 0, f_list = 0, f_showscore = 0;
	int			f_printpath = 0;
	const char		*file = NULL;
	char			*p_name, *ptr;
	struct itimerval	itv;

	/* Open the score file then revoke setgid privileges */
	open_score_file();
	setregid(getgid(), getgid());

	start_time = time(0);

	p_name = *argv++;
	while (*argv) {
#ifndef SAVEDASH
		if (**argv == '-')
			++*argv;
		else
			break;
#endif
		ptr = *argv++;
		while (*ptr) {
			switch (*ptr) {
			case '?':
			case 'u':
				f_usage++;
				break;
			case 'l':
				f_list++;
				break;
			case 's':
			case 't':
				f_showscore++;
				break;
			case 'p':
				f_printpath++;
				break;
			case 'r':
				srandom(atoi(*argv));
				seed = 1;
				argv++;
				break;
			case 'f':
			case 'g':
				file = *argv;
				argv++;
				break;
			default:
				fprintf(stderr, "Unknown option '%c'\n", *ptr);
				f_usage++;
				break;
			}
			ptr++;
		}
	}
	if (!seed)
		srandomdev();

	if (f_usage)
		fprintf(stderr,
		    "Usage: %s -[u?lstp] [-[gf] game_name] [-r random seed]\n",
			p_name);
	if (f_showscore)
		log_score(1);
	if (f_list)
		list_games();
	if (f_printpath) {
		char	buf[100];

		strcpy(buf, _PATH_GAMES);
		buf[strlen(buf) - 1] = '\0';
		puts(buf);
	}

	if (f_usage || f_showscore || f_list || f_printpath)
		exit(0);

	if (file == NULL)
		file = default_game();
	else
		file = okay_game(file);

	if (file == NULL || read_file(file) < 0)
		exit(1);

	init_gr();
	setup_screen(sp);

	addplane();

	signal(SIGINT, (sig_t)quit);
	signal(SIGQUIT, (sig_t)quit);
	signal(SIGTSTP, SIG_IGN);
	signal(SIGSTOP, SIG_IGN);
	signal(SIGHUP, (sig_t)log_score);
	signal(SIGTERM, (sig_t)log_score);

	tcgetattr(fileno(stdin), &tty_start);
	bcopy(&tty_start, &tty_new, sizeof(tty_new));
	tty_new.c_lflag &= ~(ICANON|ECHO);
	tty_new.c_cc[VMIN] = 1;
	tty_new.c_cc[VTIME] = 0;
	tcsetattr(fileno(stdin), TCSANOW, &tty_new);
	signal(SIGALRM, (sig_t)update);

	itv.it_value.tv_sec = 0;
	itv.it_value.tv_usec = 1;
	itv.it_interval.tv_sec = sp->update_secs;
	itv.it_interval.tv_usec = 0;
	setitimer(ITIMER_REAL, &itv, NULL);

	for (;;) {
		if (getcommand() != 1)
			planewin();
		else {
			itv.it_value.tv_sec = 0;
			itv.it_value.tv_usec = 0;
			setitimer(ITIMER_REAL, &itv, NULL);

			update();

			itv.it_value.tv_sec = sp->update_secs;
			itv.it_value.tv_usec = 0;
			itv.it_interval.tv_sec = sp->update_secs;
			itv.it_interval.tv_usec = 0;
			setitimer(ITIMER_REAL, &itv, NULL);
		}
	}
}