common/ctrace.c

Go to the documentation of this file.
00001 /* trace.c
00002 
00003    Subroutines that support dhcp tracing... */
00004 
00005 /*
00006  * Copyright (c) 2004,2007,2009,2014 by Internet Systems Consortium, Inc. ("ISC")
00007  * Copyright (c) 2001-2003 by Internet Software Consortium
00008  *
00009  * Permission to use, copy, modify, and distribute this software for any
00010  * purpose with or without fee is hereby granted, provided that the above
00011  * copyright notice and this permission notice appear in all copies.
00012  *
00013  * THE SOFTWARE IS PROVIDED "AS IS" AND ISC DISCLAIMS ALL WARRANTIES
00014  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
00015  * MERCHANTABILITY AND FITNESS.  IN NO EVENT SHALL ISC BE LIABLE FOR
00016  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
00017  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
00018  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT
00019  * OF OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
00020  *
00021  *   Internet Systems Consortium, Inc.
00022  *   950 Charter Street
00023  *   Redwood City, CA 94063
00024  *   <info@isc.org>
00025  *   https://www.isc.org/
00026  *
00027  */
00028 
00029 #include "dhcpd.h"
00030 
00031 #if defined (TRACING)
00032 void trace_interface_register (trace_type_t *ttype, struct interface_info *ip)
00033 {
00034         trace_interface_packet_t tipkt;
00035 
00036         if (trace_record ()) {
00037                 memset (&tipkt, 0, sizeof tipkt);
00038                 memcpy (&tipkt.hw_address,
00039                         &ip -> hw_address, sizeof ip -> hw_address);
00040                 if (ip->address_count)
00041                         memcpy(&tipkt.primary_address,
00042                                ip->addresses, sizeof(*ip->addresses));
00043                 memcpy (tipkt.name, ip -> name, sizeof ip -> name);
00044                 tipkt.index = htonl (ip -> index);
00045 
00046                 trace_write_packet (ttype, sizeof tipkt, (char *)&tipkt, MDL);
00047         }       
00048 }
00049 
00050 void trace_interface_input (trace_type_t *ttype, unsigned len, char *buf)
00051 {
00052         trace_interface_packet_t *tipkt;
00053         struct interface_info *ip;
00054         struct sockaddr_in *sin;
00055         struct iaddr addr;
00056         isc_result_t status;
00057 
00058         if (len != sizeof *tipkt) {
00059                 log_error ("trace interface packet size mismatch: %ld != %d",
00060                            (long)(sizeof *tipkt), len);
00061                 return;
00062         }
00063         tipkt = (trace_interface_packet_t *)buf;
00064         
00065         ip = (struct interface_info *)0;
00066         status = interface_allocate (&ip, MDL);
00067         if (status != ISC_R_SUCCESS) {
00068               foo:
00069                 log_error ("trace_interface_input: %s.",
00070                            isc_result_totext (status));
00071                 return;
00072         }
00073         ip -> ifp = dmalloc (sizeof *(ip -> ifp), MDL);
00074         if (!ip -> ifp) {
00075                 interface_dereference (&ip, MDL);
00076                 status = ISC_R_NOMEMORY;
00077                 goto foo;
00078         }
00079 
00080         memcpy (&ip -> hw_address, &tipkt -> hw_address,
00081                 sizeof ip -> hw_address);
00082         /* XXX: Without the full addresses state it's not quite a full
00083          * trace.
00084          */
00085         ip->address_count = ip->address_max = 1;
00086         ip->addresses = dmalloc(sizeof(*ip->addresses), MDL);
00087         memcpy(ip->addresses, &tipkt->primary_address, sizeof(*ip->addresses));
00088         memcpy (ip -> name, tipkt -> name, sizeof ip -> name);
00089         ip -> index = ntohl (tipkt -> index);
00090 
00091         interface_snorf (ip, 0);
00092         if (dhcp_interface_discovery_hook)
00093                 (*dhcp_interface_discovery_hook) (ip);
00094 
00095         /* Fake up an ifp. */
00096         memcpy (ip -> ifp -> ifr_name, ip -> name, sizeof ip -> name);
00097 #ifdef HAVE_SA_LEN
00098         ip -> ifp -> ifr_addr.sa_len = sizeof (struct sockaddr_in);
00099 #endif
00100         sin = (struct sockaddr_in *)&ip -> ifp -> ifr_addr;
00101         sin->sin_addr = ip->addresses[0];
00102 
00103         addr.len = 4;
00104         memcpy (addr.iabuf, &sin -> sin_addr.s_addr, addr.len);
00105         if (dhcp_interface_setup_hook)
00106                 (*dhcp_interface_setup_hook) (ip, &addr);
00107         interface_stash (ip);
00108 
00109         if (!quiet_interface_discovery) {
00110                 log_info ("Listening on Trace/%s/%s%s%s",
00111                           ip -> name,
00112                           print_hw_addr (ip -> hw_address.hbuf [0],
00113                                          ip -> hw_address.hlen - 1,
00114                                          &ip -> hw_address.hbuf [1]),
00115                           (ip -> shared_network ? "/" : ""),
00116                           (ip -> shared_network ?
00117                            ip -> shared_network -> name : ""));
00118                 if (strcmp (ip -> name, "fallback")) {
00119                         log_info ("Sending   on Trace/%s/%s%s%s",
00120                                   ip -> name,
00121                                   print_hw_addr (ip -> hw_address.hbuf [0],
00122                                                  ip -> hw_address.hlen - 1,
00123                                                  &ip -> hw_address.hbuf [1]),
00124                                   (ip -> shared_network ? "/" : ""),
00125                                   (ip -> shared_network ?
00126                                    ip -> shared_network -> name : ""));
00127                 }
00128         }
00129         interface_dereference (&ip, MDL);
00130 }
00131 
00132 void trace_interface_stop (trace_type_t *ttype) {
00133         /* XXX */
00134 }
00135 
00136 void trace_inpacket_stash (struct interface_info *interface,
00137                            struct dhcp_packet *packet,
00138                            unsigned len,
00139                            unsigned int from_port,
00140                            struct iaddr from,
00141                            struct hardware *hfrom)
00142 {
00143         trace_inpacket_t tip;
00144         trace_iov_t iov [2];
00145 
00146         if (!trace_record ())
00147                 return;
00148         tip.from_port = from_port;
00149         tip.from = from;
00150         tip.from.len = htonl (tip.from.len);
00151         if (hfrom) {
00152                 tip.hfrom = *hfrom;
00153                 tip.havehfrom = 1;
00154         } else {
00155                 memset (&tip.hfrom, 0, sizeof tip.hfrom);
00156                 tip.havehfrom = 0;
00157         }
00158         tip.index = htonl (interface -> index);
00159 
00160         iov [0].buf = (char *)&tip;
00161         iov [0].len = sizeof tip;
00162         iov [1].buf = (char *)packet;
00163         iov [1].len = len;
00164         trace_write_packet_iov (inpacket_trace, 2, iov, MDL);
00165 }
00166 
00167 void trace_inpacket_input (trace_type_t *ttype, unsigned len, char *buf)
00168 {
00169         trace_inpacket_t *tip;
00170         int index;
00171 
00172         if (len < sizeof *tip) {
00173                 log_error ("trace_input_packet: too short - %d", len);
00174                 return;
00175         }
00176         tip = (trace_inpacket_t *)buf;
00177         index = ntohl (tip -> index);
00178         tip -> from.len = ntohl (tip -> from.len);
00179         
00180         if (index > interface_count ||
00181             index < 0 ||
00182             !interface_vector [index]) {
00183                 log_error ("trace_input_packet: unknown interface index %d",
00184                            index);
00185                 return;
00186         }
00187 
00188         if (!bootp_packet_handler) {
00189                 log_error ("trace_input_packet: no bootp packet handler.");
00190                 return;
00191         }
00192 
00193         (*bootp_packet_handler) (interface_vector [index],
00194                                  (struct dhcp_packet *)(tip + 1),
00195                                  len - sizeof *tip,
00196                                  tip -> from_port,
00197                                  tip -> from,
00198                                  (tip -> havehfrom ?
00199                                   &tip -> hfrom
00200                                   : (struct hardware *)0));
00201 }
00202 
00203 void trace_inpacket_stop (trace_type_t *ttype) { }
00204 
00205 ssize_t trace_packet_send (struct interface_info *interface,
00206                            struct packet *packet,
00207                            struct dhcp_packet *raw,
00208                            size_t len,
00209                            struct in_addr from,
00210                            struct sockaddr_in *to,
00211                            struct hardware *hto)
00212 {
00213         trace_outpacket_t tip;
00214         trace_iov_t iov [2];
00215 
00216         if (trace_record ()) {
00217                 if (hto) {
00218                         tip.hto = *hto;
00219                         tip.havehto = 1;
00220                 } else {
00221                         memset (&tip.hto, 0, sizeof tip.hto);
00222                         tip.havehto = 0;
00223                 }
00224                 tip.from.len = 4;
00225                 memcpy (tip.from.iabuf, &from, 4);
00226                 tip.to.len = 4;
00227                 memcpy (tip.to.iabuf, &to -> sin_addr, 4);
00228                 tip.to_port = to -> sin_port;
00229                 tip.index = htonl (interface -> index);
00230 
00231                 iov [0].buf = (char *)&tip;
00232                 iov [0].len = sizeof tip;
00233                 iov [1].buf = (char *)raw;
00234                 iov [1].len = len;
00235                 trace_write_packet_iov (outpacket_trace, 2, iov, MDL);
00236         }
00237         if (!trace_playback ()) {
00238                 return send_packet (interface, packet, raw, len,
00239                                     from, to, hto);
00240         }
00241         return len;
00242 }
00243 
00244 void trace_outpacket_input (trace_type_t *ttype, unsigned len, char *buf)
00245 {
00246         trace_outpacket_t *tip;
00247         int index;
00248 
00249         if (len < sizeof *tip) {
00250                 log_error ("trace_input_packet: too short - %d", len);
00251                 return;
00252         }
00253         tip = (trace_outpacket_t *)buf;
00254         index = ntohl (tip -> index);
00255         
00256         if (index > interface_count ||
00257             index < 0 ||
00258             !interface_vector [index]) {
00259                 log_error ("trace_input_packet: unknown interface index %d",
00260                            index);
00261                 return;
00262         }
00263 
00264         /* XXX would be nice to somehow take notice of these. */
00265 }
00266 
00267 void trace_outpacket_stop (trace_type_t *ttype) { }
00268 
00269 void trace_seed_stash (trace_type_t *ttype, unsigned seed)
00270 {
00271         u_int32_t outseed;
00272         if (!trace_record ())
00273                 return;
00274         outseed = htonl (seed);
00275         trace_write_packet (ttype, sizeof outseed, (char *)&outseed, MDL);
00276         return;
00277 }
00278 
00279 void trace_seed_input (trace_type_t *ttype, unsigned length, char *buf)
00280 {
00281         u_int32_t *seed;
00282 
00283         if (length != sizeof seed) {
00284                 log_error ("trace_seed_input: wrong size (%d)", length);
00285         }
00286         seed = (u_int32_t *)buf;
00287         srandom (ntohl (*seed));
00288 }
00289 
00290 void trace_seed_stop (trace_type_t *ttype) { }
00291 #endif /* TRACING */

Generated on 5 Apr 2014 for ISC DHCP by  doxygen 1.6.1