cnetworkmanager

libnm - What are the necessary steps to create a hotspot?


I want to create a hotspot network using libnm in C. I tried following the following steps:

  1. Connect to NetworkManager using nm_client_new(NULL, NULL);
  2. Get the wlan device nm_client_get_device_by_iface(client, "wlan0");
  3. Create the connection nm_simple_connection_new():
  4. nm_setting_connection_new() with g_object_set("type", "802-11-wireless", "uuid", HOTSPOT_UUID, "id", "MyHotspot", "autoconnect", FALSE); and added to connection
  5. nm_setting_wireless_new() with mode=adhoc, ssid=MyHotspotSSID
  6. nm_setting_ip4_config_new() with method=shared
  7. nm_setting_ip6_config_new() with method=ignore
  8. nm_setting_wireless_security_new() with key-mgmt=none, wep-key0=key_str, wep-key-type=NM_WEP_KEY_TYPE_KEY
  9. nm_client_add_and_activate_connection_async(client, connection, wlan_device, NULL, NULL, on_connection_added, loop);, loop being a GMainLoop.
  10. g_main_loop_run(loop);

After those steps are done, it seemingly finishes instantly, but for some reason, there is no new connection and is not activated. Callback of nm_client_add_and_activate_connection_async never getting called. I also tried without wep-key0 or the loop, just waiting for the callback, none of which worked.

Am I missing some steps? What am I doing wrong?

Edit: In the on_connection_added, I am calling nm_client_add_and_activate_connection_finish(NM_CLIENT(source_object), res, &error) but may not matter since the callback is never reached. I also call g_main_loop_quit(user_data); after the _finish function.


Solution

  • I have found out the issue, I did take the correct steps, but the actual code was unreachable. This took a genuinly long timr to figure out what was wrong. Sorry for spamming the forum!