Lines Matching refs:pq

494 	struct pending_query		*pq;  in frontend_dispatch_resolver()  local
538 if ((pq = find_pending_query(answer_header->id)) == in frontend_dispatch_resolver()
546 error_answer(pq, LDNS_RCODE_SERVFAIL); in frontend_dispatch_resolver()
547 send_answer(pq); in frontend_dispatch_resolver()
551 if (answer_header->bogus && !(pq->flags & BIT_CD)) { in frontend_dispatch_resolver()
552 error_answer(pq, LDNS_RCODE_SERVFAIL); in frontend_dispatch_resolver()
553 send_answer(pq); in frontend_dispatch_resolver()
557 if (sldns_buffer_position(pq->abuf) == 0 && in frontend_dispatch_resolver()
558 !sldns_buffer_set_capacity(pq->abuf, in frontend_dispatch_resolver()
560 error_answer(pq, LDNS_RCODE_SERVFAIL); in frontend_dispatch_resolver()
561 send_answer(pq); in frontend_dispatch_resolver()
565 if (sldns_buffer_position(pq->abuf) + data_len > in frontend_dispatch_resolver()
566 sldns_buffer_capacity(pq->abuf)) in frontend_dispatch_resolver()
569 sldns_buffer_write(pq->abuf, data, data_len); in frontend_dispatch_resolver()
571 if (sldns_buffer_position(pq->abuf) == in frontend_dispatch_resolver()
572 sldns_buffer_capacity(pq->abuf)) { in frontend_dispatch_resolver()
573 sldns_buffer_flip(pq->abuf); in frontend_dispatch_resolver()
574 if (pq->dns64_synthesize) { in frontend_dispatch_resolver()
575 synthesize_dns64_answer(pq); in frontend_dispatch_resolver()
576 send_answer(pq); in frontend_dispatch_resolver()
578 noerror_answer(pq); in frontend_dispatch_resolver()
579 if (pq->dns64_synthesize) in frontend_dispatch_resolver()
581 resend_dns64_query(pq); in frontend_dispatch_resolver()
583 send_answer(pq); in frontend_dispatch_resolver()
680 free_pending_query(struct pending_query *pq) in free_pending_query() argument
682 if (!pq) in free_pending_query()
685 TAILQ_REMOVE(&pending_queries, pq, entry); in free_pending_query()
686 regional_destroy(pq->region); in free_pending_query()
687 sldns_buffer_free(pq->qbuf); in free_pending_query()
688 sldns_buffer_free(pq->abuf); in free_pending_query()
689 if (pq->tcp) { in free_pending_query()
690 if (event_initialized(&pq->ev)) in free_pending_query()
691 event_del(&pq->ev); in free_pending_query()
692 if (event_initialized(&pq->resp_ev)) in free_pending_query()
693 event_del(&pq->resp_ev); in free_pending_query()
694 if (event_initialized(&pq->tmo_ev)) in free_pending_query()
695 event_del(&pq->tmo_ev); in free_pending_query()
696 if (pq->fd != -1) in free_pending_query()
697 close(pq->fd); in free_pending_query()
699 free(pq); in free_pending_query()
706 struct pending_query *pq = NULL; in udp_receive() local
714 if ((pq = calloc(1, sizeof(*pq))) == NULL) { in udp_receive()
720 arc4random_buf(&pq->imsg_id, sizeof(pq->imsg_id)); in udp_receive()
721 } while(find_pending_query(pq->imsg_id) != NULL); in udp_receive()
723 TAILQ_INSERT_TAIL(&pending_queries, pq, entry); in udp_receive()
725 pq->from = udpev->from; in udp_receive()
726 pq->fd = fd; in udp_receive()
727 pq->qbuf = sldns_buffer_new(len); in udp_receive()
728 pq->abuf = sldns_buffer_new(len); /* make sure we can send errors */ in udp_receive()
729 pq->region = regional_create(); in udp_receive()
731 if (!pq->qbuf || !pq->abuf || !pq->region) { in udp_receive()
733 free_pending_query(pq); in udp_receive()
737 sldns_buffer_write(pq->qbuf, udpev->query, len); in udp_receive()
738 sldns_buffer_flip(pq->qbuf); in udp_receive()
739 handle_query(pq); in udp_receive()
743 handle_query(struct pending_query *pq) in handle_query() argument
754 sldns_wire2str_pkt(sldns_buffer_begin(pq->qbuf), in handle_query()
755 sldns_buffer_limit(pq->qbuf))) != NULL) { in handle_query()
757 &pq->from), str); in handle_query()
761 if (sldns_buffer_remaining(pq->qbuf) < LDNS_HEADER_SIZE) { in handle_query()
766 pq->id = sldns_buffer_read_u16_at(pq->qbuf, 0); in handle_query()
767 pq->flags = sldns_buffer_read_u16_at(pq->qbuf, 2); in handle_query()
769 if (!query_info_parse(&pq->qinfo, pq->qbuf)) { in handle_query()
774 rcode = check_query(pq->qbuf); in handle_query()
781 error_answer(pq, rcode); in handle_query()
785 rcode = parse_edns_from_query_pkt(pq->qbuf, &pq->edns, NULL, NULL, in handle_query()
786 NULL, 0, pq->region, NULL); in handle_query()
788 error_answer(pq, rcode); in handle_query()
792 if (!dname_valid(pq->qinfo.qname, pq->qinfo.qname_len)) { in handle_query()
793 error_answer(pq, LDNS_RCODE_FORMERR); in handle_query()
796 dname_str(pq->qinfo.qname, dname); in handle_query()
798 sldns_wire2str_class_buf(pq->qinfo.qclass, qclass_buf, in handle_query()
800 sldns_wire2str_type_buf(pq->qinfo.qtype, qtype_buf, sizeof(qtype_buf)); in handle_query()
801 log_debug("%s: %s %s %s ?", ip_port((struct sockaddr *)&pq->from), in handle_query()
814 error_answer(pq, LDNS_RCODE_REFUSED); in handle_query()
819 if (pq->qinfo.qtype == LDNS_RR_TYPE_AXFR || pq->qinfo.qtype == in handle_query()
821 error_answer(pq, LDNS_RCODE_REFUSED); in handle_query()
825 if(pq->qinfo.qtype == LDNS_RR_TYPE_OPT || in handle_query()
826 pq->qinfo.qtype == LDNS_RR_TYPE_TSIG || in handle_query()
827 pq->qinfo.qtype == LDNS_RR_TYPE_TKEY || in handle_query()
828 pq->qinfo.qtype == LDNS_RR_TYPE_MAILA || in handle_query()
829 pq->qinfo.qtype == LDNS_RR_TYPE_MAILB || in handle_query()
830 (pq->qinfo.qtype >= 128 && pq->qinfo.qtype <= 248)) { in handle_query()
831 error_answer(pq, LDNS_RCODE_FORMERR); in handle_query()
835 if (pq->qinfo.qclass == LDNS_RR_CLASS_CH) { in handle_query()
838 chaos_answer(pq); in handle_query()
840 error_answer(pq, LDNS_RCODE_REFUSED); in handle_query()
847 error_answer(pq, LDNS_RCODE_FORMERR); in handle_query()
850 query_imsg.id = pq->imsg_id; in handle_query()
851 query_imsg.t = pq->qinfo.qtype; in handle_query()
852 query_imsg.c = pq->qinfo.qclass; in handle_query()
856 error_answer(pq, LDNS_RCODE_SERVFAIL); in handle_query()
862 send_answer(pq); in handle_query()
866 free_pending_query(pq); in handle_query()
870 noerror_answer(struct pending_query *pq) in noerror_answer() argument
882 if (!query_info_parse(&skip, pq->abuf)) in noerror_answer()
885 if (reply_info_parse(pq->abuf, &alloc, &qinfo, &rinfo, pq->region, in noerror_answer()
897 !pq->dns64_synthesize && pq->qinfo.qtype == LDNS_RR_TYPE_AAAA && in noerror_answer()
898 pq->qinfo.qclass == LDNS_RR_CLASS_IN && dns64_prefix_count > 0) { in noerror_answer()
899 pq->dns64_synthesize = 1; in noerror_answer()
903 sldns_buffer_clear(pq->abuf); in noerror_answer()
904 if (reply_info_encode(&pq->qinfo, rinfo, htons(pq->id), rinfo->flags, in noerror_answer()
905 pq->abuf, 0, pq->region, pq->tcp ? UINT16_MAX : pq->edns.udp_size, in noerror_answer()
906 pq->edns.bits & EDNS_DO, MINIMIZE_ANSWER) == 0) in noerror_answer()
916 error_answer(pq, LDNS_RCODE_SERVFAIL); in noerror_answer()
920 synthesize_dns64_answer(struct pending_query *pq) in synthesize_dns64_answer() argument
928 pq->dns64_synthesize = 0; in synthesize_dns64_answer()
933 if (!query_info_parse(&skip, pq->abuf)) in synthesize_dns64_answer()
936 if (reply_info_parse(pq->abuf, &alloc, &qinfo, &rinfo, pq->region, in synthesize_dns64_answer()
943 synth_rinfo = construct_reply_info_base(pq->region, rinfo->flags, in synthesize_dns64_answer()
952 if(!reply_info_alloc_rrset_keys(synth_rinfo, NULL, pq->region)) in synthesize_dns64_answer()
971 dst_rrset_key, &dst_rrset_data, pq->region); in synthesize_dns64_answer()
977 pq->region, src_rrset_key->rk.dname, in synthesize_dns64_answer()
982 dst_rrset_data = regional_alloc_init(pq->region, in synthesize_dns64_answer()
993 if (!sldns_buffer_set_capacity(pq->abuf, pq->tcp ? UINT16_MAX : in synthesize_dns64_answer()
994 pq->edns.udp_size)) in synthesize_dns64_answer()
997 sldns_buffer_clear(pq->abuf); in synthesize_dns64_answer()
999 if (reply_info_encode(&pq->qinfo, synth_rinfo, htons(pq->id), in synthesize_dns64_answer()
1000 synth_rinfo->flags, pq->abuf, 0, pq->region, in synthesize_dns64_answer()
1001 pq->tcp ? UINT16_MAX : pq->edns.udp_size, in synthesize_dns64_answer()
1002 pq->edns.bits & EDNS_DO, MINIMIZE_ANSWER) == 0) in synthesize_dns64_answer()
1012 error_answer(pq, LDNS_RCODE_SERVFAIL); in synthesize_dns64_answer()
1018 struct pending_query *pq; in resend_dns64_query() local
1023 if ((pq = calloc(1, sizeof(*pq))) == NULL) { in resend_dns64_query()
1029 arc4random_buf(&pq->imsg_id, sizeof(pq->imsg_id)); in resend_dns64_query()
1030 } while(find_pending_query(pq->imsg_id) != NULL); in resend_dns64_query()
1032 TAILQ_INSERT_TAIL(&pending_queries, pq, entry); in resend_dns64_query()
1034 pq->from = opq->from; in resend_dns64_query()
1035 pq->fd = opq->fd; in resend_dns64_query()
1037 pq->tcp = opq->tcp; in resend_dns64_query()
1038 pq->qbuf = sldns_buffer_new(sldns_buffer_capacity(opq->qbuf)); in resend_dns64_query()
1039 pq->abuf = sldns_buffer_new(sldns_buffer_capacity(opq->abuf)); in resend_dns64_query()
1040 pq->region = regional_create(); in resend_dns64_query()
1042 if (!pq->qbuf || !pq->abuf || !pq->region) { in resend_dns64_query()
1044 free_pending_query(pq); in resend_dns64_query()
1050 sldns_buffer_write(pq->qbuf, sldns_buffer_current(opq->qbuf), in resend_dns64_query()
1052 sldns_buffer_flip(pq->qbuf); in resend_dns64_query()
1054 if (pq->tcp) { in resend_dns64_query()
1057 event_set(&pq->ev, pq->fd, EV_READ | EV_PERSIST, tcp_request, in resend_dns64_query()
1058 pq); in resend_dns64_query()
1059 event_set(&pq->resp_ev, pq->fd, EV_WRITE | EV_PERSIST, in resend_dns64_query()
1060 tcp_response, pq); in resend_dns64_query()
1061 evtimer_set(&pq->tmo_ev, tcp_timeout, pq); in resend_dns64_query()
1062 evtimer_add(&pq->tmo_ev, &timeout); in resend_dns64_query()
1065 if (sldns_buffer_remaining(pq->qbuf) < LDNS_HEADER_SIZE) { in resend_dns64_query()
1070 pq->id = sldns_buffer_read_u16_at(pq->qbuf, 0); in resend_dns64_query()
1071 pq->flags = sldns_buffer_read_u16_at(pq->qbuf, 2); in resend_dns64_query()
1073 if (!query_info_parse(&pq->qinfo, pq->qbuf)) { in resend_dns64_query()
1078 rcode = parse_edns_from_query_pkt(pq->qbuf, &pq->edns, NULL, NULL, in resend_dns64_query()
1079 NULL, 0, pq->region, NULL); in resend_dns64_query()
1081 error_answer(pq, rcode); in resend_dns64_query()
1085 dname_str(pq->qinfo.qname, dname); in resend_dns64_query()
1087 query_imsg.id = pq->imsg_id; in resend_dns64_query()
1089 query_imsg.c = pq->qinfo.qclass; in resend_dns64_query()
1091 pq->dns64_synthesize = 1; in resend_dns64_query()
1095 error_answer(pq, LDNS_RCODE_SERVFAIL); in resend_dns64_query()
1104 send_answer(pq); in resend_dns64_query()
1109 free_pending_query(pq); in resend_dns64_query()
1113 chaos_answer(struct pending_query *pq) in chaos_answer() argument
1119 if (!sldns_buffer_set_capacity(pq->abuf, in chaos_answer()
1120 sldns_buffer_capacity(pq->qbuf) + COMPRESSED_RR_SIZE + 1 + len)) { in chaos_answer()
1121 error_answer(pq, LDNS_RCODE_SERVFAIL); in chaos_answer()
1125 sldns_buffer_copy(pq->abuf, pq->qbuf); in chaos_answer()
1127 sldns_buffer_clear(pq->abuf); in chaos_answer()
1129 sldns_buffer_skip(pq->abuf, sizeof(uint16_t)); /* skip id */ in chaos_answer()
1130 sldns_buffer_write_u16(pq->abuf, 0); /* clear flags */ in chaos_answer()
1131 LDNS_QR_SET(sldns_buffer_begin(pq->abuf)); in chaos_answer()
1132 LDNS_RA_SET(sldns_buffer_begin(pq->abuf)); in chaos_answer()
1133 if (LDNS_RD_WIRE(sldns_buffer_begin(pq->qbuf))) in chaos_answer()
1134 LDNS_RD_SET(sldns_buffer_begin(pq->abuf)); in chaos_answer()
1135 if (LDNS_CD_WIRE(sldns_buffer_begin(pq->qbuf))) in chaos_answer()
1136 LDNS_CD_SET(sldns_buffer_begin(pq->abuf)); in chaos_answer()
1137 LDNS_RCODE_SET(sldns_buffer_begin(pq->abuf), LDNS_RCODE_NOERROR); in chaos_answer()
1138 sldns_buffer_write_u16(pq->abuf, 1); /* qdcount */ in chaos_answer()
1139 sldns_buffer_write_u16(pq->abuf, 1); /* ancount */ in chaos_answer()
1140 sldns_buffer_write_u16(pq->abuf, 0); /* nscount */ in chaos_answer()
1141 sldns_buffer_write_u16(pq->abuf, 0); /* arcount */ in chaos_answer()
1142 (void)query_dname_len(pq->abuf); /* skip qname */ in chaos_answer()
1143 sldns_buffer_skip(pq->abuf, sizeof(uint16_t)); /* skip qtype */ in chaos_answer()
1144 sldns_buffer_skip(pq->abuf, sizeof(uint16_t)); /* skip qclass */ in chaos_answer()
1146 sldns_buffer_write_u16(pq->abuf, 0xc00c); /* ptr to query */ in chaos_answer()
1147 sldns_buffer_write_u16(pq->abuf, LDNS_RR_TYPE_TXT); in chaos_answer()
1148 sldns_buffer_write_u16(pq->abuf, LDNS_RR_CLASS_CH); in chaos_answer()
1149 sldns_buffer_write_u32(pq->abuf, 0); /* TTL */ in chaos_answer()
1150 sldns_buffer_write_u16(pq->abuf, 1 + len); /* RDLENGTH */ in chaos_answer()
1151 sldns_buffer_write_u8(pq->abuf, len); /* length octed */ in chaos_answer()
1152 sldns_buffer_write(pq->abuf, name, len); in chaos_answer()
1153 sldns_buffer_flip(pq->abuf); in chaos_answer()
1157 error_answer(struct pending_query *pq, int rcode) in error_answer() argument
1159 sldns_buffer_clear(pq->abuf); in error_answer()
1160 error_encode(pq->abuf, rcode, &pq->qinfo, htons(pq->id), pq->flags, in error_answer()
1161 pq->edns.edns_present ? &pq->edns : NULL); in error_answer()
1206 send_answer(struct pending_query *pq) in send_answer() argument
1211 sldns_wire2str_pkt(sldns_buffer_begin(pq->abuf), in send_answer()
1212 sldns_buffer_limit(pq->abuf))) != NULL) { in send_answer()
1214 &pq->from), str); in send_answer()
1218 if (!pq->tcp) { in send_answer()
1219 if(sendto(pq->fd, sldns_buffer_current(pq->abuf), in send_answer()
1220 sldns_buffer_remaining(pq->abuf), 0, in send_answer()
1221 (struct sockaddr *)&pq->from, pq->from.ss_len) == -1) in send_answer()
1223 free_pending_query(pq); in send_answer()
1227 tmp = sldns_buffer_new(sldns_buffer_limit(pq->abuf) + 2); in send_answer()
1230 free_pending_query(pq); in send_answer()
1234 sldns_buffer_write_u16(tmp, sldns_buffer_limit(pq->abuf)); in send_answer()
1235 sldns_buffer_write(tmp, sldns_buffer_current(pq->abuf), in send_answer()
1236 sldns_buffer_remaining(pq->abuf)); in send_answer()
1238 sldns_buffer_free(pq->abuf); in send_answer()
1239 pq->abuf = tmp; in send_answer()
1240 event_add(&pq->resp_ev, NULL); in send_answer()
1268 struct pending_query *pq; in find_pending_query() local
1270 TAILQ_FOREACH(pq, &pending_queries, entry) in find_pending_query()
1271 if (pq->imsg_id == id) in find_pending_query()
1272 return pq; in find_pending_query()
1644 struct pending_query *pq; in tcp_accept() local
1669 if ((pq = calloc(1, sizeof(*pq))) == NULL) { in tcp_accept()
1676 arc4random_buf(&pq->imsg_id, sizeof(pq->imsg_id)); in tcp_accept()
1677 } while(find_pending_query(pq->imsg_id) != NULL); in tcp_accept()
1679 TAILQ_INSERT_TAIL(&pending_queries, pq, entry); in tcp_accept()
1681 pq->from = ss; in tcp_accept()
1682 pq->fd = s; in tcp_accept()
1683 pq->tcp = 1; in tcp_accept()
1684 pq->qbuf = sldns_buffer_new(DEFAULT_TCP_SIZE); in tcp_accept()
1685 pq->region = regional_create(); in tcp_accept()
1687 if (!pq->qbuf || !pq->region) { in tcp_accept()
1688 free_pending_query(pq); in tcp_accept()
1692 event_set(&pq->ev, s, EV_READ | EV_PERSIST, tcp_request, pq); in tcp_accept()
1693 event_add(&pq->ev, NULL); in tcp_accept()
1694 event_set(&pq->resp_ev, s, EV_WRITE | EV_PERSIST, tcp_response, pq); in tcp_accept()
1696 evtimer_set(&pq->tmo_ev, tcp_timeout, pq); in tcp_accept()
1697 evtimer_add(&pq->tmo_ev, &timeout); in tcp_accept()
1703 struct pending_query *pq; in tcp_request() local
1706 pq = arg; in tcp_request()
1708 n = read(fd, sldns_buffer_current(pq->qbuf), in tcp_request()
1709 sldns_buffer_remaining(pq->qbuf)); in tcp_request()
1728 sldns_buffer_skip(pq->qbuf, n); in tcp_request()
1730 if (sldns_buffer_position(pq->qbuf) >= 2 && !pq->abuf) { in tcp_request()
1735 sldns_buffer_flip(pq->qbuf); in tcp_request()
1736 len = sldns_buffer_read_u16(pq->qbuf); in tcp_request()
1740 pq->abuf = sldns_buffer_new(len); in tcp_request()
1741 if (pq->abuf == NULL) { in tcp_request()
1746 rem = sldns_buffer_remaining(pq->qbuf); in tcp_request()
1747 sldns_buffer_write(tmp, sldns_buffer_current(pq->qbuf), in tcp_request()
1749 sldns_buffer_free(pq->qbuf); in tcp_request()
1750 pq->qbuf = tmp; in tcp_request()
1752 if (sldns_buffer_remaining(pq->qbuf) == 0) { in tcp_request()
1753 sldns_buffer_flip(pq->qbuf); in tcp_request()
1755 event_del(&pq->ev); in tcp_request()
1756 handle_query(pq); in tcp_request()
1760 free_pending_query(pq); in tcp_request()
1766 struct pending_query *pq; in tcp_response() local
1769 pq = arg; in tcp_response()
1771 n = write(fd, sldns_buffer_current(pq->abuf), in tcp_response()
1772 sldns_buffer_remaining(pq->abuf)); in tcp_response()
1777 free_pending_query(pq); in tcp_response()
1780 sldns_buffer_skip(pq->abuf, n); in tcp_response()
1781 if (sldns_buffer_remaining(pq->abuf) == 0) in tcp_response()
1782 free_pending_query(pq); in tcp_response()