]>
Commit | Line | Data |
---|---|---|
870a0fe4 AT |
1 | #ifdef HAVE_CONFIG_H |
2 | #include "config.h" | |
3 | #endif | |
c1e527e3 | 4 | #include "iputils.hh" |
5 | #include "sstuff.hh" | |
6 | #include "statbag.hh" | |
7 | ||
8 | StatBag S; | |
9 | ||
10 | int main(int argc, char** argv) | |
11 | try | |
12 | { | |
4465e941 | 13 | if(argc != 4) { |
14 | cerr<<"Syntax: dumresp local-address local-port number-of-threads "<<endl; | |
c1e527e3 | 15 | exit(EXIT_FAILURE); |
16 | } | |
17 | ||
4465e941 | 18 | for(int i=1 ; i < atoi(argv[3]); ++i) { |
c1e527e3 | 19 | if(!fork()) |
20 | break; | |
21 | } | |
4465e941 | 22 | |
23 | ComboAddress local(argv[1], atoi(argv[2])); | |
24 | Socket s(local.sin4.sin_family, SOCK_DGRAM); | |
c648bdce | 25 | #ifdef SO_REUSEPORT |
c1e527e3 | 26 | int one=1; |
27 | if(setsockopt(s.getHandle(), SOL_SOCKET, SO_REUSEPORT, &one, sizeof(one)) < 0) | |
28 | unixDie("setsockopt for REUSEPORT"); | |
c648bdce | 29 | #endif |
c1e527e3 | 30 | |
31 | s.bind(local); | |
4465e941 | 32 | cout<<"Bound to "<<local.toStringWithPort()<<endl; |
c1e527e3 | 33 | char buffer[1500]; |
34 | struct dnsheader* dh = (struct dnsheader*)buffer; | |
35 | int len; | |
36 | ComboAddress rem=local; | |
37 | socklen_t socklen = rem.getSocklen(); | |
38 | for(;;) { | |
39 | len=recvfrom(s.getHandle(), buffer, sizeof(buffer), 0, (struct sockaddr*)&rem, &socklen); | |
40 | if(len < 0) | |
41 | unixDie("recvfrom"); | |
4465e941 | 42 | cout<<"Had packet: "<<string(buffer, len)<<endl; |
c1e527e3 | 43 | if(dh->qr) |
44 | continue; | |
45 | dh->qr=1; | |
46 | dh->ad=0; | |
47 | if(sendto(s.getHandle(), buffer, len, 0, (struct sockaddr*)&rem, socklen) < 0) | |
48 | unixDie("sendto"); | |
49 | ||
50 | } | |
51 | } | |
52 | catch(std::exception& e) | |
53 | { | |
54 | cerr<<"Fatal error: "<<e.what()<<endl; | |
55 | exit(EXIT_FAILURE); | |
56 | } |