int matchcmp(const struct ipt_entry_match *match, const char *srcports, const char *destports) { u_int16_t temp[2]; if (strcmp(match->u.user.name, "tcp") == 0) { struct ipt_tcp *tcpinfo = (struct ipt_tcp *)match->data; if (srcports) { parse_ports(srcports, temp); if (temp[0] != tcpinfo->spts[0] || temp[1] != tcpinfo->spts[1]) return 0; } if (destports) { parse_ports(destports, temp); if (temp[0] != tcpinfo->dpts[0] || temp[1] != tcpinfo->dpts[1]) return 0; } return 1; } else if (strcmp(match->u.user.name, "udp") == 0) { struct ipt_udp *udpinfo = (struct ipt_udp *)match->data; if (srcports) { parse_ports(srcports, temp); if (temp[0] != udpinfo->spts[0] || temp[1] != udpinfo->spts[1]) return 0; } if (destports) { parse_ports(destports, temp); if (temp[0] != udpinfo->dpts[0] || temp[1] != udpinfo->dpts[1]) return 0; } return 1; } else return 0; }
struct ipt_entry_match * get_udp_match(const char *sports, const char *dports, unsigned int *nfcache) { struct ipt_entry_match *match; struct ipt_udp *udpinfo; size_t size; size = IPT_ALIGN(sizeof(*match)) + IPT_ALIGN(sizeof(*udpinfo)); match = calloc(1, size); match->u.match_size = size; strncpy(match->u.user.name, "udp", IPT_FUNCTION_MAXNAMELEN); udpinfo = (struct ipt_udp *)match->data; udpinfo->spts[1] = udpinfo->dpts[1] = 0xFFFF; if (sports) { *nfcache |= NFC_IP_SRC_PT; parse_ports(sports, udpinfo->spts); } if (dports) { *nfcache |= NFC_IP_DST_PT; parse_ports(dports, udpinfo->dpts); } return match; }
static void REDIRECT_parse(struct xt_option_call *cb) { const struct ip6t_entry *entry = cb->xt_entry; struct nf_nat_range *range = (void *)(*cb->target)->data; int portok; if (entry->ipv6.proto == IPPROTO_TCP || entry->ipv6.proto == IPPROTO_UDP || entry->ipv6.proto == IPPROTO_SCTP || entry->ipv6.proto == IPPROTO_DCCP || entry->ipv6.proto == IPPROTO_ICMP) portok = 1; else portok = 0; xtables_option_parse(cb); switch (cb->entry->id) { case O_TO_PORTS: if (!portok) xtables_error(PARAMETER_PROBLEM, "Need TCP, UDP, SCTP or DCCP with port specification"); parse_ports(cb->arg, range); if (cb->xflags & F_RANDOM) range->flags |= NF_NAT_RANGE_PROTO_RANDOM; break; case O_RANDOM: if (cb->xflags & F_TO_PORTS) range->flags |= NF_NAT_RANGE_PROTO_RANDOM; break; } }
/* Function which parses command options; returns true if it ate an option */ static int parse(int c, char **argv, int invert, unsigned int *flags, const struct ipt_entry *entry, struct ipt_entry_target **target) { struct ip_nat_multi_range *mr = (struct ip_nat_multi_range *)(*target)->data; int portok; if (entry->ip.proto == IPPROTO_TCP || entry->ip.proto == IPPROTO_UDP || entry->ip.proto == IPPROTO_ICMP) portok = 1; else portok = 0; switch (c) { case '1': if (!portok) exit_error(PARAMETER_PROBLEM, "Need TCP or UDP with port specification"); if (check_inverse(optarg, &invert, NULL, 0)) exit_error(PARAMETER_PROBLEM, "Unexpected `!' after --to-ports"); parse_ports(optarg, mr); return 1; default: return 0; } }
static void MASQUERADE_parse(struct xt_option_call *cb) { const struct ipt_entry *entry = cb->xt_entry; int portok; struct nf_nat_multi_range *mr = cb->data; if (entry->ip.proto == IPPROTO_TCP || entry->ip.proto == IPPROTO_UDP || entry->ip.proto == IPPROTO_SCTP || entry->ip.proto == IPPROTO_DCCP || entry->ip.proto == IPPROTO_ICMP) portok = 1; else portok = 0; xtables_option_parse(cb); switch (cb->entry->id) { case O_TO_PORTS: if (!portok) xtables_error(PARAMETER_PROBLEM, "Need TCP, UDP, SCTP or DCCP with port specification"); parse_ports(cb->arg, mr); break; case O_RANDOM: mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; break; } }
void scanmain(void) { struct portinfo *pi; int ports = 0, i; int childpid; pi = shm_init(sizeof(*pi)*(MAXPORT+2)); pi[MAXPORT+1].active = 0; /* hold the average RTT */ if (pi == NULL) { fprintf(stderr, "Unable to create the shared memory"); shm_close(pi); exit(1); } for (i = 0; i <= MAXPORT; i++) { pi[i].active = 0; pi[i].retry = opt_scan_probes; } if (parse_ports(pi, opt_scanports)) { fprintf(stderr, "Ports syntax error for scan mode\n"); shm_close(pi); exit(1); } for (i = 0; i <= MAXPORT; i++) { if (!pi[i].active) pi[i].retry = 0; } for (i = 0; i <= MAXPORT; i++) ports += pi[i].active; fprintf(stderr, "%d ports to scan, use -V to see all the replies\n", ports); fprintf(stderr, "+----+-----------+---------+---+-----+-----+-----+\n"); fprintf(stderr, "|port| serv name | flags |ttl| id | win | len |\n"); fprintf(stderr, "+----+-----------+---------+---+-----+-----+-----+\n"); /* We are ready to fork, the input and output parts * are separated processes */ if ((childpid = fork()) == -1) { perror("fork"); shm_close(pi); exit(1); } /* The parent is the receiver, the child the sender. * it's almost the same but this way is simpler * to make it working in pipe with other commands like grep. */ if (childpid) { /* parent */ Signal(SIGCHLD, do_exit); Signal(SIGINT, do_exit); Signal(SIGTERM, do_exit); receiver(pi, childpid); } else { /* child */ Signal(SIGINT, do_exit); Signal(SIGTERM, do_exit); sender(pi); } /* UNREACHED */ }
/* Function which parses command options; returns true if it ate an option */ static int parse(int c, char **argv, int invert, unsigned int *flags, const struct ipt_entry *entry, struct ipt_entry_target **target) { //printf("autofw: parse\n"); // test struct ip_autofw_info *info = (struct ip_autofw_info *)(*target)->data; switch (c) { case '1': if (!strcasecmp(optarg, "tcp")) info->proto = IPPROTO_TCP; else if (!strcasecmp(optarg, "udp")) info->proto = IPPROTO_UDP; else exit_error(PARAMETER_PROBLEM, "unknown protocol `%s' specified", optarg); return 1; case '2': if (check_inverse(optarg, &invert, &optind, 0)) exit_error(PARAMETER_PROBLEM, "Unexpected `!' after --related-dport"); parse_ports(optarg, info->dport); return 1; case '3': if (check_inverse(optarg, &invert, &optind, 0)) exit_error(PARAMETER_PROBLEM, "Unexpected `!' after --related-to"); parse_ports(optarg, info->to); *flags |= IP_NAT_RANGE_PROTO_SPECIFIED; return 1; default: return 0; } }
static int REDIRECT_parse(int c, char **argv, int invert, unsigned int *flags, const void *e, struct xt_entry_target **target) { const struct ipt_entry *entry = e; struct nf_nat_multi_range *mr = (struct nf_nat_multi_range *)(*target)->data; int portok; if (entry->ip.proto == IPPROTO_TCP || entry->ip.proto == IPPROTO_UDP || entry->ip.proto == IPPROTO_SCTP || entry->ip.proto == IPPROTO_DCCP || entry->ip.proto == IPPROTO_ICMP) portok = 1; else portok = 0; switch (c) { case '1': if (!portok) xtables_error(PARAMETER_PROBLEM, "Need TCP, UDP, SCTP or DCCP with port specification"); if (xtables_check_inverse(optarg, &invert, NULL, 0, argv)) xtables_error(PARAMETER_PROBLEM, "Unexpected `!' after --to-ports"); parse_ports(optarg, mr); if (*flags & IPT_REDIRECT_OPT_RANDOM) mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; *flags |= IPT_REDIRECT_OPT_DEST; return 1; case '2': if (*flags & IPT_REDIRECT_OPT_DEST) { mr->range[0].flags |= IP_NAT_RANGE_PROTO_RANDOM; *flags |= IPT_REDIRECT_OPT_RANDOM; } else *flags |= IPT_REDIRECT_OPT_RANDOM; return 1; default: return 0; } }
int run_sockstat(int argc, char *argv[]) { int protos_defined = -1; int o, i; opt_j = -1; while ((o = getopt(argc, argv, "46cj:Llp:P:uv")) != -1) switch (o) { case '4': opt_4 = 1; break; case '6': opt_6 = 1; break; case 'c': opt_c = 1; break; case 'j': opt_j = atoi(optarg); break; case 'L': opt_L = 1; break; case 'l': opt_l = 1; break; case 'p': parse_ports(optarg); break; case 'P': protos_defined = parse_protos(optarg); break; case 'u': opt_u = 1; break; case 'v': ++opt_v; break; default: usage(); } argc -= optind; argv += optind; if (argc > 0) usage(); if ((!opt_4 && !opt_6) && protos_defined != -1) opt_4 = opt_6 = 1; if (!opt_4 && !opt_6 && !opt_u) opt_4 = opt_6 = opt_u = 1; if ((opt_4 || opt_6) && protos_defined == -1) protos_defined = set_default_protos(); if (!opt_c && !opt_l) opt_c = opt_l = 1; if (opt_4 || opt_6) { for (i = 0; i < protos_defined; i++) gather_inet(protos[i]); } if (opt_u || (protos_defined == -1 && !opt_4 && !opt_6)) { gather_unix(SOCK_STREAM); gather_unix(SOCK_DGRAM); } getfiles(); display(); return 0; }
void start_server(krb5_context context, const char *port_str) { int e; struct kadm_port *p; krb5_socket_t *socks = NULL, *tmp; unsigned int num_socks = 0; int i; if (port_str == NULL) port_str = "+"; parse_ports(context, port_str); for(p = kadm_ports; p; p = p->next) { struct addrinfo hints, *ai, *ap; char portstr[32]; memset (&hints, 0, sizeof(hints)); hints.ai_flags = AI_PASSIVE; hints.ai_socktype = SOCK_STREAM; e = getaddrinfo(NULL, p->port, &hints, &ai); if(e) { snprintf(portstr, sizeof(portstr), "%u", p->def_port); e = getaddrinfo(NULL, portstr, &hints, &ai); } if(e) { krb5_warn(context, krb5_eai_to_heim_errno(e, errno), "%s", portstr); continue; } i = 0; for(ap = ai; ap; ap = ap->ai_next) i++; tmp = realloc(socks, (num_socks + i) * sizeof(*socks)); if(tmp == NULL) { krb5_warnx(context, "failed to reallocate %lu bytes", (unsigned long)(num_socks + i) * sizeof(*socks)); continue; } socks = tmp; for(ap = ai; ap; ap = ap->ai_next) { krb5_socket_t s = socket(ap->ai_family, ap->ai_socktype, ap->ai_protocol); if(rk_IS_BAD_SOCKET(s)) { krb5_warn(context, rk_SOCK_ERRNO, "socket"); continue; } socket_set_reuseaddr(s, 1); socket_set_ipv6only(s, 1); if (rk_IS_SOCKET_ERROR(bind (s, ap->ai_addr, ap->ai_addrlen))) { krb5_warn(context, rk_SOCK_ERRNO, "bind"); rk_closesocket(s); continue; } if (rk_IS_SOCKET_ERROR(listen (s, SOMAXCONN))) { krb5_warn(context, rk_SOCK_ERRNO, "listen"); rk_closesocket(s); continue; } socks[num_socks++] = s; } freeaddrinfo (ai); } if(num_socks == 0) krb5_errx(context, 1, "no sockets to listen to - exiting"); wait_for_connection(context, socks, num_socks); }
int main(int argc, char *argv[]) { static const char short_options[] = "hVlp:"; static const struct option long_options[] = { {"help", 0, NULL, 'h'}, {"version", 0, NULL, 'V'}, {"list", 0, NULL, 'l'}, {"port", 1, NULL, 'p'}, { } }; int do_list = 0; struct pollfd *pfds; int npfds; int c, err; init_seq(); while ((c = getopt_long(argc, argv, short_options, long_options, NULL)) != -1) { switch (c) { case 'h': help(argv[0]); return 0; case 'V': version(); return 0; case 'l': do_list = 1; break; case 'p': parse_ports(optarg); break; default: help(argv[0]); return 1; } } if (optind < argc) { help(argv[0]); return 1; } if (do_list) { list_ports(); return 0; } create_port(); connect_ports(); err = snd_seq_nonblock(seq, 1); check_snd("set nonblock mode", err); if (port_count > 0) printf("Waiting for data."); else printf("Waiting for data at port %d:0.", snd_seq_client_id(seq)); printf(" Press Ctrl+C to end.\n"); printf("Source Event Ch Data\n"); signal(SIGINT, sighandler); signal(SIGTERM, sighandler); npfds = snd_seq_poll_descriptors_count(seq, POLLIN); pfds = alloca(sizeof(*pfds) * npfds); for (;;) { snd_seq_poll_descriptors(seq, pfds, npfds, POLLIN); if (poll(pfds, npfds, -1) < 0) break; do { snd_seq_event_t *event; err = snd_seq_event_input(seq, &event); if (err < 0) break; if (event) dump_event(event); } while (err > 0); fflush(stdout); if (stop) break; } snd_seq_close(seq); return 0; }
static void *ja_init(const char *device, unsigned rate, unsigned latency) { jack_t *jd = (jack_t*)calloc(1, sizeof(jack_t)); if (!jd) return NULL; pthread_cond_init(&jd->cond, NULL); pthread_mutex_init(&jd->cond_lock, NULL); const char **jports = NULL; char *dest_ports[2]; size_t bufsize = 0; int parsed = 0; jd->client = jack_client_open("RetroArch", JackNullOption, NULL); if (jd->client == NULL) goto error; g_settings.audio.out_rate = jack_get_sample_rate(jd->client); jack_set_process_callback(jd->client, process_cb, jd); jack_on_shutdown(jd->client, shutdown_cb, jd); jd->ports[0] = jack_port_register(jd->client, "left", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); jd->ports[1] = jack_port_register(jd->client, "right", JACK_DEFAULT_AUDIO_TYPE, JackPortIsOutput, 0); if (jd->ports[0] == NULL || jd->ports[1] == NULL) { RARCH_ERR("Failed to register ports.\n"); goto error; } jports = jack_get_ports(jd->client, NULL, NULL, JackPortIsPhysical | JackPortIsInput); if (jports == NULL) { RARCH_ERR("Failed to get ports.\n"); goto error; } bufsize = find_buffersize(jd, latency); jd->buffer_size = bufsize; RARCH_LOG("JACK: Internal buffer size: %d frames.\n", (int)(bufsize / sizeof(jack_default_audio_sample_t))); for (int i = 0; i < 2; i++) { jd->buffer[i] = jack_ringbuffer_create(bufsize); if (jd->buffer[i] == NULL) { RARCH_ERR("Failed to create buffers.\n"); goto error; } } parsed = parse_ports(dest_ports, jports); if (jack_activate(jd->client) < 0) { RARCH_ERR("Failed to activate Jack...\n"); goto error; } for (int i = 0; i < 2; i++) { if (jack_connect(jd->client, jack_port_name(jd->ports[i]), dest_ports[i])) { RARCH_ERR("Failed to connect to Jack port.\n"); goto error; } } for (int i = 0; i < parsed; i++) free(dest_ports[i]); jack_free(jports); return jd; error: if (jports != NULL) jack_free(jports); return NULL; }
/* Function which parses command options; returns true if it ate an option */ static int parse(int c, char **argv, int invert, unsigned int *flags, const struct ipt_entry *entry, struct ipt_entry_target **target) { struct ipt_trigger_info *info = (struct ipt_trigger_info *)(*target)->data; switch (c) { case '1': if (!strcasecmp(optarg, "dnat")) info->type = IPT_TRIGGER_DNAT; else if (!strcasecmp(optarg, "in")) info->type = IPT_TRIGGER_IN; else if (!strcasecmp(optarg, "out")) info->type = IPT_TRIGGER_OUT; else exit_error(PARAMETER_PROBLEM, "triggering type can only be one of `%s', '%s' '%s'", "dnat", "in", "out"); return 1; case '2': if (!strcasecmp(optarg, "tcp")) info->proto = IPPROTO_TCP; else if (!strcasecmp(optarg, "udp")) info->proto = IPPROTO_UDP; else if (!strcasecmp(optarg, "all")) info->proto = 0; else exit_error(PARAMETER_PROBLEM, "triggering protocol can only be one of `%s', '%s', '%s'", "tcp", "udp", "all"); return 1; case '3': if (check_inverse(optarg, &invert, &optind, 0)) exit_error(PARAMETER_PROBLEM, "Unexpected `!' after --triggering-port"); parse_ports(optarg, info->ports.mport); return 1; #ifdef ATP_SUPPORT_PRTT_RPROTOCOL case '4': if (!strcasecmp(optarg, "tcp")) info->rproto = IPPROTO_TCP; else if (!strcasecmp(optarg, "udp")) info->rproto = IPPROTO_UDP; else if (!strcasecmp(optarg, "all")) info->rproto = 0; else exit_error(PARAMETER_PROBLEM, "open protocol can only be one of '%s', '%s', '%s'", "tcp", "udp", "all"); return 1; case '5': if (check_inverse(optarg, &invert, &optind, 0)) exit_error(PARAMETER_PROBLEM, "Unexpected `!' after --open-port"); //parse_ports(optarg, info->ports.rport); parse_Openports(optarg, &(info->ports)); *flags |= IP_NAT_RANGE_PROTO_SPECIFIED; return 1; #else #if 0 case '4': if (check_inverse(optarg, &invert, &optind, 0)) exit_error(PARAMETER_PROBLEM, "Unexpected `!' after --open-port"); parse_ports(optarg, info->ports.rport); *flags |= IP_NAT_RANGE_PROTO_SPECIFIED; return 1; #endif #endif default: return 0; } }
int main(int argc, char* argv[]) { int auto_connect = 1; char* client_name = JACKOFF_DEFAULT_CLIENT_NAME; char* format_name = JACKOFF_DEFAULT_FORMAT; char* filename = NULL; int bitrate = -1; size_t channels = 0; float buffer_duration = JACKOFF_DEFAULT_RING_BUFFER_DURATION; jackoff_format_t* output_format; jack_options_t jack_options = JackNullOption; time_t duration = 0; struct port_info manual_ports; manual_ports.count = 0; manual_ports.ports = NULL; int option, long_index; while (1) { option = getopt_long(argc, argv, short_options, long_options, &long_index); if (option == -1) break; switch (option) { case 'a': auto_connect = 1; break; case 'n': client_name = optarg; break; case 'f': format_name = optarg; break; case 'b': bitrate = (int) strtol(optarg, NULL, 0); break; case 'c': channels = (size_t) strtol(optarg, NULL, 0); break; case 'd': duration = (time_t) strtol(optarg, NULL, 0); break; case 'R': buffer_duration = (float) strtod(optarg, NULL); break; case 'p': if (!parse_ports(optarg, &manual_ports)) { jackoff_error("error parsing manual port list"); } break; case 'S': jack_options |= JackNoStartServer; break; case 'v': jackoff_set_log_cutoff(JACKOFF_LOG_DEBUG); break; case 'q': jackoff_set_log_cutoff(JACKOFF_LOG_WARNING); break; default: show_usage_info(argv[0]); return 10; } } if (channels == 0) { if (manual_ports.count > 0) channels = manual_ports.count; else channels = JACKOFF_DEFAULT_CHANNELS; } if (bitrate == -1) { bitrate = JACKOFF_DEFAULT_BITRATE_PER_CHANNEL * (int) channels; } argc -= optind; argv += optind; if (argc != 1) { jackoff_error("must provide the name of a file to record to"); } else { filename = argv[0]; } output_format = jackoff_get_output_format(format_name); if (!output_format) { jackoff_error("unknown output format \"%s\"", format_name); } return run(manual_ports.count, (const char**) manual_ports.ports, client_name, filename, output_format, bitrate, channels, buffer_duration, duration, jack_options); }
int main(int argc, char **argv) { krb5_error_code ret; char **files; int optidx = 0; int e, i; krb5_log_facility *logfacility; krb5_keytab keytab; setprogname(argv[0]); ret = krb5_init_context(&context); if (ret) errx (1, "krb5_init_context failed: %d", ret); while((e = getarg(args, num_args, argc, argv, &optidx))) warnx("error at argument `%s'", argv[optidx]); if (help_flag) usage (0); if (version_flag) { print_version(NULL); exit(0); } argc -= optidx; argv += optidx; if (config_file == NULL) { asprintf(&config_file, "%s/kdc.conf", hdb_db_dir(context)); if (config_file == NULL) errx(1, "out of memory"); } ret = krb5_prepend_config_files_default(config_file, &files); if (ret) krb5_err(context, 1, ret, "getting configuration files"); ret = krb5_set_config_files(context, files); krb5_free_config_files(files); if(ret) krb5_err(context, 1, ret, "reading configuration files"); ret = krb5_openlog(context, "kadmind", &logfacility); if (ret) krb5_err(context, 1, ret, "krb5_openlog"); ret = krb5_set_warn_dest(context, logfacility); if (ret) krb5_err(context, 1, ret, "krb5_set_warn_dest"); ret = krb5_kt_register(context, &hdb_kt_ops); if(ret) krb5_err(context, 1, ret, "krb5_kt_register"); ret = krb5_kt_resolve(context, keytab_str, &keytab); if(ret) krb5_err(context, 1, ret, "krb5_kt_resolve"); kadm5_setup_passwd_quality_check (context, check_library, check_function); for (i = 0; i < policy_libraries.num_strings; i++) { ret = kadm5_add_passwd_quality_verifier(context, policy_libraries.strings[i]); if (ret) krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier"); } ret = kadm5_add_passwd_quality_verifier(context, NULL); if (ret) krb5_err(context, 1, ret, "kadm5_add_passwd_quality_verifier"); { int fd = 0; struct sockaddr_storage __ss; struct sockaddr *sa = (struct sockaddr *)&__ss; socklen_t sa_size = sizeof(__ss); krb5_auth_context ac = NULL; int debug_port; if(debug_flag) { if(port_str == NULL) debug_port = krb5_getportbyname (context, "kerberos-adm", "tcp", 749); else debug_port = htons(atoi(port_str)); mini_inetd(debug_port); } else if(roken_getsockname(STDIN_FILENO, sa, &sa_size) < 0 && errno == ENOTSOCK) { parse_ports(context, port_str ? port_str : "+"); pidfile(NULL); start_server(context); } if(realm) krb5_set_default_realm(context, realm); /* XXX */ kadmind_loop(context, ac, keytab, fd); } return 0; }
void scanmain(void) { int ports = 0, i; HANDLE childpid; DWORD threadId; struct portinfo *pi; pi = malloc(sizeof(struct portinfo) * (MAXPORT + 2)); if (pi == NULL) { fprintf(stderr, "[malloc] failed: %d\n", GetLastError()); exit(1); } pi[MAXPORT+1].active = 0; /* hold the average RTT */ for (i = 0; i <= MAXPORT; i++) { pi[i].active = 0; pi[i].retry = opt_scan_probes; } if (parse_ports(pi, opt_scanports)) { fprintf(stderr, "Ports syntax error for scan mode\n"); free(pi); exit(1); } for (i = 0; i <= MAXPORT; i++) { if (!pi[i].active) pi[i].retry = 0; } for (i = 0; i <= MAXPORT; i++) ports += pi[i].active; fprintf(stderr, "%d ports to scan, use -V to see all the replies\n", ports); fprintf(stderr, "+----+-----------+---------+---+-----+-----+\n"); fprintf(stderr, "|port| serv name | flags |ttl| id | win |\n"); fprintf(stderr, "+----+-----------+---------+---+-----+-----+\n"); /* We are ready to fork, the input and output parts * are separated processes */ childpid = CreateThread(NULL, 0, sender, pi, 0, &threadId); if (childpid == NULL) { fprintf(stderr, "[CreateThread] failed: %d\n", GetLastError()); free(pi); exit(1); } /* The parent is the receiver, the child the sender. * it's almost the same but this way is simpler * to make it working in pipe with other commands like grep. */ signal(SIGINT, do_exit); signal(SIGTERM, do_exit); receiver(pi, childpid); /* UNREACHED */ }
int main(int argc, char **argv) { unsigned char opt_flags[256] = {}; unsigned int kill_after_s = 0; const char *optstr = "i:a:p:s:d:m:T:P:SNh"; int min_port = 0, max_port = 0; struct iptnl_info tnl = {}; struct rlimit r = {RLIM_INFINITY, RLIM_INFINITY}; struct vip vip = {}; char filename[256]; int opt; int i; tnl.family = AF_UNSPEC; vip.protocol = IPPROTO_TCP; for (i = 0; i < strlen(optstr); i++) if (optstr[i] != 'h' && 'a' <= optstr[i] && optstr[i] <= 'z') opt_flags[(unsigned char)optstr[i]] = 1; while ((opt = getopt(argc, argv, optstr)) != -1) { unsigned short family; unsigned int *v6; switch (opt) { case 'i': ifindex = atoi(optarg); break; case 'a': vip.family = parse_ipstr(optarg, vip.daddr.v6); if (vip.family == AF_UNSPEC) return 1; break; case 'p': if (parse_ports(optarg, &min_port, &max_port)) return 1; break; case 'P': vip.protocol = atoi(optarg); break; case 's': case 'd': if (opt == 's') v6 = tnl.saddr.v6; else v6 = tnl.daddr.v6; family = parse_ipstr(optarg, v6); if (family == AF_UNSPEC) return 1; if (tnl.family == AF_UNSPEC) { tnl.family = family; } else if (tnl.family != family) { fprintf(stderr, "The IP version of the src and dst addresses used in the IP encapsulation does not match\n"); return 1; } break; case 'm': if (!ether_aton_r(optarg, (struct ether_addr *)tnl.dmac)) { fprintf(stderr, "Invalid mac address:%s\n", optarg); return 1; } break; case 'T': kill_after_s = atoi(optarg); break; case 'S': xdp_flags |= XDP_FLAGS_SKB_MODE; break; case 'N': xdp_flags |= XDP_FLAGS_DRV_MODE; break; default: usage(argv[0]); return 1; } opt_flags[opt] = 0; } for (i = 0; i < strlen(optstr); i++) { if (opt_flags[(unsigned int)optstr[i]]) { fprintf(stderr, "Missing argument -%c\n", optstr[i]); usage(argv[0]); return 1; } } if (setrlimit(RLIMIT_MEMLOCK, &r)) { perror("setrlimit(RLIMIT_MEMLOCK, RLIM_INFINITY)"); return 1; } snprintf(filename, sizeof(filename), "%s_kern.o", argv[0]); if (load_bpf_file(filename)) { printf("%s", bpf_log_buf); return 1; } if (!prog_fd[0]) { printf("load_bpf_file: %s\n", strerror(errno)); return 1; } signal(SIGINT, int_exit); signal(SIGTERM, int_exit); while (min_port <= max_port) { vip.dport = htons(min_port++); if (bpf_map_update_elem(map_fd[1], &vip, &tnl, BPF_NOEXIST)) { perror("bpf_map_update_elem(&vip2tnl)"); return 1; } } if (bpf_set_link_xdp_fd(ifindex, prog_fd[0], xdp_flags) < 0) { printf("link set xdp fd failed\n"); return 1; } poll_stats(kill_after_s); bpf_set_link_xdp_fd(ifindex, -1, xdp_flags); return 0; }