/* * call the dns process and have it try to translate a name */ static struct ndbtuple *dnsiplookup(char *host, struct ndbs *s) { char buf[Maxreply]; struct ndbtuple *t; spinlock_unlock(&dblock); /* save the name */ snprintf(buf, sizeof(buf), "%s", host); if (strcmp(ipattr(buf), "ip") == 0) t = dnsquery(mntpt, buf, "ptr"); else { t = dnsquery(mntpt, buf, "ip"); /* special case: query ipv6 (AAAA dns RR) too */ if (ipv6lookups) t = dnsip6lookup(mntpt, buf, t); } s->t = t; if (t == NULL) { snprintf(buf, sizeof(buf), "%r"); if (strstr(buf, "exist")) werrstr("can't translate address: %s", buf); else if (strstr(buf, "dns failure")) werrstr("temporary problem: %s", buf); } spinlock_lock(&dblock); return t; }
/* * use dns to resolve the mx request */ static int mxlookup(DS *ds, char *domain) { int i, n, nmx; Ndbtuple *t, *tmx, *tpref, *tip; strcpy(domain, ds->host); ds->netdir = "/net"; nmx = 0; if((t = dnsquery(nil, ds->host, "mx")) != nil){ for(tmx=t; (tmx=ndbfindattr(tmx->entry, nil, "mx")) != nil && nmx<Nmx; ){ for(tpref=tmx->line; tpref != tmx; tpref=tpref->line){ if(strcmp(tpref->attr, "pref") == 0){ strncpy(mx[nmx].host, tmx->val, sizeof(mx[n].host)-1); mx[nmx].pref = atoi(tpref->val); nmx++; break; } } } ndbfree(t); } /* * no mx record? try name itself. */ /* * BUG? If domain has no dots, then we used to look up ds->host * but return domain instead of ds->host in the list. Now we return * ds->host. What will this break? */ if(nmx == 0){ mx[0].pref = 1; strncpy(mx[0].host, ds->host, sizeof(mx[0].host)); nmx++; } /* * look up all ip addresses */ for(i = 0; i < nmx; i++){ if((t = dnsquery(nil, mx[i].host, "ip")) == nil) goto no; if((tip = ndbfindattr(t, nil, "ip")) == nil){ ndbfree(t); goto no; } strncpy(mx[i].ip, tip->val, sizeof(mx[i].ip)-1); ndbfree(t); continue; no: /* remove mx[i] and go around again */ nmx--; mx[i] = mx[nmx]; i--; } return nmx; }
int nbnameresolve(NbName nbname, uchar *ipaddr) { ulong r, ttl; char name[NETPATHLEN]; NbName copy; Ndbtuple *t; /* for now, just use dns */ if (nbremotenametablefind(nbname, ipaddr)) { //print("%B found in cache\n", nbname); return 1; } if (nbnsfindname(nil, nbname, ipaddr, &ttl) == 0) { nbremotenametableadd(nbname, ipaddr, ttl); return 1; } nbnamecpy(copy, nbname); copy[NbNameLen - 1] = 0; nbmkstringfromname(name, sizeof(name), copy); t = dnsquery("/net", name, "ip"); if (t == nil) return 0; r = parseip(ipaddr, t->line->val); ndbfree(t); return r != -1; }
int send_request(char *domain, int port, char *url){ int sock; struct sockaddr_in addr; char ipaddr[20]; memset(ipaddr, 0, sizeof(ipaddr)); if (-1 == (sock = socket(AF_INET, SOCK_STREAM, 0))){ return -1; } dnsquery(domain, 80, ipaddr); //fprintf(stderr, "domain=%s, ipaddr=%s\n", domain, ipaddr); memset(&addr,0,sizeof(struct sockaddr_in)); addr.sin_family=AF_INET; addr.sin_port=htons(port); addr.sin_addr.s_addr=inet_addr(ipaddr); if(connect(sock,(struct sockaddr *)&addr,sizeof(addr))) { printf("cannot connect to server\n"); close(sock); return 1; } char *request = form_http_header("MagicWiFi", domain, url); //printf("request=[%s]\n", request); send(sock, request, strlen(request), 0); free(request); char response[4096]; memset(response, 0, sizeof(response)); int len= read(sock, response, 4096); printf("response len=%d\n", len); //fprintf(stderr, "response=[%s]\n", response); //struct hostent *gethostbyname(const char *hostname); close(sock); if (strstr(response, "200 OK") || strstr(response, "302 Found") || strstr(response, "302 FOUND") ){ return 0; } return -1; }
/* * read (recipient, sender's DNS) pairs from /mail/lib/senders. * Only allow mail to recipient from any of sender's IPs. * A recipient not mentioned in the file is always permitted. */ static int senderok(char *rcpt) { int mentioned = 0, matched = 0; uchar dnsip[IPaddrlen]; Sender *snd; Ndbtuple *nt, *next, *first; rdsenders(); for (snd = sendlist; snd != nil; snd = snd->next) { if (strcmp(rcpt, snd->rcpt) != 0) continue; /* * see if this domain's ips match nci->rsys. * if not, perhaps a later entry's domain will. */ mentioned = 1; if (parseip(dnsip, snd->domain) != -1 && memcmp(rsysip, dnsip, IPaddrlen) == 0) return 1; /* * NB: nt->line links form a circular list(!). * we need to make one complete pass over it to free it all. */ first = nt = dnsquery(nci->root, snd->domain, "ip"); if (first == nil) continue; do { if (strcmp(nt->attr, "ip") == 0 && parseip(dnsip, nt->val) != -1 && memcmp(rsysip, dnsip, IPaddrlen) == 0) matched = 1; next = nt->line; free(nt); nt = next; } while (nt != first); } if (matched) return 1; else return !mentioned; }
static struct ndbtuple *dnsip6lookup(char *mntpt, char *buf, struct ndbtuple *t) { struct ndbtuple *t6, *tt; t6 = dnsquery(mntpt, buf, "ipv6"); /* lookup AAAA dns RRs */ if (t6 == NULL) return t; /* convert ipv6 attr to ip */ for (tt = t6; tt != NULL; tt = tt->entry) if (strcmp(tt->attr, "ipv6") == 0) strlcpy(tt->attr, "ip", sizeof(tt->attr)); if (t == NULL) return t6; /* append t6 list to t list */ for (tt = t; tt->entry != NULL; tt = tt->entry) ; tt->entry = t6; return t; }