akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 1 | #include<stdio.h> |
| 2 | #include<string.h> |
| 3 | #include<stdlib.h> |
| 4 | #include <unistd.h> |
| 5 | #include <getopt.h> |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 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> |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 12 | #include <sys/time.h> |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 13 | #include<sys/socket.h> |
| 14 | #include<arpa/inet.h> |
| 15 | #include<errno.h> |
| 16 | #include<netdb.h> |
akmhoque | 0476dc4 | 2012-08-13 09:59:08 -0500 | [diff] [blame] | 17 | #include <time.h> |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 18 | #include <assert.h> |
| 19 | #ifdef HAVE_CONFIG_H |
| 20 | #include <config.h> |
| 21 | #endif |
| 22 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 23 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 24 | |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 25 | #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 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 32 | |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 33 | #include "utility.h" |
| 34 | |
| 35 | |
| 36 | char * getLocalTimeStamp(void) |
| 37 | { |
akmhoque | 8876e98 | 2013-02-21 13:35:46 -0600 | [diff] [blame] | 38 | char *timestamp = (char *)malloc(sizeof(char) * 20); |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 39 | time_t ltime; |
| 40 | ltime=time(NULL); |
| 41 | struct tm *tm; |
| 42 | tm=localtime(<ime); |
| 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 | |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 50 | char * getGmTimeStamp(void) |
| 51 | { |
akmhoque | 8876e98 | 2013-02-21 13:35:46 -0600 | [diff] [blame] | 52 | char *timestamp = (char *)malloc(sizeof(char) * 20); |
akmhoque | c928669 | 2012-08-16 09:57:58 -0500 | [diff] [blame] | 53 | time_t gtime; |
| 54 | gtime=time(NULL); |
| 55 | struct tm *tm; |
| 56 | tm=gmtime(>ime); |
| 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 | |
akmhoque | 6682ca3 | 2013-02-22 00:29:35 -0600 | [diff] [blame] | 65 | int |
| 66 | get_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 | } |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 76 | |
akmhoque | f71d908 | 2012-08-22 12:51:53 -0400 | [diff] [blame] | 77 | long int |
| 78 | get_current_time_sec(void) |
| 79 | { |
| 80 | struct timeval now; |
| 81 | gettimeofday(&now,NULL); |
| 82 | return now.tv_sec; |
akmhoque | 59980a5 | 2012-08-09 12:36:09 -0500 | [diff] [blame] | 83 | } |
akmhoque | d79438d | 2012-08-27 13:31:42 -0500 | [diff] [blame] | 84 | |
| 85 | |
akmhoque | 03004e6 | 2012-09-06 01:12:28 -0500 | [diff] [blame] | 86 | void |
| 87 | get_current_timestamp_micro(char * microSec) |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 88 | { |
| 89 | struct timeval now; |
| 90 | gettimeofday(&now, NULL); |
akmhoque | 60cd6d4 | 2012-10-11 04:33:18 -0500 | [diff] [blame] | 91 | sprintf(microSec,"%ld%06ld",now.tv_sec,(long int)now.tv_usec); |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 92 | } |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 93 | |
akmhoque | 6682ca3 | 2013-02-22 00:29:35 -0600 | [diff] [blame] | 94 | char * |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame^] | 95 | get_current_timestamp_micro_v2(void) |
akmhoque | 6682ca3 | 2013-02-22 00:29:35 -0600 | [diff] [blame] | 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 | } |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 106 | |
| 107 | long int |
akmhoque | da5b683 | 2012-09-13 22:33:55 -0500 | [diff] [blame] | 108 | get_time_diff(const char *time1, const char *time2) |
akmhoque | ffacaa8 | 2012-09-13 17:48:30 -0500 | [diff] [blame] | 109 | { |
| 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; |
akmhoque | 53f6422 | 2012-09-05 13:57:51 -0500 | [diff] [blame] | 131 | } |
| 132 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 133 | |
| 134 | void |
| 135 | startLogging(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; |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 147 | struct stat st; |
| 148 | int isLogDirExists=0; |
| 149 | char *time=getLocalTimeStamp(); |
| 150 | |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame^] | 151 | 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)); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 156 | |
| 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) |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame^] | 189 | mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 190 | } |
| 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 | |
akmhoque | 7adb277 | 2013-03-05 16:30:59 -0600 | [diff] [blame^] | 216 | char * |
| 217 | get_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 | |
| 234 | char * |
| 235 | get_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 | |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 249 | void |
| 250 | writeLogg(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); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 261 | fprintf(fp,"%ld.%06u - %s, %s, %d :",(long)t.tv_sec , (unsigned)t.tv_usec , source_file , function , line); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 262 | va_list args; |
| 263 | va_start(args, format); |
| 264 | vfprintf(fp, format, args); |
| 265 | fclose(fp); |
| 266 | va_end(args); |
| 267 | } |
| 268 | } |
| 269 | } |
| 270 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 271 | |
| 272 | struct sockaddr_in * |
| 273 | get_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 | |
| 304 | int |
| 305 | get_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]) ); |
akmhoque | 6682ca3 | 2013-02-22 00:29:35 -0600 | [diff] [blame] | 319 | ip[strlen(ip)]='\0'; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 320 | return 0; |
| 321 | } |
akmhoque | 6682ca3 | 2013-02-22 00:29:35 -0600 | [diff] [blame] | 322 | return -1; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 323 | } |
| 324 | |
| 325 | |
akmhoque | edb68d9 | 2013-03-05 10:18:16 -0600 | [diff] [blame] | 326 | int |
| 327 | add_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 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 349 | |