1 /* srp-log.c 2 * 3 * Copyright (c) 2021 Apple Inc. All rights reserved. 4 * 5 * Licensed under the Apache License, Version 2.0 (the "License"); 6 * you may not use this file except in compliance with the License. 7 * You may obtain a copy of the License at 8 * 9 * https://www.apache.org/licenses/LICENSE-2.0 10 * 11 * Unless required by applicable law or agreed to in writing, software 12 * distributed under the License is distributed on an "AS IS" BASIS, 13 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 * See the License for the specific language governing permissions and 15 * limitations under the License. 16 * 17 * This file contains the functions that help to create better logs. 18 */ 19 20 #include "srp-log.h" 21 22 #ifndef THREAD_DEVKIT_ADK 23 #include <netinet/in.h> // For struct in_addr and struct in6_addr used in "dns-msg.h". 24 #endif 25 #include "dns-msg.h" // For dns_qclass_xxx and dns_rrtype_xxx. 26 27 //====================================================================================================================== 28 // MARK: - Functions 29 30 const char * 31 dns_qclass_to_string(const uint16_t qclass) 32 { 33 #define CASE_TO_STR(s) case dns_qclass_ ## s: return (#s) 34 switch(qclass) 35 { 36 CASE_TO_STR(in); 37 CASE_TO_STR(chaos); 38 CASE_TO_STR(hesiod); 39 CASE_TO_STR(none); 40 CASE_TO_STR(any); 41 } 42 #undef CASE_TO_STR 43 FAULT("Invalid qclass - qclass: %u", qclass); 44 return "<INVALID dns_qclass>"; 45 } 46 47 //====================================================================================================================== 48 49 const char * 50 dns_rrtype_to_string(const uint16_t rrtype) 51 { 52 #define CASE_TO_STR(s) case dns_rrtype_ ## s: return (#s) 53 switch(rrtype) 54 { 55 CASE_TO_STR(a); 56 CASE_TO_STR(ns); 57 CASE_TO_STR(md); 58 CASE_TO_STR(mf); 59 CASE_TO_STR(cname); 60 CASE_TO_STR(soa); 61 CASE_TO_STR(mb); 62 CASE_TO_STR(mg); 63 CASE_TO_STR(mr); 64 CASE_TO_STR(null); 65 CASE_TO_STR(wks); 66 CASE_TO_STR(ptr); 67 CASE_TO_STR(hinfo); 68 CASE_TO_STR(minfo); 69 CASE_TO_STR(mx); 70 CASE_TO_STR(txt); 71 CASE_TO_STR(rp); 72 CASE_TO_STR(afsdb); 73 CASE_TO_STR(x25); 74 CASE_TO_STR(isdn); 75 CASE_TO_STR(rt); 76 CASE_TO_STR(nsap); 77 CASE_TO_STR(nsap_ptr); 78 CASE_TO_STR(sig); 79 CASE_TO_STR(key); 80 CASE_TO_STR(px); 81 CASE_TO_STR(gpos); 82 CASE_TO_STR(aaaa); 83 CASE_TO_STR(loc); 84 CASE_TO_STR(nxt); 85 CASE_TO_STR(eid); 86 CASE_TO_STR(nimloc); 87 CASE_TO_STR(srv); 88 CASE_TO_STR(atma); 89 CASE_TO_STR(naptr); 90 CASE_TO_STR(kx); 91 CASE_TO_STR(cert); 92 CASE_TO_STR(a6); 93 CASE_TO_STR(dname); 94 CASE_TO_STR(sink); 95 CASE_TO_STR(opt); 96 CASE_TO_STR(apl); 97 CASE_TO_STR(ds); 98 CASE_TO_STR(sshfp); 99 CASE_TO_STR(ipseckey); 100 CASE_TO_STR(rrsig); 101 CASE_TO_STR(nsec); 102 CASE_TO_STR(dnskey); 103 CASE_TO_STR(dhcid); 104 CASE_TO_STR(nsec3); 105 CASE_TO_STR(nsec3param); 106 CASE_TO_STR(tlsa); 107 CASE_TO_STR(smimea); 108 CASE_TO_STR(hip); 109 CASE_TO_STR(ninfo); 110 CASE_TO_STR(rkey); 111 CASE_TO_STR(talink); 112 CASE_TO_STR(cds); 113 CASE_TO_STR(cdnskey); 114 CASE_TO_STR(openpgpkey); 115 CASE_TO_STR(csync); 116 CASE_TO_STR(zonemd); 117 CASE_TO_STR(svcb); 118 CASE_TO_STR(https); 119 CASE_TO_STR(spf); 120 CASE_TO_STR(uinfo); 121 CASE_TO_STR(uid); 122 CASE_TO_STR(gid); 123 CASE_TO_STR(unspec); 124 CASE_TO_STR(nid); 125 CASE_TO_STR(l32); 126 CASE_TO_STR(l64); 127 CASE_TO_STR(lp); 128 CASE_TO_STR(eui48); 129 CASE_TO_STR(eui64); 130 CASE_TO_STR(tkey); 131 CASE_TO_STR(tsig); 132 CASE_TO_STR(ixfr); 133 CASE_TO_STR(axfr); 134 CASE_TO_STR(mailb); 135 CASE_TO_STR(maila); 136 CASE_TO_STR(any); 137 CASE_TO_STR(uri); 138 CASE_TO_STR(caa); 139 CASE_TO_STR(avc); 140 CASE_TO_STR(doa); 141 CASE_TO_STR(amtrelay); 142 CASE_TO_STR(ta); 143 CASE_TO_STR(dlv); 144 } 145 #undef CASE_TO_STR 146 FAULT("Invalid dns_rrtype - rrtype: %u", rrtype); 147 return "<INVALID dns_rrtype>"; 148 } 149 150 #ifdef LOG_FPRINTF_STDERR 151 bool srp_log_timestamp_relative = false; 152 153 void 154 srp_log_timestamp(char *buf, size_t bufsize) 155 { 156 if (srp_log_timestamp_relative) { 157 double srp_fractional_time(void); 158 static bool have_initial_timestamp = false; 159 static double initial_timestamp = 0.0; 160 if (!have_initial_timestamp) { 161 have_initial_timestamp = true; 162 initial_timestamp = srp_fractional_time(); 163 } 164 snprintf(buf, bufsize, "%6.6lf", srp_fractional_time() - initial_timestamp); 165 } else { 166 char timebuf[20]; // YYYY-MM-DD HH:MM:SS 167 char zonebuf[6]; // [-+]HMM 168 struct timeval tv; 169 struct tm tm; 170 gettimeofday(&tv, NULL); 171 localtime_r(&tv.tv_sec, &tm); 172 strftime(timebuf, sizeof(timebuf), "%F %T", &tm); 173 strftime(zonebuf, sizeof(zonebuf), "%z", &tm); 174 snprintf(buf, bufsize, "%s.%06d%s", timebuf, tv.tv_usec, zonebuf); 175 } 176 } 177 #endif 178 179 // Local Variables: 180 // mode: C 181 // tab-width: 4 182 // c-file-style: "bsd" 183 // c-basic-offset: 4 184 // fill-column: 120 185 // indent-tabs-mode: nil 186 // End: 187