int main(int argc, char** argv) { HFONT hfont = NULL; int height = -12; int width = 0; #if 1 char *fontname = "AAてすとフォント"; #else char *fontname = "BDF M+"; #endif hfont = CreateFont( height, width, 0, 0, FW_DONTCARE, FALSE, FALSE, FALSE, DEFAULT_CHARSET, OUT_DEFAULT_PRECIS, CLIP_DEFAULT_PRECIS, DEFAULT_QUALITY, DEFAULT_PITCH | FF_DONTCARE, fontname ); if (!hfont) { printf("Can't create %s\n", fontname); } else { HWND hwnd; HDC hdc; HFONT hfntOld; TEXTMETRIC tm; hwnd = GetDesktopWindow(); hdc = GetWindowDC(hwnd); hfntOld = SelectObject(hdc, hfont); GetTextMetrics(hdc, &tm); #define print_tm(t,m) printf(#m"=%d\n", t.m) print_tm(tm, tmHeight); print_tm(tm, tmAscent); print_tm(tm, tmDescent); print_tm(tm, tmAveCharWidth); print_tm(tm, tmMaxCharWidth); SelectObject(hdc, hfntOld); ReleaseDC(hwnd, hdc); DeleteObject(hfont); } return 0; }
int main() { int i; time_t int_time; struct tm *tm_time; // print off raw times: you can see, time is just a timestamp in seconds. for (i = 1; i <= 2; i++) { // the time function is kind of weird. It returns, but it also modifies // its input, *unless* the input is null (as is `(time_t *) 0`) // here we demo both approaches. int_time = time((time_t *) 0); printf("The time is %ld\n", int_time); sleep(1); time(&int_time); printf("The time is %ld\n", int_time); sleep(1); } // gmtime can help us print a useful time. It outputs a struct of // type `*tm_ptr`. It takes an addres to time_t, which is sort of strange. // // also, the output has years as a count since 1900. A holdout from when // memory was more precious, I think. printf("\nRaw time is %ld\n", int_time); tm_time = gmtime(&int_time); printf("gmtime gives:\n"); print_tm(tm_time); tm_time = localtime(&int_time); printf("localtime gives:\n"); print_tm(tm_time); // ctime provides more convenient times if you just want human-readable // strings to start with. printf("\nctime gives: %s\n", ctime(&int_time)); // we can also work with the `tm` struct using // strftime = string format time, for converting time to string // strptime = ? string put to time ? for converting string to time // converting from string... char buf[256]; char *remaining; strcpy(buf, "Thu 26 July 2007, 17:53 and then more text after"); printf("Converting buf = \"%s\" to time...", buf); remaining = strptime(buf, "%a %d %b %Y, %R", tm_time); printf("the copied tm is:\n"); print_tm(tm_time); printf("The remaining (unconsumed) string is \"%s\"\n", remaining); // converting back to string... printf("strftime gives:\n"); strftime(buf, 256, "%A %d %B %I:%S %p", tm_time); printf("\t\"%s\"\n", buf); exit(0); }
static int print_connmark(struct action_util *au, FILE *f, struct rtattr *arg) { struct rtattr *tb[TCA_CONNMARK_MAX + 1]; struct tc_connmark *ci; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_CONNMARK_MAX, arg); if (tb[TCA_CONNMARK_PARMS] == NULL) { fprintf(f, "[NULL connmark parameters]"); return -1; } ci = RTA_DATA(tb[TCA_CONNMARK_PARMS]); fprintf(f, " connmark zone %d\n", ci->zone); fprintf(f, "\t index %d ref %d bind %d", ci->index, ci->refcnt, ci->bindcnt); if (show_stats) { if (tb[TCA_CONNMARK_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_CONNMARK_TM]); print_tm(f, tm); } } fprintf(f, "\n"); return 0; }
int main(int argc, const char *argv[]) { time_t _t = time(NULL); printf("time(NULL) = %ld\n", _t); struct tm _tm; gmtime_r(&_t, &_tm); print_tm(&_tm); // struct tm* ptm = gmtime(&_t); // print_tm(ptm); return 0; }
static int test_read_time(int fd) { int ret; struct rtc_time time; printf("==== read time ====\n"); ret = do_read_time(fd, &time); if (ret) { printf("get time failed: %s\n", strerror(errno)); return ret; } printf("\tget time OK.\n"); print_tm(&time); return 0; }
static int bpf_print_opt(struct action_util *au, FILE *f, struct rtattr *arg) { struct rtattr *tb[TCA_ACT_BPF_MAX + 1]; struct tc_act_bpf *parm; SPRINT_BUF(action_buf); if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg); if (!tb[TCA_ACT_BPF_PARMS]) { fprintf(f, "[NULL bpf parameters]"); return -1; } parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]); fprintf(f, "bpf "); if (tb[TCA_ACT_BPF_NAME]) fprintf(f, "%s ", rta_getattr_str(tb[TCA_ACT_BPF_NAME])); else if (tb[TCA_ACT_BPF_FD]) fprintf(f, "pfd %u ", rta_getattr_u32(tb[TCA_ACT_BPF_FD])); if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) { bpf_print_ops(f, tb[TCA_ACT_BPF_OPS], rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN])); fprintf(f, " "); } fprintf(f, "default-action %s\n", action_n2a(parm->action, action_buf, sizeof(action_buf))); fprintf(f, "\tindex %d ref %d bind %d", parm->index, parm->refcnt, parm->bindcnt); if (show_stats) { if (tb[TCA_ACT_BPF_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]); print_tm(f, tm); } } fprintf(f, "\n "); return 0; }
static int print_mirred(struct action_util *au, FILE *f, struct rtattr *arg) { struct tc_mirred *p; struct rtattr *tb[TCA_MIRRED_MAX + 1]; const char *dev; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg); if (tb[TCA_MIRRED_PARMS] == NULL) { print_string(PRINT_FP, NULL, "%s", "[NULL mirred parameters]"); return -1; } p = RTA_DATA(tb[TCA_MIRRED_PARMS]); dev = ll_index_to_name(p->ifindex); if (dev == 0) { fprintf(stderr, "Cannot find device %d\n", p->ifindex); return -1; } print_string(PRINT_ANY, "kind", "%s ", "mirred"); print_string(PRINT_FP, NULL, "(%s", mirred_n2a(p->eaction)); print_string(PRINT_JSON, "mirred_action", NULL, mirred_action(p->eaction)); print_string(PRINT_JSON, "direction", NULL, mirred_direction(p->eaction)); print_string(PRINT_ANY, "to_dev", " to device %s)", dev); print_action_control(f, " ", p->action, ""); print_uint(PRINT_ANY, "index", "\n \tindex %u", p->index); print_int(PRINT_ANY, "ref", " ref %d", p->refcnt); print_int(PRINT_ANY, "bind", " bind %d", p->bindcnt); if (show_stats) { if (tb[TCA_MIRRED_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]); print_tm(f, tm); } } print_string(PRINT_FP, NULL, "%s", "\n "); return 0; }
static int print_gact(struct action_util *au, FILE * f, struct rtattr *arg) { #ifdef CONFIG_GACT_PROB struct tc_gact_p *pp = NULL; struct tc_gact_p pp_dummy; #endif struct tc_gact *p = NULL; struct rtattr *tb[TCA_GACT_MAX + 1]; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_GACT_MAX, arg); if (tb[TCA_GACT_PARMS] == NULL) { fprintf(f, "[NULL gact parameters]"); return -1; } p = RTA_DATA(tb[TCA_GACT_PARMS]); fprintf(f, "gact "); print_action_control(f, "action ", p->action, ""); #ifdef CONFIG_GACT_PROB if (tb[TCA_GACT_PROB] != NULL) { pp = RTA_DATA(tb[TCA_GACT_PROB]); } else { /* need to keep consistent output */ memset(&pp_dummy, 0, sizeof(pp_dummy)); pp = &pp_dummy; } fprintf(f, "\n\t random type %s", prob_n2a(pp->ptype)); print_action_control(f, " ", pp->paction, " "); fprintf(f, "val %d", pp->pval); #endif fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt, p->bindcnt); if (show_stats) { if (tb[TCA_GACT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_GACT_TM]); print_tm(f, tm); } } fprintf(f, "\n "); return 0; }
int print_pedit(struct action_util *au,FILE * f, struct rtattr *arg) { struct tc_pedit_sel *sel; struct rtattr *tb[TCA_PEDIT_MAX + 1]; SPRINT_BUF(b1); if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_PEDIT_MAX, arg); if (tb[TCA_PEDIT_PARMS] == NULL) { fprintf(f, "[NULL pedit parameters]"); return -1; } sel = RTA_DATA(tb[TCA_PEDIT_PARMS]); fprintf(f, " pedit action %s keys %d\n ", action_n2a(sel->action, b1, sizeof (b1)),sel->nkeys); fprintf(f, "\t index %d ref %d bind %d", sel->index,sel->refcnt, sel->bindcnt); if (show_stats) { if (tb[TCA_PEDIT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_PEDIT_TM]); print_tm(f,tm); } } if (sel->nkeys) { int i; struct tc_pedit_key *key = sel->keys; for (i=0; i<sel->nkeys; i++, key++) { fprintf(f, "\n\t key #%d",i); fprintf(f, " at %d: val %08x mask %08x", (unsigned int)key->off, (unsigned int)ntohl(key->val), (unsigned int)ntohl(key->mask)); } } else { fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index,sel->nkeys); } fprintf(f, "\n "); return 0; }
static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg) { struct tc_skbedit *sel; struct rtattr *tb[TCA_SKBEDIT_MAX + 1]; SPRINT_BUF(b1); __u32 *priority; __u32 *mark; __u16 *queue_mapping; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_SKBEDIT_MAX, arg); if (tb[TCA_SKBEDIT_PARMS] == NULL) { fprintf(f, "[NULL skbedit parameters]"); return -1; } sel = RTA_DATA(tb[TCA_SKBEDIT_PARMS]); fprintf(f, " skbedit"); if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { queue_mapping = RTA_DATA(tb[TCA_SKBEDIT_QUEUE_MAPPING]); fprintf(f, " queue_mapping %u", *queue_mapping); } if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { priority = RTA_DATA(tb[TCA_SKBEDIT_PRIORITY]); fprintf(f, " priority %s", sprint_tc_classid(*priority, b1)); } if (tb[TCA_SKBEDIT_MARK] != NULL) { mark = RTA_DATA(tb[TCA_SKBEDIT_MARK]); fprintf(f, " mark %d", *mark); } if (show_stats) { if (tb[TCA_SKBEDIT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]); print_tm(f, tm); } } return 0; }
int print_mirred(struct action_util *au,FILE * f, struct rtattr *arg) { struct tc_mirred *p; struct rtattr *tb[TCA_MIRRED_MAX + 1]; const char *dev; SPRINT_BUF(b1); if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_MIRRED_MAX, arg); if (tb[TCA_MIRRED_PARMS] == NULL) { fprintf(f, "[NULL mirred parameters]"); return -1; } p = RTA_DATA(tb[TCA_MIRRED_PARMS]); /* ll_init_map(&rth); */ if ((dev = ll_index_to_name(p->ifindex)) == 0) { my_printf("Cannot find device %d\n", p->ifindex); return -1; } fprintf(f, "mirred (%s to device %s) %s", mirred_n2a(p->eaction), dev,action_n2a(p->action, b1, sizeof (b1))); fprintf(f, "\n "); fprintf(f, "\tindex %d ref %d bind %d",p->index,p->refcnt,p->bindcnt); if (show_stats) { if (tb[TCA_MIRRED_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_MIRRED_TM]); print_tm(f,tm); } } fprintf(f, "\n "); return 0; }
static int print_nat(struct action_util *au,FILE * f, struct rtattr *arg) { struct tc_nat *sel; struct rtattr *tb[TCA_NAT_MAX + 1]; char buf1[256]; char buf2[256]; SPRINT_BUF(buf3); int len; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_NAT_MAX, arg); if (tb[TCA_NAT_PARMS] == NULL) { fprintf(f, "[NULL nat parameters]"); return -1; } sel = RTA_DATA(tb[TCA_NAT_PARMS]); len = ffs(sel->mask); len = len ? 33 - len : 0; fprintf(f, " nat %s %s/%d %s %s", sel->flags & TCA_NAT_FLAG_EGRESS ? "egress" : "ingress", format_host(AF_INET, 4, &sel->old_addr, buf1, sizeof(buf1)), len, format_host(AF_INET, 4, &sel->new_addr, buf2, sizeof(buf2)), action_n2a(sel->action, buf3, sizeof (buf3))); if (show_stats) { if (tb[TCA_NAT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_NAT_TM]); print_tm(f,tm); } } return 0; }
static int print_simple(struct action_util *au, FILE * f, struct rtattr *arg) { struct tc_defact *sel; struct rtattr *tb[TCA_DEF_MAX + 1]; char *simpdata; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_DEF_MAX, arg); if (tb[TCA_DEF_PARMS] == NULL) { fprintf(f, "[NULL simple parameters]"); return -1; } sel = RTA_DATA(tb[TCA_DEF_PARMS]); if (tb[TCA_DEF_DATA] == NULL) { fprintf(f, "[missing simple string]"); return -1; } simpdata = RTA_DATA(tb[TCA_DEF_DATA]); fprintf(f, "Simple <%s>\n", simpdata); fprintf(f, "\t index %d ref %d bind %d", sel->index, sel->refcnt, sel->bindcnt); if (show_stats) { if (tb[TCA_DEF_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_DEF_TM]); print_tm(f, tm); } } fprintf(f, "\n"); return 0; }
static int print_connmark(struct action_util *au, FILE *f, struct rtattr *arg) { struct rtattr *tb[TCA_CONNMARK_MAX + 1]; struct tc_connmark *ci; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_CONNMARK_MAX, arg); if (tb[TCA_CONNMARK_PARMS] == NULL) { print_string(PRINT_FP, NULL, "%s", "[NULL connmark parameters]"); return -1; } ci = RTA_DATA(tb[TCA_CONNMARK_PARMS]); print_string(PRINT_ANY, "kind", "%s ", "connmark"); print_uint(PRINT_ANY, "zone", "zone %u", ci->zone); print_action_control(f, " ", ci->action, ""); print_string(PRINT_FP, NULL, "%s", _SL_); print_uint(PRINT_ANY, "index", "\t index %u", ci->index); print_int(PRINT_ANY, "ref", " ref %d", ci->refcnt); print_int(PRINT_ANY, "bind", " bind %d", ci->bindcnt); if (show_stats) { if (tb[TCA_CONNMARK_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_CONNMARK_TM]); print_tm(f, tm); } } print_string(PRINT_FP, NULL, "%s", _SL_); return 0; }
static int print_bpf(struct action_util *au, FILE *f, struct rtattr *arg) { struct rtattr *tb[TCA_ACT_BPF_MAX + 1]; struct tc_act_bpf *parm; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_ACT_BPF_MAX, arg); if (!tb[TCA_ACT_BPF_PARMS]) { fprintf(f, "[NULL bpf parameters]"); return -1; } parm = RTA_DATA(tb[TCA_ACT_BPF_PARMS]); fprintf(f, " bpf "); if (tb[TCA_ACT_BPF_OPS] && tb[TCA_ACT_BPF_OPS_LEN]) bpf_print_ops(f, tb[TCA_ACT_BPF_OPS], rta_getattr_u16(tb[TCA_ACT_BPF_OPS_LEN])); fprintf(f, "\n\tindex %d ref %d bind %d", parm->index, parm->refcnt, parm->bindcnt); if (show_stats) { if (tb[TCA_ACT_BPF_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_ACT_BPF_TM]); print_tm(f, tm); } } fprintf(f, "\n "); return 0; }
static int print_ipt(struct action_util *au, FILE * f, struct rtattr *arg) { struct rtattr *tb[TCA_IPT_MAX + 1]; struct xt_entry_target *t = NULL; if (arg == NULL) return -1; set_lib_dir(); parse_rtattr_nested(tb, TCA_IPT_MAX, arg); if (tb[TCA_IPT_TABLE] == NULL) { fprintf(f, "[NULL ipt table name ] assuming mangle "); } else { fprintf(f, "tablename: %s ", rta_getattr_str(tb[TCA_IPT_TABLE])); } if (tb[TCA_IPT_HOOK] == NULL) { fprintf(f, "[NULL ipt hook name ]\n "); return -1; } else { __u32 hook; hook = rta_getattr_u32(tb[TCA_IPT_HOOK]); fprintf(f, " hook: %s\n", ipthooks[hook]); } if (tb[TCA_IPT_TARG] == NULL) { fprintf(f, "\t[NULL ipt target parameters ]\n"); return -1; } else { struct xtables_target *m = NULL; t = RTA_DATA(tb[TCA_IPT_TARG]); m = find_target(t->u.user.name, TRY_LOAD); if (m != NULL) { if (build_st(m, t) < 0) { fprintf(stderr, " %s error\n", m->name); return -1; } opts = merge_options(opts, m->extra_opts, &m->option_offset); } else { fprintf(stderr, " failed to find target %s\n\n", t->u.user.name); return -1; } fprintf(f, "\ttarget "); m->print(NULL, m->t, 0); if (tb[TCA_IPT_INDEX] == NULL) { fprintf(f, " [NULL ipt target index ]\n"); } else { __u32 index; index = rta_getattr_u32(tb[TCA_IPT_INDEX]); fprintf(f, "\n\tindex %u", index); } if (tb[TCA_IPT_CNT]) { struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]); fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt); } if (show_stats) { if (tb[TCA_IPT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]); print_tm(f, tm); } } fprintf(f, "\n"); } free_opts(opts); return 0; }
static int print_csum(struct action_util *au, FILE * f, struct rtattr *arg) { struct tc_csum *sel; struct rtattr *tb[TCA_CSUM_MAX + 1]; char *uflag_1 = ""; char *uflag_2 = ""; char *uflag_3 = ""; char *uflag_4 = ""; char *uflag_5 = ""; char *uflag_6 = ""; SPRINT_BUF(action_buf); int uflag_count = 0; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_CSUM_MAX, arg); if (tb[TCA_CSUM_PARMS] == NULL) { fprintf(f, "[NULL csum parameters]"); return -1; } sel = RTA_DATA(tb[TCA_CSUM_PARMS]); if (sel->update_flags & TCA_CSUM_UPDATE_FLAG_IPV4HDR) { uflag_1 = "iph"; uflag_count++; } #define CSUM_UFLAG_BUFFER(flag_buffer, flag_value, flag_string) \ do { \ if (sel->update_flags & flag_value) { \ flag_buffer = uflag_count > 0 ? \ ", " flag_string : flag_string; \ uflag_count++; \ } \ } while(0) CSUM_UFLAG_BUFFER(uflag_2, TCA_CSUM_UPDATE_FLAG_ICMP, "icmp"); CSUM_UFLAG_BUFFER(uflag_3, TCA_CSUM_UPDATE_FLAG_IGMP, "igmp"); CSUM_UFLAG_BUFFER(uflag_4, TCA_CSUM_UPDATE_FLAG_TCP, "tcp"); CSUM_UFLAG_BUFFER(uflag_5, TCA_CSUM_UPDATE_FLAG_UDP, "udp"); CSUM_UFLAG_BUFFER(uflag_6, TCA_CSUM_UPDATE_FLAG_UDPLITE, "udplite"); if (!uflag_count) { uflag_1 = "?empty"; } fprintf(f, "csum (%s%s%s%s%s%s) action %s\n", uflag_1, uflag_2, uflag_3, uflag_4, uflag_5, uflag_6, action_n2a(sel->action, action_buf, sizeof(action_buf))); fprintf(f, "\tindex %d ref %d bind %d", sel->index, sel->refcnt, sel->bindcnt); if (show_stats) { if (tb[TCA_CSUM_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_CSUM_TM]); print_tm(f,tm); } } fprintf(f, "\n"); return 0; }
static int print_pedit(struct action_util *au, FILE *f, struct rtattr *arg) { struct tc_pedit_sel *sel; struct rtattr *tb[TCA_PEDIT_MAX + 1]; struct m_pedit_key_ex *keys_ex = NULL; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_PEDIT_MAX, arg); if (!tb[TCA_PEDIT_PARMS] && !tb[TCA_PEDIT_PARMS_EX]) { fprintf(f, "[NULL pedit parameters]"); return -1; } if (tb[TCA_PEDIT_PARMS]) { sel = RTA_DATA(tb[TCA_PEDIT_PARMS]); } else { int err; sel = RTA_DATA(tb[TCA_PEDIT_PARMS_EX]); if (!tb[TCA_PEDIT_KEYS_EX]) { fprintf(f, "Netlink error\n"); return -1; } keys_ex = calloc(sel->nkeys, sizeof(*keys_ex)); if (!keys_ex) { fprintf(f, "Out of memory\n"); return -1; } err = pedit_keys_ex_getattr(tb[TCA_PEDIT_KEYS_EX], keys_ex, sel->nkeys); if (err) { fprintf(f, "Netlink error\n"); free(keys_ex); return -1; } } fprintf(f, " pedit "); print_action_control(f, "action ", sel->action, " "); fprintf(f,"keys %d\n ", sel->nkeys); fprintf(f, "\t index %u ref %d bind %d", sel->index, sel->refcnt, sel->bindcnt); if (show_stats) { if (tb[TCA_PEDIT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_PEDIT_TM]); print_tm(f, tm); } } if (sel->nkeys) { int i; struct tc_pedit_key *key = sel->keys; struct m_pedit_key_ex *key_ex = keys_ex; for (i = 0; i < sel->nkeys; i++, key++) { enum pedit_header_type htype = TCA_PEDIT_KEY_EX_HDR_TYPE_NETWORK; enum pedit_cmd cmd = TCA_PEDIT_KEY_EX_CMD_SET; if (keys_ex) { htype = key_ex->htype; cmd = key_ex->cmd; key_ex++; } fprintf(f, "\n\t key #%d", i); fprintf(f, " at "); print_pedit_location(f, htype, key->off); fprintf(f, ": %s %08x mask %08x", cmd ? "add" : "val", (unsigned int)ntohl(key->val), (unsigned int)ntohl(key->mask)); } } else { fprintf(f, "\npedit %x keys %d is not LEGIT", sel->index, sel->nkeys); } fprintf(f, "\n "); free(keys_ex); return 0; }
static int print_ipt(struct action_util *au,FILE * f, struct rtattr *arg) { struct rtattr *tb[TCA_IPT_MAX + 1]; struct xt_entry_target *t = NULL; if (arg == NULL) return -1; xtables_init_all(&tcipt_globals, NFPROTO_IPV4); set_lib_dir(); parse_rtattr_nested(tb, TCA_IPT_MAX, arg); if (tb[TCA_IPT_TABLE] == NULL) { fprintf(f, "[NULL ipt table name ] assuming mangle "); } else { fprintf(f, "tablename: %s ", rta_getattr_str(tb[TCA_IPT_TABLE])); } if (tb[TCA_IPT_HOOK] == NULL) { fprintf(f, "[NULL ipt hook name ]\n "); return -1; } else { __u32 hook; hook = rta_getattr_u32(tb[TCA_IPT_HOOK]); fprintf(f, " hook: %s \n", ipthooks[hook]); } if (tb[TCA_IPT_TARG] == NULL) { fprintf(f, "\t[NULL ipt target parameters ] \n"); return -1; } else { struct xtables_target *m = NULL; t = RTA_DATA(tb[TCA_IPT_TARG]); m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD); if (NULL != m) { if (0 > build_st(m, t)) { fprintf(stderr, " %s error \n", m->name); return -1; } tcipt_globals.opts = xtables_merge_options( #if (XTABLES_VERSION_CODE >= 6) tcipt_globals.orig_opts, #endif tcipt_globals.opts, m->extra_opts, &m->option_offset); } else { fprintf(stderr, " failed to find target %s\n\n", t->u.user.name); return -1; } fprintf(f, "\ttarget "); m->print(NULL, m->t, 0); if (tb[TCA_IPT_INDEX] == NULL) { fprintf(f, " [NULL ipt target index ]\n"); } else { __u32 index; index = rta_getattr_u32(tb[TCA_IPT_INDEX]); fprintf(f, " \n\tindex %d", index); } if (tb[TCA_IPT_CNT]) { struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);; fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt); } if (show_stats) { if (tb[TCA_IPT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]); print_tm(f,tm); } } fprintf(f, " \n"); } xtables_free_opts(1); return 0; }
static int print_skbedit(struct action_util *au, FILE *f, struct rtattr *arg) { struct rtattr *tb[TCA_SKBEDIT_MAX + 1]; SPRINT_BUF(b1); __u32 *priority; __u32 *mark; __u16 *queue_mapping, *ptype; struct tc_skbedit *p = NULL; if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_SKBEDIT_MAX, arg); if (tb[TCA_SKBEDIT_PARMS] == NULL) { fprintf(f, "[NULL skbedit parameters]"); return -1; } p = RTA_DATA(tb[TCA_SKBEDIT_PARMS]); fprintf(f, " skbedit"); if (tb[TCA_SKBEDIT_QUEUE_MAPPING] != NULL) { queue_mapping = RTA_DATA(tb[TCA_SKBEDIT_QUEUE_MAPPING]); fprintf(f, " queue_mapping %u", *queue_mapping); } if (tb[TCA_SKBEDIT_PRIORITY] != NULL) { priority = RTA_DATA(tb[TCA_SKBEDIT_PRIORITY]); fprintf(f, " priority %s", sprint_tc_classid(*priority, b1)); } if (tb[TCA_SKBEDIT_MARK] != NULL) { mark = RTA_DATA(tb[TCA_SKBEDIT_MARK]); fprintf(f, " mark %d", *mark); } if (tb[TCA_SKBEDIT_PTYPE] != NULL) { ptype = RTA_DATA(tb[TCA_SKBEDIT_PTYPE]); if (*ptype == PACKET_HOST) fprintf(f, " ptype host"); else if (*ptype == PACKET_BROADCAST) fprintf(f, " ptype broadcast"); else if (*ptype == PACKET_MULTICAST) fprintf(f, " ptype multicast"); else if (*ptype == PACKET_OTHERHOST) fprintf(f, " ptype otherhost"); else fprintf(f, " ptype %d", *ptype); } print_action_control(f, " ", p->action, ""); fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt, p->bindcnt); if (show_stats) { if (tb[TCA_SKBEDIT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_SKBEDIT_TM]); print_tm(f, tm); } } fprintf(f, "\n "); return 0; }
static int test_alarm(int fd) { struct rtc_time rtc_tm; int ret = 0; unsigned long data; ret = do_read_time(fd, &rtc_tm); if (ret) { printf("get time failed: %s\n", strerror(errno)); return ret; } printf("Now time is: \n"); print_tm(&rtc_tm); /* Set the alarm to 5 sec in the future, and check for rollover */ rtc_tm.tm_sec += 5; if (rtc_tm.tm_sec >= 60) { rtc_tm.tm_sec %= 60; rtc_tm.tm_min++; } if (rtc_tm.tm_min == 60) { rtc_tm.tm_min = 0; rtc_tm.tm_hour++; } if (rtc_tm.tm_hour == 24) rtc_tm.tm_hour = 0; ret = ioctl(fd, RTC_ALM_SET, &rtc_tm); if (ret == -1) { if (errno == ENOTTY) { fprintf(stderr, "\n...Alarm IRQs not supported.\n"); return 0; } perror("RTC_ALM_SET ioctl"); exit(errno); } /* Read the current alarm settings */ ret = ioctl(fd, RTC_ALM_READ, &rtc_tm); if (ret == -1) { perror("RTC_ALM_READ ioctl"); exit(errno); } fprintf(stderr, "Alarm time now set to %02d:%02d:%02d.\n", rtc_tm.tm_hour, rtc_tm.tm_min, rtc_tm.tm_sec); /* Enable alarm interrupts */ ret = ioctl(fd, RTC_AIE_ON, 0); if (ret == -1) { perror("RTC_AIE_ON ioctl"); exit(errno); } fprintf(stderr, "Waiting 5 seconds for alarm..."); fflush(stderr); /* This blocks until the alarm ring causes an interrupt */ ret = read(fd, &data, sizeof(unsigned long)); if (ret == -1) { perror("read"); exit(errno); } fprintf(stderr, " okay. Alarm rang.\n"); /* Disable alarm interrupts */ ret = ioctl(fd, RTC_AIE_OFF, 0); if (ret == -1) { perror("RTC_AIE_OFF ioctl"); exit(errno); } return 0; }
static int print_ife(struct action_util *au, FILE *f, struct rtattr *arg) { struct tc_ife *p = NULL; struct rtattr *tb[TCA_IFE_MAX + 1]; __u16 ife_type = 0; __u32 mmark = 0; __u16 mtcindex = 0; __u32 mprio = 0; int has_optional = 0; SPRINT_BUF(b2); if (arg == NULL) return -1; parse_rtattr_nested(tb, TCA_IFE_MAX, arg); if (tb[TCA_IFE_PARMS] == NULL) { fprintf(f, "[NULL ife parameters]"); return -1; } p = RTA_DATA(tb[TCA_IFE_PARMS]); fprintf(f, "ife %s ", p->flags & IFE_ENCODE ? "encode" : "decode"); print_action_control(f, "action ", p->action, " "); if (tb[TCA_IFE_TYPE]) { ife_type = rta_getattr_u16(tb[TCA_IFE_TYPE]); has_optional = 1; fprintf(f, "type 0x%X ", ife_type); } if (has_optional) fprintf(f, "\n\t "); if (tb[TCA_IFE_METALST]) { struct rtattr *metalist[IFE_META_MAX + 1]; int len = 0; parse_rtattr_nested(metalist, IFE_META_MAX, tb[TCA_IFE_METALST]); if (metalist[IFE_META_SKBMARK]) { len = RTA_PAYLOAD(metalist[IFE_META_SKBMARK]); if (len) { mmark = rta_getattr_u32(metalist[IFE_META_SKBMARK]); fprintf(f, "use mark %u ", mmark); } else fprintf(f, "allow mark "); } if (metalist[IFE_META_TCINDEX]) { len = RTA_PAYLOAD(metalist[IFE_META_TCINDEX]); if (len) { mtcindex = rta_getattr_u16(metalist[IFE_META_TCINDEX]); fprintf(f, "use tcindex %d ", mtcindex); } else fprintf(f, "allow tcindex "); } if (metalist[IFE_META_PRIO]) { len = RTA_PAYLOAD(metalist[IFE_META_PRIO]); if (len) { mprio = rta_getattr_u32(metalist[IFE_META_PRIO]); fprintf(f, "use prio %u ", mprio); } else fprintf(f, "allow prio "); } } if (tb[TCA_IFE_DMAC]) { has_optional = 1; fprintf(f, "dst %s ", ll_addr_n2a(RTA_DATA(tb[TCA_IFE_DMAC]), RTA_PAYLOAD(tb[TCA_IFE_DMAC]), 0, b2, sizeof(b2))); } if (tb[TCA_IFE_SMAC]) { has_optional = 1; fprintf(f, "src %s ", ll_addr_n2a(RTA_DATA(tb[TCA_IFE_SMAC]), RTA_PAYLOAD(tb[TCA_IFE_SMAC]), 0, b2, sizeof(b2))); } fprintf(f, "\n\t index %u ref %d bind %d", p->index, p->refcnt, p->bindcnt); if (show_stats) { if (tb[TCA_IFE_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_IFE_TM]); print_tm(f, tm); } } fprintf(f, "\n"); return 0; }
static int parse_ipt(struct action_util *a,int *argc_p, char ***argv_p, int tca_id, struct nlmsghdr *n) { struct xtables_target *m = NULL; struct ipt_entry fw; struct rtattr *tail; int c; int rargc = *argc_p; char **argv = *argv_p; int argc = 0, iargc = 0; char k[16]; int size = 0; int iok = 0, ok = 0; __u32 hook = 0, index = 0; struct option *opts = NULL; xtables_init_all(&tcipt_globals, NFPROTO_IPV4); set_lib_dir(); { int i; for (i = 0; i < rargc; i++) { if (NULL == argv[i] || 0 == strcmp(argv[i], "action")) { break; } } iargc = argc = i; } if (argc <= 2) { fprintf(stderr,"bad arguements to ipt %d vs %d \n", argc, rargc); return -1; } while (1) { c = getopt_long(argc, argv, "j:", tcipt_globals.opts, NULL); if (c == -1) break; switch (c) { case 'j': m = xtables_find_target(optarg, XTF_TRY_LOAD); if (NULL != m) { if (0 > build_st(m, NULL)) { printf(" %s error \n", m->name); return -1; } #if (XTABLES_VERSION_CODE >= 6) opts = xtables_options_xfrm(tcipt_globals.orig_opts, tcipt_globals.opts, m->x6_options, &m->option_offset); #else opts = xtables_merge_options(tcipt_globals.orig_opts, tcipt_globals.opts, m->extra_opts, &m->option_offset); #endif if (opts == NULL) { fprintf(stderr, " failed to find aditional options for target %s\n\n", optarg); return -1; } else tcipt_globals.opts = opts; } else { fprintf(stderr," failed to find target %s\n\n", optarg); return -1; } ok++; break; default: memset(&fw, 0, sizeof (fw)); #if (XTABLES_VERSION_CODE >= 6) if (m != NULL && m->x6_parse != NULL ) { xtables_option_tpcall(c, argv, 0 , m, NULL); #else if (m != NULL && m->parse != NULL ) { m->parse(c - m->option_offset, argv, 0, &m->tflags, NULL, &m->t); #endif } else { fprintf(stderr,"failed to find target %s\n\n", optarg); return -1; } ok++; break; } } if (iargc > optind) { if (matches(argv[optind], "index") == 0) { if (get_u32(&index, argv[optind + 1], 10)) { fprintf(stderr, "Illegal \"index\"\n"); xtables_free_opts(1); return -1; } iok++; optind += 2; } } if (!ok && !iok) { fprintf(stderr," ipt Parser BAD!! (%s)\n", *argv); return -1; } /* check that we passed the correct parameters to the target */ #if (XTABLES_VERSION_CODE >= 6) if (m) xtables_option_tfcall(m); #else if (m && m->final_check) m->final_check(m->tflags); #endif { struct tcmsg *t = NLMSG_DATA(n); if (t->tcm_parent != TC_H_ROOT && t->tcm_parent == TC_H_MAJ(TC_H_INGRESS)) { hook = NF_IP_PRE_ROUTING; } else { hook = NF_IP_POST_ROUTING; } } tail = NLMSG_TAIL(n); addattr_l(n, MAX_MSG, tca_id, NULL, 0); fprintf(stdout, "tablename: %s hook: %s\n ", tname, ipthooks[hook]); fprintf(stdout, "\ttarget: "); if (m) m->print(NULL, m->t, 0); fprintf(stdout, " index %d\n", index); if (strlen(tname) > 16) { size = 16; k[15] = 0; } else { size = 1 + strlen(tname); } strncpy(k, tname, size); addattr_l(n, MAX_MSG, TCA_IPT_TABLE, k, size); addattr_l(n, MAX_MSG, TCA_IPT_HOOK, &hook, 4); addattr_l(n, MAX_MSG, TCA_IPT_INDEX, &index, 4); if (m) addattr_l(n, MAX_MSG, TCA_IPT_TARG, m->t, m->t->u.target_size); tail->rta_len = (void *) NLMSG_TAIL(n) - (void *) tail; argc -= optind; argv += optind; *argc_p = rargc - iargc; *argv_p = argv; optind = 0; xtables_free_opts(1); if (m) { /* Clear flags if target will be used again */ m->tflags = 0; m->used = 0; /* Free allocated memory */ if (m->t) free(m->t); } return 0; } static int print_ipt(struct action_util *au,FILE * f, struct rtattr *arg) { struct rtattr *tb[TCA_IPT_MAX + 1]; struct xt_entry_target *t = NULL; struct option *opts = NULL; if (arg == NULL) return -1; xtables_init_all(&tcipt_globals, NFPROTO_IPV4); set_lib_dir(); parse_rtattr_nested(tb, TCA_IPT_MAX, arg); if (tb[TCA_IPT_TABLE] == NULL) { fprintf(f, "[NULL ipt table name ] assuming mangle "); } else { fprintf(f, "tablename: %s ", rta_getattr_str(tb[TCA_IPT_TABLE])); } if (tb[TCA_IPT_HOOK] == NULL) { fprintf(f, "[NULL ipt hook name ]\n "); return -1; } else { __u32 hook; hook = rta_getattr_u32(tb[TCA_IPT_HOOK]); fprintf(f, " hook: %s \n", ipthooks[hook]); } if (tb[TCA_IPT_TARG] == NULL) { fprintf(f, "\t[NULL ipt target parameters ] \n"); return -1; } else { struct xtables_target *m = NULL; t = RTA_DATA(tb[TCA_IPT_TARG]); m = xtables_find_target(t->u.user.name, XTF_TRY_LOAD); if (NULL != m) { if (0 > build_st(m, t)) { fprintf(stderr, " %s error \n", m->name); return -1; } #if (XTABLES_VERSION_CODE >= 6) opts = xtables_options_xfrm(tcipt_globals.orig_opts, tcipt_globals.opts, m->x6_options, &m->option_offset); #else opts = xtables_merge_options(tcipt_globals.orig_opts, tcipt_globals.opts, m->extra_opts, &m->option_offset); #endif if (opts == NULL) { fprintf(stderr, " failed to find aditional options for target %s\n\n", optarg); return -1; } else tcipt_globals.opts = opts; } else { fprintf(stderr, " failed to find target %s\n\n", t->u.user.name); return -1; } fprintf(f, "\ttarget "); m->print(NULL, m->t, 0); if (tb[TCA_IPT_INDEX] == NULL) { fprintf(f, " [NULL ipt target index ]\n"); } else { __u32 index; index = rta_getattr_u32(tb[TCA_IPT_INDEX]); fprintf(f, " \n\tindex %d", index); } if (tb[TCA_IPT_CNT]) { struct tc_cnt *c = RTA_DATA(tb[TCA_IPT_CNT]);; fprintf(f, " ref %d bind %d", c->refcnt, c->bindcnt); } if (show_stats) { if (tb[TCA_IPT_TM]) { struct tcf_t *tm = RTA_DATA(tb[TCA_IPT_TM]); print_tm(f,tm); } } fprintf(f, " \n"); } xtables_free_opts(1); return 0; } struct action_util xt_action_util = { .id = "xt", .parse_aopt = parse_ipt, .print_aopt = print_ipt, };