ReactOS 0.4.15-dev-7887-g64a59a1
transmit.c File Reference
#include <errno.h>
#include <sys/types.h>
#include <sys/uio.h>
#include "internal.h"
#include "tvarith.h"
Include dependency graph for transmit.c:

Go to the source code of this file.

Macros

#define MKQUERY_START(vb)   (rqp= (vb)->buf+(vb)->used)
 
#define MKQUERY_ADDB(b)   *rqp++= (b)
 
#define MKQUERY_ADDW(w)   (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))
 
#define MKQUERY_STOP(vb)   ((vb)->used= rqp-(vb)->buf)
 

Functions

static adns_status mkquery_header (adns_state ads, vbuf *vb, int *id_r, int qdlen)
 
static adns_status mkquery_footer (vbuf *vb, adns_rrtype type)
 
adns_status adns__mkquery (adns_state ads, vbuf *vb, int *id_r, const char *owner, int ol, const typeinfo *typei, adns_queryflags flags)
 
adns_status adns__mkquery_frdgram (adns_state ads, vbuf *vb, int *id_r, const byte *qd_dgram, int qd_dglen, int qd_begin, adns_rrtype type, adns_queryflags flags)
 
void adns__querysend_tcp (adns_query qu, struct timeval now)
 
static void query_usetcp (adns_query qu, struct timeval now)
 
void adns__query_send (adns_query qu, struct timeval now)
 

Macro Definition Documentation

◆ MKQUERY_ADDB

#define MKQUERY_ADDB (   b)    *rqp++= (b)

Definition at line 42 of file transmit.c.

◆ MKQUERY_ADDW

#define MKQUERY_ADDW (   w)    (MKQUERY_ADDB(((w)>>8)&0x0ff), MKQUERY_ADDB((w)&0x0ff))

Definition at line 43 of file transmit.c.

◆ MKQUERY_START

#define MKQUERY_START (   vb)    (rqp= (vb)->buf+(vb)->used)

Definition at line 41 of file transmit.c.

◆ MKQUERY_STOP

#define MKQUERY_STOP (   vb)    ((vb)->used= rqp-(vb)->buf)

Definition at line 44 of file transmit.c.

Function Documentation

◆ adns__mkquery()

adns_status adns__mkquery ( adns_state  ads,
vbuf vb,
int id_r,
const char owner,
int  ol,
const typeinfo typei,
adns_queryflags  flags 
)

Definition at line 81 of file transmit.c.

83 {
84 int ll, c, nbytes;
85 byte label[255], *rqp;
86 const char *p, *pe;
87 adns_status st;
88
89 st= mkquery_header(ads,vb,id_r,ol+2); if (st) return st;
90
91 MKQUERY_START(vb);
92
93 p= owner; pe= owner+ol;
94 nbytes= 0;
95 while (p!=pe) {
96 ll= 0;
97 while (p!=pe && (c= *p++)!='.') {
98 if (c=='\\') {
100 if (ctype_digit(p[0])) {
101 if (ctype_digit(p[1]) && ctype_digit(p[2])) {
102 c= (p[0] - '0')*100 + (p[1] - '0')*10 + (p[2] - '0');
103 p += 3;
104 if (c >= 256) return adns_s_querydomaininvalid;
105 } else {
107 }
108 } else if (!(c= *p++)) {
110 }
111 }
112 if (!(flags & adns_qf_quoteok_query)) {
113 if (c == '-') {
114 if (!ll) return adns_s_querydomaininvalid;
115 } else if (!ctype_alpha(c) && !ctype_digit(c)) {
117 }
118 }
119 if (ll == sizeof(label)) return adns_s_querydomaininvalid;
120 label[ll++]= c;
121 }
122 if (!ll) return adns_s_querydomaininvalid;
124 nbytes+= ll+1;
125 if (nbytes >= DNS_MAXDOMAIN) return adns_s_querydomaintoolong;
127 memcpy(rqp,label,(size_t) ll); rqp+= ll;
128 }
129 MKQUERY_ADDB(0);
130
131 MKQUERY_STOP(vb);
132
133 st= mkquery_footer(vb,typei->type);
134
135 return adns_s_ok;
136}
adns_state ads
Definition: adh-query.c:35
adns_status
Definition: adns.h:215
@ adns_s_querydomaintoolong
Definition: adns.h:255
@ adns_s_ok
Definition: adns.h:216
@ adns_s_querydomaininvalid
Definition: adns.h:254
@ adns_qf_quoteok_query
Definition: adns.h:104
w ll
Definition: byte_order.h:167
const GLubyte * c
Definition: glext.h:8905
GLbitfield flags
Definition: glext.h:7161
GLfloat GLfloat p
Definition: glext.h:8902
#define c
Definition: ke_i.h:80
#define memcpy(s1, s2, n)
Definition: mkisofs.h:878
static const WCHAR label[]
Definition: itemdlg.c:1546
static int ctype_alpha(int c)
Definition: internal.h:699
static int ctype_digit(int c)
Definition: internal.h:698
#define DNS_MAXDOMAIN
Definition: internal.h:75
#define DNS_MAXLABEL
Definition: internal.h:74
#define MKQUERY_START(vb)
Definition: transmit.c:41
static adns_status mkquery_footer(vbuf *vb, adns_rrtype type)
Definition: transmit.c:69
static adns_status mkquery_header(adns_state ads, vbuf *vb, int *id_r, int qdlen)
Definition: transmit.c:46
#define MKQUERY_ADDB(b)
Definition: transmit.c:42
#define MKQUERY_STOP(vb)
Definition: transmit.c:44
adns_rrtype type
Definition: internal.h:125

Referenced by query_simple().

◆ adns__mkquery_frdgram()

adns_status adns__mkquery_frdgram ( adns_state  ads,
vbuf vb,
int id_r,
const byte qd_dgram,
int  qd_dglen,
int  qd_begin,
adns_rrtype  type,
adns_queryflags  flags 
)

Definition at line 138 of file transmit.c.

140 {
141 byte *rqp;
143 int lablen, labstart;
144 adns_status st;
145
146 st= mkquery_header(ads,vb,id_r,qd_dglen); if (st) return st;
147
148 MKQUERY_START(vb);
149
150 adns__findlabel_start(&fls,ads,-1,0,qd_dgram,qd_dglen,qd_dglen,qd_begin,0);
151 for (;;) {
152 st= adns__findlabel_next(&fls,&lablen,&labstart); assert(!st);
153 if (!lablen) break;
154 assert(lablen<255);
155 MKQUERY_ADDB(lablen);
156 memcpy(rqp,qd_dgram+labstart, (size_t) lablen);
157 rqp+= lablen;
158 }
159 MKQUERY_ADDB(0);
160
161 MKQUERY_STOP(vb);
162
163 st= mkquery_footer(vb,type);
164
165 return adns_s_ok;
166}
static int fls(int x)
Definition: bitops.h:157
#define assert(x)
Definition: debug.h:53
GLuint GLuint GLsizei GLenum type
Definition: gl.h:1545
adns_status adns__findlabel_next(findlabel_state *fls, int *lablen_r, int *labstart_r)
Definition: parse.c:74
void adns__findlabel_start(findlabel_state *fls, adns_state ads, int serv, adns_query qu, const byte *dgram, int dglen, int max, int dmbegin, int *dmend_rlater)
Definition: parse.c:59

Referenced by adns__procdgram(), pa_ptr(), and pap_hostaddr().

◆ adns__query_send()

void adns__query_send ( adns_query  qu,
struct timeval  now 
)

Definition at line 232 of file transmit.c.

232 {
233 struct sockaddr_in servaddr;
234 int serv, r;
236
237 assert(qu->state == query_tosend);
238 if ((qu->flags & adns_qf_usevc) || (qu->query_dglen > DNS_MAXUDP)) {
239 query_usetcp(qu,now);
240 return;
241 }
242
243 if (qu->retries >= UDPMAXRETRIES) {
245 return;
246 }
247
248 serv= qu->udpnextserver;
249 memset(&servaddr,0,sizeof(servaddr));
250
251 ads= qu->ads;
252 servaddr.sin_family= AF_INET;
253 servaddr.sin_addr= ads->servers[serv].addr;
254 servaddr.sin_port= htons(DNS_PORT);
255
257 r= sendto(ads->udpsocket,(char*)qu->query_dgram,qu->query_dglen,0,
258 (const struct sockaddr*)&servaddr,sizeof(servaddr));
260 if (r<0 && errno == EMSGSIZE) { qu->retries= 0; query_usetcp(qu,now); return; }
261 if (r<0 && ((errno != EAGAIN) && (errno != EWOULDBLOCK))) adns__warn(ads,serv,0,"sendto failed: %s (%d)",strerror(errno), errno);
262
263 qu->timeout= now;
265 qu->udpsent |= (1<<serv);
266 qu->udpnextserver= (serv+1)%ads->nservers;
267 qu->retries++;
268 LIST_LINK_TAIL(ads->udpw,qu);
269}
#define EAGAIN
Definition: acclib.h:83
@ adns_s_timeout
Definition: adns.h:226
@ adns_qf_usevc
Definition: adns.h:102
#define ADNS_CLEAR_ERRNO
Definition: adns_win32.h:108
#define ADNS_CAPTURE_ERRNO
Definition: adns_win32.h:107
INT WSAAPI sendto(IN SOCKET s, IN CONST CHAR FAR *buf, IN INT len, IN INT flags, IN CONST struct sockaddr *to, IN INT tolen)
Definition: send.c:82
#define EWOULDBLOCK
Definition: errno.h:42
#define EMSGSIZE
Definition: errno.h:97
#define AF_INET
Definition: tcpip.h:117
time_t now
Definition: finger.c:65
GLdouble GLdouble GLdouble r
Definition: gl.h:2055
#define htons(x)
Definition: module.h:215
const char * strerror(int err)
Definition: compat_str.c:23
#define errno
Definition: errno.h:18
#define LIST_LINK_TAIL(list, node)
Definition: dlist.h:51
void adns__warn(adns_state ads, int serv, adns_query qu, const char *fmt,...)
Definition: general.c:94
#define UDPMAXRETRIES
Definition: internal.h:65
#define DNS_MAXUDP
Definition: internal.h:73
void adns__query_fail(adns_query qu, adns_status stat)
Definition: query.c:547
#define DNS_PORT
Definition: internal.h:72
#define UDPRETRYMS
Definition: internal.h:66
static void query_usetcp(adns_query qu, struct timeval now)
Definition: transmit.c:223
#define memset(x, y, z)
Definition: compat.h:39
enum adns__query::@4227 state
int retries
Definition: internal.h:227
byte * query_dgram
Definition: internal.h:192
struct timeval timeout
Definition: internal.h:230
int query_dglen
Definition: internal.h:193
adns_state ads
Definition: internal.h:182
unsigned long udpsent
Definition: internal.h:229
int udpnextserver
Definition: internal.h:228
int nservers
Definition: internal.h:300
ADNS_SOCKET udpsocket
Definition: internal.h:298
struct adns__state::server servers[MAXSERVERS]
static void timevaladd(struct timeval *tv_io, long ms)
Definition: tvarith.h:31

Referenced by adns__procdgram(), query_submit(), and timeouts_queue().

◆ adns__querysend_tcp()

void adns__querysend_tcp ( adns_query  qu,
struct timeval  now 
)

Definition at line 168 of file transmit.c.

168 {
169 byte length[2];
170 struct iovec iov[2];
171 int wr, r;
173
174 if (qu->ads->tcpstate != server_ok) return;
175
176 assert(qu->state == query_tcpw);
177
178 length[0]= (qu->query_dglen&0x0ff00U) >>8;
179 length[1]= (qu->query_dglen&0x0ff);
180
181 ads= qu->ads;
182 if (!adns__vbuf_ensure(&ads->tcpsend,ads->tcpsend.used+qu->query_dglen+2)) return;
183
184 qu->retries++;
185
186 /* Reset idle timeout. */
187 ads->tcptimeout.tv_sec= ads->tcptimeout.tv_usec= 0;
188
189 if (ads->tcpsend.used) {
190 wr= 0;
191 } else {
192 iov[0].iov_base= (char*)length;
193 iov[0].iov_len= 2;
194 iov[1].iov_base= (char*)qu->query_dgram;
195 iov[1].iov_len= qu->query_dglen;
197
199 wr= writev(qu->ads->tcpsocket,iov,2);
202 if (wr < 0) {
203 if (!(errno == EAGAIN || errno == EWOULDBLOCK || errno == EINTR || errno == ENOSPC ||
204 errno == ENOBUFS || errno == ENOMEM)) {
206 return;
207 }
208 wr= 0;
209 }
210 }
211
212 if (wr<2) {
214 wr= 0;
215 } else {
216 wr-= 2;
217 }
218 if (wr<qu->query_dglen) {
220 }
221}
#define EINTR
Definition: acclib.h:80
#define ENOMEM
Definition: acclib.h:84
#define writev(FileDescriptor, iov, iovCount)
Definition: adns_win32.h:160
#define ENOSPC
Definition: errno.h:34
#define ENOBUFS
Definition: errno.h:116
GLuint GLsizei GLsizei * length
Definition: glext.h:6040
void adns__tcp_broken(adns_state ads, const char *what, const char *why)
Definition: event.c:59
int adns__vbuf_append(vbuf *vb, const byte *data, int len)
Definition: general.c:131
void adns__sigpipe_protect(adns_state ads)
Definition: general.c:340
void adns__sigpipe_unprotect(adns_state ads)
Definition: general.c:360
int adns__vbuf_ensure(vbuf *vb, int want)
Definition: general.c:116
ADNS_SOCKET tcpsocket
Definition: internal.h:298
enum adns__state::adns__tcpstate tcpstate
struct timeval tcptimeout
Definition: internal.h:305
vbuf tcpsend
Definition: internal.h:299
Definition: linux.h:1700
int used
Definition: internal.h:111

Referenced by query_usetcp(), and tcp_connected().

◆ mkquery_footer()

static adns_status mkquery_footer ( vbuf vb,
adns_rrtype  type 
)
static

Definition at line 69 of file transmit.c.

69 {
70 byte *rqp;
71
72 MKQUERY_START(vb);
74 MKQUERY_ADDW(DNS_CLASS_IN); /* QCLASS=IN */
75 MKQUERY_STOP(vb);
76 assert(vb->used <= vb->avail);
77
78 return adns_s_ok;
79}
@ adns__rrt_typemask
Definition: adns.h:114
#define DNS_CLASS_IN
Definition: internal.h:78
#define MKQUERY_ADDW(w)
Definition: transmit.c:43
int avail
Definition: internal.h:111

Referenced by adns__mkquery(), and adns__mkquery_frdgram().

◆ mkquery_header()

static adns_status mkquery_header ( adns_state  ads,
vbuf vb,
int id_r,
int  qdlen 
)
static

Definition at line 46 of file transmit.c.

46 {
47 int id;
48 byte *rqp;
49
50 if (!adns__vbuf_ensure(vb,DNS_HDRSIZE+qdlen+4)) return adns_s_nomemory;
51
52 vb->used= 0;
53 MKQUERY_START(vb);
54
55 *id_r= id= (ads->nextid++) & 0x0ffff;
56 MKQUERY_ADDW(id);
57 MKQUERY_ADDB(0x01); /* QR=Q(0), OPCODE=QUERY(0000), !AA, !TC, RD */
58 MKQUERY_ADDB(0x00); /* !RA, Z=000, RCODE=NOERROR(0000) */
59 MKQUERY_ADDW(1); /* QDCOUNT=1 */
60 MKQUERY_ADDW(0); /* ANCOUNT=0 */
61 MKQUERY_ADDW(0); /* NSCOUNT=0 */
62 MKQUERY_ADDW(0); /* ARCOUNT=0 */
63
64 MKQUERY_STOP(vb);
65
66 return adns_s_ok;
67}
@ adns_s_nomemory
Definition: adns.h:219
GLuint id
Definition: glext.h:5910
#define DNS_HDRSIZE
Definition: internal.h:76
int nextid
Definition: internal.h:297

Referenced by adns__mkquery(), and adns__mkquery_frdgram().

◆ query_usetcp()

static void query_usetcp ( adns_query  qu,
struct timeval  now 
)
static

Definition at line 223 of file transmit.c.

223 {
224 qu->state= query_tcpw;
225 qu->timeout= now;
227 LIST_LINK_TAIL(qu->ads->tcpw,qu);
230}
void adns__tcp_tryconnect(adns_state ads, struct timeval now)
Definition: event.c:90
#define TCPWAITMS
Definition: internal.h:67
void adns__querysend_tcp(adns_query qu, struct timeval now)
Definition: transmit.c:168

Referenced by adns__query_send().