char *cmd_up(void *self, char *argv[], int argc) { co_cmd_t *this = self; unsigned char mac[6]; memset(mac, '\0', sizeof(mac)); char address[16]; memset(address, '\0', sizeof(address)); char *ret; if(argc < 2) { return this->usage; } co_iface_t *iface = co_iface_add(argv[0], AF_INET); DEBUG("Bringing up iface %s", argv[0]); CHECK(iface != NULL, "Failed to create interface %s.", argv[0]); nodeid_t id = co_id_get(); if(!id.id && co_iface_get_mac(iface, mac)) { //print_mac(mac); co_id_set_from_mac(mac); } co_profile_t *prof = co_profile_find(argv[1]); CHECK(prof != NULL, "Failed to load profile %s.", argv[1]); #ifndef _OPENWRT co_profile_dump(prof); if(!strcmp("true", co_profile_get_string(prof, "ipgenerate", "true"))) { co_generate_ip(co_profile_get_string(prof, "ip", "5.0.0.0"), co_profile_get_string(prof, "ipgeneratemask", co_profile_get_string(prof, "netmask", "255.0.0") ), co_id_get(), address, 0); } DEBUG("Address: %s", address); if(iface->wireless && co_iface_wpa_connect(iface)) { co_iface_set_ssid(iface, co_profile_get_string(prof, "ssid", "\"commotionwireless.net\"")); co_iface_set_bssid(iface, co_profile_get_string(prof, "bssid", "02:CA:FF:EE:BA:BE")); co_iface_set_frequency(iface, wifi_freq(co_profile_get_int(prof, "channel", 5))); co_iface_set_mode(iface, co_profile_get_string(prof, "mode", "\"adhoc\"")); co_iface_set_apscan(iface, 0); co_iface_wireless_enable(iface); } co_set_dns(co_profile_get_string(prof, "dns", "8.8.8.8"), co_profile_get_string(prof, "domain", "mesh.local"), "/tmp/resolv.commotion"); co_iface_set_ip(iface, address, co_profile_get_string(prof, "netmask", "255.0.0.0")); #endif iface->profile = strdup(argv[1]); ret = strdup("Interface up.\n"); return ret; error: free(iface); ret = strdup("Interface up failed.\n"); return ret; }
char *cmd_nodeid(void *self, char *argv[], int argc) { char *ret = malloc(11); memset(ret, '\0', 11); nodeid_t id = co_id_get(); snprintf(ret, 11, "%u", ntohl(id.id)); INFO("Node ID: %u", ntohl(id.id)); return ret; }
int main(int argc, char *argv[]) { int opt = 0; int opt_index = 0; int daemonize = 1; int newid = 0; char *pidfile = COMMOTION_PIDFILE; char *statedir = COMMOTION_STATEDIR; char *socket_uri = COMMOTION_MANAGESOCK; //char *plugindir = COMMOTION_PLUGINDIR; char *profiledir = COMMOTION_PROFILEDIR; static const char *opt_string = "b:d:f:i:np:s:h"; static struct option long_opts[] = { {"bind", required_argument, NULL, 'b'}, {"plugins", required_argument, NULL, 'd'}, {"profiles", required_argument, NULL, 'f'}, {"nodeid", required_argument, NULL, 'i'}, {"nodaemon", no_argument, NULL, 'n'}, {"pid", required_argument, NULL, 'p'}, {"statedir", required_argument, NULL, 's'}, {"help", no_argument, NULL, 'h'} }; opt = getopt_long(argc, argv, opt_string, long_opts, &opt_index); while(opt != -1) { switch(opt) { case 'b': socket_uri = optarg; break; case 'd': //plugindir = optarg; break; case 'f': profiledir = optarg; break; case 'i': newid = atoi(optarg); break; case 'n': daemonize = 0; break; case 'p': pidfile = optarg; break; case 's': statedir = optarg; break; case 'h': default: print_usage(); return 0; break; } opt = getopt_long(argc, argv, opt_string, long_opts, &opt_index); } if(daemonize) daemon_start((char *)statedir, (char *)pidfile); co_id_set_from_int(newid); nodeid_t id = co_id_get(); DEBUG("Node ID: %d", (int) id.id); co_loop_create(); co_ifaces_create(); co_profiles_create(); co_profile_import_files(profiledir); co_cmd_add("help", cmd_help, "help <none>\n", "Print list of commands and usage information.\n", 0); co_cmd_add("list_profiles", cmd_list_profiles, "list_profiles <none>\n", "Print list of available profiles.\n", 0); co_cmd_add("up", cmd_up, "up <interface> <profile>\n", "Apply profile to interface.\n", 0); co_cmd_add("down", cmd_down, "down <interface>\n", "Bring specified interface down.\n", 0); co_cmd_add("status", cmd_status, "status <interface>\n", "Report profile of connected interface.\n", 0); co_cmd_add("state", cmd_state, "state <interface> <property>\n", "Report properties of connected interface.\n", 0); co_cmd_add("nodeid", cmd_nodeid, "nodeid <none>\n", "Print unique ID for this node\n", 0); co_cmd_add("nodeidset", cmd_set_nodeid_from_mac, "nodeid <mac>\n", "Use mac address to generate identifier for this node.\n", 0); //plugins_create(); //plugins_load_all(plugindir); co_socket_t *socket = NEW(co_socket, unix_socket); socket->poll_cb = dispatcher_cb; socket->register_cb = co_loop_add_socket; socket->bind(socket, socket_uri); co_loop_start(); co_loop_destroy(); return 0; }
char *cmd_state(void *self, char *argv[], int argc) { co_cmd_t *this = self; char *ret = NULL; char mac[6]; memset(mac, '\0', sizeof(mac)); char address[16]; memset(address, '\0', sizeof(address)); if(argc < 2) { return ret = strdup(this->usage); } char *profile_name = NULL; CHECK((profile_name = co_iface_profile(argv[0])), "Interface state is inactive."); DEBUG("profile_name: %s", profile_name); co_profile_t *prof = NULL; CHECK((prof = co_profile_find(profile_name)), "Could not load profile."); if(!strcmp(argv[1], "ssid")) { ret = co_profile_get_string(prof, "ssid", "commotionwireless.net"); } else if(!strcmp(argv[1], "bssid")) { ret = co_profile_get_string(prof, "bssid", "02:CA:FF:EE:BA:BE"); } else if(!strcmp(argv[1], "channel")) { ret = co_profile_get_string(prof, "channel", "5"); } else if(!strcmp(argv[1], "type")) { ret = co_profile_get_string(prof, "type", "mesh"); } else if(!strcmp(argv[1], "dns")) { ret = co_profile_get_string(prof, "dns", "8.8.8.8"); } else if(!strcmp(argv[1], "domain")) { ret = co_profile_get_string(prof, "domain", "mesh.local"); } else if(!strcmp(argv[1], "ipgenerate")) { ret = co_profile_get_string(prof, "ipgenerate", "true"); } else if(!strcmp(argv[1], "mode")) { ret = co_profile_get_string(prof, "mode", "adhoc"); } else if(!strcmp(argv[1], "netmask")) { ret = co_profile_get_string(prof, "netmask", "255.0.0.0"); } else if(!strcmp(argv[1], "wpa")) { ret = co_profile_get_string(prof, "wpa", "false"); } else if(!strcmp(argv[1], "wpakey")) { ret = co_profile_get_string(prof, "wpakey", "c0MM0t10n!r0cks"); } else if(!strcmp(argv[1], "servald")) { ret = co_profile_get_string(prof, "servald", "false"); } else if(!strcmp(argv[1], "servaldsid")) { ret = co_profile_get_string(prof, "servaldsid", ""); } else if(!strcmp(argv[1], "announce")) { ret = co_profile_get_string(prof, "announce", "true"); } else if(!strcmp(argv[1], "ip")) { if(!strcmp(co_profile_get_string(prof, "ipgenerate", "true"), "true")) { if(!strcmp(co_profile_get_string(prof, "type", "mesh"), "mesh")) { co_generate_ip(co_profile_get_string(prof, "ip", "5.0.0.0"), co_profile_get_string(prof, "ipgeneratemask", co_profile_get_string(prof, "netmask", "255.0.0")), co_id_get(), address, 0); } else { co_generate_ip(co_profile_get_string(prof, "ip", "5.0.0.0"), co_profile_get_string(prof, "ipgeneratemask", co_profile_get_string(prof, "netmask", "255.0.0")), co_id_get(), address, 1); } ret = address; } else { ret = co_profile_get_string(prof, "ip", "5.0.0.0"); } } else ret = NULL; if(ret) { return ret = strdup(ret); } else return ret = strdup("Failed to get variable state.\n"); error: return ret = strdup("Failed to get interface or profile.\n"); }