Browse code

New RPC commands which manipulate the content of the DNS cache:

- dns.view: dumps the cache
- dns.delete_all: deletes all the entries
- dns.add_a: adds an A record
- dns.add_aaaa: adds an AAAA record
- dns.add_srv: adds an SRV record
- dns.delete_a: deletes an A record
- dns.delete_aaaa: deletes an AAAA record
- dns.delete_srv: deletes an SRV record

Miklos Tirpak authored on 26/07/2007 11:32:01
Showing 3 changed files
... ...
@@ -43,6 +43,15 @@
43 43
 void dns_cache_debug(rpc_t* rpc, void* ctx);
44 44
 void dns_cache_debug_all(rpc_t* rpc, void* ctx);
45 45
 void dns_cache_mem_info(rpc_t* rpc, void* ctx);
46
+void dns_cache_view(rpc_t* rpc, void* ctx);
47
+void dns_cache_delete_all(rpc_t* rpc, void* ctx);
48
+void dns_cache_add_a(rpc_t* rpc, void* ctx);
49
+void dns_cache_add_aaaa(rpc_t* rpc, void* ctx);
50
+void dns_cache_add_srv(rpc_t* rpc, void* ctx);
51
+void dns_cache_delete_a(rpc_t* rpc, void* ctx);
52
+void dns_cache_delete_aaaa(rpc_t* rpc, void* ctx);
53
+void dns_cache_delete_srv(rpc_t* rpc, void* ctx);
54
+
46 55
 
47 56
 static const char* dns_cache_mem_info_doc[] = {
48 57
 	"dns cache memory info.",    /* Documentation string */
... ...
@@ -58,6 +67,45 @@ static const char* dns_cache_debug_all_doc[] = {
58 58
 	0                              /* Method signature(s) */
59 59
 };
60 60
 
61
+static const char* dns_cache_view_doc[] = {
62
+	"dns cache dump in a human-readable format",
63
+	0
64
+};
65
+
66
+static const char* dns_cache_delete_all_doc[] = {
67
+	"deletes all the entries from the DNS cache",
68
+	0
69
+};
70
+
71
+static const char* dns_cache_add_a_doc[] = {
72
+	"adds an A record to the DNS cache",
73
+	0
74
+};
75
+
76
+static const char* dns_cache_add_aaaa_doc[] = {
77
+	"adds an AAAA record to the DNS cache",
78
+	0
79
+};
80
+static const char* dns_cache_add_srv_doc[] = {
81
+	"adds an SRV record to the DNS cache",
82
+	0
83
+};
84
+
85
+static const char* dns_cache_delete_a_doc[] = {
86
+	"deletes an A record from the DNS cache",
87
+	0
88
+};
89
+
90
+static const char* dns_cache_delete_aaaa_doc[] = {
91
+	"deletes an AAAA record from the DNS cache",
92
+	0
93
+};
94
+
95
+static const char* dns_cache_delete_srv_doc[] = {
96
+	"deletes an SRV record from the DNS cache",
97
+	0
98
+};
99
+
61 100
 #ifdef DNS_WATCHDOG_SUPPORT
62 101
 void dns_set_server_state_rpc(rpc_t* rpc, void* ctx);
63 102
 
... ...
@@ -479,6 +527,14 @@ rpc_export_t core_rpc_methods[] = {
479 479
 	{"dns.mem_info",          dns_cache_mem_info,     dns_cache_mem_info_doc,     0	},
480 480
 	{"dns.debug",          dns_cache_debug,           dns_cache_debug_doc,        0	},
481 481
 	{"dns.debug_all",      dns_cache_debug_all,       dns_cache_debug_all_doc,        0	},
482
+	{"dns.view",               dns_cache_view,        dns_cache_view_doc,        0	},
483
+	{"dns.delete_all",         dns_cache_delete_all,  dns_cache_delete_all_doc,  0	},
484
+	{"dns.add_a",              dns_cache_add_a,       dns_cache_add_a_doc,       0	},
485
+	{"dns.add_aaaa",           dns_cache_add_aaaa,    dns_cache_add_aaaa_doc,    0	},
486
+	{"dns.add_srv",            dns_cache_add_srv,     dns_cache_add_srv_doc,     0	},
487
+	{"dns.delete_a",           dns_cache_delete_a,    dns_cache_delete_a_doc,    0	},
488
+	{"dns.delete_aaaa",        dns_cache_delete_aaaa, dns_cache_delete_aaaa_doc, 0	},
489
+	{"dns.delete_srv",         dns_cache_delete_srv,  dns_cache_delete_srv_doc,  0	},
482 490
 #ifdef DNS_WATCHDOG_SUPPORT
483 491
 	{"dns.set_server_state",   dns_set_server_state_rpc, dns_set_server_state_doc, 0 },
484 492
 #endif
... ...
@@ -35,6 +35,8 @@
35 35
  *  2007-06-16  naptr support (andrei)
36 36
  *  2008-07-18  DNS watchdog support -- can be used to inform the core
37 37
  *              that the DNS servers are down (Miklos)
38
+ *  2008-07-25  various rpc commands to manipulate the content
39
+ *		of the cache (Miklos)
38 40
  */
39 41
 
40 42
 #ifdef USE_DNS_CACHE
... ...
@@ -824,6 +826,61 @@ inline static struct dns_hash_entry* dns_cache_mk_ip_entry(str* name,
824 824
 	return e;
825 825
 }
826 826
 
827
+/* creates an srv hash entry from the given parameters
828
+ * returns 0 on error */
829
+static struct dns_hash_entry* dns_cache_mk_srv_entry(str* name,
830
+							unsigned short priority,
831
+							unsigned short weight,
832
+							unsigned short port,
833
+							str* rr_name,
834
+							int ttl)
835
+{
836
+	struct dns_hash_entry* e;
837
+	int size;
838
+	ticks_t now;
839
+	
840
+	/* everything is allocated in one block: dns_hash_entry + name +
841
+	 * + dns_rr + rdata;  dns_rr must start at an aligned adress,
842
+	 * hence we need to round dns_hash_entry+name size to a sizeof(long),
843
+	 * and similarly, dns_rr must be rounded to sizeof(short).
844
+	 * multiple.
845
+	 * Memory image:
846
+	 * struct dns_hash_entry
847
+	 * name (name_len+1 bytes)
848
+	 * padding to multiple of sizeof(long)
849
+	 * dns_rr
850
+	 * padding to multiple of sizeof(short)
851
+	 * rdata 
852
+	  */
853
+	size=ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1) + 
854
+		ROUND_SHORT(sizeof(struct dns_rr)) +
855
+		sizeof(struct srv_rdata)-1 +
856
+		rr_name->len+1;
857
+
858
+	e=shm_malloc(size);
859
+	if (e==0){
860
+		LOG(L_ERR, "ERROR: dns_cache_srv_ip_entry: out of memory\n");
861
+		return 0;
862
+	}
863
+	memset(e, 0, size); /* init with 0*/
864
+	e->total_size=size;
865
+	e->name_len=name->len;
866
+	e->type=T_SRV;
867
+	now=get_ticks_raw();
868
+	e->last_used=now;
869
+	e->expire=now+S_TO_TICKS(ttl);
870
+	memcpy(e->name, name->s, name->len); /* memset makes sure is 0-term. */
871
+	e->rr_lst=(void*)((char*)e+
872
+				ROUND_POINTER(sizeof(struct dns_hash_entry)+name->len-1+1));
873
+	e->rr_lst->rdata=(void*)((char*)e->rr_lst+ROUND_SHORT(sizeof(struct dns_rr)));
874
+	e->rr_lst->expire=e->expire;
875
+	((struct srv_rdata*)e->rr_lst->rdata)->priority = priority;
876
+	((struct srv_rdata*)e->rr_lst->rdata)->weight = weight;
877
+	((struct srv_rdata*)e->rr_lst->rdata)->port = port;
878
+	((struct srv_rdata*)e->rr_lst->rdata)->name_len = rr_name->len;
879
+	memcpy(((struct srv_rdata*)e->rr_lst->rdata)->name, rr_name->s, rr_name->len);
880
+	return e;
881
+}
827 882
 
828 883
 
829 884
 /* create a dns hash entry from a name and a rdata list (pkg_malloc'ed)
... ...
@@ -3098,6 +3155,505 @@ void dns_cache_debug_all(rpc_t* rpc, void* ctx)
3098 3098
 	UNLOCK_DNS_HASH();
3099 3099
 }
3100 3100
 
3101
+static char *print_type(unsigned short type)
3102
+{
3103
+	switch (type) {
3104
+		case T_A:
3105
+			return "A";
3106
+		case T_AAAA:
3107
+			return "AAAA";
3108
+		case T_SRV:
3109
+			return "SRV";
3110
+		case T_NAPTR:
3111
+			return "NAPTR";
3112
+		case T_CNAME:
3113
+			return "CNAME";
3114
+		default:
3115
+			return "unkown";
3116
+	}
3117
+}
3118
+
3119
+/* dumps the content of the cache in a human-readable format */
3120
+void dns_cache_view(rpc_t* rpc, void* ctx)
3121
+{
3122
+	int h;
3123
+	struct dns_hash_entry* e;
3124
+	struct dns_rr* rr;
3125
+	struct ip_addr ip;
3126
+	ticks_t now;
3127
+	void* handle;
3128
+	str s;
3129
+	
3130
+	now=get_ticks_raw();
3131
+	LOCK_DNS_HASH();
3132
+		for (h=0; h<DNS_HASH_SIZE; h++){
3133
+			clist_foreach(&dns_hash[h], e, next){
3134
+				rpc->add(ctx, "{", &handle);
3135
+				rpc->struct_add(handle, "s", "name", e->name);
3136
+				rpc->struct_add(handle, "s", "type", print_type(e->type));
3137
+				rpc->struct_add(handle, "d", "size (bytes)", e->total_size);
3138
+				rpc->struct_add(handle, "d", "reference counter", e->refcnt.val);
3139
+				rpc->struct_add(handle, "d", "expires in (s)", (s_ticks_t)(e->expire-now)<0?-1:
3140
+									TICKS_TO_S(e->expire-now));
3141
+				rpc->struct_add(handle, "d", "last used (s)", TICKS_TO_S(now-e->last_used));
3142
+				rpc->struct_add(handle, "d", "error flags", e->err_flags);
3143
+
3144
+				for (rr=e->rr_lst; rr; rr=rr->next){
3145
+					switch(e->type){
3146
+						case T_A:
3147
+						case T_AAAA:
3148
+							if (dns_rr2ip(e->type, rr, &ip)==0){
3149
+								rpc->struct_add(handle, "s", "rr ip",
3150
+									ip_addr2a(&ip) );
3151
+							}else{
3152
+								rpc->struct_add(handle, "s", "rr ip",
3153
+									"<error: bad rr>");
3154
+							}
3155
+							break;
3156
+						case T_SRV:
3157
+							rpc->struct_add(handle, "s", "rr name",
3158
+									((struct srv_rdata*)(rr->rdata))->name);
3159
+							rpc->struct_add(handle, "d", "rr port",
3160
+									((struct srv_rdata*)(rr->rdata))->port);
3161
+							rpc->struct_add(handle, "d", "rr priority",
3162
+									((struct srv_rdata*)(rr->rdata))->priority);
3163
+							rpc->struct_add(handle, "d", "rr weight",
3164
+									((struct srv_rdata*)(rr->rdata))->weight);
3165
+							break;
3166
+						case T_NAPTR:
3167
+							rpc->struct_add(handle, "d", "rr order",
3168
+									((struct naptr_rdata*)(rr->rdata))->order);
3169
+							rpc->struct_add(handle, "d", "rr preference",
3170
+									((struct naptr_rdata*)(rr->rdata))->pref);
3171
+
3172
+							s.s = ((struct naptr_rdata*)(rr->rdata))->flags;
3173
+							s.len = ((struct naptr_rdata*)(rr->rdata))->flags_len;
3174
+							rpc->struct_add(handle, "S", "rr flags", &s);
3175
+
3176
+							s.s = ((struct naptr_rdata*)(rr->rdata))->services;
3177
+							s.len = ((struct naptr_rdata*)(rr->rdata))->services_len;
3178
+							rpc->struct_add(handle, "S", "rr service", &s);
3179
+
3180
+							s.s = ((struct naptr_rdata*)(rr->rdata))->regexp;
3181
+							s.len = ((struct naptr_rdata*)(rr->rdata))->regexp_len;
3182
+							rpc->struct_add(handle, "S", "rr regexp", &s);
3183
+
3184
+							s.s = ((struct naptr_rdata*)(rr->rdata))->repl;
3185
+							s.len = ((struct naptr_rdata*)(rr->rdata))->repl_len;
3186
+							rpc->struct_add(handle, "S", "rr replacement", &s);
3187
+							break;
3188
+						case T_CNAME:
3189
+							rpc->struct_add(handle, "s",  "rr name",
3190
+									((struct cname_rdata*)(rr->rdata))->name);
3191
+							break;
3192
+						default:
3193
+							rpc->struct_add(ctx, "ss", "resource record",
3194
+									"unknown");
3195
+					}
3196
+					rpc->struct_add(handle, "d", "rr expires in (s)", (s_ticks_t)(rr->expire-now)<0?-1:
3197
+										TICKS_TO_S(rr->expire-now));
3198
+					rpc->struct_add(handle, "d", "rr error flags", rr->err_flags);
3199
+				}
3200
+			}
3201
+		}
3202
+	UNLOCK_DNS_HASH();
3203
+}
3204
+
3205
+
3206
+/* deletes all the entries from the cache */
3207
+void dns_cache_flush(void)
3208
+{
3209
+	int h;
3210
+	struct dns_hash_entry* e;
3211
+	struct dns_hash_entry* tmp;
3212
+
3213
+	DBG("dns_cache_flush(): removing elements from the cache\n");
3214
+	LOCK_DNS_HASH();
3215
+		for (h=0; h<DNS_HASH_SIZE; h++){
3216
+			clist_foreach_safe(&dns_hash[h], e, tmp, next){
3217
+				_dns_hash_remove(e);
3218
+			}
3219
+		}
3220
+	UNLOCK_DNS_HASH();
3221
+}
3222
+
3223
+/* deletes all the entries from the cache */
3224
+void dns_cache_delete_all(rpc_t* rpc, void* ctx)
3225
+{
3226
+	dns_cache_flush();
3227
+}
3228
+
3229
+/* clons an entry and extends the memory area of it for a new rr if rdata_size>0
3230
+ * the new dns_rr struct is initialized, but the rdata is only filled with 0.
3231
+ */
3232
+static struct dns_hash_entry *dns_cache_clone_entry(struct dns_hash_entry *e, int rdata_size, int ttl,
3233
+							struct dns_rr **_new_rr)
3234
+{
3235
+	struct dns_hash_entry *new;
3236
+	struct dns_rr *rr, *last_rr, *new_rr;
3237
+	int size, rounded_size, rr_size;
3238
+	ticks_t now;
3239
+
3240
+	now=get_ticks_raw();
3241
+	size = e->total_size;
3242
+	if (rdata_size) {
3243
+		/* we have to extend the entry */
3244
+		rounded_size = 	ROUND_POINTER(size); /* size may not have been rounded previously */
3245
+		switch (e->type) {
3246
+			case T_A:
3247
+			case T_AAAA:
3248
+			case T_CNAME:
3249
+				rr_size = sizeof(struct dns_rr);
3250
+				break;
3251
+			case T_SRV:
3252
+				rr_size = ROUND_SHORT(sizeof(struct dns_rr));
3253
+				break;
3254
+			case T_NAPTR:
3255
+				rr_size = ROUND_POINTER(sizeof(struct dns_rr));
3256
+				break;
3257
+			default:
3258
+				LOG(L_ERR, "ERROR: dns_cache_clone_entry: type %d not supported\n",
3259
+						e->type);
3260
+				return NULL;				
3261
+		}
3262
+	} else {
3263
+		rounded_size = size; /* no need to round the size, we just clone the entry
3264
+					without extending it */
3265
+		rr_size = 0;
3266
+	}
3267
+
3268
+	new=shm_malloc(rounded_size+rr_size+rdata_size);
3269
+	if (!new) {
3270
+		LOG(L_ERR, "ERROR: dns_cache_clone_entry: out of memory\n");
3271
+		return NULL;
3272
+	}
3273
+	/* clone the entry */
3274
+	memcpy(new, e, size);
3275
+	/* fix the values and pointers */
3276
+	new->next = new->prev = NULL;
3277
+#ifdef DNS_LU_LST
3278
+	new->last_used_lst.next = new->last_used_lst.next = NULL;
3279
+#endif
3280
+	new->rr_lst = (struct dns_rr*)translate_pointer((char*)new, (char*)e, (char*)new->rr_lst);
3281
+	atomic_set(&new->refcnt, 0);
3282
+	new->last_used = now;
3283
+	/* expire and total_size are fixed later if needed */
3284
+	/* fix the pointers inside the rr structures */
3285
+	last_rr = NULL;
3286
+	for (rr=new->rr_lst; rr; rr=rr->next) {
3287
+		rr->rdata = (void*)translate_pointer((char*)new, (char*)e, (char*)rr->rdata);
3288
+		if (rr->next)
3289
+			rr->next = (struct dns_rr*)translate_pointer((char*)new, (char*)e, (char*)rr->next);
3290
+		else
3291
+			last_rr = rr;
3292
+
3293
+		if (e->type == T_NAPTR) {
3294
+			/* there are pointers inside the NAPTR rdata stucture */
3295
+			((struct naptr_rdata*)rr->rdata)->flags =
3296
+				translate_pointer((char*)new, (char*)e,
3297
+					((struct naptr_rdata*)rr->rdata)->flags);
3298
+
3299
+			((struct naptr_rdata*)rr->rdata)->services =
3300
+				translate_pointer((char*)new, (char*)e,
3301
+					((struct naptr_rdata*)rr->rdata)->services);
3302
+
3303
+			((struct naptr_rdata*)rr->rdata)->regexp =
3304
+				translate_pointer((char*)new, (char*)e,
3305
+					((struct naptr_rdata*)rr->rdata)->regexp);
3306
+
3307
+			((struct naptr_rdata*)rr->rdata)->repl =
3308
+				translate_pointer((char*)new, (char*)e,
3309
+					((struct naptr_rdata*)rr->rdata)->repl);
3310
+		}
3311
+	}
3312
+	
3313
+
3314
+	if (rdata_size) {
3315
+		memset(new+size, 0, rounded_size-size+rr_size+rdata_size);
3316
+
3317
+		/* set the pointer to the new rr structure */
3318
+		new_rr = (void*)((char*)new + rounded_size);
3319
+		new_rr->rdata = (void*)((char*)new_rr+rr_size);
3320
+		new_rr->expire = now + S_TO_TICKS(ttl);
3321
+		/* link the rr to the previous one */
3322
+		last_rr->next = new_rr;
3323
+
3324
+		/* fix the total_size and expires values */
3325
+		new->total_size=rounded_size+rr_size+rdata_size;
3326
+		new->expire = MAX(new->expire, new_rr->expire);
3327
+
3328
+
3329
+		if (_new_rr)
3330
+			*_new_rr = new_rr;
3331
+	} else {
3332
+		if (_new_rr)
3333
+			*_new_rr = NULL;
3334
+	}
3335
+
3336
+	return new;
3337
+}
3338
+
3339
+/* Adds a new record to the cache.
3340
+ * If there is an existing record with the same name and value
3341
+ * (ip address in case of A/AAAA record, name in case of SRV record)
3342
+ * only the remaining fields are updated.
3343
+ *
3344
+ * Currently only A, AAAA, and SRV records are supported.
3345
+ */
3346
+static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
3347
+{
3348
+	struct dns_hash_entry *old=NULL, *new=NULL;
3349
+	struct dns_rr *rr;
3350
+	str name;
3351
+	int ttl;
3352
+	str ip, rr_name;
3353
+	int flags;
3354
+	struct ip_addr *ip_addr;
3355
+	int priority, weight, port; 
3356
+	ticks_t expire;
3357
+	int err, h;
3358
+	int size;
3359
+
3360
+	/* eliminate gcc warnings */
3361
+	ip_addr = 0;
3362
+	size = 0;
3363
+
3364
+	switch(type) {
3365
+	case T_A:
3366
+	case T_AAAA:
3367
+		if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
3368
+			return;
3369
+		break;
3370
+	case T_SRV:
3371
+		if (rpc->scan(ctx, "SddddSd", &name, &ttl, &priority, &weight, &port, &rr_name, &flags) < 7)
3372
+			return;
3373
+		break;
3374
+	case T_CNAME:
3375
+	case T_NAPTR:
3376
+		rpc->fault(ctx, 400, "not implemented");
3377
+		return;			
3378
+	default:
3379
+		rpc->fault(ctx, 400, "unknown type");
3380
+		return;			
3381
+	}
3382
+
3383
+	if (!flags) {
3384
+		/* fix-up the values */
3385
+		switch(type) {
3386
+		case T_A:
3387
+			ip_addr = str2ip(&ip);
3388
+			if (!ip_addr) {
3389
+				rpc->fault(ctx, 400, "Malformed ip address");
3390
+				goto error;
3391
+			}
3392
+			break;
3393
+		case T_AAAA:
3394
+			ip_addr = str2ip6(&ip);
3395
+			if (!ip_addr) {
3396
+				rpc->fault(ctx, 400, "Malformed ip address");
3397
+				goto error;
3398
+			}
3399
+			break;
3400
+		/* case T_SRV: nothing to do */
3401
+		}
3402
+	}
3403
+
3404
+	/* check whether there is a matching entry in the cache */
3405
+	old = dns_hash_get(&name, type, &h, &err);
3406
+	if (old && old->type!=type) {
3407
+		/* probably we found a CNAME instead of the specified type,
3408
+		it is not needed */
3409
+		dns_hash_put(old);
3410
+		old=NULL;
3411
+	}
3412
+
3413
+	/* prepare the entry */
3414
+	if (flags) {
3415
+		/* negative entry */
3416
+		new = dns_cache_mk_bad_entry(&name, type, ttl, flags);
3417
+		if (!new) {
3418
+			rpc->fault(ctx, 400, "Failed to add the entry to the cache");
3419
+			goto error;
3420
+		}
3421
+	} else {
3422
+		if (!old || old->err_flags) {
3423
+			/* there was no matching entry in the hash table,
3424
+			or the entry is a negative record with inefficient space,
3425
+			let us create a new one */
3426
+			switch(type) {
3427
+			case T_A:
3428
+			case T_AAAA:
3429
+				new = dns_cache_mk_ip_entry(&name, ip_addr);
3430
+				if (!new) {
3431
+					rpc->fault(ctx, 400, "Failed to add the entry to the cache");
3432
+					goto error;
3433
+				}
3434
+				/* fix the expiration time, dns_cache_mk_ip_entry() sets it to now-1 */
3435
+				expire = get_ticks_raw() + S_TO_TICKS(ttl);
3436
+				new->expire = expire;
3437
+				new->rr_lst->expire = expire;
3438
+				break;
3439
+			case T_SRV:
3440
+				new = dns_cache_mk_srv_entry(&name, priority, weight, port, &rr_name, ttl);
3441
+				if (!new) {
3442
+					rpc->fault(ctx, 400, "Failed to add the entry to the cache");
3443
+					goto error;
3444
+				}
3445
+			}
3446
+		} else {
3447
+			/* we must modify the entry, so better to clone it, modify the new one,
3448
+			and replace the old with the new entry in the hash table, because the
3449
+			entry is not always locked */
3450
+
3451
+			/* check whether there is an rr with the same value */
3452
+			for (rr=old->rr_lst; rr; rr=rr->next)
3453
+				if ((((type == T_A) || (type == T_AAAA)) &&
3454
+					(memcmp(ip_addr->u.addr, ((struct a_rdata*)rr->rdata)->ip, ip_addr->len)==0))
3455
+				|| ((type == T_SRV) &&
3456
+					(((struct srv_rdata*)rr->rdata)->name_len == rr_name.len) &&
3457
+					(memcmp(rr_name.s, ((struct srv_rdata*)rr->rdata)->name, rr_name.len)==0)))
3458
+				break;
3459
+
3460
+			if (rr) {
3461
+				/* the rr was found in the list */
3462
+				new = dns_cache_clone_entry(old, 0, 0, 0);
3463
+				if (!new) {
3464
+					rpc->fault(ctx, 400, "Failed to add the entry to the cache");
3465
+					goto error;
3466
+				}
3467
+				/* let the rr point to the new structure */
3468
+				rr = (struct dns_rr*)translate_pointer((char*)new, (char*)old, (char*)rr);
3469
+
3470
+				if (type == T_SRV) {
3471
+					/* fix the priority, weight, and port */
3472
+					((struct srv_rdata*)rr->rdata)->priority = priority;
3473
+					((struct srv_rdata*)rr->rdata)->weight = weight;
3474
+					((struct srv_rdata*)rr->rdata)->port = port;
3475
+				}
3476
+
3477
+				/* fix the expire value */
3478
+				rr->expire = get_ticks_raw() + S_TO_TICKS(ttl);
3479
+				new->expire = 0;
3480
+				for (rr=new->rr_lst; rr; rr=rr->next)
3481
+					new->expire = MAX(new->expire, rr->expire);
3482
+			} else {
3483
+				/* there was no matching rr, extend the structure with a new one */
3484
+				switch(type) {
3485
+				case T_A:
3486
+					size = sizeof(struct a_rdata);
3487
+					break;
3488
+				case T_AAAA:
3489
+					size = sizeof(struct aaaa_rdata);
3490
+					break;
3491
+				case T_SRV:
3492
+					size = sizeof(struct srv_rdata)-1 +
3493
+						rr_name.len+1;
3494
+					break;
3495
+				}
3496
+				new = dns_cache_clone_entry(old, size, ttl, &rr);
3497
+				if (!new) {
3498
+					rpc->fault(ctx, 400, "Failed to add the entry to the cache");
3499
+					goto error;
3500
+				}
3501
+			
3502
+				switch(type) {
3503
+				case T_A:
3504
+				case T_AAAA:
3505
+					memcpy(rr->rdata, ip_addr->u.addr, ip_addr->len);
3506
+					break;
3507
+				case T_SRV:
3508
+					((struct srv_rdata*)rr->rdata)->priority = priority;
3509
+					((struct srv_rdata*)rr->rdata)->weight = weight;
3510
+					((struct srv_rdata*)rr->rdata)->port = port;
3511
+					((struct srv_rdata*)rr->rdata)->name_len = rr_name.len;
3512
+					memcpy(((struct srv_rdata*)rr->rdata)->name, rr_name.s, rr_name.len);
3513
+				}
3514
+				/* maximum expire value has been already fixed by dns_cache_clone_entry() */
3515
+			}
3516
+		}
3517
+	}
3518
+	
3519
+	LOCK_DNS_HASH();
3520
+	if (dns_cache_add_unsafe(new)) {
3521
+		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
3522
+		UNLOCK_DNS_HASH();
3523
+		goto error;
3524
+	} else {
3525
+		/* remove the old entry from the list */
3526
+		if (old)
3527
+			_dns_hash_remove(old);
3528
+	}
3529
+	UNLOCK_DNS_HASH();
3530
+
3531
+	if (old)
3532
+		dns_hash_put(old);
3533
+	return;
3534
+
3535
+error:
3536
+	/* leave the old entry in the list, and free the new one */
3537
+	if (old)
3538
+		dns_hash_put(old);
3539
+	if (new)
3540
+		dns_destroy_entry(new);
3541
+}
3542
+
3543
+/* deletes a record from the cache */
3544
+static void dns_cache_delete_record(rpc_t* rpc, void* ctx, unsigned short type)
3545
+{
3546
+	struct dns_hash_entry *e;
3547
+	str name;
3548
+	int err, h, found=0;
3549
+
3550
+	if (rpc->scan(ctx, "S", &name) < 1)
3551
+		return;
3552
+
3553
+	LOCK_DNS_HASH();
3554
+
3555
+	e=_dns_hash_find(&name, type, &h, &err);
3556
+	if (e && (e->type==type)) {
3557
+		_dns_hash_remove(e);
3558
+		found = 1;
3559
+	}
3560
+
3561
+	UNLOCK_DNS_HASH();
3562
+
3563
+	if (!found)
3564
+		rpc->fault(ctx, 400, "Not found");
3565
+}
3566
+
3567
+/* wrapper functions for adding and deleting records */
3568
+void dns_cache_add_a(rpc_t* rpc, void* ctx)
3569
+{
3570
+	dns_cache_add_record(rpc, ctx, T_A);
3571
+}
3572
+
3573
+void dns_cache_add_aaaa(rpc_t* rpc, void* ctx)
3574
+{
3575
+	dns_cache_add_record(rpc, ctx, T_AAAA);
3576
+}
3577
+
3578
+void dns_cache_add_srv(rpc_t* rpc, void* ctx)
3579
+{
3580
+	dns_cache_add_record(rpc, ctx, T_SRV);
3581
+}
3582
+
3583
+void dns_cache_delete_a(rpc_t* rpc, void* ctx)
3584
+{
3585
+	dns_cache_delete_record(rpc, ctx, T_A);
3586
+}
3587
+
3588
+void dns_cache_delete_aaaa(rpc_t* rpc, void* ctx)
3589
+{
3590
+	dns_cache_delete_record(rpc, ctx, T_AAAA);
3591
+}
3592
+
3593
+void dns_cache_delete_srv(rpc_t* rpc, void* ctx)
3594
+{
3595
+	dns_cache_delete_record(rpc, ctx, T_SRV);
3596
+}
3597
+
3598
+
3599
+
3101 3600
 #ifdef DNS_WATCHDOG_SUPPORT
3102 3601
 /* sets the DNS server states */
3103 3602
 void dns_set_server_state_rpc(rpc_t* rpc, void* ctx)
... ...
@@ -313,6 +313,9 @@ inline static int dns_sip_resolve2su(struct dns_srv_handle* h,
313 313
 	return ret;
314 314
 }
315 315
 
316
+/* deletes all the entries from the cache */
317
+void dns_cache_flush(void);
318
+
316 319
 #ifdef DNS_WATCHDOG_SUPPORT
317 320
 /* sets the state of the DNS servers:
318 321
  * 1: at least one server is up