chiark / gitweb /
sd-dhcp: network - don't hardcode ports
authorTom Gundersen <teg@jklm.no>
Fri, 14 Feb 2014 16:23:58 +0000 (17:23 +0100)
committerTom Gundersen <teg@jklm.no>
Sat, 15 Feb 2014 11:14:45 +0000 (12:14 +0100)
We want to reuse these functions for the server library too.

src/libsystemd-dhcp/dhcp-internal.h
src/libsystemd-dhcp/dhcp-network.c
src/libsystemd-dhcp/sd-dhcp-client.c
src/libsystemd-dhcp/test-dhcp-client.c

index 3f1e000..7639069 100644 (file)
 #include "dhcp-protocol.h"
 
 int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link);
-int dhcp_network_bind_udp_socket(int index, be32_t client_address);
+int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port);
 int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
                                  const void *packet, size_t len);
-int dhcp_network_send_udp_socket(int s, be32_t server_address,
+int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
                                  const void *packet, size_t len);
 
 int dhcp_option_append(uint8_t **buf, size_t *buflen, uint8_t code,
index b2de67e..6c9d4ef 100644 (file)
@@ -56,13 +56,13 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
         return s;
 }
 
-int dhcp_network_bind_udp_socket(int index, be32_t client_address)
+int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
 {
         int s;
         union sockaddr_union src = {
                 .in.sin_family = AF_INET,
-                .in.sin_port = htobe16(DHCP_PORT_CLIENT),
-                .in.sin_addr.s_addr = client_address,
+                .in.sin_port = htobe16(port),
+                .in.sin_addr.s_addr = address,
         };
 
         s = socket(AF_INET, SOCK_DGRAM | SOCK_CLOEXEC | SOCK_NONBLOCK, 0);
@@ -90,13 +90,13 @@ int dhcp_network_send_raw_socket(int s, const union sockaddr_union *link,
         return 0;
 }
 
-int dhcp_network_send_udp_socket(int s, be32_t server_address,
+int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
                                  const void *packet, size_t len)
 {
         union sockaddr_union dest = {
                 .in.sin_family = AF_INET,
-                .in.sin_port = htobe16(DHCP_PORT_SERVER),
-                .in.sin_addr.s_addr = server_address,
+                .in.sin_port = htobe16(port),
+                .in.sin_addr.s_addr = address,
         };
 
         if (sendto(s, packet, len, 0, &dest.sa, sizeof(dest.in)) < 0)
index bff3588..83d56a2 100644 (file)
@@ -324,6 +324,7 @@ static int client_send_request(sd_dhcp_client *client, uint16_t secs) {
         if (client->state == DHCP_STATE_RENEWING) {
                 err = dhcp_network_send_udp_socket(client->fd,
                                                    client->lease->server_address,
+                                                   DHCP_PORT_SERVER,
                                                    &request->dhcp,
                                                    len - DHCP_IP_UDP_SIZE);
         } else {
@@ -535,7 +536,8 @@ static int client_timeout_t1(sd_event_source *s, uint64_t usec,
         client->attempt = 1;
 
         r = dhcp_network_bind_udp_socket(client->index,
-                                         client->lease->address);
+                                         client->lease->address,
+                                         DHCP_PORT_CLIENT);
         if (r < 0) {
                 client_stop(client, r);
                 return 0;
index 56a10b3..f341b3c 100644 (file)
@@ -191,12 +191,12 @@ int dhcp_network_bind_raw_socket(int index, union sockaddr_union *link)
         return test_fd[0];
 }
 
-int dhcp_network_bind_udp_socket(int index, be32_t client_address)
+int dhcp_network_bind_udp_socket(int index, be32_t address, uint16_t port)
 {
         return 0;
 }
 
-int dhcp_network_send_udp_socket(int s, be32_t server_address,
+int dhcp_network_send_udp_socket(int s, be32_t address, uint16_t port,
                                  const void *packet, size_t len)
 {
         return 0;