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 4086
 	/* eliminate gcc warnings */
4084 4087
 	ip_addr = 0;
4085 4088
 	size = 0;
4089
+	rr_name.s = NULL;
4090
+	rr_name.len = 0;
4086 4091
 
4087 4092
 	if (!cfg_get(core, core_cfg, use_dns_cache)){
4088
-		rpc->fault(ctx, 500, "dns cache support disabled (see use_dns_cache)");
4089
-		return;
4093
+		LOG(L_ERR, "ERROR: dns cache support disabled (see use_dns_cache)\n");
4094
+		return -1;
4090 4095
 	}
4091 4096
 	
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;
4097
+	if ((type != T_A) && (type != T_AAAA) && (type != T_SRV)) {
4098
+		LOG(L_ERR, "ERROR: rr type %d is not implemented\n",
4099
+			type);
4100
+		return -1;
4113 4101
 	}
4114 4102
 
4115 4103
 	if (!flags) {
4116 4104
 		/* fix-up the values */
4117 4105
 		switch(type) {
4118 4106
 		case T_A:
4119
-			ip_addr = str2ip(&ip);
4107
+			ip_addr = str2ip(value);
4120 4108
 			if (!ip_addr) {
4121
-				rpc->fault(ctx, 400, "Malformed ip address");
4122
-				goto error;
4109
+				LOG(L_ERR, "ERROR: Malformed ip address: %.*s\n",
4110
+					value->len, value->s);
4111
+				return -1;
4123 4112
 			}
4124 4113
 			break;
4125 4114
 		case T_AAAA:
4126 4115
 #ifdef USE_IPV6
4127
-			ip_addr = str2ip6(&ip);
4116
+			ip_addr = str2ip6(value);
4128 4117
 			if (!ip_addr) {
4129
-				rpc->fault(ctx, 400, "Malformed ip address");
4130
-				goto error;
4118
+				LOG(L_ERR, "ERROR: Malformed ip address: %.*s\n",
4119
+					value->len, value->s);
4120
+				return -1;
4131 4121
 			}
4132 4122
 			break;
4133 4123
 #else /* USE_IPV6 */
4134
-			rpc->fault(ctx, 400, "IPv6 support disabled ");
4135
-			return;
4124
+			LOG(L_ERR, "ERROR: IPv6 support is disabled\n");
4125
+			return -1;
4136 4126
 #endif /* USE_IPV6 */
4137
-		/* case T_SRV: nothing to do */
4127
+		case T_SRV:
4128
+			rr_name = *value;
4129
+			break;
4138 4130
 		}
4139 4131
 	}
4140 4132
 
4141 4133
 	/* check whether there is a matching entry in the cache */
4142
-	old = dns_hash_get(&name, type, &h, &err);
4134
+	old = dns_hash_get(name, type, &h, &err);
4143 4135
 	if (old && old->type!=type) {
4144 4136
 		/* probably we found a CNAME instead of the specified type,
4145 4137
 		it is not needed */
... ...
@@ -4150,9 +4142,10 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4150 4142
 	/* prepare the entry */
4151 4143
 	if (flags) {
4152 4144
 		/* negative entry */
4153
-		new = dns_cache_mk_bad_entry(&name, type, ttl, flags);
4145
+		new = dns_cache_mk_bad_entry(name, type, ttl, flags);
4154 4146
 		if (!new) {
4155
-			rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4147
+			LOG(L_ERR, "ERROR: Failed to create a negative "
4148
+					"DNS cache entry\n");
4156 4149
 			goto error;
4157 4150
 		}
4158 4151
 	} else {
... ...
@@ -4163,10 +4156,9 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4163 4156
 			switch(type) {
4164 4157
 			case T_A:
4165 4158
 			case T_AAAA:
4166
-				new = dns_cache_mk_ip_entry(&name, ip_addr);
4159
+				new = dns_cache_mk_ip_entry(name, ip_addr);
4167 4160
 				if (!new) {
4168
-					rpc->fault(ctx, 400, "Failed to add the entry to"
4169
-										" the cache");
4161
+					LOG(L_ERR, "ERROR: Failed to create an A/AAAA record\n");
4170 4162
 					goto error;
4171 4163
 				}
4172 4164
 				/* 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 4168
 				new->rr_lst->expire = expire;
4177 4169
 				break;
4178 4170
 			case T_SRV:
4179
-				new = dns_cache_mk_srv_entry(&name, priority, weight, port,
4171
+				new = dns_cache_mk_srv_entry(name, priority, weight, port,
4180 4172
 												&rr_name, ttl);
4181 4173
 				if (!new) {
4182
-					rpc->fault(ctx, 400, "Failed to add the entry to"
4183
-											" the cache");
4174
+					LOG(L_ERR, "ERROR: Failed to create an SRV record\n");
4184 4175
 					goto error;
4185 4176
 				}
4186 4177
 			}
... ...
@@ -4206,8 +4197,8 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4206 4197
 				/* the rr was found in the list */
4207 4198
 				new = dns_cache_clone_entry(old, 0, 0, 0);
4208 4199
 				if (!new) {
4209
-					rpc->fault(ctx, 400, "Failed to add the entry to "
4210
-											"the cache");
4200
+					LOG(L_ERR, "ERROR: Failed to clone an existing "
4201
+							"DNS cache entry\n");
4211 4202
 					goto error;
4212 4203
 				}
4213 4204
 				/* 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 4234
 				}
4244 4235
 				new = dns_cache_clone_entry(old, size, ttl, &rr);
4245 4236
 				if (!new) {
4246
-					rpc->fault(ctx, 400, "Failed to add the entry to"
4247
-											" the cache");
4237
+					LOG(L_ERR, "ERROR: Failed to clone an existing "
4238
+							"DNS cache entry\n");
4248 4239
 					goto error;
4249 4240
 				}
4250 4241
 
... ...
@@ -4269,7 +4260,7 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4269 4260
 
4270 4261
 	LOCK_DNS_HASH();
4271 4262
 	if (dns_cache_add_unsafe(new)) {
4272
-		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4263
+		LOG(L_ERR, "ERROR: Failed to add the entry to the cache\n");
4273 4264
 		UNLOCK_DNS_HASH();
4274 4265
 		goto error;
4275 4266
 	} else {
... ...
@@ -4281,7 +4272,7 @@ static void dns_cache_add_record(rpc_t* rpc, void* ctx, unsigned short type)
4281 4272
 
4282 4273
 	if (old)
4283 4274
 		dns_hash_put(old);
4284
-	return;
4275
+	return 0;
4285 4276
 
4286 4277
 error:
4287 4278
 	/* leave the old entry in the list, and free the new one */
... ...
@@ -4289,6 +4280,7 @@ error:
4289 4280
 		dns_hash_put(old);
4290 4281
 	if (new)
4291 4282
 		dns_destroy_entry(new);
4283
+	return -1;
4292 4284
 }
4293 4285
 
4294 4286
 
... ...
@@ -4356,22 +4348,76 @@ void dns_cache_rpc_lookup(rpc_t* rpc, void* ctx)
4356 4348
 /* wrapper functions for adding and deleting records */
4357 4349
 void dns_cache_add_a(rpc_t* rpc, void* ctx)
4358 4350
 {
4359
-	dns_cache_add_record(rpc, ctx, T_A);
4351
+	str	name;
4352
+	int	ttl;
4353
+	str	ip;
4354
+	int	flags;
4355
+
4356
+	if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
4357
+		return;
4358
+
4359
+	if (dns_cache_add_record(T_A,
4360
+				&name,
4361
+				ttl,
4362
+				&ip,
4363
+				0 /* priority */,
4364
+				0 /* weight */,
4365
+				0 /* port */,
4366
+				flags)
4367
+	)
4368
+		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4360 4369
 }
4361 4370
 
4362 4371
 
4363 4372
 void dns_cache_add_aaaa(rpc_t* rpc, void* ctx)
4364 4373
 {
4365
-	dns_cache_add_record(rpc, ctx, T_AAAA);
4366
-}
4374
+	str	name;
4375
+	int	ttl;
4376
+	str	ip;
4377
+	int	flags;
4378
+
4379
+	if (rpc->scan(ctx, "SdSd", &name, &ttl, &ip, &flags) < 4)
4380
+		return;
4367 4381
 
4382
+	if (dns_cache_add_record(T_AAAA,
4383
+				&name,
4384
+				ttl,
4385
+				&ip,
4386
+				0 /* priority */,
4387
+				0 /* weight */,
4388
+				0 /* port */,
4389
+				flags)
4390
+	)
4391
+		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4392
+}
4368 4393
 
4369 4394
 void dns_cache_add_srv(rpc_t* rpc, void* ctx)
4370 4395
 {
4371
-	dns_cache_add_record(rpc, ctx, T_SRV);
4396
+	str	name;
4397
+	int	ttl, priority, weight, port;
4398
+	str	rr_name;
4399
+	int	flags;
4400
+
4401
+	if (rpc->scan(ctx, "SddddSd", &name, &ttl, &priority, &weight, &port,
4402
+					&rr_name, &flags) < 7
4403
+	)
4404
+		return;
4405
+
4406
+	if (dns_cache_add_record(T_SRV,
4407
+				&name,
4408
+				ttl,
4409
+				&rr_name,
4410
+				priority,
4411
+				weight,
4412
+				port,
4413
+				flags)
4414
+	)
4415
+		rpc->fault(ctx, 400, "Failed to add the entry to the cache");
4372 4416
 }
4373 4417
 
4374 4418
 
4419
+
4420
+
4375 4421
 void dns_cache_delete_a(rpc_t* rpc, void* ctx)
4376 4422
 {
4377 4423
 	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