blob: 51afa86a8c565004ae896d91ee606ac36c557b1f [file] [log] [blame]
akmhoque59980a52012-08-09 12:36:09 -05001#include<stdio.h>
2#include<string.h>
3#include<stdlib.h>
4#include <unistd.h>
5#include <getopt.h>
akmhoquebfefef22012-09-26 10:09:34 -05006#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>
akmhoque59980a52012-08-09 12:36:09 -050012#include <sys/time.h>
akmhoqueb77b95f2013-02-08 12:28:47 -060013#include<sys/socket.h>
14#include<arpa/inet.h>
15#include<errno.h>
16#include<netdb.h>
akmhoque0476dc42012-08-13 09:59:08 -050017#include <time.h>
akmhoque59980a52012-08-09 12:36:09 -050018#include <assert.h>
19#ifdef HAVE_CONFIG_H
20#include <config.h>
21#endif
22
akmhoquebfefef22012-09-26 10:09:34 -050023
akmhoqueb77b95f2013-02-08 12:28:47 -060024
akmhoque53f64222012-09-05 13:57:51 -050025#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
akmhoquebfefef22012-09-26 10:09:34 -050032
akmhoque59980a52012-08-09 12:36:09 -050033#include "utility.h"
34
35
36char * getLocalTimeStamp(void)
37{
akmhoque8876e982013-02-21 13:35:46 -060038 char *timestamp = (char *)malloc(sizeof(char) * 20);
akmhoque59980a52012-08-09 12:36:09 -050039 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
akmhoquec9286692012-08-16 09:57:58 -050050char * getGmTimeStamp(void)
51{
akmhoque8876e982013-02-21 13:35:46 -060052 char *timestamp = (char *)malloc(sizeof(char) * 20);
akmhoquec9286692012-08-16 09:57:58 -050053 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
akmhoque6682ca32013-02-22 00:29:35 -060065int
66get_width_of_number(long int number)
67{
68 int i=0;
69 while(number>0)
70 {
71 i++;
72 number/=10;
73 }
74 return i;
75}
akmhoque59980a52012-08-09 12:36:09 -050076
akmhoquef71d9082012-08-22 12:51:53 -040077long int
78get_current_time_sec(void)
79{
80 struct timeval now;
81 gettimeofday(&now,NULL);
82 return now.tv_sec;
akmhoque59980a52012-08-09 12:36:09 -050083}
akmhoqued79438d2012-08-27 13:31:42 -050084
85
akmhoque03004e62012-09-06 01:12:28 -050086void
87get_current_timestamp_micro(char * microSec)
akmhoque53f64222012-09-05 13:57:51 -050088{
89 struct timeval now;
90 gettimeofday(&now, NULL);
akmhoque60cd6d42012-10-11 04:33:18 -050091 sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec);
akmhoqueffacaa82012-09-13 17:48:30 -050092}
akmhoque53f64222012-09-05 13:57:51 -050093
akmhoque6682ca32013-02-22 00:29:35 -060094char *
akmhoque7adb2772013-03-05 16:30:59 -060095get_current_timestamp_micro_v2(void)
akmhoque6682ca32013-02-22 00:29:35 -060096{
97 struct timeval now;
98 gettimeofday(&now, NULL);
99 //sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec);
100 char *microSec=(char *)calloc(get_width_of_number(now.tv_sec)+7,sizeof(char));
101 sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec);
102 microSec[strlen(microSec)]='\0';
103
104 return microSec;
105}
akmhoqueffacaa82012-09-13 17:48:30 -0500106
107long int
akmhoqueda5b6832012-09-13 22:33:55 -0500108get_time_diff(const char *time1, const char *time2)
akmhoqueffacaa82012-09-13 17:48:30 -0500109{
110 long int diff_secs;
111
112 long int time1_in_sec, time2_in_sec;
113
114 char *time1_sec=(char *)malloc(strlen(time1)-6+1);
115 memset(time1_sec,0,strlen(time1)-6+1);
116 memcpy(time1_sec,time1,strlen(time1)-6);
117
118 char *time2_sec=(char *)malloc(strlen(time2)-6+1);
119 memset(time2_sec,0,strlen(time2)-6+1);
120 memcpy(time2_sec,time2,strlen(time2)-6);
121
122 time1_in_sec=strtol(time1_sec,NULL,10);
123 time2_in_sec=strtol(time2_sec,NULL,10);
124
125 diff_secs=time1_in_sec-time2_in_sec;
126
127 free(time1_sec);
128 free(time2_sec);
129
130 return diff_secs;
akmhoque53f64222012-09-05 13:57:51 -0500131}
132
akmhoquebfefef22012-09-26 10:09:34 -0500133
134void
135startLogging(char *loggingDir)
136{
137 struct passwd pd;
138 struct passwd* pwdptr=&pd;
139 struct passwd* tempPwdPtr;
140 char *pwdbuffer;
141 int pwdlinelen = 200;
142 char *logDir;
143 char *logFileName;
144 char *ret;
145 char *logExt;
146 char *defaultLogDir;
akmhoquebfefef22012-09-26 10:09:34 -0500147 struct stat st;
148 int isLogDirExists=0;
149 char *time=getLocalTimeStamp();
150
akmhoque7adb2772013-03-05 16:30:59 -0600151 pwdbuffer=(char *)calloc(200,sizeof(char));
152 logDir=(char *)calloc(200,sizeof(char));
153 logFileName=(char *)calloc(200,sizeof(char));
154 logExt=(char *)calloc(5,sizeof(char));
155 defaultLogDir=(char *)calloc(10,sizeof(char));
akmhoquebfefef22012-09-26 10:09:34 -0500156
157 memcpy(logExt,".log",4);
158 logExt[4]='\0';
159 memcpy(defaultLogDir,"/nlsrLog",9);
160 defaultLogDir[9]='\0';
161
162 if(loggingDir!=NULL)
163 {
164 if( stat( loggingDir, &st)==0)
165 {
166 if ( st.st_mode & S_IFDIR )
167 {
168 if( st.st_mode & S_IWUSR)
169 {
170 isLogDirExists=1;
171 memcpy(logDir,loggingDir,strlen(loggingDir)+1);
172 }
173 else printf("User do not have write permission to %s \n",loggingDir);
174 }
175 else printf("Provided path for %s is not a directory!!\n",loggingDir);
176 }
177 else printf("Log directory: %s does not exists\n",loggingDir);
178 }
179
180 if(isLogDirExists == 0)
181 {
182 if ((getpwuid_r(getuid(),pwdptr,pwdbuffer,pwdlinelen,&tempPwdPtr))!=0)
183 perror("getpwuid_r() error.");
184 else
185 {
186 memcpy(logDir,pd.pw_dir,strlen(pd.pw_dir)+1);
187 memcpy(logDir+strlen(logDir),defaultLogDir,strlen(defaultLogDir)+1);
188 if(stat(logDir,&st) != 0)
akmhoque7adb2772013-03-05 16:30:59 -0600189 mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
akmhoquebfefef22012-09-26 10:09:34 -0500190 }
191 }
192 memcpy(logFileName,logDir,strlen(logDir)+1);
193 if( logDir[strlen(logDir)-1]!='/')
194 {
195 memcpy(logFileName+strlen(logFileName),"/",1);
196 memcpy(logFileName+strlen(logFileName),"\0",1);
197 }
198 memcpy(logFileName+strlen(logFileName),time,strlen(time)+1);
199 memcpy(logFileName+strlen(logFileName),logExt,strlen(logExt)+1);
200 ret=(char *)malloc(strlen(logFileName)+1);
201 memset(ret,0,strlen(logFileName)+1);
202 memcpy(ret,logFileName,strlen(logFileName)+1);
203
204 setenv("NLSR_LOG_FILE",ret,1);
205
206 free(time);
207 free(logDir);
208 free(logFileName);
209 free(pwdbuffer);
210 free(logExt);
211 free(defaultLogDir);
212 free(ret);
213}
214
215
akmhoque7adb2772013-03-05 16:30:59 -0600216char *
217get_current_user_home(void){
218
219 const char *homeDir = getenv("HOME");
220
221 if (!homeDir ) {
222 struct passwd* pwd = getpwuid(getuid());
223 if (pwd)
224 homeDir = pwd->pw_dir;
225 }
226
227 char *home=(char *)calloc(strlen(homeDir)+1,sizeof(char));
228 memcpy(home,homeDir,strlen(homeDir)+1);
229
230 return home;
231}
232
233
234char *
235get_current_user_default_keystore(void){
236
237 char *home=get_current_user_home();
238 char *def_keystore=(char *)calloc(strlen(home)+strlen("/.ccnx") +
239 strlen("/.ccnx_keystore")+1,sizeof(char));
240 memcpy(def_keystore,home,strlen(home));
241 memcpy(def_keystore+strlen(def_keystore),"/.ccnx/.ccnx_keystore",
242 strlen("/.ccnx/.ccnx_keystore"));
243 def_keystore[strlen(def_keystore)]='\0';
244 free(home);
245
246 return def_keystore;
247}
248
akmhoquebfefef22012-09-26 10:09:34 -0500249void
250writeLogg(const char *source_file, const char *function, const int line, const char *format, ...)
251{
252 char *file=getenv("NLSR_LOG_FILE");
253 if (file != NULL)
254 {
255 FILE *fp = fopen(file, "a");
256
257 if (fp != NULL)
258 {
259 struct timeval t;
260 gettimeofday(&t, NULL);
akmhoque9e9fc722012-09-26 14:03:25 -0500261 fprintf(fp,"%ld.%06u - %s, %s, %d :",(long)t.tv_sec , (unsigned)t.tv_usec , source_file , function , line);
akmhoquebfefef22012-09-26 10:09:34 -0500262 va_list args;
263 va_start(args, format);
264 vfprintf(fp, format, args);
265 fclose(fp);
266 va_end(args);
267 }
268 }
269}
270
akmhoqueb77b95f2013-02-08 12:28:47 -0600271
272struct sockaddr_in *
273get_ip_from_hostname(char *hostname )
274{
275
276
277 struct addrinfo hints, *servinfo, *p;
278 int res;
279 struct sockaddr_in * ip;
280 memset(&hints, 0, sizeof hints);
281 hints.ai_family = AF_UNSPEC;
282 hints.ai_socktype = SOCK_STREAM;
283
284 if ( (res = getaddrinfo( hostname , "9696", &hints , &servinfo)) != 0)
285 {
286 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
287 return NULL;
288 }
289 int i=0;
290 for(p = servinfo; p != NULL; p = p->ai_next)
291 {
292 ip = (struct sockaddr_in *) p->ai_addr;
293 i++;
294
295 }
296 freeaddrinfo(servinfo);
297 return ip;
298
299
300}
301
302
303
304int
305get_ip_from_hostname_02(char * hostname , char* ip)
306{
307 struct hostent *he;
308 struct in_addr **addr_list;
309 int i;
310 if ( (he = gethostbyname( hostname ) ) == NULL)
311 {
312 herror("gethostbyname");
313 return 1;
314 }
315 addr_list = (struct in_addr **) he->h_addr_list;
316 for(i = 0; addr_list[i] != NULL; i++)
317 {
318 strcpy(ip , inet_ntoa(*addr_list[i]) );
akmhoque6682ca32013-02-22 00:29:35 -0600319 ip[strlen(ip)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -0600320 return 0;
321 }
akmhoque6682ca32013-02-22 00:29:35 -0600322 return -1;
akmhoqueb77b95f2013-02-08 12:28:47 -0600323}
324
325
akmhoqueedb68d92013-03-05 10:18:16 -0600326int
327add_ccn_uri_name(struct ccn_charbuf *res_name, struct ccn_charbuf *add){
328
329 int i, res;
330 struct ccn_indexbuf *idx=ccn_indexbuf_create();
331 res=ccn_name_split(add,idx);
332 if ( res < 0 ){
333 ccn_indexbuf_destroy(&idx);
334 return -1;
335 }
336
337 const unsigned char *comp_ptr1;
338 size_t comp_size;
339 for(i=0;i<idx->n-1;i++){
340 ccn_name_comp_get(add->buf,idx,i,&comp_ptr1, &comp_size);
341 ccn_name_append_str(res_name,(char *)comp_ptr1);
342 }
343 ccn_indexbuf_destroy(&idx);
344
345 return 0;
346
347}
348
akmhoqueb77b95f2013-02-08 12:28:47 -0600349