Hacker Newsnew | past | comments | ask | show | jobs | submitlogin

It sure it possible. Seems like the perfect case for the multiprovide() module (CTRL+F on wiki for more info, and also the in-source documentation http://code.google.com/p/badvpn/source/browse/trunk/ncd/modu... ).

  process lan {
      # Set device.
      var("eth0") dev;
  
      # Wait for device, set it up, and wait for network cable.
      net.backend.waitdevice(dev);
      net.up(dev);
      net.backend.waitlink(dev);
  
      # Start doing DHCP and waiting for timeout. This spawns two additional
      # processes to run concurrently (see their code below).
      process_manager() mgr;
      mgr->start("dhcp", "conf_dhcp_template", {dev});
      mgr->start("static", "conf_static_template", {dev});
  
      # Wait for either multiprovide("LAN-DHCP") or multiprovide("LAN-STATIC"),
      # but always prefer LAN-DHCP. Even if we've already grabbed LAN-STATIC
      # and LAN-DHCP comes up, we backtrack and switch to LAN-DHCP.
      multidepend({"LAN-DHCP", "LAN-STATIC"}) conf_dep;
  
      # We can reach objects as seen from multiprovide() by going
      # through our multidepend() statement.
      println("got ", conf_dep.result_type);
      rprintln("lost ", conf_dep.result_type);
  
      # Check IP address - make sure it's not local.
      ip_in_network(conf_dep.addr, "127.0.0.0", "8") test_local;
      ifnot(test_local);
  
      # Assign IP address.
      net.ipv4.addr(dev, conf_dep.addr, conf_dep.addr_prefix);
  
      # Add default route.
      net.ipv4.route("0.0.0.0", "0", conf_dep.gateway, "20", dev);
  
      # Configure DNS servers.
      net.dns(conf_dep.dns_servers, "20");
  }
  
  template conf_dhcp_template {
      # First argument is device name.
      var(_arg0) dev;
  
      # Do DHCP.
      net.ipv4.dhcp(dev) dhcp;
  
      # Expose results.
      var("dhcp") result_type;
      var(dhcp.addr) addr;
      var(dhcp.prefix) addr_prefix;
      var(dhcp.gateway) gateway;
      var(dhcp.dns_servers) dns_servers;
  
      multiprovide("LAN-DHCP");
  }
  
  template conf_static_template {
      # First argument is device name.
      var(_arg0) dev;
  
      # Wait 5 seconds.
      sleep("5000", "0");
  
      # Expose results.
      var("static") result_type;
      var("192.168.111.230") addr;
      var("24") addr_prefix;
      var("192.168.111.1") gateway;
      var({"192.168.111.14"}) dns_servers;
  
      multiprovide("LAN-STATIC");
  }


Awesome, thank you. I think I'll be trying this out tomorrow.




Consider applying for YC's Fall 2026 batch! Applications are open till July 27.

Guidelines | FAQ | Lists | API | Security | Legal | Apply to YC | Contact

Search: