00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021 #include "includes.h"
00022 #include "net.h"
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037 static void print_cache_entry(const char* keystr, const char* datastr,
00038 const time_t timeout, void* dptr)
00039 {
00040 char *timeout_str;
00041 char *alloc_str = NULL;
00042 time_t now_t = time(NULL);
00043 struct tm timeout_tm, *now_tm;
00044
00045
00046
00047 now_tm = localtime(&timeout);
00048 if (!now_tm) {
00049 return;
00050 }
00051 memcpy(&timeout_tm, now_tm, sizeof(struct tm));
00052 now_tm = localtime(&now_t);
00053 if (!now_tm) {
00054 return;
00055 }
00056
00057
00058 if (timeout_tm.tm_year != now_tm->tm_year ||
00059 timeout_tm.tm_mon != now_tm->tm_mon ||
00060 timeout_tm.tm_mday != now_tm->tm_mday) {
00061
00062 timeout_str = asctime(&timeout_tm);
00063 if (!timeout_str) {
00064 return;
00065 }
00066 timeout_str[strlen(timeout_str) - 1] = '\0';
00067 } else {
00068 asprintf(&alloc_str, "%.2d:%.2d:%.2d", timeout_tm.tm_hour,
00069 timeout_tm.tm_min, timeout_tm.tm_sec);
00070 if (!alloc_str) {
00071 return;
00072 }
00073 timeout_str = alloc_str;
00074 }
00075
00076 d_printf("Key: %s\t Timeout: %s\t Value: %s %s\n", keystr,
00077 timeout_str, datastr, timeout > now_t ? "": "(expired)");
00078
00079 SAFE_FREE(alloc_str);
00080 }
00081
00082 static void delete_cache_entry(const char* keystr, const char* datastr,
00083 const time_t timeout, void* dptr)
00084 {
00085 if (!gencache_del(keystr))
00086 d_fprintf(stderr, "Couldn't delete entry! key = %s\n", keystr);
00087 }
00088
00089
00090
00091
00092
00093
00094
00095
00096 static time_t parse_timeout(const char* timeout_str)
00097 {
00098 char sign = '\0', *number = NULL, unit = '\0';
00099 int len, number_begin, number_end;
00100 time_t timeout;
00101
00102
00103 if (timeout_str[0] == '!' || timeout_str[0] == '+') {
00104 sign = timeout_str[0];
00105 number_begin = 1;
00106 } else {
00107 number_begin = 0;
00108 }
00109
00110
00111 len = strlen(timeout_str);
00112 switch (timeout_str[len - 1]) {
00113 case 's':
00114 case 'm':
00115 case 'h':
00116 case 'd':
00117 case 'w': unit = timeout_str[len - 1];
00118 }
00119
00120
00121 len = (sign) ? strlen(&timeout_str[number_begin]) : len;
00122 number_end = (unit) ? len - 1 : len;
00123 number = SMB_STRNDUP(&timeout_str[number_begin], number_end);
00124
00125
00126 timeout = (time_t)atoi(number);
00127
00128 switch (unit) {
00129 case 'm': timeout *= 60; break;
00130 case 'h': timeout *= 60*60; break;
00131 case 'd': timeout *= 60*60*24; break;
00132 case 'w': timeout *= 60*60*24*7; break;
00133 }
00134
00135 switch (sign) {
00136 case '!': timeout = time(NULL) - timeout; break;
00137 case '+':
00138 default: timeout += time(NULL); break;
00139 }
00140
00141 if (number) SAFE_FREE(number);
00142 return timeout;
00143 }
00144
00145
00146
00147
00148
00149
00150
00151
00152 static int net_cache_add(int argc, const char **argv)
00153 {
00154 const char *keystr, *datastr, *timeout_str;
00155 time_t timeout;
00156
00157 if (argc < 3) {
00158 d_printf("\nUsage: net cache add <key string> <data string> <timeout>\n");
00159 return -1;
00160 }
00161
00162 keystr = argv[0];
00163 datastr = argv[1];
00164 timeout_str = argv[2];
00165
00166
00167 timeout = parse_timeout(timeout_str);
00168 if (!timeout) {
00169 d_fprintf(stderr, "Invalid timeout argument.\n");
00170 return -1;
00171 }
00172
00173 if (gencache_set(keystr, datastr, timeout)) {
00174 d_printf("New cache entry stored successfully.\n");
00175 gencache_shutdown();
00176 return 0;
00177 }
00178
00179 d_fprintf(stderr, "Entry couldn't be added. Perhaps there's already such a key.\n");
00180 gencache_shutdown();
00181 return -1;
00182 }
00183
00184
00185
00186
00187
00188
00189
00190 static int net_cache_del(int argc, const char **argv)
00191 {
00192 const char *keystr = argv[0];
00193
00194 if (argc < 1) {
00195 d_printf("\nUsage: net cache del <key string>\n");
00196 return -1;
00197 }
00198
00199 if(gencache_del(keystr)) {
00200 d_printf("Entry deleted.\n");
00201 return 0;
00202 }
00203
00204 d_fprintf(stderr, "Couldn't delete specified entry\n");
00205 return -1;
00206 }
00207
00208
00209
00210
00211
00212
00213
00214
00215 static int net_cache_get(int argc, const char **argv)
00216 {
00217 const char* keystr = argv[0];
00218 char* valuestr;
00219 time_t timeout;
00220
00221 if (argc < 1) {
00222 d_printf("\nUsage: net cache get <key>\n");
00223 return -1;
00224 }
00225
00226 if (gencache_get(keystr, &valuestr, &timeout)) {
00227 print_cache_entry(keystr, valuestr, timeout, NULL);
00228 return 0;
00229 }
00230
00231 d_fprintf(stderr, "Failed to find entry\n");
00232 return -1;
00233 }
00234
00235
00236
00237
00238
00239
00240
00241
00242 static int net_cache_search(int argc, const char **argv)
00243 {
00244 const char* pattern;
00245
00246 if (argc < 1) {
00247 d_printf("Usage: net cache search <pattern>\n");
00248 return -1;
00249 }
00250
00251 pattern = argv[0];
00252 gencache_iterate(print_cache_entry, NULL, pattern);
00253 return 0;
00254 }
00255
00256
00257
00258
00259
00260
00261
00262
00263 static int net_cache_list(int argc, const char **argv)
00264 {
00265 const char* pattern = "*";
00266 gencache_iterate(print_cache_entry, NULL, pattern);
00267 gencache_shutdown();
00268 return 0;
00269 }
00270
00271
00272
00273
00274
00275
00276
00277
00278 static int net_cache_flush(int argc, const char **argv)
00279 {
00280 const char* pattern = "*";
00281 gencache_iterate(delete_cache_entry, NULL, pattern);
00282 gencache_shutdown();
00283 return 0;
00284 }
00285
00286
00287
00288
00289
00290
00291
00292
00293 static int net_cache_usage(int argc, const char **argv)
00294 {
00295 d_printf(" net cache add \t add add new cache entry\n");
00296 d_printf(" net cache del \t delete existing cache entry by key\n");
00297 d_printf(" net cache flush \t delete all entries existing in the cache\n");
00298 d_printf(" net cache get \t get cache entry by key\n");
00299 d_printf(" net cache search \t search for entries in the cache, by given pattern\n");
00300 d_printf(" net cache list \t list all cache entries (just like search for \"*\")\n");
00301 return -1;
00302 }
00303
00304
00305
00306
00307
00308
00309
00310
00311 int net_cache(int argc, const char **argv)
00312 {
00313 struct functable func[] = {
00314 {"add", net_cache_add},
00315 {"del", net_cache_del},
00316 {"get", net_cache_get},
00317 {"search", net_cache_search},
00318 {"list", net_cache_list},
00319 {"flush", net_cache_flush},
00320 {NULL, NULL}
00321 };
00322
00323 return net_run_function(argc, argv, func, net_cache_usage);
00324 }