1 | /*
|
---|
2 | * Copyright (c) 2013 Jiri Svoboda
|
---|
3 | * All rights reserved.
|
---|
4 | *
|
---|
5 | * Redistribution and use in source and binary forms, with or without
|
---|
6 | * modification, are permitted provided that the following conditions
|
---|
7 | * are met:
|
---|
8 | *
|
---|
9 | * - Redistributions of source code must retain the above copyright
|
---|
10 | * notice, this list of conditions and the following disclaimer.
|
---|
11 | * - Redistributions in binary form must reproduce the above copyright
|
---|
12 | * notice, this list of conditions and the following disclaimer in the
|
---|
13 | * documentation and/or other materials provided with the distribution.
|
---|
14 | * - The name of the author may not be used to endorse or promote products
|
---|
15 | * derived from this software without specific prior written permission.
|
---|
16 | *
|
---|
17 | * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
|
---|
18 | * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
|
---|
19 | * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
|
---|
20 | * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
|
---|
21 | * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
|
---|
22 | * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
---|
23 | * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
---|
24 | * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
---|
25 | * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
|
---|
26 | * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
---|
27 | */
|
---|
28 |
|
---|
29 | /** @addtogroup dhcp
|
---|
30 | * @{
|
---|
31 | */
|
---|
32 | /**
|
---|
33 | * @file
|
---|
34 | * @brief DHCP client
|
---|
35 | */
|
---|
36 |
|
---|
37 | #include <bitops.h>
|
---|
38 | #include <inet/addr.h>
|
---|
39 | #include <inet/dnsr.h>
|
---|
40 | #include <inet/inetcfg.h>
|
---|
41 | #include <io/log.h>
|
---|
42 | #include <loc.h>
|
---|
43 | #include <net/in.h>
|
---|
44 | #include <net/inet.h>
|
---|
45 | #include <net/socket.h>
|
---|
46 | #include <stdio.h>
|
---|
47 | #include <stdlib.h>
|
---|
48 |
|
---|
49 | #include "dhcp.h"
|
---|
50 | #include "dhcp_std.h"
|
---|
51 | #include "transport.h"
|
---|
52 |
|
---|
53 | #define MAX_MSG_SIZE 1024
|
---|
54 | static uint8_t msgbuf[MAX_MSG_SIZE];
|
---|
55 |
|
---|
56 | typedef struct {
|
---|
57 | /** Message type */
|
---|
58 | enum dhcp_msg_type msg_type;
|
---|
59 | /** Offered address */
|
---|
60 | inet_naddr_t oaddr;
|
---|
61 | /** Server address */
|
---|
62 | inet_addr_t srv_addr;
|
---|
63 | /** Router address */
|
---|
64 | inet_addr_t router;
|
---|
65 | /** DNS server */
|
---|
66 | inet_addr_t dns_server;
|
---|
67 | } dhcp_offer_t;
|
---|
68 |
|
---|
69 | static int dhcp_recv_fibril(void *);
|
---|
70 |
|
---|
71 | int dhcp_send(dhcp_transport_t *dt, void *msg, size_t size)
|
---|
72 | {
|
---|
73 | struct sockaddr_in addr;
|
---|
74 | int rc;
|
---|
75 |
|
---|
76 | addr.sin_family = AF_INET;
|
---|
77 | addr.sin_port = htons(dhcp_server_port);
|
---|
78 | addr.sin_addr.s_addr = htonl(addr32_broadcast_all_hosts);
|
---|
79 |
|
---|
80 | rc = sendto(dt->fd, msg, size, 0,
|
---|
81 | (struct sockaddr *)&addr, sizeof(addr));
|
---|
82 | if (rc != EOK) {
|
---|
83 | log_msg(LOG_DEFAULT, LVL_ERROR, "Sending failed");
|
---|
84 | return rc;
|
---|
85 | }
|
---|
86 |
|
---|
87 | return EOK;
|
---|
88 | }
|
---|
89 |
|
---|
90 | static int dhcp_recv_msg(dhcp_transport_t *dt, void **rmsg, size_t *rsize)
|
---|
91 | {
|
---|
92 | struct sockaddr_in src_addr;
|
---|
93 | socklen_t src_addr_size;
|
---|
94 | size_t recv_size;
|
---|
95 | int rc;
|
---|
96 |
|
---|
97 | src_addr_size = sizeof(src_addr);
|
---|
98 | rc = recvfrom(dt->fd, msgbuf, MAX_MSG_SIZE, 0,
|
---|
99 | (struct sockaddr *)&src_addr, &src_addr_size);
|
---|
100 | if (rc < 0) {
|
---|
101 | log_msg(LOG_DEFAULT, LVL_ERROR, "recvfrom failed (%d)", rc);
|
---|
102 | return rc;
|
---|
103 | }
|
---|
104 |
|
---|
105 | recv_size = (size_t)rc;
|
---|
106 | *rmsg = msgbuf;
|
---|
107 | *rsize = recv_size;
|
---|
108 |
|
---|
109 | return EOK;
|
---|
110 | }
|
---|
111 |
|
---|
112 | int dhcp_transport_init(dhcp_transport_t *dt, service_id_t link_id,
|
---|
113 | dhcp_recv_cb_t recv_cb, void *arg)
|
---|
114 | {
|
---|
115 | int fd;
|
---|
116 | struct sockaddr_in laddr;
|
---|
117 | int fid;
|
---|
118 | int rc;
|
---|
119 |
|
---|
120 | log_msg(LOG_DEFAULT, LVL_DEBUG, "dhcptransport_init()");
|
---|
121 |
|
---|
122 | laddr.sin_family = AF_INET;
|
---|
123 | laddr.sin_port = htons(dhcp_client_port);
|
---|
124 | laddr.sin_addr.s_addr = INADDR_ANY;
|
---|
125 |
|
---|
126 | fd = socket(PF_INET, SOCK_DGRAM, IPPROTO_UDP);
|
---|
127 | if (fd < 0) {
|
---|
128 | rc = EIO;
|
---|
129 | goto error;
|
---|
130 | }
|
---|
131 |
|
---|
132 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Bind socket.");
|
---|
133 | rc = bind(fd, (struct sockaddr *)&laddr, sizeof(laddr));
|
---|
134 | if (rc != EOK) {
|
---|
135 | rc = EIO;
|
---|
136 | goto error;
|
---|
137 | }
|
---|
138 |
|
---|
139 | log_msg(LOG_DEFAULT, LVL_DEBUG, "Set socket options");
|
---|
140 | rc = setsockopt(fd, SOL_SOCKET, SO_IPLINK, &link_id, sizeof(link_id));
|
---|
141 | if (rc != EOK) {
|
---|
142 | rc = EIO;
|
---|
143 | goto error;
|
---|
144 | }
|
---|
145 |
|
---|
146 | dt->fd = fd;
|
---|
147 | dt->recv_cb = recv_cb;
|
---|
148 | dt->cb_arg = arg;
|
---|
149 |
|
---|
150 | fid = fibril_create(dhcp_recv_fibril, dt);
|
---|
151 | if (fid == 0) {
|
---|
152 | rc = ENOMEM;
|
---|
153 | goto error;
|
---|
154 | }
|
---|
155 |
|
---|
156 | dt->recv_fid = fid;
|
---|
157 | fibril_add_ready(fid);
|
---|
158 |
|
---|
159 | return EOK;
|
---|
160 | error:
|
---|
161 | closesocket(fd);
|
---|
162 | return rc;
|
---|
163 | }
|
---|
164 |
|
---|
165 | void dhcp_transport_fini(dhcp_transport_t *dt)
|
---|
166 | {
|
---|
167 | closesocket(dt->fd);
|
---|
168 | }
|
---|
169 |
|
---|
170 | static int dhcp_recv_fibril(void *arg)
|
---|
171 | {
|
---|
172 | dhcp_transport_t *dt = (dhcp_transport_t *)arg;
|
---|
173 | void *msg;
|
---|
174 | size_t size = (size_t) -1;
|
---|
175 | int rc;
|
---|
176 |
|
---|
177 | while (true) {
|
---|
178 | rc = dhcp_recv_msg(dt, &msg, &size);
|
---|
179 | if (rc != EOK)
|
---|
180 | break;
|
---|
181 |
|
---|
182 | assert(size != (size_t) -1);
|
---|
183 |
|
---|
184 | dt->recv_cb(dt->cb_arg, msg, size);
|
---|
185 | }
|
---|
186 |
|
---|
187 | return EOK;
|
---|
188 | }
|
---|
189 |
|
---|
190 | /** @}
|
---|
191 | */
|
---|