blob: cf7452aa991af6baa9d519b5e18a6eaaecd0087e [file] [log] [blame]
akmhoque8fdd6412012-12-04 15:05:33 -06001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
6#include<ctype.h>
7#include<stdarg.h>
8#include <sys/types.h>
9#include <pwd.h>
10#include <errno.h>
11#include <sys/stat.h>
12#include <sys/time.h>
akmhoque09c0afa2012-12-14 09:27:00 -060013#include<sys/socket.h>
14#include<arpa/inet.h>
15#include<errno.h>
16#include<netdb.h>
akmhoque8fdd6412012-12-04 15:05:33 -060017#include <time.h>
18#include <assert.h>
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
23
akmhoque09c0afa2012-12-14 09:27:00 -060024
akmhoque8fdd6412012-12-04 15:05:33 -060025#include <ccn/ccn.h>
26#include <ccn/uri.h>
27#include <ccn/keystore.h>
28#include <ccn/signing.h>
29#include <ccn/schedule.h>
30#include <ccn/hashtb.h>
31
32
33#include "utility.h"
34
35
36char * getLocalTimeStamp(void)
37{
38 char *timestamp = (char *)malloc(sizeof(char) * 16);
39 time_t ltime;
40 ltime=time(NULL);
41 struct tm *tm;
42 tm=localtime(&ltime);
43
44 sprintf(timestamp, "%04d%02d%02d%02d%02d%02d", tm->tm_year+1900, tm->tm_mon+1,
45 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
46
47 return timestamp;
48}
49
50char * getGmTimeStamp(void)
51{
52 char *timestamp = (char *)malloc(sizeof(char) * 16);
53 time_t gtime;
54 gtime=time(NULL);
55 struct tm *tm;
56 tm=gmtime(&gtime);
57
58 sprintf(timestamp, "%04d%02d%02d%02d%02d%02d", tm->tm_year+1900, tm->tm_mon+1,
59 tm->tm_mday, tm->tm_hour, tm->tm_min, tm->tm_sec);
60
61 return timestamp;
62}
63
64
65
66
67long int
68get_current_time_sec(void)
69{
70 struct timeval now;
71 gettimeofday(&now,NULL);
72 return now.tv_sec;
73}
74
75
76void
77get_current_timestamp_micro(char * microSec)
78{
79 struct timeval now;
80 gettimeofday(&now, NULL);
81 sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec);
82}
83
84
85long int
86get_time_diff(const char *time1, const char *time2)
87{
88 long int diff_secs;
89
90 long int time1_in_sec, time2_in_sec;
91
92 char *time1_sec=(char *)malloc(strlen(time1)-6+1);
93 memset(time1_sec,0,strlen(time1)-6+1);
94 memcpy(time1_sec,time1,strlen(time1)-6);
95
96 char *time2_sec=(char *)malloc(strlen(time2)-6+1);
97 memset(time2_sec,0,strlen(time2)-6+1);
98 memcpy(time2_sec,time2,strlen(time2)-6);
99
100 time1_in_sec=strtol(time1_sec,NULL,10);
101 time2_in_sec=strtol(time2_sec,NULL,10);
102
103 diff_secs=time1_in_sec-time2_in_sec;
104
105 free(time1_sec);
106 free(time2_sec);
107
108 return diff_secs;
109}
110
111
112void
113startLogging(char *loggingDir)
114{
115 struct passwd pd;
116 struct passwd* pwdptr=&pd;
117 struct passwd* tempPwdPtr;
118 char *pwdbuffer;
119 int pwdlinelen = 200;
120 char *logDir;
121 char *logFileName;
122 char *ret;
123 char *logExt;
124 char *defaultLogDir;
125 int status;
126 struct stat st;
127 int isLogDirExists=0;
128 char *time=getLocalTimeStamp();
129
130 pwdbuffer=(char *)malloc(sizeof(char)*200);
131 memset(pwdbuffer,0,200);
132 logDir=(char *)malloc(sizeof(char)*200);
133 memset(logDir,0,200);
134 logFileName=(char *)malloc(sizeof(char)*200);
135 memset(logFileName,0,200);
136 logExt=(char *)malloc(sizeof(char)*5);
137 memset(logExt,0,5);
138 defaultLogDir=(char *)malloc(sizeof(char)*10);
139 memset(defaultLogDir,0,10);
140
141 memcpy(logExt,".log",4);
142 logExt[4]='\0';
143 memcpy(defaultLogDir,"/nlsrLog",9);
144 defaultLogDir[9]='\0';
145
146 if(loggingDir!=NULL)
147 {
148 if( stat( loggingDir, &st)==0)
149 {
150 if ( st.st_mode & S_IFDIR )
151 {
152 if( st.st_mode & S_IWUSR)
153 {
154 isLogDirExists=1;
155 memcpy(logDir,loggingDir,strlen(loggingDir)+1);
156 }
157 else printf("User do not have write permission to %s \n",loggingDir);
158 }
159 else printf("Provided path for %s is not a directory!!\n",loggingDir);
160 }
161 else printf("Log directory: %s does not exists\n",loggingDir);
162 }
163
164 if(isLogDirExists == 0)
165 {
166 if ((getpwuid_r(getuid(),pwdptr,pwdbuffer,pwdlinelen,&tempPwdPtr))!=0)
167 perror("getpwuid_r() error.");
168 else
169 {
170 memcpy(logDir,pd.pw_dir,strlen(pd.pw_dir)+1);
171 memcpy(logDir+strlen(logDir),defaultLogDir,strlen(defaultLogDir)+1);
172 if(stat(logDir,&st) != 0)
173 status = mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
174 }
175 }
176 memcpy(logFileName,logDir,strlen(logDir)+1);
177 if( logDir[strlen(logDir)-1]!='/')
178 {
179 memcpy(logFileName+strlen(logFileName),"/",1);
180 memcpy(logFileName+strlen(logFileName),"\0",1);
181 }
182 memcpy(logFileName+strlen(logFileName),time,strlen(time)+1);
183 memcpy(logFileName+strlen(logFileName),logExt,strlen(logExt)+1);
184 ret=(char *)malloc(strlen(logFileName)+1);
185 memset(ret,0,strlen(logFileName)+1);
186 memcpy(ret,logFileName,strlen(logFileName)+1);
187
188 setenv("NLSR_LOG_FILE",ret,1);
189
190 free(time);
191 free(logDir);
192 free(logFileName);
193 free(pwdbuffer);
194 free(logExt);
195 free(defaultLogDir);
196 free(ret);
197}
198
199
200void
201writeLogg(const char *source_file, const char *function, const int line, const char *format, ...)
202{
203 char *file=getenv("NLSR_LOG_FILE");
204 if (file != NULL)
205 {
206 FILE *fp = fopen(file, "a");
207
208 if (fp != NULL)
209 {
210 struct timeval t;
211 gettimeofday(&t, NULL);
212 fprintf(fp,"%ld.%06u - %s, %s, %d :",(long)t.tv_sec , (unsigned)t.tv_usec , source_file , function , line);
213 va_list args;
214 va_start(args, format);
215 vfprintf(fp, format, args);
216 fclose(fp);
217 va_end(args);
218 }
219 }
220}
221
akmhoque09c0afa2012-12-14 09:27:00 -0600222
223struct sockaddr_in *
224get_ip_from_hostname(char *hostname )
225{
226
227
228 struct addrinfo hints, *servinfo, *p;
229 int res;
230 struct sockaddr_in * ip;
231 memset(&hints, 0, sizeof hints);
232 hints.ai_family = AF_UNSPEC;
233 hints.ai_socktype = SOCK_STREAM;
234
akmhoquea37b52c2012-12-14 11:16:36 -0600235 if ( (res = getaddrinfo( hostname , "9696", &hints , &servinfo)) != 0)
akmhoque09c0afa2012-12-14 09:27:00 -0600236 {
237 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
238 return NULL;
239 }
akmhoquea37b52c2012-12-14 11:16:36 -0600240 int i=0;
akmhoque09c0afa2012-12-14 09:27:00 -0600241 for(p = servinfo; p != NULL; p = p->ai_next)
242 {
243 ip = (struct sockaddr_in *) p->ai_addr;
akmhoquea37b52c2012-12-14 11:16:36 -0600244 i++;
akmhoque09c0afa2012-12-14 09:27:00 -0600245
246 }
247 freeaddrinfo(servinfo);
248 return ip;
249
250
251}
252
253
akmhoquea37b52c2012-12-14 11:16:36 -0600254
255int
256get_ip_from_hostname_02(char * hostname , char* ip)
257{
258 struct hostent *he;
259 struct in_addr **addr_list;
260 int i;
261 if ( (he = gethostbyname( hostname ) ) == NULL)
262 {
263 herror("gethostbyname");
264 return 1;
265 }
266 addr_list = (struct in_addr **) he->h_addr_list;
267 for(i = 0; addr_list[i] != NULL; i++)
268 {
269 strcpy(ip , inet_ntoa(*addr_list[i]) );
270 return 0;
271 }
272 return 1;
273}
274
275
276