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

Wow. You developed a programming language for handling network connections; that is actually really neat. It's way past my bedtime, so I might've missed it, but is there any way in NCD to detect, say, a new wired connection, and then a timeout waiting for dhcp, and then as a fallback, configure the interface with a static ip?

I'm not sure what I think about having to edit a file every time I want to connect to a new network, but on the other hand, it would be super nice not to have to reboot every N times I switch network connections.



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.


It seems I missed the part where you asked about detecting new connections. That's possible too :) http://code.google.com/p/badvpn/wiki/NCD#Handling_events_wit...

But then be careful with your dependency names, because they are global. You can prefix them with the interface name, e.g.: concat(dev, "-DHCP") dhcp_name; .




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

Search: