Browse code

dns_cahce: add_record function exported

dns_cache_add_record() function has been separated
from the RPC interface so that it can be used also
from other places.

Miklos Tirpak authored on 09/11/2009 15:35:29
Showing 2 changed files
... ...
@@ -4066,16 +4066,19 @@ static struct dns_hash_entry *dns_cache_clone_entry(struct dns_hash_entry *e,
4066 4066
  *
4067 4067
  * Currently only A, AAAA, and SRV records are supported.
4068 4068
  */
4069
-static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4069
+int dns_cache_add_record(unsigned short type,
4070
+			str *name,
4071
+			int ttl,
4072
+			str *value,
4073
+			int priority,
4074
+			int weight,
4075
+			int port,
4076
+			int flags)
4070 4077
 {
4071 4078
 	struct dns_hash_entry *old=NULL, *new=NULL;
4072 4079
 	struct dns_rr *rr;
4073
-	str name;
4074
-	int ttl;
4075
-	str ip, rr_name;
4076
-	int flags;
4080
+	str rr_name;
4077 4081
 	struct ip_addr *ip_addr;
4078
-	int priority, weight, port;
4079 4082
 	ticks_t expire;
4080 4083
 	int err, h;
4081 4084
 	int size;
... ...
@@ -4083,63 +4086,52 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4083 4083
 	/* eliminate gcc warnings */
4084 4084
 	ip_addr = 0;
4085 4085
 	size = 0;
4086
+	rr_name.s = NULL;
4087
+	rr_name.len = 0;
4086 4088
 
4087 4089
 	if (!cfg_get(core, core_cfg, use_dns_cache)){
4088
-		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
4089
-		return;
4090
+		LOG(L_ERR, "ERROR: dns cache support disabled (see use_dns_cache)\n");
4091
+		return -1;
4090 4092
 	}
4091 4093
 	
4092
-	switch(type) {
4093
-	case T_A:
4094
-	case T_AAAA:
4095
-		if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
4096
-			return;
4097
-		break;
4098
-	case T_SRV:
4099
-		if (rpc->scan(ctx, "SddddSd", &name, &ttl, &priority, &weight, &port,
4100
-							&rr_name, &flags) < 7)
4101
-			return;
4102
-		break;
4103
-	case T_CNAME:
4104
-	case T_NAPTR:
4105
-	case T_TXT:
4106
-	case T_EBL:
4107
-	case T_PTR:
4108
-		rpc->fault(ctx, 400, "not implemented");
4109
-		return;
4110
-	default:
4111
-		rpc->fault(ctx, 400, "unknown type");
4112
-		return;
4094
+	if ((type != T_A) && (type != T_AAAA) && (type != T_SRV)) {
4095
+		LOG(L_ERR, "ERROR: rr type %d is not implemented\n",
4096
+			type);
4097
+		return -1;
4113 4098
 	}
4114 4099
 
4115 4100
 	if (!flags) {
4116 4101
 		/* fix-up the values */
4117 4102
 		switch(type) {
4118 4103
 		case T_A:
4119
-			ip_addr = str2ip(&ip);
4104
+			ip_addr = str2ip(value);
4120 4105
 			if (!ip_addr) {
4121
-				rpc->fault(ctx, 400, "Malformed ip address");
4122
-				goto error;
4106
+				LOG(L_ERR, "ERROR: Malformed ip address: %.*s\n",
4107
+					value->len, value->s);
4108
+				return -1;
4123 4109
 			}
4124 4110
 			break;
4125 4111
 		case T_AAAA:
4126 4112
 #ifdef USE_IPV6
4127
-			ip_addr = str2ip6(&ip);
4113
+			ip_addr = str2ip6(value);
4128 4114
 			if (!ip_addr) {
4129
-				rpc->fault(ctx, 400, "Malformed ip address");
4130
-				goto error;
4115
+				LOG(L_ERR, "ERROR: Malformed ip address: %.*s\n",
4116
+					value->len, value->s);
4117
+				return -1;
4131 4118
 			}
4132 4119
 			break;
4133 4120
 #else /* USE_IPV6 */
4134
-			rpc->fault(ctx, 400, "IPv6 support disabled ");
4135
-			return;
4121
+			LOG(L_ERR, "ERROR: IPv6 support is disabled\n");
4122
+			return -1;
4136 4123
 #endif /* USE_IPV6 */
4137
-		/* case T_SRV: nothing to do */
4124
+		case T_SRV:
4125
+			rr_name = *value;
4126
+			break;
4138 4127
 		}
4139 4128
 	}
4140 4129
 
4141 4130
 	/* check whether there is a matching entry in the cache */
4142
-	old = dns_hash_get(&name, type, &h, &err);
4131
+	old = dns_hash_get(name, type, &h, &err);
4143 4132
 	if (old && old->type!=type) {
4144 4133
 		/* probably we found a CNAME instead of the specified type,
4145 4134
 		it is not needed */
... ...
@@ -4150,9 +4142,10 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4150 4150
 	/* prepare the entry */
4151 4151
 	if (flags) {
4152 4152
 		/* negative entry */
4153
-		new = dns_cache_mk_bad_entry(&name, type, ttl, flags);
4153
+		new = dns_cache_mk_bad_entry(name, type, ttl, flags);
4154 4154
 		if (!new) {
4155
-			rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4155
+			LOG(L_ERR, "ERROR: Failed to create a negative "
4156
+					"DNS cache entry\n");
4156 4157
 			goto error;
4157 4158
 		}
4158 4159
 	} else {
... ...
@@ -4163,10 +4156,9 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4163 4163
 			switch(type) {
4164 4164
 			case T_A:
4165 4165
 			case T_AAAA:
4166
-				new = dns_cache_mk_ip_entry(&name, ip_addr);
4166
+				new = dns_cache_mk_ip_entry(name, ip_addr);
4167 4167
 				if (!new) {
4168
-					rpc->fault(ctx, 400, "Failed to add the entry to"
4169
-										" the cache");
4168
+					LOG(L_ERR, "ERROR: Failed to create an A/AAAA record\n");
4170 4169
 					goto error;
4171 4170
 				}
4172 4171
 				/* fix the expiration time, dns_cache_mk_ip_entry() sets it 
... ...
@@ -4176,11 +4168,10 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4176 4176
 				new->rr_lst->expire = expire;
4177 4177
 				break;
4178 4178
 			case T_SRV:
4179
-				new = dns_cache_mk_srv_entry(&name, priority, weight, port,
4179
+				new = dns_cache_mk_srv_entry(name, priority, weight, port,
4180 4180
 												&rr_name, ttl);
4181 4181
 				if (!new) {
4182
-					rpc->fault(ctx, 400, "Failed to add the entry to"
4183
-											" the cache");
4182
+					LOG(L_ERR, "ERROR: Failed to create an SRV record\n");
4184 4183
 					goto error;
4185 4184
 				}
4186 4185
 			}
... ...
@@ -4206,8 +4197,8 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4206 4206
 				/* the rr was found in the list */
4207 4207
 				new = dns_cache_clone_entry(old, 0, 0, 0);
4208 4208
 				if (!new) {
4209
-					rpc->fault(ctx, 400, "Failed to add the entry to "
4210
-											"the cache");
4209
+					LOG(L_ERR, "ERROR: Failed to clone an existing "
4210
+							"DNS cache entry\n");
4211 4211
 					goto error;
4212 4212
 				}
4213 4213
 				/* let the rr point to the new structure */
... ...
@@ -4243,8 +4234,8 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4243 4243
 				}
4244 4244
 				new = dns_cache_clone_entry(old, size, ttl, &rr);
4245 4245
 				if (!new) {
4246
-					rpc->fault(ctx, 400, "Failed to add the entry to"
4247
-											" the cache");
4246
+					LOG(L_ERR, "ERROR: Failed to clone an existing "
4247
+							"DNS cache entry\n");
4248 4248
 					goto error;
4249 4249
 				}
4250 4250
 
... ...
@@ -4269,7 +4260,7 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4269 4269
 
4270 4270
 	LOCK_DNS_HASH();
4271 4271
 	if (dns_cache_add_unsafe(new)) {
4272
-		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4272
+		LOG(L_ERR, "ERROR: Failed to add the entry to the cache\n");
4273 4273
 		UNLOCK_DNS_HASH();
4274 4274
 		goto error;
4275 4275
 	} else {
... ...
@@ -4281,7 +4272,7 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4281 4281
 
4282 4282
 	if (old)
4283 4283
 		dns_hash_put(old);
4284
-	return;
4284
+	return 0;
4285 4285
 
4286 4286
 error:
4287 4287
 	/* leave the old entry in the list, and free the new one */
... ...
@@ -4289,6 +4280,7 @@ error:
4289 4289
 		dns_hash_put(old);
4290 4290
 	if (new)
4291 4291
 		dns_destroy_entry(new);
4292
+	return -1;
4292 4293
 }
4293 4294
 
4294 4295
 
... ...
@@ -4356,22 +4348,76 @@ void dns_cache_rpc_lookup(rpc_t* rpc, void* ctx)
4356 4356
 /* wrapper functions for adding and deleting records */
4357 4357
 void dns_cache_add_a(rpc_t* rpc, void* ctx)
4358 4358
 {
4359
-	dns_cache_add_record(rpc, ctx, T_A);
4359
+	str	name;
4360
+	int	ttl;
4361
+	str	ip;
4362
+	int	flags;
4363
+
4364
+	if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
4365
+		return;
4366
+
4367
+	if (dns_cache_add_record(T_A,
4368
+				&name,
4369
+				ttl,
4370
+				&ip,
4371
+				0 /* priority */,
4372
+				0 /* weight */,
4373
+				0 /* port */,
4374
+				flags)
4375
+	)
4376
+		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4360 4377
 }
4361 4378
 
4362 4379
 
4363 4380
 void dns_cache_add_aaaa(rpc_t* rpc, void* ctx)
4364 4381
 {
4365
-	dns_cache_add_record(rpc, ctx, T_AAAA);
4366
-}
4382
+	str	name;
4383
+	int	ttl;
4384
+	str	ip;
4385
+	int	flags;
4386
+
4387
+	if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
4388
+		return;
4367 4389
 
4390
+	if (dns_cache_add_record(T_AAAA,
4391
+				&name,
4392
+				ttl,
4393
+				&ip,
4394
+				0 /* priority */,
4395
+				0 /* weight */,
4396
+				0 /* port */,
4397
+				flags)
4398
+	)
4399
+		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4400
+}
4368 4401
 
4369 4402
 void dns_cache_add_srv(rpc_t* rpc, void* ctx)
4370 4403
 {
4371
-	dns_cache_add_record(rpc, ctx, T_SRV);
4404
+	str	name;
4405
+	int	ttl, priority, weight, port;
4406
+	str	rr_name;
4407
+	int	flags;
4408
+
4409
+	if (rpc->scan(ctx, "SddddSd", &name, &ttl, &priority, &weight, &port,
4410
+					&rr_name, &flags) < 7
4411
+	)
4412
+		return;
4413
+
4414
+	if (dns_cache_add_record(T_SRV,
4415
+				&name,
4416
+				ttl,
4417
+				&rr_name,
4418
+				priority,
4419
+				weight,
4420
+				port,
4421
+				flags)
4422
+	)
4423
+		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4372 4424
 }
4373 4425
 
4374 4426
 
4427
+
4428
+
4375 4429
 void dns_cache_delete_a(rpc_t* rpc, void* ctx)
4376 4430
 {
4377 4431
 	dns_cache_delete_record(rpc, ctx, T_A);
... ...
@@ -350,4 +350,21 @@ void dns_set_server_state(int state);
350 350
 int dns_get_server_state(void);
351 351
 #endif /* DNS_WATCHDOG_SUPPORT */
352 352
 
353
+/* Adds a new record to the cache.
354
+ * If there is an existing record with the same name and value
355
+ * (ip address in case of A/AAAA record, name in case of SRV record)
356
+ * only the remaining fields are updated.
357
+ *
358
+ * Currently only A, AAAA, and SRV records are supported.
359
+ */
360
+int dns_cache_add_record(unsigned short type,
361
+			str *name,
362
+			int ttl,
363
+			str *value,
364
+			int priority,
365
+			int weight,
366
+			int port,
367
+			int flags);
368
+
369
+
353 370
 #endif