Back to index

courier  0.68.2
rfc1035dump.c
Go to the documentation of this file.
00001 /*
00002 ** Copyright 1998 - 2000 Double Precision, Inc.
00003 ** See COPYING for distribution information.
00004 */
00005 
00006 #include      "rfc1035.h"
00007 #include      <stdlib.h>
00008 #include      <arpa/inet.h>
00009 
00010 
00011 static void print_hostname(FILE *f, const char *p)
00012 {
00013        for ( ; *p; p++)
00014        {
00015               if (*p < ' ' || *p >= 127)
00016               {
00017                      fprintf(f, "\\%03o", (int)(unsigned char)*p);
00018                      continue;
00019               }
00020 
00021               if (*p == '\\')
00022               {
00023                      fprintf(f, "\\\\");
00024                      continue;
00025               }
00026               putc(*p, f);
00027        }
00028 }
00029 
00030 static void tostr_callback(const char *ptr, void *vp)
00031 {
00032        FILE *fp=*(FILE **)vp;
00033 
00034        fprintf(fp, "%s", ptr);
00035 }
00036 
00037 void rfc1035_dump(struct rfc1035_reply *r, FILE *f)
00038 {
00039 unsigned n;
00040 char namebuf[RFC1035_MAXNAMESIZE+1];
00041 char timebuf[RFC1035_MAXTIMEBUFSIZE+1];
00042 char   ipbuf[RFC1035_NTOABUFSIZE];
00043 struct rfc1035_reply *qr;
00044 
00045        fprintf(f, ";;HEADER");
00046 
00047        rfc1035_ntoa((const RFC1035_ADDR *)&r->server_addr, ipbuf);
00048 
00049        fprintf(f, " (server %s)", ipbuf);
00050 
00051        fprintf(f, ":\n;;  Bytes: %ld\n", 
00052               (long)r->replylen);
00053 
00054        fprintf(f, ";;  Opcode: %s\n", 
00055               rfc1035_opcode_itostr(r->opcode));
00056        fprintf(f, ";;  Flags:");
00057        if (r->qr)
00058               fprintf(f, " qr");
00059        if (r->aa)
00060               fprintf(f, " aa");
00061        if (r->tc)
00062               fprintf(f, " tc");
00063        if (r->rd)
00064               fprintf(f, " rd");
00065        if (r->ra)
00066               fprintf(f, " ra");
00067        if (r->ad)
00068               fprintf(f, " ad");
00069        if (r->cd)
00070               fprintf(f, " cd");
00071        fprintf(f, "\n;;  Status: %s\n", rfc1035_rcode_itostr(r->rcode));
00072        fprintf(f, ";;  # questions: %u\n", r->qdcount);
00073        fprintf(f, ";;  # answers: %u\n", r->ancount);
00074        fprintf(f, ";;  # authoritative: %u\n", r->nscount);
00075        fprintf(f, ";;  # additional: %u\n", r->arcount);
00076 
00077        fprintf(f, ";;\n;;QUESTIONS:\n");
00078        for (n=0; n<r->qdcount; n++)
00079        {
00080               fprintf(f, ";;  ");
00081               print_hostname(f, rfc1035_replyhostname(r, r->qdptr[n].name,
00082                                                  namebuf));
00083               fprintf(f,".\t%s ",
00084                      rfc1035_class_itostr(r->qdptr[n].qclass));
00085 
00086               rfc1035_type_itostr(r->qdptr[n].qtype, tostr_callback, &f);
00087 
00088               fprintf(f, "\n");
00089        }
00090 
00091        fprintf(f, "\n;;ANSWERS:\n");
00092        for (qr=r; qr; qr=qr->next)
00093        {
00094               for (n=0; n<qr->ancount; n++)
00095               {
00096                      char   *c;
00097 
00098                      fprintf(f, " ");
00099                      print_hostname(f,
00100                                    rfc1035_replyhostname(qr,
00101                                                       qr->anptr[n]
00102                                                       .rrname,
00103                                                       namebuf));
00104                      fprintf(f, ".\t%s\t%s ",
00105                             rfc1035_fmttime(qr->anptr[n].ttl, timebuf),
00106                             rfc1035_class_itostr(qr->anptr[n].rrclass));
00107 
00108                      rfc1035_type_itostr(qr->anptr[n].rrtype, tostr_callback,
00109                                        &f);
00110 
00111                      c=rfc1035_dumprrdata(qr, qr->anptr+n);
00112                      if (c)
00113                      {
00114                             fprintf(f, "\t%s", c);
00115                             free(c);
00116                      }
00117                      fprintf(f, "\n");
00118               }
00119        }
00120        fprintf(f, "\n;;AUTHORITATIVE:\n");
00121        for (n=0; n<r->nscount; n++)
00122        {
00123               char   *c;
00124 
00125               fprintf(f, " ");
00126               print_hostname(f, rfc1035_replyhostname(r,
00127                                                  r->nsptr[n].rrname,
00128                                                  namebuf));
00129               fprintf(f, ".\t%s\t%s ",
00130                      rfc1035_fmttime(r->nsptr[n].ttl, timebuf),
00131                      rfc1035_class_itostr(r->nsptr[n].rrclass));
00132 
00133               rfc1035_type_itostr(r->nsptr[n].rrtype, tostr_callback, &f);
00134 
00135               c=rfc1035_dumprrdata(r, r->nsptr+n);
00136               if (c)
00137               {
00138                      fprintf(f, "\t%s", c);
00139                      free(c);
00140               }
00141               fprintf(f, "\n");
00142        }
00143 
00144        fprintf(f, "\n;;ADDITIONAL:\n");
00145        for (n=0; n<r->arcount; n++)
00146        {
00147               char   *c;
00148 
00149               if (r->arptr[n].rrtype == RFC1035_TYPE_OPT)
00150                      continue;
00151 
00152               fprintf(f, " ");
00153               print_hostname(f, rfc1035_replyhostname(r,
00154                                                  r->arptr[n].rrname,
00155                                                  namebuf));
00156               fprintf(f, ".\t%s\t%s ",
00157                      rfc1035_fmttime(r->arptr[n].ttl, timebuf),
00158                      rfc1035_class_itostr(r->arptr[n].rrclass));
00159 
00160               rfc1035_type_itostr(r->arptr[n].rrtype, tostr_callback, &f);
00161 
00162               c=rfc1035_dumprrdata(r, r->arptr+n);
00163               if (c)
00164               {
00165                      fprintf(f, "\t%s", c);
00166                      free(c);
00167               }
00168               fprintf(f, "\n");
00169        }
00170 }