1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315
|
#include <stdlib.h>
#include <string.h>
#include <stdio.h>
#include <unistd.h>
#include <string>
#include "dnsname.hh"
#include "namespaces.hh"
#include "dnswriter.hh"
#include "misc.hh"
namespace {
void appendSplit(vector<string>& ret, string& segment, char c)
{
if(segment.size()>254) {
ret.push_back(segment);
segment.clear();
}
segment.append(1, c);
}
}
vector<string> segmentDNSText(const string& input )
{
// cerr<<"segmentDNSText("<<input<<")"<<endl;
%%{
machine dnstext;
write data;
alphtype unsigned char;
}%%
(void)dnstext_error; // silence warnings
(void)dnstext_en_main;
const char *p = input.c_str(), *pe = input.c_str() + input.length();
const char* eof = pe;
int cs;
char val = 0;
string segment;
vector<string> ret;
%%{
action segmentEnd {
ret.push_back(segment);
segment.clear();
}
action segmentBegin {
segment.clear();
}
action reportEscaped {
char c = *fpc;
appendSplit(ret, segment, c);
}
action reportEscapedNumber {
char c = *fpc;
val *= 10;
val += c-'0';
}
action doneEscapedNumber {
appendSplit(ret, segment, val);
val=0;
}
action reportPlain {
appendSplit(ret, segment, *(fpc));
}
escaped = '\\' (([^0-9]@reportEscaped) | ([0-9]{3}$reportEscapedNumber%doneEscapedNumber));
plain = ((extend-'\\'-'"')|'\n'|'\t') $ reportPlain;
txtElement = escaped | plain;
main := (('"' txtElement* '"' space?) >segmentBegin %segmentEnd)+;
# Initialize and execute.
write init;
write exec;
}%%
if ( cs < dnstext_first_final ) {
throw runtime_error("Unable to parse DNS TXT '"+input+"'");
}
return ret;
};
DNSName::string_t segmentDNSNameRaw(const char* realinput, size_t inputlen)
{
%%{
machine dnsnameraw;
write data;
alphtype unsigned char;
}%%
(void)dnsnameraw_error; // silence warnings
(void)dnsnameraw_en_main;
DNSName::string_t ret;
if(!*realinput || *realinput == '.') {
ret.append(1, (char)0);
return ret;
}
ret.reserve(inputlen+1);
const char *p = realinput, *pe = realinput + inputlen;
const char* eof = pe;
int cs;
char val = 0;
unsigned char labellen=0;
unsigned int lenpos=0;
%%{
action labelEnd {
if (labellen > 63) {
throw runtime_error("Unable to parse DNS name '"+string(realinput)+"': invalid label length "+std::to_string(labellen));
}
ret[lenpos]=labellen;
labellen=0;
}
action labelBegin {
lenpos=ret.size();
ret.append(1, (char)0);
labellen=0;
}
action reportEscaped {
char c = *fpc;
ret.append(1, c);
labellen++;
}
action reportEscapedNumber {
char c = *fpc;
val *= 10;
val += c-'0';
}
action doneEscapedNumber {
ret.append(1, val);
labellen++;
val=0;
}
action reportPlain {
ret.append(1, *(fpc));
labellen++;
}
escaped = '\\' (([^0-9]@reportEscaped) | ([0-9]{3}$reportEscapedNumber%doneEscapedNumber));
plain = (extend-'\\'-'.') $ reportPlain;
labelElement = escaped | plain;
label = labelElement+ >labelBegin %labelEnd;
main:= label ('.' label )* '.'?;
#main := labelElement((labelElement+ '.') >labelBegin %labelEnd)+;
# label = (plain | escaped | escdecb)+ >label_init %label_fin;
# dnsname := '.'? label ('.' label >label_sep)* '.'?;
# Initialize and execute.
write init;
write exec;
}%%
if ( cs < dnsnameraw_first_final ) {
throw runtime_error("Unable to parse DNS name '"+string(realinput)+"': cs="+std::to_string(cs));
}
ret.append(1, (char)0);
return ret;
};
// Reads an RFC 1035 character string from 'in', puts the resulting bytes in 'out'.
// Returns the amount of bytes read from 'in'
size_t parseRFC1035CharString(const std::string &in, std::string &val) {
val.clear();
val.reserve(in.size());
const char *p = in.c_str();
const char *pe = p + in.size();
int cs = 0;
uint8_t escaped_octet = 0;
// Keeps track of how many chars we read from the source string
size_t counter=0;
/* This parses an RFC 1035 char-string.
* It was created from the ABNF in draft-ietf-dnsop-svcb-https-02 with
* https://github.com/zinid/abnfc and modified to put all the characters in the
* right place.
*/
%%{
machine dns_text_to_string;
action doEscapedNumber {
escaped_octet *= 10;
escaped_octet += fc-'0';
counter++;
}
action doneEscapedNumber {
val += escaped_octet;
escaped_octet = 0;
}
action addToVal {
val += fc;
counter++;
}
action incrementCounter {
counter++;
}
# generated rules, define required actions
DIGIT = 0x30..0x39;
DQUOTE = "\"";
HTAB = "\t";
SP = " ";
WSP = (SP | HTAB)@addToVal;
non_special = "!" | 0x23..0x27 | 0x2a..0x3a | 0x3c..0x5b | 0x5d..0x7e;
non_digit = 0x21..0x2f | 0x3a..0x7e;
dec_octet = ( ( "0" | "1" ) DIGIT{2} ) | ( "2" ( ( 0x30..0x34 DIGIT ) | ( "5" 0x30..0x35 ) ) );
escaped = '\\'@incrementCounter ( non_digit$addToVal | dec_octet$doEscapedNumber@doneEscapedNumber );
contiguous = ( non_special$addToVal | escaped )+;
quoted = DQUOTE@incrementCounter ( contiguous | ( '\\'? WSP ) )* DQUOTE@incrementCounter;
char_string = (contiguous | quoted);
# instantiate machine rules
main := char_string;
write data;
write init;
}%%
// silence warnings
(void) dns_text_to_string_first_final;
(void) dns_text_to_string_error;
(void) dns_text_to_string_en_main;
%% write exec;
return counter;
}
size_t parseSVCBValueListFromParsedRFC1035CharString(const std::string &in, std::vector<std::string> &val) {
val.clear();
const char *p = in.c_str();
const char *pe = p + in.size();
int cs = 0;
const char* eof = pe;
// Keeps track of how many chars we read from the source string
size_t counter=0;
// Here we store the parsed value until we hit a comma or are done
std::string tmp;
%%{
machine dns_text_to_value_list;
alphtype unsigned char;
action addToVal {
tmp += fc;
counter++;
}
action addToValNoIncrement {
tmp += fc;
}
action addToVector {
val.push_back(tmp);
tmp.clear();
counter++;
}
action incrementCounter {
counter++;
}
# generated rules, define required actions
OCTET = 0x00..0xff;
item_allowed = 0x00..0x2b | 0x2d..0x5b | 0x5d..0xff;
escaped_item = ( item_allowed$addToVal | '\\,'$incrementCounter@addToValNoIncrement | '\\\\'$incrementCounter@addToValNoIncrement )+;
comma_separated = ( escaped_item%addToVector ( ","@incrementCounter escaped_item%addToVector )* )?;
# instantiate machine rules
main := comma_separated;
write data;
write init;
}%%
// silence warnings
(void) dns_text_to_value_list_first_final;
(void) dns_text_to_value_list_error;
(void) dns_text_to_value_list_en_main;
%% write exec;
if ( cs < dns_text_to_value_list_first_final ) {
throw runtime_error("Unable to parse DNS SVCB value list '"+in+"'");
}
return counter;
}
#if 0
int main()
{
//char blah[]="\"blah\" \"bleh\" \"bloeh\\\"bleh\" \"\\97enzo\"";
char blah[]="\"v=spf1 ip4:67.106.74.128/25 ip4:63.138.42.224/28 ip4:65.204.46.224/27 \\013\\010ip4:66.104.217.176/28 \\013\\010ip4:209.48.147.0/27 ~all\"";
//char blah[]="\"abc \\097\\098 def\"";
printf("Input: '%s'\n", blah);
vector<string> res=dnstext(blah);
cerr<<res.size()<<" segments"<<endl;
cerr<<res[0]<<endl;
}
#endif
|