summaryrefslogtreecommitdiff
path: root/source3/lib
diff options
context:
space:
mode:
authorAndrew Bartlett <abartlet@samba.org>2003-01-04 08:48:15 +0000
committerAndrew Bartlett <abartlet@samba.org>2003-01-04 08:48:15 +0000
commit863e9ca2c640ce7a94acf81cff7408edc6f64e01 (patch)
tree26c5290da0e40d5202f5f0abc10002b1767521b8 /source3/lib
parente3ed8eaa2fd47a72c7b89d769ee594f238130ff7 (diff)
downloadsamba-863e9ca2c640ce7a94acf81cff7408edc6f64e01.tar.gz
samba-863e9ca2c640ce7a94acf81cff7408edc6f64e01.tar.bz2
samba-863e9ca2c640ce7a94acf81cff7408edc6f64e01.zip
Merge from HEAD - mimir's new gencache based namecache code.
Andrew Bartlett (This used to be commit f79324f730c400342f445c931b0d75ff756d7cc7)
Diffstat (limited to 'source3/lib')
-rw-r--r--source3/lib/gencache.c25
-rw-r--r--source3/lib/util_str.c152
2 files changed, 164 insertions, 13 deletions
diff --git a/source3/lib/gencache.c b/source3/lib/gencache.c
index a872f1331c..a7df7c95db 100644
--- a/source3/lib/gencache.c
+++ b/source3/lib/gencache.c
@@ -91,8 +91,8 @@ BOOL gencache_shutdown(void)
/**
- * Add one entry to the cache file.
- * (it part of tridge's proposed API)
+ * Set an entry in the cache file. If there's no such
+ * one, then add it.
*
* @param key string that represents a key of this entry
* @param value text representation value being cached
@@ -102,7 +102,7 @@ BOOL gencache_shutdown(void)
* false on the attempt's failure
**/
-BOOL gencache_add(const char *keystr, const char *value, time_t timeout)
+BOOL gencache_set(const char *keystr, const char *value, time_t timeout)
{
int ret;
TDB_DATA keybuf, databuf;
@@ -122,7 +122,7 @@ BOOL gencache_add(const char *keystr, const char *value, time_t timeout)
= %s (%d seconds %s)\n", keybuf.dptr, value, ctime(&timeout),
(int)(timeout - time(NULL)), timeout > time(NULL) ? "ahead" : "in the past"));
- ret = tdb_store(cache, keybuf, databuf, TDB_INSERT);
+ ret = tdb_store(cache, keybuf, databuf, 0);
SAFE_FREE(valstr);
SAFE_FREE(keybuf.dptr);
SAFE_FREE(databuf.dptr);
@@ -133,7 +133,6 @@ BOOL gencache_add(const char *keystr, const char *value, time_t timeout)
/**
* Set existing entry to the cache file.
- * (it part of tridge's proposed API)
*
* @param key string that represents a key of this entry
* @param value text representation value being cached
@@ -143,7 +142,7 @@ BOOL gencache_add(const char *keystr, const char *value, time_t timeout)
* false on the attempt's failure
**/
-BOOL gencache_set(const char *keystr, const char *valstr, time_t timeout)
+BOOL gencache_set_only(const char *keystr, const char *valstr, time_t timeout)
{
int ret = -1;
TDB_DATA keybuf, databuf;
@@ -189,7 +188,6 @@ BOOL gencache_set(const char *keystr, const char *valstr, time_t timeout)
/**
* Delete one entry from the cache file.
- * (it part of tridge's proposed API)
*
* @param key string that represents a key of this entry
*
@@ -219,11 +217,10 @@ BOOL gencache_del(const char *keystr)
/**
* Get existing entry from the cache file.
- * (it part of tridge's proposed API)
*
* @param key string that represents a key of this entry
* @param value buffer that is allocated and filled with the entry value
- * buffer's disposing is done outside
+ * buffer's disposing must be done outside
* @param timeout pointer to a time_t that is filled with entry's
* timeout
*
@@ -272,12 +269,14 @@ BOOL gencache_get(const char *keystr, char **valstr, time_t *timeout)
*
* @param fn pointer to the function that will be supplied with each single
* matching cache entry (key, value and timeout) as an arguments
+ * @param data void pointer to an arbitrary data that is passed directly to the fn
+ * function on each call
* @param keystr_pattern pattern the existing entries' keys are matched to
*
**/
-void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout),
- const char* keystr_pattern)
+void gencache_iterate(void (*fn)(const char* key, const char *value, time_t timeout, void* dptr),
+ void* data, const char* keystr_pattern)
{
TDB_LIST_NODE *node, *first_node;
TDB_DATA databuf;
@@ -289,7 +288,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time
if (!gencache_init()) return;
- DEBUG(5, ("Searching cache keys with pattern %s", keystr_pattern));
+ DEBUG(5, ("Searching cache keys with pattern %s\n", keystr_pattern));
node = tdb_search_keys(cache, keystr_pattern);
first_node = node;
@@ -314,7 +313,7 @@ void gencache_iterate(void (*fn)(const char* key, const char *value, time_t time
DEBUG(10, ("Calling function with arguments (key = %s, value = %s, timeout = %s)\n",
keystr, valstr, ctime(&timeout)));
- fn(keystr, valstr, timeout);
+ fn(keystr, valstr, timeout, data);
SAFE_FREE(valstr);
SAFE_FREE(entry);
diff --git a/source3/lib/util_str.c b/source3/lib/util_str.c
index c519fd8497..b1fc53144e 100644
--- a/source3/lib/util_str.c
+++ b/source3/lib/util_str.c
@@ -1367,6 +1367,158 @@ BOOL str_list_substitute(char **list, const char *pattern, const char *insert)
return True;
}
+
+#define IPSTR_LIST_SEP ","
+
+/**
+ * Add ip string representation to ipstr list. Used also
+ * as part of @function ipstr_list_make
+ *
+ * @param ipstr_list pointer to string containing ip list;
+ * MUST BE already allocated and IS reallocated if necessary
+ * @param ipstr_size pointer to current size of ipstr_list (might be changed
+ * as a result of reallocation)
+ * @param ip IP address which is to be added to list
+ * @return pointer to string appended with new ip and possibly
+ * reallocated to new length
+ **/
+
+char* ipstr_list_add(char** ipstr_list, const struct in_addr *ip)
+{
+ char* new_ipstr = NULL;
+
+ /* arguments checking */
+ if (!ipstr_list || !ip) return NULL;
+
+ /* attempt to convert ip to a string and append colon separator to it */
+ if (*ipstr_list) {
+ asprintf(&new_ipstr, "%s%s%s", *ipstr_list, IPSTR_LIST_SEP,inet_ntoa(*ip));
+ SAFE_FREE(*ipstr_list);
+ } else {
+ asprintf(&new_ipstr, "%s", inet_ntoa(*ip));
+ }
+ *ipstr_list = new_ipstr;
+ return *ipstr_list;
+}
+
+
+/**
+ * Allocate and initialise an ipstr list using ip adresses
+ * passed as arguments.
+ *
+ * @param ipstr_list pointer to string meant to be allocated and set
+ * @param ip_list array of ip addresses to place in the list
+ * @param ip_count number of addresses stored in ip_list
+ * @return pointer to allocated ip string
+ **/
+
+char* ipstr_list_make(char** ipstr_list, const struct in_addr* ip_list, int ip_count)
+{
+ int i;
+
+ /* arguments checking */
+ if (!ip_list && !ipstr_list) return 0;
+
+ *ipstr_list = NULL;
+
+ /* process ip addresses given as arguments */
+ for (i = 0; i < ip_count; i++)
+ *ipstr_list = ipstr_list_add(ipstr_list, &ip_list[i]);
+
+ return (*ipstr_list);
+}
+
+
+/**
+ * Parse given ip string list into array of ip addresses
+ * (as in_addr structures)
+ *
+ * @param ipstr ip string list to be parsed
+ * @param ip_list pointer to array of ip addresses which is
+ * allocated by this function and must be freed by caller
+ * @return number of succesfully parsed addresses
+ **/
+
+int ipstr_list_parse(const char* ipstr_list, struct in_addr** ip_list)
+{
+ fstring token_str;
+ int count;
+
+ if (!ipstr_list || !ip_list) return 0;
+
+ for (*ip_list = NULL, count = 0;
+ next_token(&ipstr_list, token_str, IPSTR_LIST_SEP, FSTRING_LEN);
+ count++) {
+
+ struct in_addr addr;
+
+ /* convert single token to ip address */
+ if ( (addr.s_addr = inet_addr(token_str)) == INADDR_NONE )
+ break;
+
+ /* prepare place for another in_addr structure */
+ *ip_list = Realloc(*ip_list, (count + 1) * sizeof(struct in_addr));
+ if (!*ip_list) return -1;
+
+ (*ip_list)[count] = addr;
+ }
+
+ return count;
+}
+
+
+/**
+ * Safely free ip string list
+ *
+ * @param ipstr_list ip string list to be freed
+ **/
+
+void ipstr_list_free(char* ipstr_list)
+{
+ SAFE_FREE(ipstr_list);
+}
+
+
+/***********************************************************
+ Unescape a URL encoded string, in place.
+***********************************************************/
+
+void rfc1738_unescape(char *buf)
+{
+ char *p=buf;
+
+ while ((p=strchr_m(p,'+')))
+ *p = ' ';
+
+ p = buf;
+
+ while (p && *p && (p=strchr_m(p,'%'))) {
+ int c1 = p[1];
+ int c2 = p[2];
+
+ if (c1 >= '0' && c1 <= '9')
+ c1 = c1 - '0';
+ else if (c1 >= 'A' && c1 <= 'F')
+ c1 = 10 + c1 - 'A';
+ else if (c1 >= 'a' && c1 <= 'f')
+ c1 = 10 + c1 - 'a';
+ else {p++; continue;}
+
+ if (c2 >= '0' && c2 <= '9')
+ c2 = c2 - '0';
+ else if (c2 >= 'A' && c2 <= 'F')
+ c2 = 10 + c2 - 'A';
+ else if (c2 >= 'a' && c2 <= 'f')
+ c2 = 10 + c2 - 'a';
+ else {p++; continue;}
+
+ *p = (c1<<4) | c2;
+
+ memmove(p+1, p+3, strlen(p+3)+1);
+ p++;
+ }
+}
+
#ifdef VALGRIND
size_t valgrind_strlen(const char *s)
{