int main (int argc, char *argv[]) { /* Query Processor */ int stat = 0; char * query = proc_query(); static char cmd[MAX_COMMAND_SIZE]; if(query == NULL) return -1; printf("\n %s", query); sprintf(cmd, "echo '%s' | awk -f ../src/table.awk", query); system(cmd); return 0; }
void *__hxcpp_get_proc_address(String inLib, String full_name,bool inNdllProc,bool inQuietFail) { String bin = __hxcpp_get_bin_dir(); String deviceExt = __hxcpp_get_dll_extension(); #ifdef HX_ANDROID String module_name = HX_CSTRING("lib") + inLib; #else String module_name = inLib; #endif #ifdef IPHONE gLoadDebug = true; setenv("DYLD_PRINT_APIS","1",true); #elif !defined(HX_WINRT) gLoadDebug = gLoadDebug || getenv("HXCPP_LOAD_DEBUG"); #endif if (!sgLibPathIsInit) { sgLibPathIsInit = true; sgLibPath.push_back("./"); #ifdef HX_MACOS sgLibPath.push_back("@executable_path/"); #endif sgLibPath.push_back(""); #ifdef HXCPP_TRY_HAXELIB String hxcpp = GetEnv("HXCPP"); if (hxcpp.length==0) hxcpp = FindHaxelib( HX_CSTRING("hxcpp") ); if (hxcpp.length!=0) __hxcpp_push_dll_path(hxcpp+HX_CSTRING("/bin/") + bin + HX_CSTRING("/")); #endif } Module module = sgLoadedModule[module_name.__s]; bool new_module = module==0; if (!module && sgRegisteredPrims) { void *registered = (*sgRegisteredPrims)[full_name.__CStr()]; // Try with lib name ... if (!registered) { String libString = inLib + HX_CSTRING("_") + full_name; registered = (*sgRegisteredPrims)[libString.__CStr()]; } if (registered) return registered; } if (!module && gLoadDebug) { #ifdef ANDROID __android_log_print(ANDROID_LOG_INFO, "loader", "Searching for %s...", module_name.__s); #else printf("Searching for %s...\n", inLib.__s); #endif } String haxelibPath; for(int e=0; module==0 && e<3; e++) { String extension = e==0 ? deviceExt : e==1 ? HX_CSTRING(".ndll") : HX_CSTRING(""); for(int path=0;path<sgLibPath.size();path++) { String testPath = String( sgLibPath[path].c_str() ) + module_name + extension; if (gLoadDebug) { #ifndef ANDROID printf(" try %s...\n", testPath.__s); #else __android_log_print(ANDROID_LOG_INFO, "loader", "Try %s", testPath.__s); #endif } module = hxLoadLibrary(testPath); if (module) { if (gLoadDebug) { #ifndef ANDROID printf("Found %s\n", testPath.__s); #else __android_log_print(ANDROID_LOG_INFO, "loader", "Found %s", testPath.__s); #endif } break; } } #ifdef HXCPP_TRY_HAXELIB if (!module) { if (e==0) haxelibPath = FindHaxelib(inLib); if (haxelibPath.length!=0) { String testPath = haxelibPath + HX_CSTRING("/ndll/") + bin + HX_CSTRING("/") + inLib + extension; if (gLoadDebug) printf(" try %s...\n", testPath.__s); module = hxLoadLibrary(testPath); if (module && gLoadDebug) { printf("Found %s\n", testPath.__s); } } } #endif } if (!module) { hx::Throw(HX_CSTRING("Could not load module ") + inLib + HX_CSTRING("@") + full_name); } if (new_module) { sgLoadedModule[module_name.__s] = module; sgOrderedModules.push_back(module); SetLoaderProcFunc set_loader = (SetLoaderProcFunc)hxFindSymbol(module,"hx_set_loader"); if (set_loader) set_loader(hx_cffi); GetNekoEntryFunc func = (GetNekoEntryFunc)hxFindSymbol(module,"__neko_entry_point"); if (func) { NekoEntryFunc entry = (NekoEntryFunc)func(); if (entry) entry(); } } FundFunc proc_query = (FundFunc)hxFindSymbol(module,full_name.__CStr()); if (!proc_query) proc_query = (FundFunc)hxFindSymbol(module, (inLib + HX_CSTRING("_") + full_name).__CStr()); if (!proc_query && !inQuietFail) { #ifdef ANDROID __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not find primitive %s in %p", full_name.__CStr(), module); #else fprintf(stderr,"Could not find primitive %s.\n", full_name.__CStr()); #endif return 0; } if (!inNdllProc) return (void *)proc_query; void *proc = proc_query(); if (!proc && !inQuietFail) { #ifdef ANDROID __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not identify primitive %s in %s", full_name.__CStr(), inLib.__CStr() ); #else fprintf(stderr,"Could not identify primitive %s in %s\n", full_name.__CStr(),inLib.__CStr()); #endif } return proc; }
int main(int argc, char **argv) { js_string *mararc_loc, *errors, *chrootn, *uidstr, *maxpstr, *kvar_query, *bind_address, *incoming, *uncomp, *verbstr; unsigned char chroot_zt[255]; uid_t uid; gid_t gid; int errorn, value, sock, maxprocs, counter; int cache_size; int min_ttl_n = 300, min_ttl_c = 300; int max_glueless; /* Maximum allowed glueless level */ int max_q_total; /* Maximum total queries in attempt to resolve hostname */ int timeout; /* Maximum time to wait for a remote server when performing a recursive query */ struct sockaddr client; struct sockaddr_in *clin; /* So we can log the IP */ #ifndef DARWIN struct rlimit rlim; #endif clin = (struct sockaddr_in *)&client; /* Initialize the strings (allocate memory for them, etc.) */ if((mararc_loc = js_create(256,1)) == 0) harderror(L_MLC); /* "Could not create mararc_loc string" */ if(js_set_encode(mararc_loc,MARA_LOCALE) == JS_ERROR) harderror(L_MLL); /* "Could not set locale for mararc_loc string" */ if((errors = js_create(256,1)) == 0) harderror(L_EC); /* "Could not create errors string" */ if(js_set_encode(errors,MARA_LOCALE) == JS_ERROR) harderror(L_EL); /* "Could not set locale for errors string" */ if((uidstr = js_create(256,1)) == 0) harderror(L_UC); /* "Could not create uidstr string" */ if(js_set_encode(uidstr,MARA_LOCALE) == JS_ERROR) harderror(L_UL); /* "Could not set locale for uidstr string" */ if((verbstr = js_create(256,1)) == 0) harderror(L_VC); /* "Could not create verbstr string" */ if(js_set_encode(verbstr,MARA_LOCALE) == JS_ERROR) harderror(L_VL); /* "Could not set locale for verbstr string" */ if((maxpstr = js_create(256,1)) == 0) harderror(L_MC); /* "Could not create maxpstr string" */ if(js_set_encode(maxpstr,MARA_LOCALE) == JS_ERROR) harderror(L_ML); /* "Could not set locale for maxpstr string" */ if((chrootn = js_create(256,1)) == 0) harderror(L_CC); /* "Could not create chrootn string" */ if(js_set_encode(chrootn,MARA_LOCALE) == JS_ERROR) harderror(L_CL); /* "Could not set locale for chrootn string" */ if((kvar_query = js_create(256,1)) == 0) harderror(L_KQC); /* "Could not create kvar_query string" */ if(js_set_encode(kvar_query,MARA_LOCALE) == JS_ERROR) harderror(L_KQL); /* "Could not set locale for kvar_query string" */ if((bind_address = js_create(64,1)) == 0) harderror(L_BAC); /* "Could not create bins_address string" */ if(js_set_encode(bind_address,MARA_LOCALE) == JS_ERROR) harderror(L_BAL); /* "Could not set locale for bind_address string" */ if((incoming = js_create(768,1)) == 0) harderror(L_IC); /* "Could not create incoming string" */ if(js_set_encode(incoming,MARA_LOCALE) == JS_ERROR) harderror(L_IL); /* "Could not set locale for incoming string" */ if((uncomp = js_create(768,1)) == 0) harderror(L_UCC); /* "Could not create uncomp string" */ if(js_set_encode(uncomp,MARA_LOCALE) == JS_ERROR) harderror(L_UCL); /* "Could not set locale for uncomp string" */ /* First, find the mararc file */ if(argc == 1) { /* No arguments */ if(find_mararc(mararc_loc) == JS_ERROR) harderror(L_LOC_MARARC); /* "Error locating mararc file" */ } else if(argc==3) { /* maradns -f /wherever/mararc */ if(js_qstr2js(mararc_loc,argv[2]) == JS_ERROR) harderror(L_MARARC_ARG); /* "Could not get mararc from command line" */ } else harderror(L_USAGE); /* "Usage: mararc [-f mararc_location]" */ /* Then parse that file */ if(read_mararc(mararc_loc,errors,&errorn) == JS_ERROR) { harderror(L_MARARC_PARSE); /* "Error parsing contents of mararc file" */ } if(errorn != 0) { /* Print this out at log level 0 because it is a fatal error */ if(errorn != -1) /* "Error parsing contents of mararc file on line " */ printf("%s%d%s",L_MARARC_LINE,errorn,L_N); /* errorn, "\n" */ printf("%s",L_ERROR_CODE); /* "Error code: " */ js_show_stdout(errors); printf("%s",L_N); /* "\n" */ exit(2); } /* There are too many greedy lawyers in the US */ if(js_qstr2js(kvar_query,"hide_disclaimer") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,verbstr) != JS_SUCCESS) { printf("%s","THIS SOFTWARE IS PROVIDED BY THE AUTHORS ''AS IS'' AND ANY EXPRESS OR\n"); printf("%s","IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES\n"); printf("%s","OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.\n"); printf("%s","IN NO EVENT SHALL THE AUTHORS OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT,\n"); printf("%s","INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES\n"); printf("%s","(INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR\n"); printf("%s","SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)\n"); printf("%s","HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,\n"); printf("%s","STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING\n"); printf("%s","IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE\n"); printf("%s","POSSIBILITY OF SUCH DAMAGE.\n"); printf("\nTo not display this message, add the follwing to your "); printf("mararc file:\n\nhide_disclaimer = \"YES\"\n\n"); } /* Get in to a state of least privledge ASAP */ /* Limit the maximum number of processes */ if(js_qstr2js(kvar_query,"maxprocs") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,maxpstr) == JS_ERROR) harderror(L_MAXPROC); /* "Problem getting maxprocs value.\nmaxprocs must be set before starting the MaraDNS server" */ if((maxprocs = js_atoi(maxpstr,0)) == 0) harderror(L_MAXPROC_NUM); /* "Problem converting maxprocs to a number\nThis must be a non-zero number" */ /* MaraDNS has a known problem with hanging if maxprocs is too high */ if(maxprocs > 200) { maxprocs = 200; mlog(L_MAXPROC_MAX); /* "Maxprocs can not be greater than 200\nThere are known problems with MaraDNS hanging using higher numbers" */ } #ifndef DARWIN rlim.rlim_cur = rlim.rlim_max = maxprocs; /* If this OS supports setrlimit and if setrlimit fails, bail (the ENOSYS check is there so OSes w/o setrlimit support can still run MaraDNS) */ #ifndef SOLARIS if(setrlimit(RLIMIT_NPROC,&rlim) != 0 && errno != ENOSYS) sys_harderror(L_MAXPROC_SET); /* "Unable to set maximum number of processes" */ #endif /* SOLARIS */ #endif /* DARWIN */ /* Anything after this does not need recursion enabled for the kvar in question to be read */ /* Determine if we are root */ if(geteuid() == 0) { /* Change the root directory */ if(js_qstr2js(kvar_query,"chroot_dir") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,chrootn) == JS_ERROR) harderror(L_CHROOT_KVAR); /* "Problem getting chroot kvar.\nYou must have chroot_dir set if you start this as root" */ if(js_js2str(chrootn,chroot_zt,200) == JS_ERROR) harderror(L_CHROOT_NT); /* "Problem making chroot nt string.\nMake sure the chroot directory is 200 chars or less" */ if(chdir(chroot_zt) != 0) sys_harderror(L_CHROOT_CHANGE); /* "Problem changing to chroot dir.\nMake sure chroot_dir points to a valid directory" */ if(chroot(chroot_zt) != 0) sys_harderror(L_CHROOT_DO); /* "Problem changing the root directory." */ mlog(L_CHROOT_SUCCESS); /* "Root directory changed" */ /* Bind to port 53 To Do: use capset to give us privledged bind abilities without needing to be root. */ if(js_qstr2js(kvar_query,"bind_address") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,bind_address) == JS_ERROR) harderror(L_NOBIND); /* "Problem getting chroot kvar.\nYou must have bind_address set to the IP maradns will listen on" */ if(udpbind(&sock,bind_address) == JS_ERROR) sys_harderror(L_BINDFAIL); /* "Problem binding to port 53.\nMost likely, another process is already listening on port 53" */ zjlog(L_BIND2ADDR,bind_address); /* "Binding to address " */ mlog(L_BIND_SUCCESS); /* "Socket opened on UDP port 53" */ /* Drop the elevated privileges */ /* First, change the GID */ if(js_qstr2js(kvar_query,"maradns_gid") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,uidstr) == JS_SUCCESS) { gid = js_atoi(uidstr,0); /* Drop all supplemtal groups */ setgroups(1,&gid); /* Set the group ID */ setgid(gid); } /* Next, change the UID */ if(js_qstr2js(kvar_query,"maradns_uid") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,uidstr) == JS_ERROR) harderror(L_NOUID); /* "Problem getting maradns_uid kvar.\nYou must have maradns_uid set if you start this as root" */ if((uid = js_atoi(uidstr,0)) < 10) harderror(L_BADUID); /* "maradns_uid is less than 10 or not a number.\nThis uid must have a value of 10 or more" */ if(setuid(uid) != 0) sys_harderror(L_NODROP); /* "Could not drop root uid" */ /* Workaround for known Linux kernel security problem circa early 2000 */ if(setuid(0) == 0) sys_harderror(L_STILL_ROOT); /* "We seem to still be root" */ mlog(L_DROP_SUCCESS); /* "Root privileges dropped" */ } else { /* Bind to port 53 as a non-root user */ if(js_qstr2js(kvar_query,"bind_address") == JS_ERROR) harderror(L_KVAR_Q); /* "Could not create kvar_query" */ if(read_kvar(kvar_query,bind_address) == JS_ERROR) harderror(L_NOBIND); /* "Problem getting chroot kvar.\nYou must have bind_address set to the IP maradns will listen on" */ if(udpbind(&sock,bind_address) == JS_ERROR) sys_harderror(L_BEROOT); /* "Problem binding to port 53.\nYou should run this as root" */ mlog(L_BIND_SUCCESS); /* "Socket opened on UDP port 53" */ } /* Flush out any messages that have already appeared */ fflush(stdout); /* Listen for data on the UDP socket */ for(;;) { if(getudp(sock,(struct sockaddr *)&client,incoming,512) == JS_ERROR) continue; if(decompress_data(incoming,uncomp) == JS_ERROR) continue; proc_query(uncomp,(struct sockaddr_in *)&client,sock); } }
void *__hxcpp_get_proc_address(String inLib, String full_name,bool inNdllProc) { #ifdef ANDROID inLib = HX_CSTRING("lib") + inLib; //__android_log_print(ANDROID_LOG_INFO, "loader", "%s: %s", inLib.__CStr(), inPrim.__CStr() ); #endif #ifdef IPHONE gLoadDebug = true; setenv("DYLD_PRINT_APIS","1",true); #elif !defined(HX_WINRT) gLoadDebug = gLoadDebug || getenv("HXCPP_LOAD_DEBUG"); #endif String ext = #if defined(_WIN32) HX_CSTRING(".dll"); #elif defined(IPHONEOS) HX_CSTRING(".ios.dylib"); #elif defined(IPHONESIM) HX_CSTRING(".sim.dylib"); #elif defined(__APPLE__) HX_CSTRING(".dylib"); #elif defined(ANDROID) || defined(GPH) || defined(WEBOS) || defined(BLACKBERRY) || defined(EMSCRIPTEN) || defined(TIZEN) HX_CSTRING(".so"); #else HX_CSTRING(".dso"); #endif String bin = #ifdef _WIN32 HX_CSTRING("Windows"); // Unix... #elif defined(__APPLE__) #ifdef HXCPP_M64 HX_CSTRING("Mac64"); #else HX_CSTRING("Mac"); #endif #elif defined (ANDROID) HX_CSTRING("Android"); #elif defined(WEBOS) HX_CSTRING("webOS"); #elif defined(BLACKBERRY) HX_CSTRING("BlackBerry"); #elif defined(RASPBERRYPI) HX_CSTRING("RPi"); #elif defined(EMSCRIPTEN) HX_CSTRING("Emscripten"); #elif defined(TIZEN) HX_CSTRING("Tizen"); #elif defined(IPHONESIM) HX_CSTRING("IPhoneSim"); #elif defined(IPHONEOS) HX_CSTRING("IPhoneOs"); #else #ifdef HXCPP_M64 HX_CSTRING("Linux64"); #else HX_CSTRING("Linux"); #endif #endif int passes = 4; #ifdef ANDROID std::string module_name = inLib.__CStr(); Module module = sgLoadedModule[module_name]; #else Module module = sgLoadedModule[inLib.__s]; #endif bool new_module = module==0; if (!module && sgRegisteredPrims) { void *registered = (*sgRegisteredPrims)[full_name.__CStr()]; if (registered) return registered; } if (!module && gLoadDebug) { #ifdef ANDROID __android_log_print(ANDROID_LOG_INFO, "loader", "Searching for %s...", module_name.c_str()); #else printf("Searching for %s...\n", inLib.__CStr()); #endif } for(int pass=0;module==0 && pass<2;pass++) { String dll_ext = HX_CSTRING("./") + inLib + ( (pass&1) ? HX_CSTRING(".ndll") : ext ); // Try Current directory first ... if (gLoadDebug) { #ifndef ANDROID printf(" try %s...\n", dll_ext.__CStr()); #else __android_log_print(ANDROID_LOG_INFO, "loader", "Try %s", dll_ext.__CStr()); #endif } module = hxLoadLibrary(dll_ext); if (module) break; dll_ext = inLib + ( (pass&1) ? HX_CSTRING(".ndll") : ext ); if (gLoadDebug) { #ifndef ANDROID printf(" try %s...\n", dll_ext.__CStr()); #else __android_log_print(ANDROID_LOG_INFO, "loader", "Try %s", dll_ext.__CStr()); #endif } module = hxLoadLibrary(dll_ext); // Try exactly as specified... if (!module) { String dll_ext = pass==0 ? inLib : HX_CSTRING("./") + inLib; if (gLoadDebug) { #ifndef ANDROID printf(" try %s...\n", dll_ext.__CStr()); #else __android_log_print(ANDROID_LOG_INFO, "loader", "Try %s", dll_ext.__CStr()); #endif } module = hxLoadLibrary(dll_ext); } #ifdef HX_MACOS if (!module) { String exe_path = HX_CSTRING("@executable_path/") + inLib + ( (pass&1) ? HX_CSTRING(".ndll") : ext ); if (gLoadDebug) { printf(" try %s...\n", exe_path.__CStr()); } module = hxLoadLibrary(exe_path); } #endif #if !defined(ANDROID) && !defined(HX_WINRT) && !defined(IPHONE) && !defined(EMSCRIPTEN) if (!module) { String hxcpp = GetEnv("HXCPP"); if (hxcpp.length!=0) { String name = hxcpp + HX_CSTRING("/bin/") + bin + HX_CSTRING("/") + dll_ext; if (gLoadDebug) printf(" try %s...\n", name.__CStr()); module = hxLoadLibrary(name); } } if (!module) { String hxcpp = FindHaxelib( HX_CSTRING("hxcpp") ); if (hxcpp.length!=0) { String name = hxcpp + HX_CSTRING("/bin/") + bin + HX_CSTRING("/") + dll_ext; if (gLoadDebug) printf(" try %s...\n", name.__CStr()); module = hxLoadLibrary(name); } } if (!module) { String path = FindHaxelib(inLib); if (path.length!=0) { String full_path = path + HX_CSTRING("/ndll/") + bin + HX_CSTRING("/") + dll_ext; if (gLoadDebug) printf(" try %s...\n", full_path.__CStr()); module = hxLoadLibrary(full_path); } } #endif } if (!module) { throw Dynamic(HX_CSTRING("Could not load module ") + inLib + HX_CSTRING("@") + full_name); } if (new_module) { #ifdef ANDROID sgLoadedModule[module_name] = module; #else sgLoadedModule[inLib.__s] = module; #endif SetLoaderProcFunc set_loader = (SetLoaderProcFunc)hxFindSymbol(module,"hx_set_loader"); if (set_loader) set_loader(hx_cffi); GetNekoEntryFunc func = (GetNekoEntryFunc)hxFindSymbol(module,"__neko_entry_point"); if (func) { NekoEntryFunc entry = (NekoEntryFunc)func(); if (entry) entry(); } } FundFunc proc_query = (FundFunc)hxFindSymbol(module,full_name.__CStr()); if (!proc_query) { #ifdef ANDROID __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not find primitive %s in %p", full_name.__CStr(), module); #else fprintf(stderr,"Could not find primitive %s.\n", full_name.__CStr()); #endif return 0; } if (!inNdllProc) return (void *)proc_query; void *proc = proc_query(); if (!proc) { #ifdef ANDROID __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not identify primitive %s in %s\n", full_name.__CStr(),inLib.__CStr()); fprintf(stderr,"Could not identify primitive %s in %s\n", full_name.__CStr(),inLib.__CStr()); #elif defined(ANDROID) __android_log_print(ANDROID_LOG_ERROR, "loader", "Could not identify primitive %s in %s", inPrim.__CStr(), inLib.__CStr() ); #else fprintf(stderr,"Could not identify primitive %s in %s\n", full_name.__CStr(),inLib.__CStr()); #endif } return proc; }