int main(int argc, char **argv) { int opt; char *loader_un_path = "/tmp/osmocom_loader"; const char *debugopt; while((opt = getopt(argc, argv, "d:hl:m:v")) != -1) { switch(opt) { case 'd': debugopt = optarg; while(*debugopt) { setdebug(argv[0], *debugopt); debugopt++; } break; case 'l': loader_un_path = optarg; break; case 'm': puts("model selection not implemented"); exit(2); break; case 'v': version(argv[0]); break; case 'h': default: usage(argv[0]); break; } } osmoload.quit = 0; loader_connect(loader_un_path); loader_command(argv[0], argc - optind, argv + optind); while(!osmoload.quit) { osmo_select_main(0); } if(osmoload.binfile) { fclose(osmoload.binfile); } return 0; }
int main(int argc, char **argv) { const char *av = NULL; int func_total, file_total; char arg_dbpath[MAXPATHLEN]; const char *index = NULL; int optchar; int option_index = 0; STATISTICS_TIME *tim; arg_dbpath[0] = 0; basic_check(); /* * Setup GTAGSCONF and GTAGSLABEL environment variable * according to the --gtagsconf and --gtagslabel option. */ preparse_options(argc, argv); /* * Load configuration values. */ if (!vgetcwd(cwdpath, sizeof(cwdpath))) die("cannot get current directory."); openconf(cwdpath); configuration(); /* * setup_langmap() is needed to use decide_lang(). */ setup_langmap(langmap); save_environment(argc, argv); /* * insert htags_options at the head of argv. */ setenv_from_config(); { char *env = getenv("HTAGS_OPTIONS"); if (env && *env) argv = prepend_options(&argc, argv, env); } while ((optchar = getopt_long(argc, argv, "acd:DfFghIm:nNoqst:Tvwx", long_options, &option_index)) != EOF) { switch (optchar) { case 0: /* already flags set */ break; case OPT_AUTO_COMPLETION: auto_completion = 1; if (optarg) { if (atoi(optarg) > 0) auto_completion_limit = optarg; else die("The option value of --auto-completion must be numeric."); } break; case OPT_CFLOW: call_file = optarg; break; case OPT_CALL_TREE: call_file = optarg; break; case OPT_CALLEE_TREE: callee_file = optarg; break; case OPT_CVSWEB: cvsweb_url = optarg; break; case OPT_CVSWEB_CVSROOT: cvsweb_cvsroot = optarg; break; case OPT_GTAGSCONF: case OPT_GTAGSLABEL: /* These options are already parsed in preparse_options() */ break; case OPT_INSERT_FOOTER: insert_footer = optarg; break; case OPT_INSERT_HEADER: insert_header = optarg; break; case OPT_HTML_HEADER: { STATIC_STRBUF(sb); if (!test("r", optarg)) die("file '%s' not found.", optarg); strbuf_clear(sb); loadfile(optarg, sb); html_header = strbuf_value(sb); } break; case OPT_ITEM_ORDER: item_order = optarg; break; case OPT_TABS: if (atoi(optarg) > 0) tabs = atoi(optarg); else die("--tabs option requires numeric value."); break; case OPT_NCOL: if (atoi(optarg) > 0) ncol = atoi(optarg); else die("--ncol option requires numeric value."); break; case OPT_TREE_VIEW: tree_view = 1; if (optarg) tree_view_type = optarg; break; case 'a': aflag++; break; case 'd': strlimcpy(arg_dbpath, optarg, sizeof(arg_dbpath)); break; case 'D': dynamic = 1; break; case 'f': fflag++; break; case 'F': Fflag++; break; case 'g': gflag++; break; case 'h': definition_header = AFTER_HEADER; if (optarg) { if (!strcmp(optarg, "before")) definition_header = BEFORE_HEADER; else if (!strcmp(optarg, "right")) definition_header = RIGHT_HEADER; else if (!strcmp(optarg, "after")) definition_header = AFTER_HEADER; else die("The option value of --func-header must be one of 'before', 'right' and 'after'."); } break; case 'I': Iflag++; break; case 'm': main_func = optarg; break; case 'n': nflag++; if (optarg) { if (atoi(optarg) > 0) ncol = atoi(optarg); else die("The option value of --line-number must be numeric."); } break; case 'o': other_files = 1; break; case 's': symbol = 1; break; case 'T': table_flist = 1; if (optarg) { if (atoi(optarg) > 0) flist_fields = atoi(optarg); else die("The option value of the --table-flist must be numeric."); } break; case 't': title = optarg; break; case 'q': qflag++; setquiet(); break; case 'v': vflag++; setverbose(); break; case 'w': wflag++; break; default: usage(); break; } } /* * Leaving everything to htags. * Htags selects popular options for you. */ if (suggest2) suggest = 1; if (suggest) { int gtags_not_found = 0; char dbpath[MAXPATHLEN]; aflag = Iflag = nflag = vflag = 1; setverbose(); definition_header = AFTER_HEADER; other_files = symbol = show_position = table_flist = fixed_guide = 1; if (arg_dbpath[0]) { if (!test("f", makepath(arg_dbpath, dbname(GTAGS), NULL))) gtags_not_found = 1; } else if (gtagsexist(".", dbpath, sizeof(dbpath), 0) == 0) { gtags_not_found = 1; } if (gtags_not_found) gflag = 1; } if (suggest2) { Fflag = 1; /* uses frame */ fflag = dynamic = 1; /* needs a HTTP server */ auto_completion = tree_view = 1; /* needs javascript */ } if (call_file && !test("fr", call_file)) die("cflow file not found. '%s'", call_file); if (callee_file && !test("fr", callee_file)) die("cflow file not found. '%s'", callee_file); if (insert_header && !test("fr", insert_header)) die("page header file '%s' not found.", insert_header); if (insert_footer && !test("fr", insert_footer)) die("page footer file '%s' not found.", insert_footer); if (!fflag) auto_completion = 0; argc -= optind; argv += optind; if (!av) av = (argc > 0) ? *argv : NULL; if (debug) setdebug(); settabs(tabs); /* setup tab skip */ if (qflag) { setquiet(); vflag = 0; } if (show_version) version(av, vflag); if (show_help) help(); /* * Invokes gtags beforehand. */ if (gflag) { STRBUF *sb = strbuf_open(0); strbuf_puts(sb, gtags_path); if (vflag) strbuf_puts(sb, " -v"); if (wflag) strbuf_puts(sb, " -w"); if (suggest2 && enable_idutils && usable("mkid")) strbuf_puts(sb, " -I"); if (arg_dbpath[0]) { strbuf_putc(sb, ' '); strbuf_puts(sb, arg_dbpath); } if (system(strbuf_value(sb))) die("cannot execute gtags(1) command."); strbuf_close(sb); } /* * get dbpath. */ if (arg_dbpath[0]) { strlimcpy(dbpath, arg_dbpath, sizeof(dbpath)); } else { int status = setupdbpath(0); if (status < 0) die_with_code(-status, "%s", gtags_dbpath_error); strlimcpy(dbpath, get_dbpath(), sizeof(dbpath)); } if (!title) { char *p = strrchr(cwdpath, sep); title = p ? p + 1 : cwdpath; } if (cvsweb_url && test("d", "CVS")) use_cvs_module = 1; /* * decide directory in which we make hypertext. */ if (av) { char realpath[MAXPATHLEN]; if (!test("dw", av)) die("'%s' is not writable directory.", av); if (chdir(av) < 0) die("directory '%s' not found.", av); if (!vgetcwd(realpath, sizeof(realpath))) die("cannot get current directory"); if (chdir(cwdpath) < 0) die("cannot return to original directory."); snprintf(distpath, sizeof(distpath), "%s/HTML", realpath); } else { snprintf(distpath, sizeof(distpath), "%s/HTML", cwdpath); } /* * Existence check of tag files. */ { int i; const char *path; GTOP *gtop; for (i = GPATH; i < GTAGLIM; i++) { path = makepath(dbpath, dbname(i), NULL); gtags_exist[i] = test("fr", path); } /* * Real GRTAGS includes virtual GSYMS. */ gtags_exist[GSYMS] = symbol ? 1 : 0; if (!gtags_exist[GPATH] || !gtags_exist[GTAGS] || !gtags_exist[GRTAGS]) die("GPATH, GTAGS and/or GRTAGS not found. Please reexecute htags with the -g option."); /* * version check. * Do nothing, but the version of tag file will be checked. */ gtop = gtags_open(dbpath, cwdpath, GTAGS, GTAGS_READ, 0); gtags_close(gtop); /* * Check whether GRTAGS is empty. */ gtop = gtags_open(dbpath, cwdpath, GRTAGS, GTAGS_READ, 0); if (gtags_first(gtop, NULL, 0) == NULL) grtags_is_empty = 1; gtags_close(gtop); } /* * make dbpath absolute. */ { char buf[MAXPATHLEN]; if (realpath(dbpath, buf) == NULL) die("cannot get realpath of dbpath."); strlimcpy(dbpath, buf, sizeof(dbpath)); } /* * The older version (4.8.7 or former) of GPATH doesn't have files * other than source file. The oflag requires new version of GPATH. */ if (other_files) { GFIND *gp = gfind_open(dbpath, NULL, 0, 0); if (gp->version < 2) die("GPATH is old format. Please remake it by invoking gtags(1)."); gfind_close(gp); } /* * for global(1) and gtags(1). */ set_env("GTAGSROOT", cwdpath); set_env("GTAGSDBPATH", dbpath); set_env("GTAGSLIBPATH", ""); /*------------------------------------------------------------------ * MAKE FILES *------------------------------------------------------------------ * HTML/cgi-bin/global.cgi ... CGI program (1) * HTML/cgi-bin/ghtml.cgi ... unzip script (1) * HTML/.htaccess ... skeleton of .htaccess (1) * HTML/help.html ... help file (2) * HTML/R/ ... references (3) * HTML/D/ ... definitions (3) * HTML/search.html ... search index (4) * HTML/defines.html ... definitions index (5) * HTML/defines/ ... definitions index (5) * HTML/files/ ... file index (6) * HTML/index.html ... index file (7) * HTML/mains.html ... main index (8) * HTML/null.html ... main null html (8) * HTML/S/ ... source files (9) * HTML/I/ ... include file index (9) * HTML/rebuild.sh ... rebuild script (10) * HTML/style.css ... style sheet (11) *------------------------------------------------------------------ */ /* for clean up */ signal_setup(); sethandler(clean); HTML = normal_suffix; message("[%s] Htags started", now()); init_statistics(); /* * (#) check if GTAGS, GRTAGS is the latest. */ if (get_dbpath()) message(" Using %s/GTAGS.", get_dbpath()); if (grtags_is_empty) message(" GRTAGS is empty."); if (gpath_open(dbpath, 0) < 0) die("GPATH not found."); if (!w32) { /* UNDER CONSTRUCTION */ } if (auto_completion || tree_view) { STATIC_STRBUF(sb); strbuf_clear(sb); strbuf_puts_nl(sb, "<script type='text/javascript' src='js/jquery.js'></script>"); if (auto_completion) loadfile(makepath(datadir, "gtags/jscode_suggest", NULL), sb); if (tree_view) loadfile(makepath(datadir, "gtags/jscode_treeview", NULL), sb); jscode = strbuf_value(sb); } /* * (0) make directories */ message("[%s] (0) making directories ...", now()); if (!test("d", distpath)) if (mkdir(distpath, 0777) < 0) die("cannot make directory '%s'.", distpath); make_directory_in_distpath("files"); make_directory_in_distpath("defines"); make_directory_in_distpath(SRCS); make_directory_in_distpath(INCS); make_directory_in_distpath(INCREFS); if (!dynamic) { make_directory_in_distpath(DEFS); make_directory_in_distpath(REFS); if (symbol) make_directory_in_distpath(SYMS); } if (fflag || dynamic) make_directory_in_distpath("cgi-bin"); if (Iflag) make_directory_in_distpath("icons"); if (auto_completion || tree_view) make_directory_in_distpath("js"); /* * (1) make CGI program */ if (fflag || dynamic) { char cgidir[MAXPATHLEN]; snprintf(cgidir, sizeof(cgidir), "%s/cgi-bin", distpath); message("[%s] (1) making CGI program ...", now()); if (fflag || dynamic) makeprogram(cgidir, "global.cgi", 0755); if (auto_completion) makeprogram(cgidir, "completion.cgi", 0755); makehtaccess(cgidir, ".htaccess", 0644); } else { message("[%s] (1) making CGI program ...(skipped)", now()); } if (av) { const char *path = makepath(distpath, "GTAGSROOT", NULL); FILE *op = fopen(path, "w"); if (op == NULL) die("cannot make file '%s'.", path); fputs(cwdpath, op); fputc('\n', op); fclose(op); } /* * (2) make help file */ message("[%s] (2) making help.html ...", now()); makehelp("help.html"); /* * (#) load GPATH */ load_gpath(dbpath); /* * (3) make function entries (D/ and R/) * MAKING TAG CACHE */ message("[%s] (3) making tag lists ...", now()); cache_open(); tim = statistics_time_start("Time of making tag lists"); func_total = makedupindex(); statistics_time_end(tim); message("Total %d functions.", func_total); /* * (4) search index. (search.html) */ if (Fflag && fflag) { message("[%s] (4) making search index ...", now()); makesearchindex("search.html"); } { STRBUF *defines = strbuf_open(0); STRBUF *files = strbuf_open(0); /* * (5) make definition index (defines.html and defines/) * PRODUCE @defines */ message("[%s] (5) making definition index ...", now()); tim = statistics_time_start("Time of making definition index"); func_total = makedefineindex("defines.html", func_total, defines); statistics_time_end(tim); message("Total %d functions.", func_total); /* * (6) make file index (files.html and files/) * PRODUCE @files, %includes */ message("[%s] (6) making file index ...", now()); init_inc(); tim = statistics_time_start("Time of making file index"); file_total = makefileindex("files.html", files); statistics_time_end(tim); message("Total %d files.", file_total); html_count += file_total; /* * (7) make call tree using cflow(1)'s output (cflow.html) */ if (call_file || callee_file) { message("[%s] (7) making cflow index ...", now()); tim = statistics_time_start("Time of making cflow index"); if (call_file) if (makecflowindex("call.html", call_file) < 0) call_file = NULL; if (callee_file) if (makecflowindex("callee.html", callee_file) < 0) callee_file = NULL; statistics_time_end(tim); } /* * [#] make include file index. */ message("[%s] (#) making include file index ...", now()); tim = statistics_time_start("Time of making include file index"); makeincludeindex(); statistics_time_end(tim); /* * [#] make a common part for mains.html and index.html * USING @defines @files */ message("[%s] (#) making a common part ...", now()); index = makecommonpart(title, strbuf_value(defines), strbuf_value(files)); strbuf_close(defines); strbuf_close(files); } /* * (7)make index file (index.html) */ message("[%s] (7) making index file ...", now()); makeindex("index.html", title, index); /* * (8) make main index (mains.html) */ message("[%s] (8) making main index ...", now()); makemainindex("mains.html", index); /* * (9) make HTML files (SRCS/) * USING TAG CACHE, %includes and anchor database. */ message("[%s] (9) making hypertext from source code ...", now()); tim = statistics_time_start("Time of making hypertext"); makehtml(file_total); statistics_time_end(tim); /* * (10) rebuild script. (rebuild.sh) * * Don't grant execute permission to rebuild script. */ makerebuild("rebuild.sh"); if (chmod(makepath(distpath, "rebuild.sh", NULL), 0640) < 0) die("cannot chmod rebuild script."); /* * (11) style sheet file (style.css) */ if (enable_xhtml) { char src[MAXPATHLEN]; char dist[MAXPATHLEN]; snprintf(src, sizeof(src), "%s/gtags/style.css", datadir); snprintf(dist, sizeof(dist), "%s/style.css", distpath); copyfile(src, dist); } if (auto_completion || tree_view) { char src[MAXPATHLEN]; char dist[MAXPATHLEN]; snprintf(src, sizeof(src), "%s/gtags/jquery", datadir); snprintf(dist, sizeof(dist), "%s/js", distpath); copydirectory(src, dist); snprintf(src, sizeof(src), "%s/gtags/jquery/images", datadir); snprintf(dist, sizeof(dist), "%s/js/images", distpath); copydirectory(src, dist); } message("[%s] Done.", now()); if (vflag && (fflag || dynamic || auto_completion)) { message("\n[Information]\n"); message(" o Htags was invoked with the -f, -c, -D or --auto-completion option. You should"); message(" start http server so that cgi-bin/*.cgi is executed as a CGI script."); message("\n If you are using Apache, 'HTML/.htaccess' might be helpful for you.\n"); message(" Good luck!\n"); } if (Iflag) { char src[MAXPATHLEN]; char dist[MAXPATHLEN]; snprintf(src, sizeof(src), "%s/gtags/icons", datadir); snprintf(dist, sizeof(dist), "%s/icons", distpath); copydirectory(src, dist); } gpath_close(); /* * Print statistics information. */ print_statistics(statistics); clean(); return 0; }
END_TEST START_TEST(test_dhcp_nak_no_endmarker) { ip4_addr_t addr; ip4_addr_t netmask; ip4_addr_t gw; u8_t dhcp_nack_no_endmarker[] = { 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0x54, 0x75, 0xd0, 0x26, 0xd0, 0x0d, 0x08, 0x00, 0x45, 0x00, 0x01, 0x15, 0x38, 0x86, 0x00, 0x00, 0xff, 0x11, 0xc0, 0xa8, 0xc0, 0xa8, 0x01, 0x01, 0xff, 0xff, 0xff, 0xff, 0x00, 0x43, 0x00, 0x44, 0x01, 0x01, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x7a, 0xcb, 0xba, 0xf2, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x35, 0x01, 0x06, 0x36, 0x04, 0xc0, 0xa8, 0x01, 0x01, 0x31, 0xef, 0xad, 0x72, 0x31, 0x43, 0x4e, 0x44, 0x30, 0x32, 0x35, 0x30, 0x43, 0x52, 0x47, 0x44, 0x38, 0x35, 0x36, 0x3c, 0x08, 0x4d, 0x53, 0x46, 0x54, 0x20, 0x35, 0x2e, 0x30, 0x37, 0x0d, 0x01, 0x0f, 0x03, 0x06, 0x2c, 0x2e, 0x2f, 0x1f, 0x21, 0x79, 0xf9, 0x2b, 0xfc, 0xff, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0xe2, 0x71, 0xf3, 0x5b, 0xe2, 0x71, 0x2e, 0x01, 0x08, 0x03, 0x04, 0xc0, 0xa8, 0x01, 0x01, 0xff, 0xeb, 0x1e, 0x44, 0xec, 0xeb, 0x1e, 0x30, 0x37, 0x0c, 0x01, 0x0f, 0x03, 0x06, 0x2c, 0x2e, 0x2f, 0x1f, 0x21, 0x79, 0xf9, 0x2b, 0xff, 0x25, 0xc0, 0x09, 0xd6, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; u32_t xid; LWIP_UNUSED_ARG(_i); tcase = TEST_LWIP_DHCP_NAK_NO_ENDMARKER; setdebug(0); IP4_ADDR(&addr, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); IP4_ADDR(&gw, 0, 0, 0, 0); netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input); netif_set_up(&net_test); dhcp_start(&net_test); fail_unless(txpacket == 1); /* DHCP discover sent */ xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */ memcpy(&dhcp_offer[46], &xid, 4); send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); /* IP addresses should be zero */ fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t))); fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t))); fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ xid = htonl(netif_dhcp_data(&net_test)->xid); memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */ send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); fail_unless(netif_dhcp_data(&net_test)->state == DHCP_STATE_REQUESTING); fail_unless(txpacket == 2); /* No more sent */ xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */ memcpy(&dhcp_nack_no_endmarker[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, dhcp_nack_no_endmarker, sizeof(dhcp_nack_no_endmarker)); /* NAK should put us in another state for a while, no other way detecting it */ fail_unless(netif_dhcp_data(&net_test)->state != DHCP_STATE_REQUESTING); netif_remove(&net_test); }
END_TEST /* * Test case based on captured data where * replies are sent from a different IP than the * one the client unicasted to. */ START_TEST(test_dhcp_relayed) { u8_t relay_offer[] = { 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60, 0x08, 0x00, 0x45, 0x00, 0x01, 0x38, 0xfd, 0x53, 0x00, 0x00, 0x40, 0x11, 0x78, 0x46, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x51, 0x35, 0xb6, 0xa0, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00, 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00, 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08, 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1, 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04, 0x00, 0x00, 0x54, 0x49, 0x35, 0x01, 0x02, 0x36, 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff }; u8_t relay_ack1[] = { 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60, 0x08, 0x00, 0x45, 0x00, 0x01, 0x38, 0xfd, 0x55, 0x00, 0x00, 0x40, 0x11, 0x78, 0x44, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x51, 0x35, 0xb6, 0xa1, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00, 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00, 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08, 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1, 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04, 0x00, 0x00, 0x54, 0x49, 0x35, 0x01, 0x05, 0x36, 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff }; u8_t relay_ack2[] = { 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60, 0x08, 0x00, 0x45, 0x00, 0x01, 0x38, 0xfa, 0x18, 0x00, 0x00, 0x40, 0x11, 0x7b, 0x81, 0x4f, 0x8a, 0x32, 0x02, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x43, 0x00, 0x44, 0x01, 0x24, 0x00, 0x00, 0x02, 0x01, 0x06, 0x00, 0x49, 0x8b, 0x6e, 0xab, 0x00, 0x00, 0x00, 0x00, 0x4f, 0x8a, 0x33, 0x05, 0x4f, 0x8a, 0x33, 0x05, 0x00, 0x00, 0x00, 0x00, 0x0a, 0xb5, 0x04, 0x01, 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, 0x01, 0x04, 0xff, 0xff, 0xfe, 0x00, 0x03, 0x04, 0x4f, 0x8a, 0x32, 0x01, 0x06, 0x08, 0x4f, 0x8a, 0x00, 0xb4, 0x55, 0x08, 0x1f, 0xd1, 0x1c, 0x04, 0x4f, 0x8a, 0x33, 0xff, 0x33, 0x04, 0x00, 0x00, 0x54, 0x60, 0x35, 0x01, 0x05, 0x36, 0x04, 0x0a, 0xb5, 0x04, 0x01, 0xff }; const u8_t arp_resp[] = { 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, /* DEST */ 0x00, 0x22, 0x93, 0x5a, 0xf7, 0x60, /* SRC */ 0x08, 0x06, /* Type: ARP */ 0x00, 0x01, /* HW: Ethernet */ 0x08, 0x00, /* PROTO: IP */ 0x06, /* HW size */ 0x04, /* PROTO size */ 0x00, 0x02, /* OPCODE: Reply */ 0x12, 0x34, 0x56, 0x78, 0x9a, 0xab, /* Target MAC */ 0x4f, 0x8a, 0x32, 0x01, /* Target IP */ 0x00, 0x23, 0xc1, 0x00, 0x06, 0x50, /* src mac */ 0x4f, 0x8a, 0x33, 0x05, /* src ip */ /* Padding follows.. */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00 }; ip4_addr_t addr; ip4_addr_t netmask; ip4_addr_t gw; int i; u32_t xid; LWIP_UNUSED_ARG(_i); tcase = TEST_LWIP_DHCP_RELAY; setdebug(0); IP4_ADDR(&addr, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); IP4_ADDR(&gw, 0, 0, 0, 0); netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input); netif_set_up(&net_test); dhcp_start(&net_test); fail_unless(txpacket == 1); /* DHCP discover sent */ /* IP addresses should be zero */ fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t))); fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t))); fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ xid = htonl(netif_dhcp_data(&net_test)->xid); memcpy(&relay_offer[46], &xid, 4); /* insert correct transaction id */ send_pkt(&net_test, relay_offer, sizeof(relay_offer)); /* request sent? */ fail_unless(txpacket == 2, "txpkt = %d, should be 2", txpacket); xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */ memcpy(&relay_ack1[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, relay_ack1, sizeof(relay_ack1)); for (i = 0; i < 25; i++) { tick_lwip(); } fail_unless(txpacket == 5, "txpkt should be 5, is %d", txpacket); /* ARP requests sent */ /* Interface up */ fail_unless(netif_is_up(&net_test)); /* Now it should have taken the IP */ IP4_ADDR(&addr, 79, 138, 51, 5); IP4_ADDR(&netmask, 255, 255, 254, 0); IP4_ADDR(&gw, 79, 138, 50, 1); fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t))); fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t))); fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 5, "txpacket = %d", txpacket); for (i = 0; i < 108000 - 25; i++) { tick_lwip(); } fail_unless(netif_is_up(&net_test)); fail_unless(txpacket == 6, "txpacket = %d", txpacket); /* We need to send arp response here.. */ send_pkt(&net_test, arp_resp, sizeof(arp_resp)); fail_unless(txpacket == 7, "txpacket = %d", txpacket); fail_unless(netif_is_up(&net_test)); xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */ memcpy(&relay_ack2[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, relay_ack2, sizeof(relay_ack2)); for (i = 0; i < 100000; i++) { tick_lwip(); } fail_unless(txpacket == 7, "txpacket = %d", txpacket); netif_remove(&net_test); }
END_TEST /* * Test that IP address is not taken and NAK is sent if someone * replies to ARP requests for the offered address. */ START_TEST(test_dhcp_nak) { ip4_addr_t addr; ip4_addr_t netmask; ip4_addr_t gw; u32_t xid; LWIP_UNUSED_ARG(_i); tcase = TEST_LWIP_DHCP; setdebug(0); IP4_ADDR(&addr, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); IP4_ADDR(&gw, 0, 0, 0, 0); netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input); netif_set_up(&net_test); dhcp_start(&net_test); fail_unless(txpacket == 1); /* DHCP discover sent */ xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */ memcpy(&dhcp_offer[46], &xid, 4); send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); /* IP addresses should be zero */ fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t))); fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t))); fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ xid = htonl(netif_dhcp_data(&net_test)->xid); memcpy(&dhcp_offer[46], &xid, 4); /* insert correct transaction id */ send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); fail_unless(txpacket == 2); /* DHCP request sent */ xid = netif_dhcp_data(&net_test)->xid; /* Write bad xid, not using htonl! */ memcpy(&dhcp_ack[46], &xid, 4); send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack)); fail_unless(txpacket == 2); /* No more sent */ xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */ memcpy(&dhcp_ack[46], &xid, 4); /* insert transaction id */ send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack)); fail_unless(txpacket == 3); /* ARP request sent */ tcase = TEST_LWIP_DHCP_NAK; /* Switch testcase */ /* Send arp reply, mark offered IP as taken */ send_pkt(&net_test, arpreply, sizeof(arpreply)); fail_unless(txpacket == 4); /* DHCP nak sent */ netif_remove(&net_test); }
END_TEST START_TEST(test_dhcp_invalid_overload) { u8_t dhcp_offer_invalid_overload[] = { 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, /* To unit */ 0x00, 0x0F, 0xEE, 0x30, 0xAB, 0x22, /* From Remote host */ 0x08, 0x00, /* Protocol: IP */ 0x45, 0x10, 0x01, 0x48, 0x00, 0x00, 0x00, 0x00, 0x80, 0x11, 0x36, 0xcc, 0xc3, 0xaa, 0xbd, 0xab, 0xc3, 0xaa, 0xbd, 0xc8, /* IP header */ 0x00, 0x43, 0x00, 0x44, 0x01, 0x34, 0x00, 0x00, /* UDP header */ 0x02, /* Type == Boot reply */ 0x01, 0x06, /* Hw Ethernet, 6 bytes addrlen */ 0x00, /* 0 hops */ 0xAA, 0xAA, 0xAA, 0xAA, /* Transaction id, will be overwritten */ 0x00, 0x00, /* 0 seconds elapsed */ 0x00, 0x00, /* Flags (unicast) */ 0x00, 0x00, 0x00, 0x00, /* Client ip */ 0xc3, 0xaa, 0xbd, 0xc8, /* Your IP */ 0xc3, 0xaa, 0xbd, 0xab, /* DHCP server ip */ 0x00, 0x00, 0x00, 0x00, /* relay agent */ 0x00, 0x23, 0xc1, 0xde, 0xd0, 0x0d, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* MAC addr + padding */ /* Empty server name */ 0x34, 0x01, 0x02, 0xff, /* Overload: SNAME + END */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Empty boot file name */ 0x34, 0x01, 0x01, 0xff, /* Overload FILE + END */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x63, 0x82, 0x53, 0x63, /* Magic cookie */ 0x35, 0x01, 0x02, /* Message type: Offer */ 0x36, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Server identifier (IP) */ 0x33, 0x04, 0x00, 0x00, 0x00, 0x78, /* Lease time 2 minutes */ 0x03, 0x04, 0xc3, 0xaa, 0xbd, 0xab, /* Router IP */ 0x01, 0x04, 0xff, 0xff, 0xff, 0x00, /* Subnet mask */ 0x34, 0x01, 0x03, /* Overload: FILE + SNAME */ 0xff, /* End option */ 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, /* Padding */ }; ip4_addr_t addr; ip4_addr_t netmask; ip4_addr_t gw; u32_t xid; LWIP_UNUSED_ARG(_i); tcase = TEST_LWIP_DHCP_INVALID_OVERLOAD; setdebug(0); IP4_ADDR(&addr, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); IP4_ADDR(&gw, 0, 0, 0, 0); netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input); netif_set_up(&net_test); dhcp_start(&net_test); fail_unless(txpacket == 1); /* DHCP discover sent */ xid = htonl(netif_dhcp_data(&net_test)->xid); memcpy(&dhcp_offer_invalid_overload[46], &xid, 4); /* insert correct transaction id */ dhcp_offer_invalid_overload[311] = 3; send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload)); /* IP addresses should be zero */ fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t))); fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t))); fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ dhcp_offer_invalid_overload[311] = 2; send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload)); /* IP addresses should be zero */ fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t))); fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t))); fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ dhcp_offer_invalid_overload[311] = 1; send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer_invalid_overload)); /* IP addresses should be zero */ fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(ip4_addr_t))); fail_if(memcmp(&netmask, &net_test.netmask, sizeof(ip4_addr_t))); fail_if(memcmp(&gw, &net_test.gw, sizeof(ip4_addr_t))); fail_unless(txpacket == 1); /* Nothing more sent */ dhcp_offer_invalid_overload[311] = 0; send_pkt(&net_test, dhcp_offer_invalid_overload, sizeof(dhcp_offer)); fail_unless(netif_dhcp_data(&net_test)->state == DHCP_STATE_REQUESTING); fail_unless(txpacket == 2); /* No more sent */ xid = htonl(netif_dhcp_data(&net_test)->xid); /* xid updated */ netif_remove(&net_test); }
END_TEST /* * Test that IP address is not taken and NAK is sent if someone * replies to ARP requests for the offered address. */ START_TEST(test_dhcp_nak) { struct ip_addr addr; struct ip_addr netmask; struct ip_addr gw; u32_t xid; LWIP_UNUSED_ARG(_i); tcase = TEST_LWIP_DHCP; setdebug(0); IP4_ADDR(&addr, 0, 0, 0, 0); IP4_ADDR(&netmask, 0, 0, 0, 0); IP4_ADDR(&gw, 0, 0, 0, 0); netif_add(&net_test, &addr, &netmask, &gw, &net_test, testif_init, ethernet_input); dhcp_start(&net_test); fail_unless(txpacket == 1); // DHCP discover sent xid = net_test.dhcp->xid; // Write bad xid, not using htonl! memcpy(&dhcp_offer[46], &xid, 4); send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); // Interface down fail_if(netif_is_up(&net_test)); // IP addresses should be zero fail_if(memcmp(&addr, &net_test.ip_addr, sizeof(struct ip_addr))); fail_if(memcmp(&netmask, &net_test.netmask, sizeof(struct ip_addr))); fail_if(memcmp(&gw, &net_test.gw, sizeof(struct ip_addr))); fail_unless(txpacket == 1); // Nothing more sent xid = htonl(net_test.dhcp->xid); memcpy(&dhcp_offer[46], &xid, 4); // insert correct transaction id send_pkt(&net_test, dhcp_offer, sizeof(dhcp_offer)); fail_unless(txpacket == 2); // DHCP request sent xid = net_test.dhcp->xid; // Write bad xid, not using htonl! memcpy(&dhcp_ack[46], &xid, 4); send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack)); fail_unless(txpacket == 2); // No more sent xid = htonl(net_test.dhcp->xid); // xid updated memcpy(&dhcp_ack[46], &xid, 4); // insert transaction id send_pkt(&net_test, dhcp_ack, sizeof(dhcp_ack)); fail_unless(txpacket == 3); // ARP request sent tcase = TEST_LWIP_DHCP_NAK; // Switch testcase // Send arp reply, mark offered IP as taken send_pkt(&net_test, arpreply, sizeof(arpreply)); fail_unless(txpacket == 4); // DHCP nak sent netif_remove(&net_test); }
/************************************************************* ** MAIN ** Reads the config file. Binds to a port. Launches the ** worker and other threads. Listens for requests. */ int main(int argc, char **argv) { char * conf = NULL; int nthreads; int c, ret; char** ary; char* prog; u_int numfds = 0; int xerror; int bg_flag = TRUE; int only_check_config; char ebuf[BUFSIZ]; char nbuf[64]; u_char * gifimage = NULL; u_char * favicon = NULL; int gifimagelen = 0; int faviconlen = 0; TPOOL_CTX * tpool_ctx = NULL; DEBUGS * dp; LISTEN_CTX *gctx = NULL; LISTEN_CTX *dctx = NULL; LISTEN_CTX *jctx = NULL; pthread_t gif_tid; pthread_t data_tid; pthread_t upload_tid; #if HAVE_RESOURCE_H || HAVE_SYS_RESOURCE_H struct rlimit rl; #endif time_t servertimeout; prog = basename(argv[0]); if (prog == NULL) prog = argv[0]; only_check_config = FALSE; servertimeout = 20; while ((c = getopt(argc, argv, "C:d:c:f")) != -1) { switch (c) { case 'f': bg_flag = FALSE; break; case 'C': only_check_config = TRUE; /*FALLTHROUGH*/ case 'c': conf = optarg; break; case 'd': /* * Debugging turned of if in background */ if (bg_flag == TRUE) break; for (dp = DebugNames; dp->name != NULL; dp++) { if (strncasecmp(dp->name, optarg, strlen(optarg)) == 0) { setdebug(dp->flag); if (dp->flag == BUG_THREADS || dp->flag == BUG_ALL) tpool_debug_set(TRUE); break; } } if (dp->name != NULL) break; printf("Unknown debug flag: %s, select from:\n", optarg); for (dp = DebugNames; dp->name != NULL; dp++) printf("\t%s\n", dp->name); return 0; case '?': default: usage: printf("Usage: %s " "[-c /path/onepixd.conf " "or " "-C /path/onepixd.conf] " "-d what " "[-f]\n", prog); return 0; } } if (argc != optind) goto usage; if (conf == NULL || strlen(conf) == 0) { (void) fprintf(stderr, "Required Parameter \"-c configfile\" missing.\n"); goto usage; } ret = config_read_file(conf); if (ret != 0) return 0; /* * All errors are logged, so we need to set up * logging before checking the config file. */ ary = config_lookup(CONF_LOG_FACILITY); if (ary == NULL) ret = log_init(DEFAULT_LOG_FACILITY, prog); else ret = log_init(ary[0], prog); if (ret != 0) return 0; ret = config_validate(); if (ret != 0) return 0; if (ret == 0 && only_check_config == TRUE) (void) printf("%s: FILE IS OKAY TO USE\n", conf); if (only_check_config == TRUE) return 0; ary = config_lookup(CONF_HOME_DIR); if (ary != NULL) { if (chdir(ary[0]) != 0) { xerror = errno; (void) snprintf(ebuf, sizeof ebuf, "Attempt to chdir(\"%s\"): %s", ary[0], strerror(xerror)); log_emit(LOG_ERR, NULL, __FILE__, __LINE__, ebuf); goto shutdown_server; } } if (util_bg(bg_flag) != 0) { (void) snprintf(ebuf, sizeof ebuf, "fork(): %s\n", strerror(errno)); log_emit(LOG_ERR, NULL, __FILE__, __LINE__, ebuf); goto shutdown_server; } #if HAVE_RESOURCE_H || HAVE_SYS_RESOURCE_H #ifdef RLIMIT_AS rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; (void) setrlimit(RLIMIT_AS, &rl); /* max size virtual memory */ #endif #ifdef RLIMIT_CPU rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; (void) setrlimit(RLIMIT_CPU, &rl); /* unlimited CPU usage */ #endif #ifdef RLIMIT_DATA rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; (void) setrlimit(RLIMIT_DATA, &rl); /* unlimited memory */ #endif #ifdef RLIMIT_FSIZE rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; (void) setrlimit(RLIMIT_FSIZE, &rl); /* unlimited file sizes */ #endif #ifdef RLIMIT_STACK rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; (void) setrlimit(RLIMIT_STACK, &rl); /* unlimited stack */ #endif #ifdef RLIMIT_CORE rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; (void) setrlimit(RLIMIT_CORE, &rl); /* allow core dumps */ #endif #ifdef RLIMIT_NOFILE rl.rlim_cur = RLIM_INFINITY; rl.rlim_max = RLIM_INFINITY; (void) setrlimit(RLIMIT_NOFILE, &rl); /* maximum file descriptors */ (void) getrlimit(RLIMIT_NOFILE, &rl); numfds = (rl.rlim_cur); #endif #endif /* HAVE_RESOURCE_H */ ary = config_lookup(CONF_BECOME_USER); if (ary != NULL) { /* * Note that setrunasuser() logs its own errors */ if (setrunasuser(ary[0]) != 0) goto shutdown_server; } ary = config_lookup(CONF_NUMTHREADS); if (ary == NULL) nthreads = DEFAULT_THREADS; else { nthreads = strtoul(ary[0], NULL, 10); if (nthreads <= 0) { (void) fprintf(stderr, "Configuration item \"%s\" illegal value: %d.\n", CONF_NUMTHREADS, nthreads); return 0; } } if (numfds == 0 || numfds > (nthreads * 3)) numfds = nthreads * 3; (void) io_init(numfds); gifimage = gif_get1x1gif(&gifimagelen); if (gifimage == NULL) { (void) snprintf(ebuf, sizeof ebuf, "Load gif image: %s\n", strerror(errno)); log_emit(LOG_ERR, NULL, __FILE__, __LINE__, ebuf); goto shutdown_server; } favicon = gif_getfavicon_ico(&faviconlen); /* * Prepare to launch the thread to listen for inbound * gif requests. */ gctx = str_alloc(sizeof(LISTEN_CTX), 1, __FILE__, __LINE__); if (gctx == NULL) { (void) snprintf(ebuf, sizeof ebuf, "alloc(): %s\n", strerror(errno)); log_emit(LOG_ERR, NULL, __FILE__, __LINE__, ebuf); goto shutdown_server; } gctx->type = LISTEN_TYPE_GIF; /* gif image only, no favicon */ gctx->gifimage = gifimage; gctx->gifimagelen = gifimagelen; gctx->favicon = NULL; gctx->faviconlen = 0; ary = config_lookup(CONF_GIF_PORT); if (ary == NULL) (void) strlcpy(gctx->port, "80", sizeof gctx->port); else (void) strlcpy(gctx->port, ary[0], sizeof gctx->port); ary = config_lookup(CONF_GIF_INTERFACE); if (ary == NULL) (void) strlcpy(gctx->interface, "INADDR_ANY", sizeof gctx->interface); else (void) strlcpy(gctx->interface, ary[0], sizeof gctx->interface); /* * Everything from here down must be thread safe. */ tpool_ctx = tpool_init(nthreads, ebuf, sizeof ebuf); if (tpool_ctx == NULL) goto shutdown_server; gctx->tpool_ctx = tpool_ctx; /* * The GIF server may have to bind to a privaliged port, such * as port 80, so we launch it and expect it to change the * user id after its bind. */ ret = pthread_create(&gif_tid, NULL, thread_listener, (void *)gctx); if (ret != 0) { } /* * Prepare to launch the thread to listen for inbound * data requests. */ dctx = str_alloc(sizeof(LISTEN_CTX), 1, __FILE__, __LINE__); if (dctx == NULL) { (void) snprintf(ebuf, sizeof ebuf, "alloc(): %s\n", strerror(errno)); log_emit(LOG_ERR, NULL, __FILE__, __LINE__, ebuf); goto shutdown_server; } dctx->type = LISTEN_TYPE_DATA; /* a favicon only, no gif */ dctx->gifimage = NULL; dctx->favicon = favicon; dctx->gifimagelen = 0; dctx->faviconlen = faviconlen; ary = config_lookup(CONF_DATA_PORT); if (ary == NULL) (void) strlcpy(dctx->port, "8100", sizeof dctx->port); else (void) strlcpy(dctx->port, ary[0], sizeof dctx->port); ary = config_lookup(CONF_DATA_INTERFACE); if (ary == NULL) (void) strlcpy(dctx->interface, "INADDR_ANY", sizeof dctx->interface); else (void) strlcpy(dctx->interface, ary[0], sizeof dctx->interface); dctx->tpool_ctx = tpool_ctx; ret = pthread_create(&data_tid, NULL, thread_listener, (void *)dctx); if (ret != 0) { } /* * Prepare to launch the thread to listen for uploads of more. */ (void) verify_threads_locking_init(); jctx = str_alloc(sizeof(LISTEN_CTX), 1, __FILE__, __LINE__); if (jctx == NULL) { (void) snprintf(ebuf, sizeof ebuf, "alloc(): %s\n", strerror(errno)); log_emit(LOG_ERR, NULL, __FILE__, __LINE__, ebuf); goto shutdown_server; } jctx->type = LISTEN_TYPE_UPLOAD; /* a favicon only, no gif */ jctx->gifimage = NULL; jctx->favicon = favicon; jctx->gifimagelen = 0; jctx->faviconlen = faviconlen; ary = config_lookup(CONF_UPLOAD_PORT); if (ary == NULL) (void) strlcpy(jctx->port, "8101", sizeof jctx->port); else (void) strlcpy(jctx->port, ary[0], sizeof jctx->port); ary = config_lookup(CONF_UPLOAD_INTERFACE); if (ary == NULL) (void) strlcpy(jctx->interface, "INADDR_ANY", sizeof jctx->interface); else (void) strlcpy(jctx->interface, ary[0], sizeof jctx->interface); jctx->tpool_ctx = tpool_ctx; ret = pthread_create(&upload_tid, NULL, thread_listener, (void *)jctx); if (ret != 0) { } ary = config_lookup(CONF_PIDFILE); if (ary != NULL) { /* * Note that write_pid_file() logs its own errors */ if (write_pid_file(ary[0]) != 0) goto shutdown_server; } (void) strlcpy(ebuf, "Startup: version=", sizeof ebuf); (void) strlcat(ebuf, VERSION, sizeof ebuf); (void) strlcat(ebuf, ", gif_interface=", sizeof ebuf); (void) strlcat(ebuf, gctx->interface, sizeof ebuf); (void) strlcat(ebuf, ", gif_port=", sizeof ebuf); (void) strlcat(ebuf, gctx->port, sizeof ebuf); (void) strlcat(ebuf, ", data_interface=", sizeof ebuf); (void) strlcat(ebuf, dctx->interface, sizeof ebuf); (void) strlcat(ebuf, ", data_port=", sizeof ebuf); (void) strlcat(ebuf, dctx->port, sizeof ebuf); (void) strlcat(ebuf, ", threads=", sizeof ebuf); (void) str_ultoa(nthreads, nbuf, sizeof nbuf); (void) strlcat(ebuf, nbuf, sizeof ebuf); log_emit(LOG_INFO, NULL, __FILE__, __LINE__, ebuf); Global_Die = FALSE; (void) signal(SIGINT, catch_sig); (void) signal(SIGQUIT, catch_sig); (void) signal(SIGKILL, catch_sig); (void) signal(SIGTERM, catch_sig); (void) signal(SIGPIPE, SIG_IGN); if (Global_Die == TRUE) goto shutdown_server; (void) file_prune_garbage(&Global_Die); shutdown_server: Global_Die = TRUE; (void) pthread_join(data_tid, NULL); (void) pthread_join(gif_tid, NULL); (void) pthread_join(upload_tid, NULL); (void) verify_threads_locking_shutdown(); if (tpool_ctx != NULL) tpool_ctx = tpool_shutdown(tpool_ctx, ebuf, sizeof ebuf); if (gctx != NULL) gctx = str_free(gctx, __FILE__, __LINE__); if (dctx != NULL) dctx = str_free(dctx, __FILE__, __LINE__); if (jctx != NULL) jctx = str_free(jctx, __FILE__, __LINE__); if (gifimage != NULL) gifimage = str_free(gifimage, __FILE__, __LINE__); if (favicon != NULL) favicon = str_free(favicon, __FILE__, __LINE__); ary = config_lookup(CONF_PIDFILE); if (ary != NULL) (void) unlink(ary[0]); io_shutdown(); config_shutdown(); (void) strlcpy(ebuf, "Shutdown: version=", sizeof ebuf); (void) strlcat(ebuf, VERSION, sizeof ebuf); log_emit(LOG_INFO, NULL, __FILE__, __LINE__, ebuf); /* * This str_shutdown must always be last. */ str_shutdown(); return 0; }