int resolv_init(struct ev_loop *loop, char **nameservers, int nameserver_num, int ipv6first) { if (ipv6first) resolv_mode = MODE_IPV6_FIRST; else resolv_mode = MODE_IPV4_FIRST; struct dns_ctx *ctx = &dns_defctx; if (nameservers == NULL) { /* Nameservers not specified, use system resolver config */ dns_init(ctx, 0); } else { dns_reset(ctx); for (int i = 0; i < nameserver_num; i++) { char *server = nameservers[i]; dns_add_serv(ctx, server); } } int sockfd = dns_open(ctx); if (sockfd < 0) { FATAL("Failed to open DNS resolver socket"); } if (nameserver_num == 1 && nameservers != NULL) { if (strncmp("127.0.0.1", nameservers[0], 9) == 0 || strncmp("::1", nameservers[0], 3) == 0) { if (verbose) { LOGI("bind UDP resolver to %s", nameservers[0]); } if (bind_to_address(sockfd, nameservers[0]) == -1) ERROR("bind_to_address"); } } #ifdef __MINGW32__ setnonblocking(sockfd); #else int flags = fcntl(sockfd, F_GETFL, 0); fcntl(sockfd, F_SETFL, flags | O_NONBLOCK); #endif ev_io_init(&resolv_io_watcher, resolv_sock_cb, sockfd, EV_READ); resolv_io_watcher.data = ctx; ev_io_start(loop, &resolv_io_watcher); ev_timer_init(&resolv_timeout_watcher, resolv_timeout_cb, 0.0, 0.0); resolv_timeout_watcher.data = ctx; dns_set_tmcbck(ctx, dns_timer_setup_cb, loop); return sockfd; }
static dns_ctx_handle_t *dns_ctx_alloc(const char *ns, int port) { void *vh; dns_ctx_handle_t *h = NULL; pthread_mutex_lock(&dns_ctx_store_lock); if(ns == NULL && default_ctx_handle != NULL) { /* special case -- default context */ h = default_ctx_handle; noit_atomic_inc32(&h->refcnt); goto bail; } if(ns && noit_hash_retrieve(&dns_ctx_store, ns, strlen(ns), &vh)) { h = (dns_ctx_handle_t *)vh; noit_atomic_inc32(&h->refcnt); } else { int failed = 0; h = calloc(1, sizeof(*h)); h->ns = ns ? strdup(ns) : NULL; h->ctx = dns_new(NULL); if(dns_init(h->ctx, 0) != 0) failed++; if(ns) { if(dns_add_serv(h->ctx, NULL) < 0) failed++; if(dns_add_serv(h->ctx, ns) < 0) failed++; } if(port && port != DNS_PORT) { dns_set_opt(h->ctx, DNS_OPT_PORT, port); } if(dns_open(h->ctx) < 0) failed++; if(failed) { noitL(nlerr, "dns_open failed\n"); free(h->ns); free(h); h = NULL; goto bail; } dns_set_tmcbck(h->ctx, eventer_dns_utm_fn, h); h->e = eventer_alloc(); h->e->mask = EVENTER_READ | EVENTER_EXCEPTION; h->e->closure = h; h->e->callback = dns_eventer_callback; h->e->fd = dns_sock(h->ctx); eventer_add(h->e); h->refcnt = 1; if(!ns) default_ctx_handle = h; else noit_hash_store(&dns_ctx_store, h->ns, strlen(h->ns), h); } bail: pthread_mutex_unlock(&dns_ctx_store_lock); return h; }
int dns_init(struct dns_ctx *ctx, int do_open) { if (!ctx) ctx = &dns_defctx; dns_reset(ctx); #ifdef WINDOWS if (dns_initns_iphlpapi(ctx) != 0) dns_initns_registry(ctx); /*XXX WINDOWS: probably good to get default domain and search list too... * And options. Something is in registry. */ /*XXX WINDOWS: maybe environment variables are also useful? */ #else dns_init_resolvconf(ctx); #endif return do_open ? dns_open(ctx) : 0; }
static void *bep034_worker() { #ifdef __MACH__ dns_handle_t dns_handle = dns_open(0); #else void *dns_handle = 0; #endif bep034_dolock(); while( 1 ) { bep034_job * myjob = 0; bep034_hostrecord * hr = 0; char * reply = 0; /* Waking up, grab one job from the work queue */ myjob = bep034_getjob( ); if( !myjob ) continue; /* Fill host record with results from DNS query or cache, owner of the hr is the cache, not us. This can block (but releases lock while blocking) */ if( myjob->status == BEP_034_INPROGRESS ) myjob->status = bep034_fill_hostrecord( myjob->hostname, &hr, (uintptr_t)dns_handle ); /* Function returns with the bep034_lock locked, so that hr will be valid until we're finished with it */ if( myjob->status == BEP_034_INPROGRESS ) { bep034_actonrecord( myjob, hr ); bep034_build_announce_url( myjob, &reply ); } else reply = strdup( myjob->announce_url ); /* Return mutex */ bep034_dounlock(); if( g_callback ) g_callback( myjob->lookup_id, myjob->status, reply ); free( reply ); /* Clean up structure */ bep034_finishjob( myjob ); /* Acquire lock to loop */ bep034_dolock(); } }
static int Resolver_init(Resolver *self, PyObject *args) { int r = 0, create_new = 0, do_open = 1; if (!PyArg_ParseTuple(args, "|ii", &create_new, &do_open)) { PyErr_SetString(PyExc_TypeError, "Resolver(create_new=False, do_open=True) wrong arguments. See help(Resolver) for details."); return -1; } //FIXME if (0 == do_open) { PyErr_SetString(PyExc_NotImplementedError, "Resolver() do_open=False not yet implemented. Pass True for now. Sorry."); return -1; } self->ctx = NULL; if (1 == create_new) { self->ctx = dns_new(NULL); if (NULL == self->ctx) { PyErr_SetString(PyExc_MemoryError, "Resolver() failed to create new udns context."); return -1; } } r = dns_init(self->ctx, 0); if (r < 0) { PyErr_SetString(PyExc_Exception, "Resolver() failed to init udns context."); return -1; } if (1 == do_open) { self->fd = dns_open(self->ctx); if (self->fd < 0) { PyErr_SetString(PyExc_IOError, "Resolver() failed to open udns socket."); return -1; } } return 0; }
void QCrawlerDNS::init(struct ev_loop * loop) { dns_loop = loop; if (dns_init(NULL, 0) < 0 ) { LOG(FATAL) << "init dns fatal"; } dns_fd = dns_open(NULL); if (dns_fd < 0) { LOG(FATAL) << "open dns fatal"; } if (dns_loop == NULL) { dns_loop = ev_default_loop(0); } ev_io_init(&dns_io_watcher, dns_io_cb, dns_fd, EV_READ); ev_io_start(dns_loop, &dns_io_watcher); ev_timer_init (&dns_timeout_watcher, dns_timeout_cb, 1, 0.); ev_timer_start (dns_loop, &dns_timeout_watcher); }
int main(int argc, char **argv) { dns_handle_t dns = dns_open(NULL); uint16_t dnstype = 0; if (dns_type_number("TXT", &dnstype) != 0) { fprintf(stderr, "Don't know what TXT type means.\n"); exit(1); } uint16_t dnsclass = 0; if (dns_class_number("IN", &dnsclass) != 0) { fprintf(stderr, "Don't know what IN class means.\n"); exit(1); } fprintf(stderr, "Doing lookup\n"); dns_reply_t *reply = dns_lookup(dns, argv[1], dnsclass, dnstype); if (reply == NULL) { fprintf(stderr, "Couldn't find %s\n", argv[1]); exit(1); } fprintf(stderr, "Printing reply: %p (status=%d)\n", reply, reply->status); for (int i = 0; reply->answer[i]; i++) { dns_resource_record_t *rr = reply->answer[i]; fprintf(stderr, "answer rr %p\n", rr); fprintf(stdout, " Type: %s (%d)\n", dns_type_string(rr->dnstype), rr->dnstype); if (rr->dnstype == dnstype) { fprintf(stderr, " Data: "); for (int j = 0; j < rr->data.TXT->string_count; j++) { if (j > 0) { fprintf(stderr, ", "); } fprintf(stderr, "\"%s\"", rr->data.TXT->strings[j]); } fprintf(stderr, "\n"); } } }
void noit_check_resolver_init() { eventer_t e; if(dns_init(NULL, 0) < 0) noitL(noit_error, "dns initialization failed.\n"); dns_ctx = dns_new(NULL); if(dns_init(dns_ctx, 0) != 0 || dns_open(dns_ctx) < 0) { noitL(noit_error, "dns initialization failed.\n"); } eventer_name_callback("dns_cache_callback", dns_cache_callback); dns_set_tmcbck(dns_ctx, dns_cache_utm_fn, dns_ctx); e = eventer_alloc(); e->mask = EVENTER_READ | EVENTER_EXCEPTION; e->closure = dns_ctx; e->callback = dns_cache_callback; e->fd = dns_sock(dns_ctx); eventer_add(e); noit_skiplist_init(&nc_dns_cache); noit_skiplist_set_compare(&nc_dns_cache, name_lookup, name_lookup_k); noit_skiplist_add_index(&nc_dns_cache, refresh_idx, refresh_idx_k); noit_check_resolver_loop(NULL, 0, NULL, NULL); register_console_dns_cache_commands(); }
int main(int argc, char **argv) { int c; struct ipcheck ipc; char *nameserver = NULL; int zgiven = 0; if (!(progname = strrchr(argv[0], '/'))) progname = argv[0]; else argv[0] = ++progname; while((c = getopt(argc, argv, "hqtvms:S:cn:")) != EOF) switch(c) { case 's': ++zgiven; addzone(optarg); break; case 'S': ++zgiven; if (addzonefile(optarg)) break; fprintf(stderr, "%s: unable to read %s\n", progname, optarg); return 1; case 'c': ++zgiven; nzones = 0; break; case 'q': --verbose; break; case 'v': ++verbose; break; case 't': do_txt = 1; break; case 'n': nameserver = optarg; break; case 'm': ++stopfirst; break; case 'h': printf("%s: %s.\n", progname, version); printf("Usage is: %s [options] address..\n", progname); printf( "Where options are:\n" " -h - print this help and exit\n" " -s service - add the service (DNSBL zone) to the serice list\n" " -S service-file - add the DNSBL zone(s) read from the given file\n" " -c - clear service list\n" " -v - increase verbosity level (more -vs => more verbose)\n" " -q - decrease verbosity level (opposite of -v)\n" " -t - obtain and print TXT records if any\n" " -m - stop checking after first address match in any list\n" " -n ipaddr - use the given nameserver instead of the default\n" "(if no -s or -S option is given, use $RBLCHECK_ZONES, ~/.rblcheckrc\n" "or /etc/rblcheckrc in that order)\n" ); return 0; default: fprintf(stderr, "%s: use `%s -h' for help\n", progname, progname); return 1; } if (!zgiven) { char *s = getenv("RBLCHECK_ZONES"); if (s) { char *k; s = strdup(s); k = strtok(s, " \t"); while(k) { addzone(k); k = strtok(NULL, " \t"); } free(s); } else { char *path; char *home = getenv("HOME"); if (!home) home = "."; path = malloc(strlen(home) + 1 + sizeof(".rblcheckrc")); sprintf(path, "%s/.rblcheckrc", home); if (!addzonefile(path)) addzonefile("/etc/rblcheckrc"); free(path); } } if (!nzones) { fprintf(stderr, "%s: no service (zone) list specified (-s or -S option)\n", progname); return 1; } argv += optind; argc -= optind; if (!argc) return 0; if (dns_init(0) < 0) { fprintf(stderr, "%s: unable to initialize DNS library: %s\n", progname, strerror(errno)); return 1; } if (nameserver) { dns_add_serv(NULL, NULL); if (dns_add_serv(NULL, nameserver) < 0) fprintf(stderr, "%s: unable to use nameserver %s: %s\n", progname, nameserver, strerror(errno)); } if (dns_open(NULL) < 0) { fprintf(stderr, "%s: unable to initialize DNS library: %s\n", progname, strerror(errno)); return 1; } for (c = 0; c < argc; ++c) { if (c && (verbose > 1 || (verbose == 1 && do_txt))) putchar('\n'); ipc.name = argv[c]; submit(&ipc); waitdns(&ipc); display_result(&ipc); if (stopfirst > 1 && listed) break; } return listed ? 100 : failures ? 2 : 0; }
void noit_check_resolver_init() { int cnt; mtev_conf_section_t *servers, *searchdomains; eventer_t e; if(dns_init(NULL, 0) < 0) mtevL(noit_error, "dns initialization failed.\n"); dns_ctx = dns_new(NULL); if(dns_init(dns_ctx, 0) != 0) { mtevL(noit_error, "dns initialization failed.\n"); exit(-1); } /* Optional servers */ servers = mtev_conf_get_sections(NULL, "//resolver//server", &cnt); if(cnt) { int i; char server[128]; dns_add_serv(dns_ctx, NULL); /* reset */ for(i=0;i<cnt;i++) { if(mtev_conf_get_stringbuf(servers[i], "self::node()", server, sizeof(server))) { if(dns_add_serv(dns_ctx, server) < 0) { mtevL(noit_error, "Failed adding DNS server: %s\n", server); } } } free(servers); } searchdomains = mtev_conf_get_sections(NULL, "//resolver//search", &cnt); if(cnt) { int i; char search[128]; dns_add_srch(dns_ctx, NULL); /* reset */ for(i=0;i<cnt;i++) { if(mtev_conf_get_stringbuf(searchdomains[i], "self::node()", search, sizeof(search))) { if(dns_add_srch(dns_ctx, search) < 0) { mtevL(noit_error, "Failed adding DNS search path: %s\n", search); } else if(dns_search_flag) dns_search_flag = 0; /* enable search */ } } free(searchdomains); } if(mtev_conf_get_int(NULL, "//resolver/@ndots", &cnt)) dns_set_opt(dns_ctx, DNS_OPT_NDOTS, cnt); if(mtev_conf_get_int(NULL, "//resolver/@ntries", &cnt)) dns_set_opt(dns_ctx, DNS_OPT_NTRIES, cnt); if(mtev_conf_get_int(NULL, "//resolver/@timeout", &cnt)) dns_set_opt(dns_ctx, DNS_OPT_TIMEOUT, cnt); if(dns_open(dns_ctx) < 0) { mtevL(noit_error, "dns open failed.\n"); exit(-1); } eventer_name_callback("dns_cache_callback", dns_cache_callback); dns_set_tmcbck(dns_ctx, dns_cache_utm_fn, dns_ctx); e = eventer_alloc(); e->mask = EVENTER_READ | EVENTER_EXCEPTION; e->closure = dns_ctx; e->callback = dns_cache_callback; e->fd = dns_sock(dns_ctx); eventer_add(e); mtev_skiplist_init(&nc_dns_cache); mtev_skiplist_set_compare(&nc_dns_cache, name_lookup, name_lookup_k); mtev_skiplist_add_index(&nc_dns_cache, refresh_idx, refresh_idx_k); /* maybe load it from cache */ if(noit_resolver_cache_load_hook_exists()) { struct timeval now; char *key; void *data; int len; gettimeofday(&now, NULL); while(noit_resolver_cache_load_hook_invoke(&key, &data, &len) == MTEV_HOOK_CONTINUE) { dns_cache_node *n; n = calloc(1, sizeof(*n)); if(dns_cache_node_deserialize(n, data, len) >= 0) { n->target = strdup(key); /* if the TTL indicates that it will expire in less than 60 seconds * (including stuff that should have already expired), then fudge * the last_updated time to make it expire some random time within * the next 60 seconds. */ if(n->last_needed > now.tv_sec || n->last_updated > now.tv_sec) break; /* impossible */ n->last_needed = now.tv_sec; if(n->last_updated + n->ttl < now.tv_sec + 60) { int fudge = MIN(60, n->ttl) + 1; n->last_updated = now.tv_sec - n->ttl + (lrand48() % fudge); } DCLOCK(); mtev_skiplist_insert(&nc_dns_cache, n); DCUNLOCK(); n = NULL; } else { mtevL(noit_error, "Failed to deserialize resolver cache record.\n"); } if(n) dns_cache_node_free(n); if(key) free(key); if(data) free(data); } } noit_check_resolver_loop(NULL, 0, NULL, NULL); register_console_dns_cache_commands(); mtev_hash_init(&etc_hosts_cache); noit_check_etc_hosts_cache_refresh(NULL, 0, NULL, NULL); }
int import_classtable(int x, int y) { char atrn[256], sendform[512]; char cookie[80], condensation[9]; int cookie_len; int sockfd; int cc; char *xhead; char pool[2048], buf[1025]; FILE *fp; char *s; char fname[80], classtime[80]; char User[14], Password[14], ans, ansc; CLASS_TABLE2 classtable; if (!vget(b_lines, 0, "帳號(不加s) : " , User, sizeof(User), DOECHO) || !vget(b_lines, 0, "密碼 : " , Password, sizeof(Password), NOECHO)) return 0; ans = vans("是否覆蓋\原始資料 [y/N]: "); ansc = vans("中文課程簡稱 [y/N]: "); /* stage1 */ sprintf(sendform, "GET %s HTTP/1.0\r\n\r\n", CGI_stage1); if ((sockfd = dns_open(SERVER_student, HTTP_PORT)) < 0) { vmsg("無法與伺服器取得連結,查詢失敗"); return 1; } else { // vmsg("正在連接伺服器,請稍後 (1)............."); } write(sockfd, sendform, strlen(sendform)); shutdown(sockfd, 1); for (;;) { // *xhead = '\0'; xhead = pool; cc = read(sockfd, xhead, sizeof(pool)); if (*xhead != '\0' && cc > 0) { xhead[cc] = '\0'; s = strstr(xhead, "Cookie:"); if (s != NULL) { s[strlen(s) - strlen(strstr(s, "\n")) + 1] = '\0'; strcpy(cookie, s); break; } } else { vmsg("MISS"); return 1; } } close(sockfd); /* stage2 */ sprintf(atrn, "User=%s&Password=%s", User, Password); cookie_len = strlen(atrn); sprintf(sendform, "POST %s HTTP/1.0\r\n%sContent-Type: application/x-www-form-urlencoded\r\nContent-Length: %d\r\n\r\n%s\r\n\r\n", CGI_stage2, cookie, cookie_len, atrn); if ((sockfd = dns_open(SERVER_student, HTTP_PORT)) < 0) { vmsg("無法與伺服器取得連結,查詢失敗"); return 1; } else { // vmsg("正在連接伺服器,請稍後 (2)............."); } write(sockfd, sendform, strlen(sendform)); shutdown(sockfd, 1); for (;;) { // *xhead = '\0'; xhead = pool; cc = read(sockfd, xhead, sizeof(pool)); if (*xhead != '\0' && cc > 0) { xhead[cc] = '\0'; s = strstr(xhead, "有誤"); if (s != NULL) { vmsg("您輸入的帳號或密碼有誤!"); return 1; } } else { // vmsg("您輸入的帳號或密碼正確!"); break; } } close(sockfd); /* stage3 */ sprintf(sendform, "GET %s HTTP/1.0\r\n%s\r\n", CGI_stage3, cookie); if ((sockfd = dns_open(SERVER_student, HTTP_PORT)) < 0) { vmsg("無法與伺服器取得連結,查詢失敗\n"); return 1; } else { // vmsg("正在連接伺服器,請稍後 (3).............\n"); } write(sockfd, sendform, strlen(sendform)); shutdown(sockfd, 1); sprintf(fname, "tmp/%s.student", cuser.userid); fp = fopen(fname, "w"); if (fp) { for (;;) { // *xhead = '\0'; xhead = pool; cc = read(sockfd, xhead, sizeof(pool)); if (*xhead != '\0' && cc > 0) { xhead[cc] = '\0'; fputs(xhead, fp); } if (cc <= 0) { break; } } fclose(fp); } close(sockfd); /* stage4 */ fp = fopen(fname, "r"); memset(&classtable, 0, sizeof(CLASS_TABLE2)); if (fp) { while (fgets(buf, 1024, fp) != NULL) { s = strstr(buf, " <CENTER>"); if (s != NULL) // 課程編號 { s += 9; s[strlen(s) - strlen(strstr(s, "</CENTER>"))] = 0; strcpy(classtable.condensation, s); fgets(buf, 1024, fp); // 開課班別 s = strstr(buf, "<CENTER>"); s += 8; s[strlen(s) - strlen(strstr(s, "</CENTER>"))] = 0; strcat(classtable.condensation, "_"); strcat(classtable.condensation, s); fgets(buf, 1024, fp); // 選別 fgets(buf, 1024, fp); // 課名 s = strstr(buf, "<CENTER>"); s += 8; s[strlen(s) - strlen(strstr(s, "</CENTER>"))] = 0; strncpy(condensation, s, 8); condensation[8] = '\0'; strncpy(classtable.name, s, 28); strcat(classtable.name, "["); strcat(classtable.name, classtable.condensation); strcat(classtable.name, "]"); fgets(buf, 1024, fp); // 學分數 s = strstr(buf, "<CENTER>"); s += 8; s[strlen(s) - strlen(strstr(s, "</CENTER>"))] = 0; sprintf(sendform, "學分數 [%s]", s); fgets(buf, 1024, fp); // 期中評量 s = strstr(buf, "<CENTER>"); s += 8; s[strlen(s) - strlen(strstr(s, "</CENTER>"))] = 0; sprintf(classtable.other, "%s,期中評量 [%s]", sendform, s); fgets(buf, 1024, fp); // 空白 fgets(buf, 1024, fp); // 空白 fgets(buf, 1024, fp); // 時間 s = strstr(buf, ">"); s += 1; s[strlen(s) - strlen(strstr(s, "</TD>"))] = 0; strcpy(classtime, s); fgets(buf, 1024, fp); // 地點 s = strstr(buf, ">"); s += 1; s[strlen(s) - strlen(strstr(s, "</TD>"))] = 0; if (!strstr(s, "無資料")) s[strlen(s) - strlen(strstr(s, ","))] = 0; strcpy(classtable.room, s); // 時間分析 s = classtime; while (!strstr(s, "無資料") && strlen(s) > 3) { int p, x, y; x = atoi(s + 1); s[1] = '\0'; y = atoi(s); p = (x - 1) + (y - 1) * 13; if (tmp_table[p].valid == 0 || ans == 'y' || ans == 'Y') { classtable.x = x; classtable.y = y; classtable.valid = 1; if (strlen(classtable.teacher) <= 0) teacher_classtable(&classtable); // vmsg(classtable.teacher); memcpy(&tmp_table[p], &classtable, sizeof(CLASS_TABLE2)); if (ansc == 'y' || ansc == 'Y') { memcpy(tmp_table[p].condensation, condensation, sizeof(condensation)); show_classtable(x, y, condensation); } else show_classtable(x, y, classtable.condensation); } s += 4; } memset(&classtable, 0, sizeof(CLASS_TABLE2)); } } fclose(fp); unlink(fname); } show_icon_classtable(x, y, 1); help_classtable(); return 0; }
int teacher_classtable(CLASS_TABLE2 *classtable) { char atrn[256], sendform[512]; char cookie[80], pool[2048]; char *xhead; int cookie_len; int sockfd; int cc; char mCos_Id[80], *mCos_Class; /* stage1 */ sprintf(sendform, "GET %s HTTP/1.0\r\n\r\n", CGI_stage4); if ((sockfd = dns_open(SERVER_student, HTTP_PORT)) < 0) { vmsg("無法與伺服器取得連結,查詢失敗\n"); return 1; } else { // vmsg("正在連接伺服器,請稍後(按任意鍵離開).............\n"); } write(sockfd, sendform, strlen(sendform)); shutdown(sockfd, 1); for (;;) { // *xhead = '\0'; xhead = pool; cc = read(sockfd, xhead, sizeof(pool)); if (*xhead != '\0' && cc > 0) { char *s; xhead[cc] = '\0'; s = strstr(xhead, "Cookie:"); if (s != NULL) { s[strlen(s) - strlen(strstr(s, "\n")) + 1] = '\0'; strcpy(cookie, s); // vmsg(cookie); break; } } else { return 1; break; } } close(sockfd); /* stage2 */ mCos_Class = strstr(classtable->condensation, "_") + 1; strcpy(mCos_Id, classtable->condensation); mCos_Id[strlen(mCos_Id)-strlen(strstr(mCos_Id, "_"))] = 0; sprintf(atrn, "mCos_Id=%s&mCos_Class=%s&mSmtr=%d", mCos_Id, mCos_Class, CLASS_GRADUATE); // vmsg(atrn); cookie_len = strlen(atrn); sprintf(sendform, "GET %s?%s HTTP/1.0\r\n%s\r\n", CGI_stage5, atrn, cookie); if ((sockfd = dns_open(SERVER_student, HTTP_PORT)) < 0) { vmsg("無法與伺服器取得連結,查詢失敗\n"); return 1; } else { // vmsg("正在連接伺服器,請稍後(按任意鍵離開).............\n"); } write(sockfd, sendform, strlen(sendform)); shutdown(sockfd, 1); for (;;) { // *xhead = '\0'; // xhead = pool; cc = read(sockfd, xhead, sizeof(pool)); if (*xhead != '\0' && cc > 0) { char *s; xhead[cc] = 0; s = strstr(xhead, "任課老師"); if (s != NULL && strlen(s) > 41) { s += 41; s[strlen(s) - strlen(strstr(s, " </td>"))] = 0; strcpy(classtable->teacher, s); // vmsg(classtable->teacher); break; } else if (s != NULL) break; } else { break; } } close(sockfd); }
static dns_ctx_handle_t *dns_module_dns_ctx_alloc(noit_module_t *self, const char *ns, int port) { void *vh; char *hk = NULL; dns_mod_config_t *conf = noit_module_get_userdata(self); int randkey = random() % conf->contexts; dns_ctx_handle_t *h = NULL; if(ns && *ns == '\0') ns = NULL; pthread_mutex_lock(&dns_ctx_store_lock); if(ns == NULL && default_ctx_handle != NULL) { /* special case -- default context */ h = default_ctx_handle; dns_module_dns_ctx_acquire(h); goto bail; } if (ns != NULL) { int len = snprintf(NULL, 0, "%s:%d:%d", ns, port, randkey); hk = (char *)malloc(len+1); snprintf(hk, len+1, "%s:%d:%d", ns, port, randkey); } if(ns && noit_hash_retrieve(&dns_ctx_store, hk, strlen(hk), &vh)) { h = (dns_ctx_handle_t *)vh; dns_module_dns_ctx_acquire(h); free(hk); } else { int failed = 0; h = calloc(1, sizeof(*h)); h->ns = ns ? strdup(ns) : NULL; h->ctx = dns_new(NULL); if(dns_init(h->ctx, 0) != 0) { noitL(nlerr, "dns_init failed\n"); failed++; } dns_set_dbgfn(h->ctx, dns_debug_wrap); if(ns) { if(dns_add_serv(h->ctx, NULL) < 0) { noitL(nlerr, "dns_add_serv(NULL) failed\n"); failed++; } if(dns_add_serv(h->ctx, ns) < 0) { noitL(nlerr, "dns_add_serv(%s) failed\n", ns); failed++; } } if(port && port != DNS_PORT) { dns_set_opt(h->ctx, DNS_OPT_PORT, port); } if(dns_open(h->ctx) < 0) { noitL(nlerr, "dns_open failed\n"); failed++; } if(failed) { free(h->ns); dns_free(h->ctx); free(h); free(hk); h = NULL; goto bail; } h->hkey = hk; dns_set_tmcbck(h->ctx, dns_module_eventer_dns_utm_fn, h); h->e = eventer_alloc(); h->e->mask = EVENTER_READ | EVENTER_EXCEPTION; h->e->closure = h; h->e->callback = dns_module_eventer_callback; h->e->fd = dns_sock(h->ctx); eventer_add(h->e); h->refcnt = 1; if(!ns) default_ctx_handle = h; else noit_hash_store(&dns_ctx_store, h->hkey, strlen(h->hkey), h); } bail: pthread_mutex_unlock(&dns_ctx_store_lock); return h; }
int main(int argc, char **argv) { struct hostent * host = NULL; int net_preopen_result; #ifdef ENABLE_IPV6 struct addrinfo hints, *res; int error; struct hostent trhost; char * alptr[2]; struct sockaddr_in * sa4; struct sockaddr_in6 * sa6; #endif /* Get the raw sockets first thing, so we can drop to user euid immediately */ if ( ( net_preopen_result = net_preopen () ) ) { fprintf( stderr, "mtr: unable to get raw sockets.\n" ); exit( EXIT_FAILURE ); } /* Now drop to user permissions */ if (setgid(getgid()) || setuid(getuid())) { fprintf (stderr, "mtr: Unable to drop permissions.\n"); exit(1); } /* Double check, just in case */ if ((geteuid() != getuid()) || (getegid() != getgid())) { fprintf (stderr, "mtr: Unable to drop permissions.\n"); exit(1); } /* reset the random seed */ srand (getpid()); display_detect(&argc, &argv); /* The field options are now in a static array all together, but that requires a run-time initialization. */ init_fld_options (); parse_mtr_options (getenv ("MTR_OPTIONS")); parse_arg (argc, argv); while (optind < argc) { char* name = argv[optind++]; append_to_names(argv[0], name); } /* Now that we know mtrtype we can select which socket to use */ if (net_selectsocket() != 0) { fprintf( stderr, "mtr: Couldn't determine raw socket type.\n" ); exit( EXIT_FAILURE ); } if (PrintVersion) { printf ("mtr " MTR_VERSION "\n"); exit(0); } if (PrintHelp) { printf("usage: %s [-hvrwctglspniuT46] [--help] [--version] [--report]\n" "\t\t[--report-wide] [--report-cycles=COUNT] [--curses] [--gtk]\n" "\t\t[--csv|-C] [--raw] [--split] [--mpls] [--no-dns] [--show-ips]\n" "\t\t[--address interface] [--filename=FILE|-F]\n" /* BL */ #ifdef IPINFO "\t\t[--ipinfo=item_no|-y item_no]\n" "\t\t[--aslookup|-z]\n" #endif "\t\t[--psize=bytes/-s bytes]\n" /* ok */ "\t\t[--report-wide|-w] [-u|-T] [--port=PORT] [--timeout=SECONDS]\n" /* rew */ "\t\t[--interval=SECONDS] HOSTNAME\n", argv[0]); exit(0); } time_t now = time(NULL); if (!names) append_to_names (argv[0], "localhost"); // default: localhost. names_t* head = names; while (names != NULL) { Hostname = names->name; // if (Hostname == NULL) Hostname = "localhost"; // no longer necessary. if (gethostname(LocalHostname, sizeof(LocalHostname))) { strcpy(LocalHostname, "UNKNOWNHOST"); } if (net_preopen_result != 0) { fprintf(stderr, "mtr: Unable to get raw socket. (Executable not suid?)\n"); if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE); else { names = names->next; continue; } } #ifdef ENABLE_IPV6 /* gethostbyname2() is deprecated so we'll use getaddrinfo() instead. */ bzero( &hints, sizeof hints ); hints.ai_family = af; hints.ai_socktype = SOCK_DGRAM; error = getaddrinfo( Hostname, NULL, &hints, &res ); if ( error ) { if (error == EAI_SYSTEM) perror ("Failed to resolve host"); else fprintf (stderr, "Failed to resolve host: %s\n", gai_strerror(error)); if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE); else { names = names->next; continue; } } /* Convert the first addrinfo into a hostent. */ host = &trhost; bzero( host, sizeof trhost ); host->h_name = res->ai_canonname; host->h_aliases = NULL; host->h_addrtype = res->ai_family; af = res->ai_family; host->h_length = res->ai_addrlen; host->h_addr_list = alptr; switch ( af ) { case AF_INET: sa4 = (struct sockaddr_in *) res->ai_addr; alptr[0] = (void *) &(sa4->sin_addr); break; case AF_INET6: sa6 = (struct sockaddr_in6 *) res->ai_addr; alptr[0] = (void *) &(sa6->sin6_addr); break; default: fprintf( stderr, "mtr unknown address type\n" ); if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE); else { names = names->next; continue; } } alptr[1] = NULL; #else host = gethostbyname(Hostname); if (host == NULL) { herror("mtr gethostbyname"); if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE); else { names = names->next; continue; } } af = host->h_addrtype; #endif if (net_open(host) != 0) { fprintf(stderr, "mtr: Unable to start net module.\n"); if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE); else { names = names->next; continue; } } if (net_set_interfaceaddress (InterfaceAddress) != 0) { fprintf( stderr, "mtr: Couldn't set interface address.\n" ); if ( DisplayMode != DisplayCSV ) exit(EXIT_FAILURE); else { names = names->next; continue; } } lock(argv[0], stdout); display_open(); dns_open(); display_mode = 0; display_loop(); net_end_transit(); display_close(now); unlock(argv[0], stdout); if ( DisplayMode != DisplayCSV ) break; else names = names->next; } net_close(); while (head != NULL) { names_t* item = head; free(item->name); item->name = NULL; head = head->next; free(item); item = NULL; } head=NULL; return 0; }