예제 #1
0
파일: test.c 프로젝트: firecrow/libfirecrow
int test(){
  int ret = 0;
  write_cstr(1, ">>> test >>>\n");
  struct arr *a = arr_alloc(4);
  arr_insert(a, 0, "this", 4);
  ret |= test_print_eq(a, "this", "arr 0");
  arr_insert(a, a->c, " is", 3);
  test_print_eq(a, "this is", "arr 0.1");
  arr_insert(a, 0, "hi ", 3);
  test_print_eq(a, "hi this is", "arr 0.2");
  arr_insert(a, 0, "this is a big string being added. ", strlen("this is a big string being added. "));
  test_print_eq(a, "this is a big string being added. hi this is", "arr 1");
  struct arr *b = arr_alloc(4);
  arr_insert(b, 0, "starting:", strlen("starting:"));
  test_print_eq(b, "starting:", "arr 2");
  arr_append_int_str(b, 3);
  test_print_eq(b, "starting:3", "arr 3");
  arr_append_int_str(b, 40012);
  ret |= test_print_eq(b, "starting:340012", "arr 4");
  struct arr *c = arr_alloc(4);
  arr_append(c, "h");
  arr_append(c, "i");
  test_print_eq(c, "hi", "arr 5");
  write(1, "\n", 1);
  return ret;
}
예제 #2
0
파일: test.c 프로젝트: firecrow/libfirecrow
int test_uarr(){
  int ret = 0;
  write_cstr(1, ">>> test_uarr >>>\n");
  int l;

  /* test insert & append */
  struct arr *msg = arr_alloc(16);
  struct uarr *p = uarr_alloc(4, sizeof(int));
  int iarr[] = {1,2,3,4};
  uarr_insert(p, uarr_count(p), &iarr[0], 1);
  uarr_insert(p, uarr_count(p), &iarr[1], 1);
  uarr_append(p, &iarr[2]);
  uarr_append(p, &iarr[3]);
  int *ip = (int *)p->v;
  l = uarr_count(p);
  while(l--){
    arr_append_int_str(msg, *ip);
    arr_insert(msg, msg->c, ",", 1);
    ip++;
  }
  ret |= test_print_eq(msg, "1,2,3,4,", "uarr 1");
  arr_free(msg);
  uarr_free(p);
  write(1, "\n", 1);

  /* test append */
  struct arr *msg2 = arr_alloc(16);
  struct uarr *p2 = uarr_alloc(4, sizeof(int));
  int iarr2[] = {1,2,3,4};
  uarr_append(p2, &iarr2[0]);
  uarr_append(p2, &iarr2[1]);
  uarr_append(p2, &iarr2[2]);
  uarr_append(p2, &iarr2[3]);
  int *ip2 = (int *)p2->v;
  l = uarr_count(p2);
  while(l--){
    arr_append_int_str(msg2, *ip2);
    arr_insert(msg2, msg2->c, ",", 1);
    ip2++;
  }
  ret |= test_print_eq(msg2, "1,2,3,4,", "uarr 2");
  arr_free(msg2);
  uarr_free(p2);
  write(1, "\n", 1);
  ret |= test_uarr_ptr();
  return ret;
}
예제 #3
0
파일: tapio.c 프로젝트: lukaszle/vpnbox
int main(int argc, char **argv)
{
    char buf[BUF_SIZE], dev[IFNAMSIZ]="/dev/net/tun", tapdev[IFNAMSIZ], *progname;
    int tap, r, tun = IFF_TUN;
    int c, verbose = 0, interval = 0, keep = 0, keep_count = 0;
    time_t last_keep;
    struct timeval tv;
    struct rlimit rlim;
    fd_set rfd;

    while ( (c=getopt(argc,argv,"vi:k:")) != -1 ) {
        switch (c) {
            case 'v':
                verbose++;
                break;
            case 'i':
                interval = atoi(optarg);
                break;
            case 'k':
                keep = atoi(optarg);
                break;
        }
    }

    if ((progname = strrchr(argv[0], '/')) == NULL) {
        progname = argv[0];
    } else {
        progname++;
    }

    if (keep && !interval) {
        write_str(STDERR_FILENO, progname);
        write_cstr(STDERR_FILENO, ": keep alive need interval value.\n");
        return 1;
    }

    signal(SIGPIPE,signalHandler);

    if (strcmp("tunio", progname)) tun = IFF_TAP;

    if (argc>optind) {
        if (!strncmp(argv[optind],"/dev/",5)) argv[optind] += 5;
        if (verbose) {
            write_str(STDERR_FILENO, progname);
            write_cstr(STDERR_FILENO, ": Forced to ");
            write_str(STDERR_FILENO,argv[optind]);
            write_cstr(STDERR_FILENO, "\n");
        }
        strstart(tapdev);
        strarray(tapdev);
        strannex(tapdev,argv[optind]);
        tap = tap_alloc (dev, tapdev, tun);
    } else {
        strstart(tapdev);
        for (r=0 ; r<1000 ; r++) {
            strarray(tapdev);
            if (tun == IFF_TUN) {
                strannex(tapdev, "tun");
            } else {
                strannex(tapdev, "tap");
            }
            strannex_uint(tapdev, r);
            tap = tap_alloc (dev, tapdev, tun);
            if (tap > 0) break; // success
        }
    }
    if (tap <= 0) {
        write_str(STDERR_FILENO, progname);
        if (tun == IFF_TUN) {
            write_cstr(STDERR_FILENO, ": Cannot open TUN\n");
        } else {
            write_cstr(STDERR_FILENO, ": Cannot open TAP\n");
        }
        return 1;
    }

    gettimeofday(&tv, NULL);
    last_keep = tv.tv_sec;

    rlim.rlim_cur = 0;
    setrlimit(RLIMIT_NOFILE, &rlim);

    /* ------------ main loop without length ---------- */
    for (;;) {
        FD_ZERO(&rfd);
        FD_SET(STDIN_FILENO, &rfd);
        FD_SET(tap,          &rfd);
        tv.tv_sec  = 1;
        tv.tv_usec = 0;
        r = select(tap+1,&rfd,NULL,NULL,&tv);
        if(r == -1) {
            write_str(STDERR_FILENO, progname);
            write_cstr(STDERR_FILENO, ": select() error.\n");
            return errno;
        }
        if (interval) {
            gettimeofday(&tv, NULL);
            if (verbose && (tv.tv_sec % 10)==0) {
                write_str(STDERR_FILENO, progname);
                write_str_uint(STDERR_FILENO, ": keep count: ", keep_count);
                writeln_str_uint(STDERR_FILENO, ", keep: ", keep);
            }
            if (tv.tv_sec >= (last_keep+interval)) {
                write(STDOUT_FILENO, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0", 20);
                if (verbose) {
                    write_str(STDERR_FILENO, progname);
                    write_cstr(STDERR_FILENO, ": Sent a keep alive.\n");
                }
                last_keep = tv.tv_sec;
            }
        }
        if (keep) {
            keep_count++;
            if (verbose) {
                write_str(STDERR_FILENO, progname);
                writeln_str_uint(STDERR_FILENO, ": keep count increased: ", keep_count);
            }
        }

        if (FD_ISSET(STDIN_FILENO,&rfd)) {
            r = read(STDIN_FILENO, buf, BUF_SIZE);
            if (r > 0 && keep) { // reset count on read.
                keep_count = 0;
                if (verbose) {
                    write_str(STDERR_FILENO, progname);
                    write_cstr(STDERR_FILENO, ": data received, keep count reseted.\n");
                }
            }
            if (r > 20) { // ignore packet inferior to 21 bytes, keep alive
                write(tap, buf, r);
            } else if (r <= 0) {
                if (verbose) {
                    write_str(STDERR_FILENO, progname);
                    write_cstr(STDERR_FILENO, ": stdin error.\n");
                }
                return 1;
            }
        }
        if (FD_ISSET(tap,&rfd)) {
            r = read(tap, buf, BUF_SIZE);
            if (r > 0) {
                write(STDOUT_FILENO, buf, r);
            } else if (r <= 0) {
                if (verbose) {
                    write_str(STDERR_FILENO, progname);
                    write_cstr(STDERR_FILENO, ": stdout error.\n");
                }
                return 2;
            }
        }
        if (keep && keep_count >= keep) {
            write_str(STDERR_FILENO, progname);
            writeln_str_uint(STDERR_FILENO, ": Keep count exceeded ", keep_count);
            kill(getppid(), SIGPIPE);
            return 0;
        }
    }
    return 0;
}
예제 #4
0
int main(int argc, char **argv)
{
    int cr = 0, cs = 0;
    char buf[BUF_SIZE];
    char fifo[MULT*BUF_SIZE], *progname;
    char *head  = fifo, *tail  = fifo;
    char *head2 = NULL;
    unsigned int len2 = 0;
    int fd, r, pipe_in[2], pipe_out[2];
    struct rlimit rlim;

    if ((progname = strrchr(argv[0], '/')) == NULL) {
        progname = argv[0];
    } else {
        progname++;
    }

    if (argc < 2) {
        write_str(STDERR_FILENO, progname);
        write_cstr(STDERR_FILENO,": command missing\n");
        return -1;
    }

    if (pipe2(pipe_in,  O_DIRECT) == -1) {
        write_str(STDERR_FILENO, progname);
        write_cstr(STDERR_FILENO,": Warning, pipe2 pipe_in failed, using pipe.\n");
        pipe(pipe_in);
    }
    if (pipe2(pipe_out, O_DIRECT) == -1) {
        write_str(STDERR_FILENO, progname);
        write_cstr(STDERR_FILENO,": Warning, pipe2 pipe_out failed, using pipe.\n");
        pipe(pipe_out);
    }

    signal(SIGCHLD,signalHandler);
    signal(SIGPIPE,signalHandler);

    r = fork();
    switch(r) {
        case -1:
            write_str(STDERR_FILENO, progname);
            write_cstr(STDERR_FILENO,": unable to fork.\n");
            return(-1);
        case 0:
            r = fork();
            switch(r) {
                case -1:
                    write_str(STDERR_FILENO, progname);
                    write_cstr(STDERR_FILENO,": unable to fork.\n");
                    return(-1);
                case 0: // children to deal with encapsulation.
                    close(pipe_in [STDIN_FILENO]);
                    close(pipe_in [STDOUT_FILENO]);
                    close(pipe_out[STDOUT_FILENO]);
                    close(STDIN_FILENO);
                    memcpy(argv[0], "bundle", 6);
                    char *a = argv[0]+6;
                    while(*a) *a++ = ' ';

                    rlim.rlim_cur = 0;
                    setrlimit(RLIMIT_NOFILE, &rlim);
                    for(;;) {
                        r = read(pipe_out[STDIN_FILENO], buf+2, BUF_SIZE-6);
                        if (r > 0) {
                            unsigned short len = htons(r);
                            unsigned short *p  = (unsigned short *)buf;
                            p[0] = len;
                            write(STDOUT_FILENO, buf, r+2);
                        } else {
                            return 2;
                        }
                    }
                    return 0;
            }

            // children to deal with exec.
            if (dup2(pipe_in[STDIN_FILENO], STDIN_FILENO) == -1) {
                write_str(STDERR_FILENO, progname);
                write_cstr(STDERR_FILENO,": unable to move descriptor to stdin.\n");
                return(-1);
            }
            close(pipe_in[STDOUT_FILENO]);
            if (dup2(pipe_out[STDOUT_FILENO], STDOUT_FILENO) == -1) {
                write_str(STDERR_FILENO, progname);
                write_cstr(STDERR_FILENO,": unable to move descriptor to stdout.\n");
                return(-1);
            }
            close(pipe_out[STDIN_FILENO]);
            return(execvp(argv[1], argv+1));
    }
    close(pipe_in [STDIN_FILENO]);
    close(pipe_out[STDIN_FILENO]);
    close(pipe_out[STDOUT_FILENO]);
    close(STDOUT_FILENO);

    // first loop deal with decapsulation.

    rlim.rlim_cur = 0;
    setrlimit(RLIMIT_NOFILE, &rlim);

    for(;;) {
        if (tail + BUF_SIZE >= fifo+sizeof(fifo)) {
            write_str(STDERR_FILENO, progname);
            write_cstr(2,": Might not fit!\n"); return 3;
        }
        r = read(STDIN_FILENO, tail, BUF_SIZE-2);
        if (r>0) {
            tail += r;
        } else {
            return 1;
        }
        for (;;) {
            int n = tail - head + len2;
            if (n<=2) break;
            char *p = head2 ? head2 : head;
            unsigned short len;
            union c_short {
               short s;
               char c[2];
            } s;
            s.c[0] = *p++;
            s.c[1] = (len2==1) ? *head : *p;
            len = ntohs(s.s);
            if (len > (BUF_SIZE-2)) len = BUF_SIZE-2;
            if (n<len+2) break;
            if (len2) {
                if (len2<2) {
                    write(pipe_in[STDOUT_FILENO],head+1,len);
                    head += len+1;
                    head2 = NULL;
                    len2  = 0;
                } else {
                    const struct iovec vec[2] = {
                        { head2+2, len2-2      },
                        { head,    len-len2+2  }
                    };
                    writev(pipe_in[STDOUT_FILENO],vec,2);
                    head += len-len2+2;
                    head2 = NULL;
                    len2  = 0;
                }
            } else {
                write(pipe_in[STDOUT_FILENO],head+2,len);
                head += len+2;
            }
        }
        if (head == tail) {
            head = tail = fifo;
        } else {
            if (head > fifo+2*BUF_SIZE) {
                head2 = head;
                len2  = tail - head;
                head  = tail = fifo;
            }
        }
    }
    return 0;
}
예제 #5
0
파일: Serial.cpp 프로젝트: SysMan-One/es40
void CSerial::WaitForConnection()
{
  struct sockaddr_in  Address;
  socklen_t           nAddressSize = sizeof(struct sockaddr_in);
  const char*               telnet_options = "%c%c%c";
  char                buffer[8];
  char                s[1000];
  char*               nargv = s;
  int                 i = 0;

#if !defined(LS_SLAVE)
  char                s2[200];
  char*               argv[20];

  strncpy(s, myCfg->get_text_value("action", ""), 999);
  s[999] = '\0';

  //printf("%s: Specified : %s\n",devid_string,s);
  if(strcmp(s, ""))
  {

    // spawn external program (telnet client)...
    while(*nargv)
    {
      argv[i] = nargv;
      if(nargv[0] == '\"')
        nargv = strchr(nargv + 1, '\"');
      if(nargv)
        nargv = strchr(nargv, ' ');
      if(!nargv)
        break;
      *nargv++ = '\0';
      i++;
      argv[i] = NULL;
    }

    argv[i + 1] = NULL;
    strcpy(s2, argv[0]);
    nargv = s2;
    if(nargv[0] == '\"')
    {
      nargv++;
      *(strchr(nargv, '\"')) = '\0';
    }

    //printf("%s: Starting %s\n", devid_string,nargv);
#if defined(_WIN32)
    if (_spawnvp(_P_NOWAIT, nargv, argv) < 0)
        FAILURE_1(Runtime,"Exec of '%s' has failed.\n", argv[0]);
#elif !defined(__VMS)
    pid_t child;
    int   status;
    if(!(child = fork()))
    {
      execvp(argv[0], argv);
      FAILURE_1(Runtime,"Exec of '%s' failed.\n", argv[0]);
    }
    else
    {
      sleep(1); // give it a chance to start up.
      waitpid(child, &status, WNOHANG); // reap it, if needed.
      if(kill(child, 0) < 0)
      { // uh oh, no kiddo.
        FAILURE_1(Runtime,"Exec of '%s' has failed.\n", argv[0]);
      }
    }
#endif
  }
#endif
  Address.sin_addr.s_addr = INADDR_ANY;
  Address.sin_port = htons((u16) listenPort);
  Address.sin_family = AF_INET;

  //  Wait until we have a connection
  connectSocket = INVALID_SOCKET;
  while(connectSocket == INVALID_SOCKET)
  {
    connectSocket = (int) accept(listenSocket, (struct sockaddr*) &Address,
                                 &nAddressSize);
  }

  state.serial_cycles = 0;

  if (state.iNumber != 1)
  {
    // Send some control characters to the telnet client to handle
    // character-at-a-time mode.
    sprintf(buffer, telnet_options, IAC, DO, TELOPT_ECHO);
    write_cstr(buffer);

    sprintf(buffer, telnet_options, IAC, DO, TELOPT_NAWS);
    write_cstr(buffer);

    sprintf(buffer, telnet_options, IAC, DO, TELOPT_LFLOW);
    write_cstr(buffer);

    sprintf(buffer, telnet_options, IAC, WILL, TELOPT_ECHO);
    write_cstr(buffer);

    sprintf(buffer, telnet_options, IAC, WILL, TELOPT_SGA);
    write_cstr(buffer);

    sprintf(s, "This is serial port #%d on ES40 Emulator\r\n", state.iNumber);
    write_cstr(s);
  }
}
예제 #6
0
파일: Serial.cpp 프로젝트: SysMan-One/es40
void CSerial::serial_menu()
{
  fd_set          readset;
  unsigned char   buffer[FIFO_SIZE + 1];
  ssize_t         size;
  struct timeval  tv;
  bool            exitLoop = false;

  cSystem->stop_threads();

  write_cstr("\r\n<BREAK> received. What do you want to do?\r\n");
  write_cstr("     0. Continue\r\n");
#if defined(IDB)
  write_cstr("     1. End run\r\n");
#else
  write_cstr("     1. Exit emulator gracefully\r\n");
  write_cstr("     2. Abort emulator (no changes saved)\r\n");
  write_cstr("     3. Save state to autosave.axp and continue\r\n");
  write_cstr("     4. Load state from autosave.axp and continue\r\n");
#endif
  while(!exitLoop)
  {
    FD_ZERO(&readset);
    FD_SET(connectSocket, &readset);
    tv.tv_sec = 60;
    tv.tv_usec = 0;
    if(select(connectSocket + 1, &readset, NULL, NULL, &tv) <= 0)
    {
      write_cstr("%SRL-I-TIMEOUT: no timely answer received. Continuing emulation.\r\n");
      break;  // leave loop
    }

#if defined(_WIN32) || defined(__VMS)
    size = recv(connectSocket, (char*) buffer, FIFO_SIZE, 0);
#else
    size = read(connectSocket, &buffer, FIFO_SIZE);
#endif
    switch(buffer[0])
    {
    case '0':
      write_cstr("%SRL-I-CONTINUE: continuing emulation.\r\n");
      exitLoop = true;
      break;

    case '1':
      write_cstr("%SRL-I-EXIT: exiting emulation gracefully.\r\n");
      FAILURE(Graceful, "Graceful exit");
      exitLoop = true;
      break;

    case '2':
      write_cstr("%SRL-I-ABORT: aborting emulation.\r\n");
      FAILURE(Abort, "Aborting");
      exitLoop = true;
      break;

    case '3':
      write_cstr("%SRL-I-SAVESTATE: Saving state to autosave.axp.\r\n");
      cSystem->SaveState("autosave.axp");
      write_cstr("%SRL-I-CONTINUE: continuing emulation.\r\n");
      exitLoop = true;
      break;

    case '4':
      write_cstr("%SRL-I-LOADSTATE: Loading state from autosave.axp.\r\n");
      cSystem->RestoreState("autosave.axp");
      write_cstr("%SRL-I-CONTINUE: continuing emulation.\r\n");
      exitLoop = true;
      break;

    default:
      write_cstr("%SRL-W-INVALID: Not a valid answer.\r\n");
    }
  }

  breakHit = false;
  cSystem->start_threads();
}
예제 #7
0
파일: test.c 프로젝트: firecrow/libfirecrow
int test_table(){
  write_cstr(1, ">>> table >>>\n");
	struct table *tbl;
	tbl = table_alloc();
	int hash = 0;
	puts("--- testing hash ---");
	hash = hash_key(tbl, "hi");
	printf("result hi %d\n", hash);
	hash = hash_key(tbl, "hello");
	printf("result hello %d\n", hash);
	hash = hash_key(tbl, "denial");
	printf("result denial %d\n", hash);
	hash = hash_key(tbl, "I was a donkey once upon a time");
	printf("result I was a donkey once upon a time %d\n", hash);
	hash = hash_key(tbl, "dominence");
	printf("result what's dominence %d\n", hash);
	hash = hash_key(tbl, "power up it's drinking time");
	printf("result power up it's drinking time %d\n", hash);

	puts("--- testing size by level ---");
	printf("size by level 1:%d\n", size_by_level(1));
	printf("size by level 2:%d\n", size_by_level(2));
	printf("size by level 3:%d\n", size_by_level(3));

	puts("--- testing values and add ---");

	table_add(tbl, string_copy("hello"), string_copy("there"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("apple"), string_copy("tree"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("bannana"), string_copy("split"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("lash"), string_copy("O'Ninetails"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("fire"), string_copy("crow"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("denial"), string_copy("is hidden"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("one"), string_copy("1"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("two"), string_copy("2"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("three"), string_copy("3"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("four"), string_copy("4"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("five"), string_copy("5"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("six"), string_copy("6"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("seven"), string_copy("7"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("eight"), string_copy("8"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("nine"), string_copy("9"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("ten"), string_copy("10"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("eleven"), string_copy("11"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("twelve"), string_copy("12"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("thirteen"), string_copy("13"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("fourteen"), string_copy("14"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("fifteen"), string_copy("15"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("sixteen"), string_copy("16"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("seventeen"), string_copy("17"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("eighteen"), string_copy("18"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("nineteen"), string_copy("19"));
	table_print_debug(tbl, stdout);
	table_add(tbl, string_copy("twenty"), string_copy("20"));
	table_print_debug(tbl, stdout);

	puts("--- testing get ---");
	struct table_item *item = table_get(tbl, "hello");
	if(item != NULL){
		fprintf(stdout, "%s->%s\n", item->key_val, item->content);
	}
	struct table_item *item2 = table_get(tbl, "not here");
	if(item2 != NULL){
		fprintf(stdout, "%s->%s\n", item2->key_val, item2->content);
	}else{
		puts("item is null");
	}
	struct table_item *item3 = table_get(tbl, "bannana");
	if(item3 != NULL){
		fprintf(stdout, "%s->%s\n", item3->key_val, item3->content);
	}
	struct table_item *item4 = table_get(tbl, "one");
	if(item4 != NULL){
		fprintf(stdout, "%s->%s\n", item4->key_val, item4->content);
	}

	return 0;
}