blob: f04ba395e9f86971576402570313267e30de632e [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{
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
akmhoquec9286692012-08-16 09:57:58 -050050char * 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
akmhoque59980a52012-08-09 12:36:09 -050065
akmhoque59980a52012-08-09 12:36:09 -050066
akmhoquef71d9082012-08-22 12:51:53 -040067long int
68get_current_time_sec(void)
69{
70 struct timeval now;
71 gettimeofday(&now,NULL);
72 return now.tv_sec;
akmhoque59980a52012-08-09 12:36:09 -050073}
akmhoqued79438d2012-08-27 13:31:42 -050074
75
akmhoque03004e62012-09-06 01:12:28 -050076void
77get_current_timestamp_micro(char * microSec)
akmhoque53f64222012-09-05 13:57:51 -050078{
79 struct timeval now;
80 gettimeofday(&now, NULL);
akmhoque60cd6d42012-10-11 04:33:18 -050081 sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec);
akmhoqueffacaa82012-09-13 17:48:30 -050082}
akmhoque53f64222012-09-05 13:57:51 -050083
akmhoqueffacaa82012-09-13 17:48:30 -050084
85long int
akmhoqueda5b6832012-09-13 22:33:55 -050086get_time_diff(const char *time1, const char *time2)
akmhoqueffacaa82012-09-13 17:48:30 -050087{
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;
akmhoque53f64222012-09-05 13:57:51 -0500109}
110
akmhoquebfefef22012-09-26 10:09:34 -0500111
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;
akmhoque60a40f32013-02-12 11:17:49 -0600125 //int status;
akmhoquebfefef22012-09-26 10:09:34 -0500126 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)
akmhoque60a40f32013-02-12 11:17:49 -0600173 mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
174 //printf("Status: %d\n",status);
akmhoquebfefef22012-09-26 10:09:34 -0500175 }
176 }
177 memcpy(logFileName,logDir,strlen(logDir)+1);
178 if( logDir[strlen(logDir)-1]!='/')
179 {
180 memcpy(logFileName+strlen(logFileName),"/",1);
181 memcpy(logFileName+strlen(logFileName),"\0",1);
182 }
183 memcpy(logFileName+strlen(logFileName),time,strlen(time)+1);
184 memcpy(logFileName+strlen(logFileName),logExt,strlen(logExt)+1);
185 ret=(char *)malloc(strlen(logFileName)+1);
186 memset(ret,0,strlen(logFileName)+1);
187 memcpy(ret,logFileName,strlen(logFileName)+1);
188
189 setenv("NLSR_LOG_FILE",ret,1);
190
191 free(time);
192 free(logDir);
193 free(logFileName);
194 free(pwdbuffer);
195 free(logExt);
196 free(defaultLogDir);
197 free(ret);
198}
199
200
201void
202writeLogg(const char *source_file, const char *function, const int line, const char *format, ...)
203{
204 char *file=getenv("NLSR_LOG_FILE");
205 if (file != NULL)
206 {
207 FILE *fp = fopen(file, "a");
208
209 if (fp != NULL)
210 {
211 struct timeval t;
212 gettimeofday(&t, NULL);
akmhoque9e9fc722012-09-26 14:03:25 -0500213 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 -0500214 va_list args;
215 va_start(args, format);
216 vfprintf(fp, format, args);
217 fclose(fp);
218 va_end(args);
219 }
220 }
221}
222
akmhoqueb77b95f2013-02-08 12:28:47 -0600223
224struct sockaddr_in *
225get_ip_from_hostname(char *hostname )
226{
227
228
229 struct addrinfo hints, *servinfo, *p;
230 int res;
231 struct sockaddr_in * ip;
232 memset(&hints, 0, sizeof hints);
233 hints.ai_family = AF_UNSPEC;
234 hints.ai_socktype = SOCK_STREAM;
235
236 if ( (res = getaddrinfo( hostname , "9696", &hints , &servinfo)) != 0)
237 {
238 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
239 return NULL;
240 }
241 int i=0;
242 for(p = servinfo; p != NULL; p = p->ai_next)
243 {
244 ip = (struct sockaddr_in *) p->ai_addr;
245 i++;
246
247 }
248 freeaddrinfo(servinfo);
249 return ip;
250
251
252}
253
254
255
256int
257get_ip_from_hostname_02(char * hostname , char* ip)
258{
259 struct hostent *he;
260 struct in_addr **addr_list;
261 int i;
262 if ( (he = gethostbyname( hostname ) ) == NULL)
263 {
264 herror("gethostbyname");
265 return 1;
266 }
267 addr_list = (struct in_addr **) he->h_addr_list;
268 for(i = 0; addr_list[i] != NULL; i++)
269 {
270 strcpy(ip , inet_ntoa(*addr_list[i]) );
271 return 0;
272 }
273 return 1;
274}
275
276
277