From d35d88d2bb1a0c08599fddd19c14bf592f8c749f Mon Sep 17 00:00:00 2001 From: Richard Fuchs Date: Tue, 14 Jan 2014 14:37:32 -0500 Subject: [PATCH] create critical() for code prettification --- dhtest.c | 43 ++++++---------------- functions.c | 101 +++++++++++++++++++++++----------------------------- headers.h | 3 ++ 3 files changed, 58 insertions(+), 89 deletions(-) diff --git a/dhtest.c b/dhtest.c index d9bebff..010b6c0 100644 --- a/dhtest.c +++ b/dhtest.c @@ -374,13 +374,8 @@ int main(int argc, char *argv[]) exit(2); } /* Opens the PF_PACKET socket */ - if(open_socket() < 0) { - if (nagios_flag) - printf("CRITICAL: Socket error."); - else - fprintf(stderr, "Socket error\n"); - exit(2); - } + if(open_socket() < 0) + critical("Socket error: %m"); /* Sets the promiscuous mode */ set_promisc(); @@ -398,20 +393,10 @@ int main(int argc, char *argv[]) * and unlinks it from the system */ if(dhcp_release_flag) { - if(get_dhinfo() == ERR_FILE_OPEN) { - if (nagios_flag) - printf("CRITICAL: Error on opening DHCP info file."); - else - fprintf(stderr, "Error on opening DHCP info file\n"); - exit(2); - } - if (!server_id) { - if (nagios_flag) - printf("CRITICAL: Can't release IP without an active lease"); - else - fprintf(stderr, "Can't release IP without an active lease\n"); - exit(2); - } + if(get_dhinfo() == ERR_FILE_OPEN) + critical("Error on opening DHCP info file: %m"); + if (!server_id) + critical("Can't release IP without an active lease"); build_option53(DHCP_MSGRELEASE); /* Option53 DHCP release */ if(hostname_flag) { build_option12_hostname(); @@ -463,11 +448,8 @@ int main(int argc, char *argv[]) if(timeout) { time_now = time(NULL); - if((time_now - time_last) > timeout) { - if (nagios_flag) - printf("CRITICAL: Timeout reached: DISCOVER."); - exit(2); - } + if((time_now - time_last) > timeout) + critical("Timeout reached: DISCOVER"); } if(dhcp_offer_state == DHCP_OFFR_RCVD) { if (!nagios_flag && !quiet) @@ -508,13 +490,8 @@ int main(int argc, char *argv[]) if(timeout) { time_now = time(NULL); - if((time_now - time_last) > timeout) { - if (nagios_flag) - printf("CRITICAL: Timeout reached: REQUEST."); - else - fprintf(stderr, "Timeout reached. Exiting\n"); - exit(1); - } + if((time_now - time_last) > timeout) + critical("Timeout reached: REQUEST"); } if(dhcp_ack_state == DHCP_ACK_RCVD) { diff --git a/functions.c b/functions.c index bf01c63..2d21c88 100644 --- a/functions.c +++ b/functions.c @@ -13,6 +13,7 @@ #include #include #include +#include #include "headers.h" @@ -56,10 +57,8 @@ static int map_all_layer_ptr(int pkt_type); int open_socket() { sock_packet = socket(PF_PACKET, SOCK_RAW, htons(ETH_P_ALL)); - if(sock_packet < 0) { - perror("--Error on creating the socket--"); + if(sock_packet < 0) return SOCKET_ERR; - } /* Set link layer parameters */ ll.sll_family = AF_PACKET; ll.sll_protocol = htons(ETH_P_ALL); @@ -107,11 +106,7 @@ static int set_clear_promisc(int op) return 0; error: - if (nagios_flag) - printf("CRITICAL: Error setting promisc."); - else - perror("Error on setting promisc"); - exit(2); + critical("Error on setting promisc: %m"); } int set_promisc() @@ -136,13 +131,9 @@ u_int32_t get_interface_address() ifr.ifr_addr.sa_family = AF_INET; status = ioctl(sock_packet, SIOCGIFADDR, &ifr); - if(status < 0) { - if (nagios_flag) - printf("CRITICAL: Error getting interface address."); - else - perror("Error getting interface address."); - exit(2); - } + if(status < 0) + critical("Error getting interface address: %m"); + return ((struct sockaddr_in *) &ifr.ifr_addr)->sin_addr.s_addr; } @@ -187,30 +178,23 @@ int send_packet(int pkt_type) abort(); } - if(ret < 0) { - if (nagios_flag) - printf("CRITICAL: Packet send failure."); - else - perror("Packet send failure"); - close(sock_packet); - exit(2); - return PACK_SEND_ERR; - } else { - if(pkt_type == DHCP_MSGDISCOVER) { - if (!nagios_flag && !quiet) { - printf("DHCP discover sent\t - "); - printf("Client MAC : " ETH_F_FMT "\n", ETH_F_ARG(dhmac)); - } - } else if (pkt_type == DHCP_MSGREQUEST) { - if (!nagios_flag && !quiet) { - printf("DHCP request sent\t - "); - printf("Client MAC : " ETH_F_FMT "\n", ETH_F_ARG(dhmac)); - } - } else if (pkt_type == DHCP_MSGRELEASE) { - if (!nagios_flag && !quiet) { - printf("DHCP release sent\t - "); - printf("Client MAC : " ETH_F_FMT "\n", ETH_F_ARG(dhmac)); - } + if(ret < 0) + critical("Packet send failure: %m"); + + if(pkt_type == DHCP_MSGDISCOVER) { + if (!nagios_flag && !quiet) { + printf("DHCP discover sent\t - "); + printf("Client MAC : " ETH_F_FMT "\n", ETH_F_ARG(dhmac)); + } + } else if (pkt_type == DHCP_MSGREQUEST) { + if (!nagios_flag && !quiet) { + printf("DHCP request sent\t - "); + printf("Client MAC : " ETH_F_FMT "\n", ETH_F_ARG(dhmac)); + } + } else if (pkt_type == DHCP_MSGRELEASE) { + if (!nagios_flag && !quiet) { + printf("DHCP release sent\t - "); + printf("Client MAC : " ETH_F_FMT "\n", ETH_F_ARG(dhmac)); } } return 0; @@ -932,13 +916,8 @@ int log_dhinfo() struct dhcp_status ds; dh_file = open(dhmac_fname, O_WRONLY | O_CREAT | O_TRUNC, 0666); - if (dh_file < 0) { - if (nagios_flag) - printf("CRITICAL: Error opening file."); - else - perror("Error opening file."); - exit(2); - } + if (dh_file < 0) + critical("Error opening file: %m"); memset(&ds, 0, sizeof(ds)); @@ -956,13 +935,9 @@ int log_dhinfo() if (ip_listen_flag) ds.listen_pid = getpid(); - if (write(dh_file, &ds, sizeof(ds)) != sizeof(ds)) { - if (nagios_flag) - printf("CRITICAL: Error writing to file."); - else - perror("Error writing to file."); - exit(2); - } + if (write(dh_file, &ds, sizeof(ds)) != sizeof(ds)) + critical("Error writing to file: %m"); + close(dh_file); return 0; @@ -978,11 +953,10 @@ int get_dhinfo() struct dhcp_status ds; dh_file = open(dhmac_fname, O_RDONLY); - - if (read(dh_file, &ds, sizeof(ds)) != sizeof(ds)) + if(dh_file < 0) return ERR_FILE_OPEN; - if(dh_file < 0) + if (read(dh_file, &ds, sizeof(ds)) != sizeof(ds)) return ERR_FILE_OPEN; memcpy(dhmac, ds.client_mac, sizeof(dhmac)); @@ -1006,3 +980,18 @@ char *get_ip_str(u_int32_t ip) ip_str, sizeof(ip_str)); return ip_str; } + +void critical (const char *fmt, ...) { + va_list ap; + + va_start(ap, fmt); + if (nagios_flag) { + printf("CRITICAL: "); + vprintf(fmt, ap); + } + else { + vfprintf(stderr, fmt, ap); + fprintf(stderr, "\n"); + } + exit(2); +} diff --git a/headers.h b/headers.h index 5e53c21..6f2b848 100644 --- a/headers.h +++ b/headers.h @@ -46,6 +46,9 @@ char *get_ip_str(u_int32_t ip); /* Convert in_addr to string */ u_int32_t get_interface_address(void); /* Return the IP address of the interface. */ int set_serv_id_opt50(void); /* Sets the server_ip and option50 ip */ + +void critical(const char *fmt, ...) __attribute__ ((noreturn, format (printf, 1, 2))); + /* * Libnet defines header sizes for every builder function exported. */