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 * |
| 95 | get_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 | } |
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 | 60a40f3 | 2013-02-12 11:17:49 -0600 | [diff] [blame] | 147 | //int status; |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 148 | 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) |
akmhoque | 60a40f3 | 2013-02-12 11:17:49 -0600 | [diff] [blame] | 195 | mkdir(logDir, S_IRWXU | S_IRWXG | S_IROTH | S_IXOTH); |
| 196 | //printf("Status: %d\n",status); |
akmhoque | bfefef2 | 2012-09-26 10:09:34 -0500 | [diff] [blame] | 197 | } |
| 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 | |
| 223 | void |
| 224 | writeLogg(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); |
akmhoque | 9e9fc72 | 2012-09-26 14:03:25 -0500 | [diff] [blame] | 235 | 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] | 236 | va_list args; |
| 237 | va_start(args, format); |
| 238 | vfprintf(fp, format, args); |
| 239 | fclose(fp); |
| 240 | va_end(args); |
| 241 | } |
| 242 | } |
| 243 | } |
| 244 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 245 | |
| 246 | struct sockaddr_in * |
| 247 | get_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 | |
| 278 | int |
| 279 | get_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]) ); |
akmhoque | 6682ca3 | 2013-02-22 00:29:35 -0600 | [diff] [blame] | 293 | ip[strlen(ip)]='\0'; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 294 | return 0; |
| 295 | } |
akmhoque | 6682ca3 | 2013-02-22 00:29:35 -0600 | [diff] [blame] | 296 | return -1; |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 297 | } |
| 298 | |
| 299 | |
akmhoque | edb68d9 | 2013-03-05 10:18:16 -0600 | [diff] [blame] | 300 | int |
| 301 | add_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 | |
akmhoque | b77b95f | 2013-02-08 12:28:47 -0600 | [diff] [blame] | 323 | |