Commit 171b8014 authored by nanahira's avatar nanahira

support random response

parent cb53f9e2
Pipeline #16752 failed with stages
in 3 minutes and 31 seconds
......@@ -10,8 +10,6 @@
#include "replay_mode.h"
#ifdef _WIN32
#include <Windns.h>
#else
#include <resolv.h>
#endif
namespace ygo {
......@@ -4392,24 +4390,25 @@ bool DuelClient::LookupSRV(char *hostname, HostResult* result) {
if(status < 0)
return false;
ns_msg nsMsg;
ns_rr rr;
ns_initparse(answer, status, &nsMsg);
bool found = false;
for (int i = 0; i < ns_msg_count(nsMsg, ns_s_an); i++) {
if (ns_parserr(&nsMsg, ns_s_an, i, &rr) < 0 || ns_rr_type(rr) != T_SRV)
continue;
// priority = ns_get16(ns_rr_rdata(rr));
// weight = ns_get16(ns_rr_rdata(rr) + NS_INT16SZ);
result->port = ns_get16(ns_rr_rdata(rr) + 2 * NS_INT16SZ);
// decompress domain name
if (dn_expand(ns_msg_base(nsMsg), ns_msg_end(nsMsg), ns_rr_rdata(rr) + 3 * NS_INT16SZ, resolved, sizeof(resolved)) < 0)
std::vector<RetrivedSRVRecord> records;
for(int i = 0; i < ns_msg_count(nsMsg, ns_s_an); i++) {
auto record = RetrivedSRVRecord(nsMsg, i);
if(!record.valid)
continue;
found = true;
for (int j = 0; j < record.weight; ++j) {
records.push_back(record);
}
}
if(!found)
if (!records.size())
return false;
rnd.reset((unsigned int)time(nullptr));
rnd.shuffle_vector(records);
auto record = records.front();
printf("Result: %s:%d\n", record.host, record.port);
memcpy(resolved, record.host, 100);
result->port = record.port;
#endif
result->host = LookupHost(resolved);
return true;
......
......@@ -30,6 +30,30 @@ public:
}
};
#ifndef _WIN32
#include <resolv.h>
class RetrivedSRVRecord {
public:
bool valid;
unsigned short priority;
unsigned short weight;
unsigned short port;
char host[100];
RetrivedSRVRecord(ns_msg nsMsg, int i) {
valid = false;
ns_rr rr;
if (ns_parserr(&nsMsg, ns_s_an, i, &rr) < 0 || ns_rr_type(rr) != T_SRV)
return;
priority = ns_get16(ns_rr_rdata(rr));
weight = ns_get16(ns_rr_rdata(rr) + NS_INT16SZ);
port = ns_get16(ns_rr_rdata(rr) + 2 * NS_INT16SZ);
if (dn_expand(ns_msg_base(nsMsg), ns_msg_end(nsMsg), ns_rr_rdata(rr) + 3 * NS_INT16SZ, host, sizeof(host)) < 0)
return;
valid = true;
}
};
#endif
class DuelClient {
private:
static unsigned int connect_state;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment