/* brings interface down and runs a user-supplied script */ static int if_release() { wmlog_msg(2, "Starting if-release script..."); raise_event("if-release"); tap_close(tap_fd, tap_dev); return 0; }
int main(int argc, char **argv) { struct tap tapin, tapout; FILE *infile; long pulse; getopts(argc, argv); if (optind == argc) { infile = stdin; } else if (optind == argc - 1) { if (!strcmp(argv[optind], "-")) { infile = stdin; } else { infile = fopen(argv[optind], "rb"); if (!infile) { perror(argv[optind]); return 1; } } } else { fprintf(stderr, "%s: too many arguments\n", argv0); usage(); return 1; } if (tap_read_header(&tapin, infile)) { fprintf(stderr, "%s: error reading TAP file\n", argv0); return 1; } if (outversion < 0) tapout.version = tapin.version; else tapout.version = outversion; if (tap_write_header(&tapout, stdout)) { fprintf(stderr, "%s: error writing TAP file\n", argv0); return 1; } while ((pulse = get_pulse(&tapin)) >= 0) { if (tapin.version == 0 && pulse == V0_LONG_PULSE) { pulse = LONG_PULSE; } pulse /= speed; put_pulse(pulse, &tapout); } tap_close(&tapout); return 0; }
int main() { long pulse; struct tap tapfile; tapfile.version = 1; if (tap_write_header(&tapfile, stdout)) { fprintf(stderr, "error writing TAP file\n"); return 1; } while (scanf("%ld", &pulse) == 1) { if (pulse <= 0) break; tap_put_pulse(pulse, &tapfile); } tap_close(&tapfile); return 0; }
int tape_image_close(tape_image_t *tape_image) { int retval = 0; switch (tape_image->type) { case TAPE_TYPE_T64: retval = t64_close((t64_t *)tape_image->data); break; case TAPE_TYPE_TAP: retval = tap_close((tap_t *)tape_image->data); break; } lib_free(tape_image->name); tape_image->name = NULL; return retval; }
int main(int argc, char **argv) { char dev_if[16]; int opt; int err = 0; int daemonize = 0; int log_level = LOG_WARN; char *log_file = NULL; memset(dev_if, '\0', sizeof(dev_if)); strncpy(dev_if, PHOST_DEFAULT_TAP_DEVICE, sizeof(dev_if) - 1); phost_set_program_name(basename(argv[0])); /* parse options */ while(1){ opt = getopt(argc, argv, "Dd:i:p:l:n:v"); if(opt < 0){ break; } switch(opt){ case 'D': daemonize = 1; break; case 'd': if(optarg){ if(atoi(optarg) < LOG_LEVEL_MAX){ log_level = atoi(optarg); } else{ phost_print_usage(); exit(1); } } else{ err |= 1; } break; case 'i': if(optarg){ memset(dev_if, '\0', sizeof(dev_if)); strncpy(dev_if, optarg, sizeof(dev_if) - 1); } else{ err |= 1; } break; case 'p': if(optarg){ memset(pid_dir, '\0', sizeof(pid_dir)); strncpy(pid_dir, optarg, sizeof(pid_dir) - 1); } else{ err |= 1; } break; case 'l': if(optarg){ memset(log_dir, '\0', sizeof(log_dir)); strncpy(log_dir, optarg, sizeof(log_dir) - 1); } else{ err |= 1; } break; case 'n': if(optarg){ memset(host_name, '\0', sizeof(host_name)); strncpy(host_name, optarg, sizeof(host_name) - 1); } else{ err |= 1; } break; case 'v': log_file = LOG_OUT_STDOUT; log_level = LOG_DEBUG; break; default: err |= 1; } } if(err){ phost_print_usage(); exit(1); } if(log_file == NULL){ log_file = (char*)malloc(sizeof(char)*(strlen(log_dir)+strlen(PHOST_LOG_FILE)+strlen(dev_if)+3)); sprintf(log_file, "%s/%s.%s", log_dir, PHOST_LOG_FILE, dev_if); } /* check if this process is run with root privilege */ if(getuid() != 0){ fprintf(stderr, "[ERROR] `%s' must be run with root privilege.\n", program_name); exit(1); } /* register signal handler */ signal(SIGINT, phost_handle_signals); signal(SIGTERM, phost_handle_signals); signal(SIGPIPE, SIG_IGN); signal(SIGUSR1, SIG_IGN); signal(SIGUSR2, SIG_IGN); /* initializations */ if(log_init(log_level, log_file) < 0){ fprintf(stderr, "[ERROR] cannot initialize logger.\n"); exit(1); } if(daemonize && (phost_daemonize() < 0)){ fprintf(stderr, "[ERROR] cannot daemonize.\n"); exit(1); } if(strncmp(dev_if, "tap", 3) != 0){ if(ethdev_init(dev_if) < 0){ fprintf(stderr, "[ERROR] cannot initialize Ethernet device (%s).\n", dev_if); exit(1); } trx_init(ethdev_read, ethdev_send); } else{ if(tap_init(dev_if) < 0){ fprintf(stderr, "[ERROR] cannot create tap device (%s).\n", dev_if); exit(1); } trx_init(tap_read, tap_send); } phost_set_global_params(); phost_create_pid_file(host_name); cmdif_init(dev_if, stats_udp_send_update); arp_init(host_mac_addr, host_ip_addr); ipv4_init(host_ip_addr, host_ip_mask); udp_init(stats_udp_recv_update); pkt_dump = (char*)malloc(sizeof(char)*PKT_BUF_SIZE*2); while(run){ phost_run(); cmdif_run(); arp_age_entries(); } free(pkt_dump); cmdif_close(); tap_close(); phost_delete_pid_file(host_name); phost_unset_global_params(); log_close(); return 0; }
int main(int argc, char **argv) { int result, index; char *bptr, *tptr; char *app; int i, j, c; struct getap_state *gs; index = 0; app = argv[0]; i = j = 1; tptr = "tgtap%d"; bptr = "/proc/%d/hw/ioreg/ten_Gbe_v2"; gs = malloc(sizeof(struct getap_state)); if(gs == NULL){ fprintf(stderr, "%s: unable to allocate %d bytes\n", app, sizeof(struct getap_state)); return EX_USAGE; } gs->s_address_name[0] = '\0'; gs->s_gateway_name[0] = '\0'; gs->s_mac_name[0] = '\0'; gs->s_port = 0; gs->s_verbose = 1; gs->s_testing = 0; while (i < argc) { if (argv[i][0] == '-') { c = argv[i][j]; switch (c) { case 'h' : usage(app); return EX_OK; case 'v' : gs->s_verbose++; j++; break; case 'T' : gs->s_testing++; j++; break; case 'a' : case 'g' : case 'm' : case 'p' : case 'n' : case 't' : case 'b' : j++; if (argv[i][j] == '\0') { j = 0; i++; } if (i >= argc) { fprintf(stderr, "%s: -%c needs a parameter\n", app, c); return EX_USAGE; } switch(c){ case 'a' : strncpy(gs->s_address_name, argv[i] + j, IP_BUFFER - 1); gs->s_address_name[IP_BUFFER - 1] = '\0'; break; case 'g' : strncpy(gs->s_gateway_name, argv[i] + j, IP_BUFFER - 1); gs->s_gateway_name[IP_BUFFER - 1] = '\0'; break; case 'm' : strncpy(gs->s_mac_name, argv[i] + j, MAC_BUFFER - 1); gs->s_mac_name[MAC_BUFFER - 1] = '\0'; case 'p' : gs->s_port = atoi(argv[i] + j); break; case 'n' : index = atoi(argv[i] + j); break; case 't' : tptr = NULL; strncpy(gs->s_tap_name, argv[i] + j, NAME_BUFFER - 1); gs->s_tap_name[NAME_BUFFER - 1] = '\0'; break; case 'b' : bptr = NULL; strncpy(gs->s_borph_name, argv[i] + j, NAME_BUFFER - 1); gs->s_borph_name[NAME_BUFFER - 1] = '\0'; break; } i++; j = 1; break; case '\0': j = 1; i++; break; default: fprintf(stderr, "%s: unknown option -%c\n", app, argv[i][j]); return EX_USAGE; } } else { fprintf(stderr, "%s: bad argument %s\n", app, argv[i]); return EX_USAGE; } } if(tptr){ result = snprintf(gs->s_tap_name, NAME_BUFFER - 1, tptr, index); if((result < 0) || (result >= NAME_BUFFER)){ fprintf(stderr, "unable to expand %s: Value too long\n", tptr); return EX_USAGE; } } if(bptr){ result = snprintf(gs->s_borph_name, NAME_BUFFER - 1, bptr, getpid()); if((result < 0) || (result >= NAME_BUFFER)){ fprintf(stderr, "unable to expand %s: Value too long\n", bptr); return EX_USAGE; } } if(gs->s_address_name[0] == '\0'){ fprintf(stderr, "%s: need an ip address\n", app); return EX_OSERR; } if(gs->s_gateway_name[0] == '\0'){ /* risky, gateware may not like it */ strncpy(gs->s_gateway_name, gs->s_address_name, IP_BUFFER); } if(gs->s_mac_name[0] == '\0'){ make_mac(gs); } if(gs->s_verbose){ printf("%s: tap interface name: %s\n", app, gs->s_tap_name); printf("%s: borph file: %s\n", app, gs->s_borph_name); printf("%s: %s file interface \n", app, gs->s_testing ? "testing" : "borph"); printf("%s: ip address %s\n", app, gs->s_address_name); printf("%s: mac address %s\n", app, gs->s_mac_name); } gs->s_tfd = tap_open(gs->s_tap_name); if(gs->s_tfd < 0){ fprintf(stderr, "%s: unable to set up tap device %s: %s\n", app, gs->s_tap_name, strerror(errno)); return EX_OSERR; } if(setup_borph(gs) < 0){ fprintf(stderr, "%s: unable to initialise borph register file %s: %s\n", app, gs->s_borph_name, strerror(errno)); return EX_OSERR; } if(setup_tap(gs)){ fprintf(stderr, "%s: unable to configure tap device %s\n", app, gs->s_tap_name); return EX_OSERR; } printf("%s: my arp table index %d\n", app, gs->s_index); if(gs->s_verbose){ printf("%s: my network: 0x%08x\n", app, gs->s_network_binary); } run = 1; /* signal stuff */ mainloop(gs); tap_close(gs->s_tfd); close(gs->s_bfd); return EX_OK; }