libdhcp.c

00001 /* libdhcp.c
00002  *
00003  *  libdhcp, a minimal interface to the ISC dhcp IPv4 libdhcp4client library,
00004  *  and to the dhcpv6 DHCPv6 client libdhcp6client library. 
00005  *
00006  * Copyright (C) 2006  Red Hat, Inc. All rights reserved.
00007  *
00008  * This copyrighted material is made available to anyone wishing to use,
00009  * modify, copy, or redistribute it subject to the terms and conditions of
00010  * the GNU General Public License v.2.  This program is distributed in the
00011  * hope that it will be useful, but WITHOUT ANY WARRANTY expressed or
00012  * implied, including the implied warranties of MERCHANTABILITY or FITNESS
00013  * FOR A PARTICULAR PURPOSE. See the GNU General Public License for more
00014  * details.  You should have received a copy of the GNU General Public
00015  * License along with this program; if not, write to the Free Software
00016  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301,
00017  * USA. Any Red Hat trademarks that are incorporated in the source code or
00018  * documentation are not subject to the GNU General Public License and may
00019  * only be used or replicated with the express permission of Red Hat, Inc.
00020  *
00021  * Red Hat Author(s): Jason Vas Dias
00022  *                    David Cantrell
00023  */
00024 #include <sys/types.h>
00025 #include <unistd.h>
00026 #include <time.h>
00027 #include <sys/syslog.h>
00028 #include <libdhcp.h>
00029 #include <stdio.h>
00030 #include <malloc.h>
00031 
00032 LIBDHCP_Control 
00033 *libdhcp_control_new
00034 (   LIBDHCP_Callback       callback,
00035     LIBDHCP_Capability     dhc_cap, 
00036     time_t                 timeout,
00037     void                  *arg,
00038     LIBDHCP_Error_Handler  error_handler,
00039     uint8_t                log_level
00040 )   /* creates new LIBDHCP_Control handle */
00041 {
00042     LIBDHCP_Control *dhc = calloc( 1, sizeof( LIBDHCP_Control ) ); 
00043     if ( dhc == 0L )
00044         return 0L;
00045     dhc->capability = dhc_cap;
00046     dhc->callback = callback;
00047     dhc->timeout = timeout;
00048     dhc->arg = arg;
00049     dhc->eh = error_handler;
00050     dhc->log_level = log_level;
00051     return dhc;
00052 }
00053 
00054 void libdhcp_control_free(LIBDHCP_Control *dhc) {
00055         free(dhc);
00056 }
00057 
00058 extern char **environ;
00059 
00060 int libdhcp_call_client 
00061 (   LIBDHCP_Control *control,
00062     DHCP_Client client,
00063     ...
00064 )
00065 {
00066     va_list ap;
00067     int  argc=0;
00068     char *argv [ 32 ], **argvp=argv, *p;
00069 
00070     va_start(ap, client);
00071         
00072     while( (argc < 32) && ((p = va_arg(ap, char*) ) != 0 ) )
00073     {
00074         *(argvp++)=p;
00075         argc++;
00076     }
00077     *argvp = 0;
00078     va_end(ap);
00079     
00080     return (*client)(control, argc, argv, environ);
00081 }    
00082 
00083 
00084 char *libdhcp_state_string( DHCP_State s, char *buf )
00085 {
00086     static char sbuf[32];
00087     char *p = buf ? &(buf[0]) : &(sbuf[0]);
00088     
00089     sprintf
00090     (   p, "%s",
00091         (s == DHC4_NBI)
00092         ?"DHC4_NBI"
00093         :(s == DHC4_PREINIT)
00094         ?"DHC4_PREINIT"
00095         :(s == DHC4_BOUND)
00096         ?"DHC4_BOUND"
00097         :(s == DHC4_RENEW)
00098         ?"DHC4_RENEW"
00099         :(s == DHC4_REBOOT)
00100         ?"DHC4_REBOOT"
00101         :(s == DHC4_REBIND)
00102         ?"DHC4_REBIND"
00103         :(s == DHC4_STOP)
00104         ?"DHC4_STOP"
00105         :(s == DHC4_MEDIUM)
00106         ?"DHC4_MEDIUM"
00107         :(s == DHC4_TIMEOUT)
00108         ?"DHC4_TIMEOUT"
00109         :(s == DHC4_FAIL)
00110         ?"DHC4_FAIL"
00111         :(s == DHC4_EXPIRE)
00112         ?"DHC4_EXPIRE"
00113         :(s == DHC4_RELEASE)
00114         ?"DHC4_RELEASE"
00115         :(s == DHC_TIMEDOUT)
00116         ?"DHC_TIMEDOUT"
00117         :(s == DHC6_BOUND)
00118         ?"DHC6_BOUND"
00119         :(s == DHC6_REBIND)
00120         ?"DHC6_REBIND"
00121         :(s == DHC6_RELEASE)
00122         ?"DHC6_RELEASE"
00123         :"DHC_INVALID"
00124     );
00125 
00126     return p;
00127 }
00128 
00129 int 
00130 libdhcp_stderr_logger
00131 (   struct libdhcp_control_s *ctl,
00132     int priority,  /* ala syslog(3): LOG_EMERG=0 - LOG_DEBUG=7 (+ LOG_FATAL=8 : finished -> 1)   */
00133     const char *fmt,
00134     va_list ap
00135 )
00136 {
00137     struct tm tm;
00138     int len=0;
00139     if ( (priority != LOG_FATAL) && ctl && (priority > ctl->log_level) )
00140         return 0;
00141     time_t t=time(0);
00142     tm=*localtime(&t);  
00143     fprintf(stderr, "%.4u-%.2u-%.2u_%.2u:%.2u:%.2u libdhcp [%d] %s:",
00144            tm.tm_year + 1900, tm.tm_mon + 1, tm.tm_mday, tm.tm_hour, tm.tm_min, tm.tm_sec,
00145            getpid(),
00146            (priority == LOG_FATAL)
00147            ? "FATAL"
00148            : (priority == LOG_ERR)
00149            ? "ERROR"
00150            : (priority == LOG_INFO)
00151            ? "INFO"
00152            : (priority == LOG_DEBUG)
00153            ? "DBG"
00154            : ""
00155         );
00156     if(priority == LOG_FATAL)
00157         if( ctl )
00158             ctl->finished = 1;
00159     len+=vprintf(fmt,ap);
00160     printf("\n");
00161     fflush(stderr);
00162     return len;
00163 }
00164 
00165 int
00166 libdhcp_syslogger
00167 (   struct libdhcp_control_s *ctl,
00168     int priority,  /* ala syslog(3): LOG_EMERG=0 - LOG_DEBUG=7 (+ LOG_FATAL=8 : finished -> 1)   */
00169     const char *fmt,
00170     va_list ap
00171 )
00172 {
00173     if ( (priority != LOG_FATAL) && ctl && (priority > ctl->log_level) )
00174         return 0;
00175     if(priority == LOG_FATAL)
00176     {
00177         if(ctl)
00178             ctl->finished = 1;  
00179         priority = LOG_ERR;
00180     }
00181     vsyslog( priority, fmt, ap );
00182     return 1;
00183 }

Generated on Mon Aug 14 17:25:56 2006 for libdhcp by  doxygen 1.4.7