@@ -232,6 +232,47 @@ grisp_init_network_ifconfig_lo0(void)
232232 assert (exit_code == EX_OK );
233233}
234234
235+ // Gets the start position of value from "key=value" string
236+ char *
237+ get_env_value (char * const env ) {
238+ unsigned pos = 0 ;
239+ for (unsigned i = 0 ; env [i ] != '\0' ; i ++ ) {
240+ if (env [i ] == '=' ) pos = i ;
241+ }
242+ assert (pos != 0 );
243+ return env + ++ pos * sizeof (char );
244+ }
245+
246+ void
247+ write_resolv_conf (char * dns_ips ) {
248+ FILE * file = fopen ("/etc/resolv.conf" , "w" );
249+ if (file == NULL ) {
250+ perror ("Error opening /etc/resolv.conf" );
251+ } else {
252+ fprintf (file , "domain grisp.local\n" );
253+ char * token = strtok (dns_ips , " " );
254+ while (token != NULL ) {
255+ fprintf (file , "nameserver %s\n" , token );
256+ token = strtok (NULL , " " );
257+ }
258+ fclose (file );
259+ }
260+ }
261+
262+ void
263+ grisp_dhcpcd_hook_handler (rtems_dhcpcd_hook * hook , char * const * env ) {
264+ (void )hook ;
265+ while (* env != NULL ) {
266+ if (strstr (* env , "new_domain_name_servers" ) != NULL ) {
267+ char * ip_list_ptr = get_env_value (* env );
268+ char * dns_ips = strdup (ip_list_ptr );
269+ write_resolv_conf (dns_ips );
270+ free (dns_ips );
271+ }
272+ ++ env ;
273+ }
274+ }
275+
235276void
236277grisp_init_dhcpcd_with_config (rtems_task_priority prio , const char * conf )
237278{
@@ -254,6 +295,12 @@ grisp_init_dhcpcd_with_config(rtems_task_priority prio, const char *conf)
254295
255296 config .priority = prio ;
256297
298+ static rtems_dhcpcd_hook dhcpcd_hook = {
299+ .name = "grisp_dhcp_handler" ,
300+ .handler = grisp_dhcpcd_hook_handler
301+ };
302+ rtems_dhcpcd_add_hook (& dhcpcd_hook );
303+
257304 rtems_dhcpcd_start (& config );
258305}
259306
0 commit comments