isc_result_t dhcp_subnet_destroy (omapi_object_t *h, const char *file, int line) { struct subnet *subnet; isc_result_t status; if (h -> type != dhcp_type_subnet) return ISC_R_INVALIDARG; subnet = (struct subnet *)h; #if defined (DEBUG_MEMORY_LEAKAGE) || \ defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT) if (subnet -> next_subnet) subnet_dereference (&subnet -> next_subnet, file, line); if (subnet -> next_sibling) subnet_dereference (&subnet -> next_sibling, file, line); if (subnet -> shared_network) shared_network_dereference (&subnet -> shared_network, file, line); if (subnet -> interface) interface_dereference (&subnet -> interface, file, line); if (subnet -> group) group_dereference (&subnet -> group, file, line); #endif return ISC_R_SUCCESS; }
isc_result_t dhcp_shared_network_destroy (omapi_object_t *h, const char *file, int line) { /* In this function h should be a (struct shared_network *) */ #if defined (DEBUG_MEMORY_LEAKAGE) || \ defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT) struct shared_network *shared_network; #endif if (h -> type != dhcp_type_shared_network) return DHCP_R_INVALIDARG; #if defined (DEBUG_MEMORY_LEAKAGE) || \ defined (DEBUG_MEMORY_LEAKAGE_ON_EXIT) shared_network = (struct shared_network *)h; if (shared_network -> next) shared_network_dereference (&shared_network -> next, file, line); if (shared_network -> name) { dfree (shared_network -> name, file, line); shared_network -> name = 0; } if (shared_network -> subnets) subnet_dereference (&shared_network -> subnets, file, line); if (shared_network -> interface) interface_dereference (&shared_network -> interface, file, line); if (shared_network -> pools) omapi_object_dereference ((omapi_object_t **) &shared_network -> pools, file, line); if (shared_network -> group) group_dereference (&shared_network -> group, file, line); #if defined (FAILOVER_PROTOCOL) if (shared_network -> failover_peer) omapi_object_dereference ((omapi_object_t **) &shared_network -> failover_peer, file, line); #endif #endif /* DEBUG_MEMORY_LEAKAGE */ return ISC_R_SUCCESS; }
int main(int argc, char **argv) { int fd; int i; struct servent *ent; struct server_list *sp = NULL; int no_daemon = 0; int quiet = 0; isc_result_t status; char *s; /* Make sure that file descriptors 0 (stdin), 1, (stdout), and 2 (stderr) are open. To do this, we assume that when we open a file the lowest available file descriptor is used. */ fd = open("/dev/null", O_RDWR); if (fd == 0) fd = open("/dev/null", O_RDWR); if (fd == 1) fd = open("/dev/null", O_RDWR); if (fd == 2) log_perror = 0; /* No sense logging to /dev/null. */ else if (fd != -1) close(fd); openlog ("dhcrelay", LOG_NDELAY|LOG_PID, LOG_DAEMON); #if !defined(DEBUG) setlogmask (LOG_UPTO (LOG_INFO)); #endif /* Set up the OMAPI. */ status = omapi_init (); if (status != ISC_R_SUCCESS) log_fatal ("Can't initialize OMAPI: %s", isc_result_totext (status)); /* Set up the OMAPI wrappers for the interface object. */ interface_setup (); for (i = 1; i < argc; i++) { if (!strcmp (argv [i], "-p")) { if (++i == argc) usage (); local_port = htons (atoi (argv [i])); log_debug ("binding to user-specified port %d", ntohs (local_port)); } else if (!strcmp (argv [i], "-d")) { no_daemon = 1; } else if (!strcmp (argv [i], "-i")) { struct interface_info *tmp = (struct interface_info *)0; status = interface_allocate (&tmp, MDL); if (status != ISC_R_SUCCESS) log_fatal ("%s: interface_allocate: %s", argv [i], isc_result_totext (status)); if (++i == argc) { usage (); } strcpy (tmp -> name, argv [i]); interface_snorf (tmp, INTERFACE_REQUESTED); interface_dereference (&tmp, MDL); } else if (!strcmp (argv [i], "-q")) { quiet = 1; quiet_interface_discovery = 1; } else if (!strcmp (argv [i], "-a")) { add_agent_options = 1; } else if (!strcmp (argv [i], "-c")) { int hcount; if (++i == argc) usage (); hcount = atoi(argv[i]); if (hcount <= 255) max_hop_count= hcount; else usage (); } else if (!strcmp (argv [i], "-A")) { if (++i == argc) usage (); dhcp_max_agent_option_packet_length = atoi (argv [i]); } else if (!strcmp (argv [i], "-m")) { if (++i == argc) usage (); if (!strcasecmp (argv [i], "append")) { agent_relay_mode = forward_and_append; } else if (!strcasecmp (argv [i], "replace")) { agent_relay_mode = forward_and_replace; } else if (!strcasecmp (argv [i], "forward")) { agent_relay_mode = forward_untouched; } else if (!strcasecmp (argv [i], "discard")) { agent_relay_mode = discard; } else usage (); } else if (!strcmp (argv [i], "-D")) { drop_agent_mismatches = 1; } else if (argv [i][0] == '-') { usage (); } else if (!strcmp (argv [i], "--version")) { log_info ("isc-dhcrelay-%s", PACKAGE_VERSION); exit (0); } else { struct hostent *he; struct in_addr ia, *iap = (struct in_addr *)0; if (inet_aton (argv [i], &ia)) { iap = &ia; } else { he = gethostbyname (argv [i]); if (!he) { log_error ("%s: host unknown", argv [i]); } else { iap = ((struct in_addr *) he -> h_addr_list [0]); } } if (iap) { sp = ((struct server_list *) dmalloc (sizeof *sp, MDL)); if (!sp) log_fatal ("no memory for server.\n"); sp -> next = servers; servers = sp; memcpy (&sp -> to.sin_addr, iap, sizeof *iap); } } } if ((s = getenv ("PATH_DHCRELAY_PID"))) { path_dhcrelay_pid = s; } if (!quiet) { log_info ("%s %s", message, PACKAGE_VERSION); log_info (copyright); log_info (arr); log_info (url); } else { quiet = 0; log_perror = 0; } /* Default to the DHCP/BOOTP port. */ if (!local_port) { ent = getservbyname ("dhcps", "udp"); if (!ent) local_port = htons (67); else local_port = ent -> s_port; endservent (); } remote_port = htons (ntohs (local_port) + 1); /* We need at least one server. */ if (!sp) { usage (); } /* Set up the server sockaddrs. */ for (sp = servers; sp; sp = sp -> next) { sp -> to.sin_port = local_port; sp -> to.sin_family = AF_INET; #ifdef HAVE_SA_LEN sp -> to.sin_len = sizeof sp -> to; #endif } /* Get the current time... */ time(&cur_time); /* Discover all the network interfaces. */ discover_interfaces (DISCOVER_RELAY); /* Set up the bootp packet handler... */ bootp_packet_handler = relay; /* Become a daemon... */ if (!no_daemon) { int pid; FILE *pf; int pfdesc; log_perror = 0; #ifdef __uClinux__ if ((pid = vfork()) < 0) #else if ((pid = fork()) < 0) #endif log_fatal ("can't fork daemon: %m"); else if (pid) exit (0); pfdesc = open (path_dhcrelay_pid, O_CREAT | O_TRUNC | O_WRONLY, 0644); if (pfdesc < 0) { log_error ("Can't create %s: %m", path_dhcrelay_pid); } else { pf = fdopen (pfdesc, "w"); if (!pf) log_error ("Can't fdopen %s: %m", path_dhcrelay_pid); else { fprintf (pf, "%ld\n", (long)getpid ()); fclose (pf); } } close (0); close (1); close (2); pid = setsid (); chdir("/"); } /* Start dispatching packets and timeouts... */ dispatch (); /*NOTREACHED*/ return 0; }
void trace_interface_input (trace_type_t *ttype, unsigned len, char *buf) { trace_interface_packet_t *tipkt; struct interface_info *ip; struct sockaddr_in *sin; struct iaddr addr; isc_result_t status; if (len != sizeof *tipkt) { log_error ("trace interface packet size mismatch: %ld != %d", (long)(sizeof *tipkt), len); return; } tipkt = (trace_interface_packet_t *)buf; ip = (struct interface_info *)0; status = interface_allocate (&ip, MDL); if (status != ISC_R_SUCCESS) { foo: log_error ("trace_interface_input: %s.", isc_result_totext (status)); return; } ip -> ifp = dmalloc (sizeof *(ip -> ifp), MDL); if (!ip -> ifp) { interface_dereference (&ip, MDL); status = ISC_R_NOMEMORY; goto foo; } memcpy (&ip -> hw_address, &tipkt -> hw_address, sizeof ip -> hw_address); /* XXX: Without the full addresses state it's not quite a full * trace. */ ip->address_count = ip->address_max = 1; ip->addresses = dmalloc(sizeof(*ip->addresses), MDL); memcpy(ip->addresses, &tipkt->primary_address, sizeof(*ip->addresses)); memcpy (ip -> name, tipkt -> name, sizeof ip -> name); ip -> index = ntohl (tipkt -> index); interface_snorf (ip, 0); if (dhcp_interface_discovery_hook) (*dhcp_interface_discovery_hook) (ip); /* Fake up an ifp. */ memcpy (ip -> ifp -> ifr_name, ip -> name, sizeof ip -> name); #ifdef HAVE_SA_LEN ip -> ifp -> ifr_addr.sa_len = sizeof (struct sockaddr_in); #endif sin = (struct sockaddr_in *)&ip -> ifp -> ifr_addr; sin->sin_addr = ip->addresses[0]; addr.len = 4; memcpy (addr.iabuf, &sin -> sin_addr.s_addr, addr.len); if (dhcp_interface_setup_hook) (*dhcp_interface_setup_hook) (ip, &addr); interface_stash (ip); if (!quiet_interface_discovery) { log_info ("Listening on Trace/%s/%s%s%s", ip -> name, print_hw_addr (ip -> hw_address.hbuf [0], ip -> hw_address.hlen - 1, &ip -> hw_address.hbuf [1]), (ip -> shared_network ? "/" : ""), (ip -> shared_network ? ip -> shared_network -> name : "")); if (strcmp (ip -> name, "fallback")) { log_info ("Sending on Trace/%s/%s%s%s", ip -> name, print_hw_addr (ip -> hw_address.hbuf [0], ip -> hw_address.hlen - 1, &ip -> hw_address.hbuf [1]), (ip -> shared_network ? "/" : ""), (ip -> shared_network ? ip -> shared_network -> name : "")); } } interface_dereference (&ip, MDL); }