1
0
mirror of https://github.com/systemd/systemd.git synced 2024-12-22 17:35:35 +03:00

sd-dhcp: Add support to emit and retrieve SMTP server

This commit is contained in:
Susant Sahani 2020-03-23 10:39:37 +01:00
parent 69d06b7acf
commit 2c649ca1c9
8 changed files with 71 additions and 2 deletions

View File

View File

@ -64,6 +64,9 @@ struct sd_dhcp_lease {
struct in_addr *pop3_server;
size_t pop3_server_size;
struct in_addr *smtp_server;
size_t smtp_server_size;
struct sd_dhcp_route *static_route;
size_t static_route_size, static_route_allocated;

View File

@ -55,8 +55,8 @@ struct sd_dhcp_server {
char *timezone;
struct in_addr *ntp, *dns, *sip, *pop3_server;
unsigned n_ntp, n_dns, n_sip, n_pop3_server;
struct in_addr *ntp, *dns, *sip, *pop3_server, *smtp_server;
unsigned n_ntp, n_dns, n_sip, n_pop3_server, n_smtp_server;
OrderedHashmap *extra_options;
OrderedHashmap *vendor_options;

View File

@ -140,6 +140,17 @@ int sd_dhcp_lease_get_pop3_server(sd_dhcp_lease *lease, const struct in_addr **a
return (int) lease->pop3_server_size;
}
int sd_dhcp_lease_get_smtp_server(sd_dhcp_lease *lease, const struct in_addr **addr) {
assert_return(lease, -EINVAL);
assert_return(addr, -EINVAL);
if (lease->smtp_server_size <= 0)
return -ENODATA;
*addr = lease->smtp_server;
return (int) lease->smtp_server_size;
}
int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname) {
assert_return(lease, -EINVAL);
assert_return(domainname, -EINVAL);
@ -291,6 +302,7 @@ static sd_dhcp_lease *dhcp_lease_free(sd_dhcp_lease *lease) {
free(lease->ntp);
free(lease->sip);
free(lease->pop3_server);
free(lease->smtp_server);
free(lease->static_route);
free(lease->client_id);
free(lease->vendor_specific);
@ -619,6 +631,12 @@ int dhcp_lease_parse_options(uint8_t code, uint8_t len, const void *option, void
log_debug_errno(r, "Failed to parse POP3 server, ignoring: %m");
break;
case SD_DHCP_OPTION_SMTP_SERVER:
r = lease_parse_in_addrs(option, len, &lease->smtp_server, &lease->smtp_server_size);
if (r < 0)
log_debug_errno(r, "Failed to parse SMTP server, ignoring: %m");
break;
case SD_DHCP_OPTION_STATIC_ROUTE:
r = lease_parse_routes(option, len, &lease->static_route, &lease->static_route_size, &lease->static_route_allocated);
if (r < 0)
@ -1056,6 +1074,7 @@ int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
*ntp = NULL,
*sip = NULL,
*pop3_server = NULL,
*smtp_server = NULL,
*mtu = NULL,
*routes = NULL,
*domains = NULL,
@ -1086,6 +1105,7 @@ int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
"NTP", &ntp,
"SIP", &sip,
"POP3_SERVERS", &pop3_server,
"SMTP_SERVERS", &smtp_server,
"MTU", &mtu,
"DOMAINNAME", &lease->domainname,
"HOSTNAME", &lease->hostname,
@ -1206,6 +1226,14 @@ int dhcp_lease_load(sd_dhcp_lease **ret, const char *lease_file) {
lease->pop3_server_size = r;
}
if (smtp_server) {
r = deserialize_in_addrs(&lease->smtp_server, smtp_server);
if (r < 0)
log_debug_errno(r, "Failed to deserialize SMTP server %s, ignoring: %m", smtp_server);
else
lease->smtp_server_size = r;
}
if (mtu) {
r = safe_atou16(mtu, &lease->mtu);
if (r < 0)

View File

@ -141,6 +141,7 @@ static sd_dhcp_server *dhcp_server_free(sd_dhcp_server *server) {
free(server->ntp);
free(server->sip);
free(server->pop3_server);
free(server->smtp_server);
hashmap_free(server->leases_by_client_id);
@ -523,6 +524,15 @@ static int server_send_ack(sd_dhcp_server *server, DHCPRequest *req,
return r;
}
if (server->n_smtp_server > 0) {
r = dhcp_option_append(
&packet->dhcp, req->max_optlen, &offset, 0,
SD_DHCP_OPTION_SMTP_SERVER,
sizeof(struct in_addr) * server->n_smtp_server, server->smtp_server);
if (r < 0)
return r;
}
if (server->timezone) {
r = dhcp_option_append(
&packet->dhcp, req->max_optlen, &offset, 0,
@ -1216,6 +1226,31 @@ int sd_dhcp_server_set_pop3_server(sd_dhcp_server *server, const struct in_addr
return 1;
}
int sd_dhcp_server_set_smtp_server(sd_dhcp_server *server, const struct in_addr smtp_server[], unsigned n) {
assert_return(server, -EINVAL);
assert_return(smtp_server || n <= 0, -EINVAL);
if (server->n_smtp_server == n &&
memcmp(server->smtp_server, smtp_server, sizeof(struct in_addr) * n) == 0)
return 0;
if (n <= 0) {
server->smtp_server = mfree(server->smtp_server);
server->n_smtp_server = 0;
} else {
struct in_addr *c;
c = newdup(struct in_addr, smtp_server, n);
if (!c)
return -ENOMEM;
free_and_replace(server->smtp_server, c);
server->n_smtp_server = n;
}
return 1;
}
int sd_dhcp_server_set_emit_router(sd_dhcp_server *server, int enabled) {
assert_return(server, -EINVAL);

View File

@ -83,6 +83,7 @@ enum {
SD_DHCP_OPTION_REBINDING_T2_TIME = 59,
SD_DHCP_OPTION_VENDOR_CLASS_IDENTIFIER = 60,
SD_DHCP_OPTION_CLIENT_IDENTIFIER = 61,
SD_DHCP_OPTION_SMTP_SERVER = 69,
SD_DHCP_OPTION_POP3_SERVER = 70,
SD_DHCP_OPTION_USER_CLASS = 77,
SD_DHCP_OPTION_FQDN = 81,

View File

@ -46,6 +46,7 @@ int sd_dhcp_lease_get_dns(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_ntp(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_sip(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_pop3_server(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_smtp_server(sd_dhcp_lease *lease, const struct in_addr **addr);
int sd_dhcp_lease_get_mtu(sd_dhcp_lease *lease, uint16_t *mtu);
int sd_dhcp_lease_get_domainname(sd_dhcp_lease *lease, const char **domainname);
int sd_dhcp_lease_get_search_domains(sd_dhcp_lease *lease, char ***domains);

View File

@ -51,6 +51,7 @@ int sd_dhcp_server_set_dns(sd_dhcp_server *server, const struct in_addr dns[], u
int sd_dhcp_server_set_ntp(sd_dhcp_server *server, const struct in_addr ntp[], unsigned n);
int sd_dhcp_server_set_sip(sd_dhcp_server *server, const struct in_addr sip[], unsigned n);
int sd_dhcp_server_set_pop3_server(sd_dhcp_server *server, const struct in_addr pop3_server[], unsigned n);
int sd_dhcp_server_set_smtp_server(sd_dhcp_server *server, const struct in_addr smtp_server[], unsigned n);
int sd_dhcp_server_set_emit_router(sd_dhcp_server *server, int enabled);
int sd_dhcp_server_add_option(sd_dhcp_server *server, sd_dhcp_option *v);