/*************************************************************************
 *
 * Copyright (C) 2018-2025 Ruilin Peng (Nick) <pymumu@gmail.com>.
 *
 * smartdns is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * smartdns is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include "srv_record.h"
#include "set_file.h"
#include "smartdns/lib/stringutil.h"
#include "smartdns/util.h"
#include "domain_rule.h"


static int _confg_srv_record_add(const char *domain, const char *host, unsigned short priority, unsigned short weight,
								 unsigned short port)
{
	struct dns_srv_record_rule *srv_rule = NULL;
	struct dns_srv_record *srv_record = NULL;
	int is_new = 0;
	enum domain_rule type = DOMAIN_RULE_SRV;

	srv_rule = dns_conf_get_domain_rule(domain, type);
	if (srv_rule == NULL) {
		srv_rule = _new_dns_rule(type);
		if (srv_rule == NULL) {
			goto errout;
		}
		INIT_LIST_HEAD(&srv_rule->record_list);
		is_new = 1;
	}

	srv_record = zalloc(1, sizeof(*srv_record));
	if (srv_record == NULL) {
		goto errout;
	}
	safe_strncpy(srv_record->host, host, DNS_MAX_CONF_CNAME_LEN);
	srv_record->priority = priority;
	srv_record->weight = weight;
	srv_record->port = port;
	list_add_tail(&srv_record->list, &srv_rule->record_list);

	if (is_new) {
		if (_config_domain_rule_add(domain, type, srv_rule) != 0) {
			goto errout;
		}
		_dns_rule_put(&srv_rule->head);
		srv_rule = NULL;
	}

	return 0;
errout:
	if (is_new && srv_rule != NULL) {
		_dns_rule_put(&srv_rule->head);
	}
	if (srv_record && srv_record->list.next == NULL) {
		free(srv_record);
	}
	return -1;
}

int _config_srv_record(void *data, int argc, char *argv[])
{
	char *value = NULL;
	char domain[DNS_MAX_CONF_CNAME_LEN];
	char buff[DNS_MAX_CONF_CNAME_LEN];
	char *ptr = NULL;
	int ret = -1;

	char *host_s;
	char *priority_s;
	char *weight_s;
	char *port_s;

	unsigned short priority = 0;
	unsigned short weight = 0;
	unsigned short port = 1;

	if (argc < 2) {
		goto errout;
	}

	value = argv[1];
	if (_get_domain(value, domain, DNS_MAX_CONF_CNAME_LEN, &value) != 0) {
		goto errout;
	}

	safe_strncpy(buff, value, sizeof(buff));

	host_s = strtok_r(buff, ",", &ptr);
	if (host_s == NULL) {
		host_s = "";
		goto out;
	}

	port_s = strtok_r(NULL, ",", &ptr);
	if (port_s != NULL) {
		port = atoi(port_s);
	}

	priority_s = strtok_r(NULL, ",", &ptr);
	if (priority_s != NULL) {
		priority = atoi(priority_s);
	}

	weight_s = strtok_r(NULL, ",", &ptr);
	if (weight_s != NULL) {
		weight = atoi(weight_s);
	}
out:
	ret = _confg_srv_record_add(domain, host_s, priority, weight, port);
	if (ret != 0) {
		goto errout;
	}

	return 0;

errout:
	tlog(TLOG_ERROR, "add srv-record %s:%s failed", domain, value);
	return -1;
}
