00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
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 )
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,
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,
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 }