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