blob: ece8dea30a01c354cf89bce38a510eee500c3c88 [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 *
95get_current_timestamp_micro_v2()
96{
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;
akmhoque60a40f32013-02-12 11:17:49 -0600147 //int status;
akmhoquebfefef22012-09-26 10:09:34 -0500148 struct stat st;
149 int isLogDirExists=0;
150 char *time=getLocalTimeStamp();
151
152 pwdbuffer=(char *)malloc(sizeof(char)*200);
153 memset(pwdbuffer,0,200);
154 logDir=(char *)malloc(sizeof(char)*200);
155 memset(logDir,0,200);
156 logFileName=(char *)malloc(sizeof(char)*200);
157 memset(logFileName,0,200);
158 logExt=(char *)malloc(sizeof(char)*5);
159 memset(logExt,0,5);
160 defaultLogDir=(char *)malloc(sizeof(char)*10);
161 memset(defaultLogDir,0,10);
162
163 memcpy(logExt,".log",4);
164 logExt[4]='\0';
165 memcpy(defaultLogDir,"/nlsrLog",9);
166 defaultLogDir[9]='\0';
167
168 if(loggingDir!=NULL)
169 {
170 if( stat( loggingDir, &st)==0)
171 {
172 if ( st.st_mode & S_IFDIR )
173 {
174 if( st.st_mode & S_IWUSR)
175 {
176 isLogDirExists=1;
177 memcpy(logDir,loggingDir,strlen(loggingDir)+1);
178 }
179 else printf("User do not have write permission to %s \n",loggingDir);
180 }
181 else printf("Provided path for %s is not a directory!!\n",loggingDir);
182 }
183 else printf("Log directory: %s does not exists\n",loggingDir);
184 }
185
186 if(isLogDirExists == 0)
187 {
188 if ((getpwuid_r(getuid(),pwdptr,pwdbuffer,pwdlinelen,&tempPwdPtr))!=0)
189 perror("getpwuid_r() error.");
190 else
191 {
192 memcpy(logDir,pd.pw_dir,strlen(pd.pw_dir)+1);
193 memcpy(logDir+strlen(logDir),defaultLogDir,strlen(defaultLogDir)+1);
194 if(stat(logDir,&st) != 0)
akmhoque60a40f32013-02-12 11:17:49 -0600195 mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH);
196 //printf("Status: %d\n",status);
akmhoquebfefef22012-09-26 10:09:34 -0500197 }
198 }
199 memcpy(logFileName,logDir,strlen(logDir)+1);
200 if( logDir[strlen(logDir)-1]!='/')
201 {
202 memcpy(logFileName+strlen(logFileName),"/",1);
203 memcpy(logFileName+strlen(logFileName),"\0",1);
204 }
205 memcpy(logFileName+strlen(logFileName),time,strlen(time)+1);
206 memcpy(logFileName+strlen(logFileName),logExt,strlen(logExt)+1);
207 ret=(char *)malloc(strlen(logFileName)+1);
208 memset(ret,0,strlen(logFileName)+1);
209 memcpy(ret,logFileName,strlen(logFileName)+1);
210
211 setenv("NLSR_LOG_FILE",ret,1);
212
213 free(time);
214 free(logDir);
215 free(logFileName);
216 free(pwdbuffer);
217 free(logExt);
218 free(defaultLogDir);
219 free(ret);
220}
221
222
223void
224writeLogg(const char *source_file, const char *function, const int line, const char *format, ...)
225{
226 char *file=getenv("NLSR_LOG_FILE");
227 if (file != NULL)
228 {
229 FILE *fp = fopen(file, "a");
230
231 if (fp != NULL)
232 {
233 struct timeval t;
234 gettimeofday(&t, NULL);
akmhoque9e9fc722012-09-26 14:03:25 -0500235 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 -0500236 va_list args;
237 va_start(args, format);
238 vfprintf(fp, format, args);
239 fclose(fp);
240 va_end(args);
241 }
242 }
243}
244
akmhoqueb77b95f2013-02-08 12:28:47 -0600245
246struct sockaddr_in *
247get_ip_from_hostname(char *hostname )
248{
249
250
251 struct addrinfo hints, *servinfo, *p;
252 int res;
253 struct sockaddr_in * ip;
254 memset(&hints, 0, sizeof hints);
255 hints.ai_family = AF_UNSPEC;
256 hints.ai_socktype = SOCK_STREAM;
257
258 if ( (res = getaddrinfo( hostname , "9696", &hints , &servinfo)) != 0)
259 {
260 fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(res));
261 return NULL;
262 }
263 int i=0;
264 for(p = servinfo; p != NULL; p = p->ai_next)
265 {
266 ip = (struct sockaddr_in *) p->ai_addr;
267 i++;
268
269 }
270 freeaddrinfo(servinfo);
271 return ip;
272
273
274}
275
276
277
278int
279get_ip_from_hostname_02(char * hostname , char* ip)
280{
281 struct hostent *he;
282 struct in_addr **addr_list;
283 int i;
284 if ( (he = gethostbyname( hostname ) ) == NULL)
285 {
286 herror("gethostbyname");
287 return 1;
288 }
289 addr_list = (struct in_addr **) he->h_addr_list;
290 for(i = 0; addr_list[i] != NULL; i++)
291 {
292 strcpy(ip , inet_ntoa(*addr_list[i]) );
akmhoque6682ca32013-02-22 00:29:35 -0600293 ip[strlen(ip)]='\0';
akmhoqueb77b95f2013-02-08 12:28:47 -0600294 return 0;
295 }
akmhoque6682ca32013-02-22 00:29:35 -0600296 return -1;
akmhoqueb77b95f2013-02-08 12:28:47 -0600297}
298
299
akmhoqueedb68d92013-03-05 10:18:16 -0600300int
301add_ccn_uri_name(struct ccn_charbuf *res_name, struct ccn_charbuf *add){
302
303 int i, res;
304 struct ccn_indexbuf *idx=ccn_indexbuf_create();
305 res=ccn_name_split(add,idx);
306 if ( res < 0 ){
307 ccn_indexbuf_destroy(&idx);
308 return -1;
309 }
310
311 const unsigned char *comp_ptr1;
312 size_t comp_size;
313 for(i=0;i<idx->n-1;i++){
314 ccn_name_comp_get(add->buf,idx,i,&comp_ptr1, &comp_size);
315 ccn_name_append_str(res_name,(char *)comp_ptr1);
316 }
317 ccn_indexbuf_destroy(&idx);
318
319 return 0;
320
321}
322
akmhoqueb77b95f2013-02-08 12:28:47 -0600323