示例#1
0
char * get_network_setting(const char *config)
{
  char * ret = 0;
  struct mode_list_elem *data;

  if(!strcmp(config, NETWORK_IP_KEY))
  {
	ret = get_network_ip();
	if(!ret)
		ret = strdup("192.168.2.15");
  }
  else if(!strcmp(config, NETWORK_INTERFACE_KEY))
  {
	data = get_usb_mode_data();
	if(data)
	{
		if(data->network_interface)
		{
			ret = strdup(data->network_interface);
			goto end;
		}
	}
	ret = get_network_interface();
	if(!ret)
		ret = strdup("usb0");
  }
  else if(!strcmp(config, NETWORK_GATEWAY_KEY))
	return(get_network_gateway());
  else
	/* no matching keys, return error */
	return(NULL);
end:
   return(ret);
}
示例#2
0
/**
 * Activate the network interface
 *
 */
int usb_network_up(struct mode_list_elem *data)
{
  char *ip = NULL, *gateway = NULL;
  int ret = -1;

#if CONNMAN_WORKS_BETTER
  DBusConnection *dbus_conn_connman = NULL;
  DBusMessage *msg = NULL, *reply = NULL;
  DBusMessageIter iter, variant, dict;
  DBusMessageIter msg_iter;
  DBusMessageIter dict_entry;
  DBusError error;
  const char *service = NULL;

  /* make sure connman will recognize the gadget interface NEEDED? */
  //system("/bin/dbus-send --print-reply --type=method_call --system --dest=net.connman /net/connman/technology/gadget net.connman.Technology.SetProperty string:Powered variant:boolean:true");
  //system("/sbin/ifconfig rndis0 up");

  log_debug("waiting for connman to pick up interface\n");
  sleep(1);
  dbus_error_init(&error);

  if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
  {
         log_err("Could not connect to dbus for connman\n");
  }

  /* get list of services so we can find out which one is the usb gadget */
  if ((msg = dbus_message_new_method_call("net.connman", "/", "net.connman.Manager", "GetServices")) != NULL)
  {
        if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
        {
            service = connman_parse_manager_reply(reply, "gadget");
            dbus_message_unref(reply);
        }
        dbus_message_unref(msg);
  }

  if(service == NULL)
	return(1);
  log_debug("gadget = %s\n", service);

  /* now we need to configure the connection */
  if ((msg = dbus_message_new_method_call("net.connman", service, "net.connman.Service", "SetProperty")) != NULL)
  {
	log_debug("iter init\n");
	dbus_message_iter_init_append(msg, &msg_iter);
	log_debug("iter append\n");
	// TODO: crashes here, need to rework this whole bit, connman dbus is hell
	dbus_message_iter_append_basic(&msg_iter, DBUS_TYPE_STRING, "IPv4.Configuration");
	log_debug("iter open container\n");
	dbus_message_iter_open_container(&msg_iter, DBUS_TYPE_VARIANT,
			DBUS_TYPE_ARRAY_AS_STRING
				DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
					DBUS_TYPE_STRING_AS_STRING
					DBUS_TYPE_VARIANT_AS_STRING
				DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
			&variant);

	log_debug("iter open container 2\n");
	dbus_message_iter_open_container(&variant, DBUS_TYPE_ARRAY,
			DBUS_DICT_ENTRY_BEGIN_CHAR_AS_STRING
				DBUS_TYPE_STRING_AS_STRING
				DBUS_TYPE_VARIANT_AS_STRING
			DBUS_DICT_ENTRY_END_CHAR_AS_STRING,
			&dict);

	log_debug("Set Method\n");
	dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
	append_variant(&dict_entry, "Method", DBUS_TYPE_STRING, "manual");
	dbus_message_iter_close_container(&dict, &dict_entry);

	log_debug("Set ip\n");
	ip = get_network_ip();
	if(ip == NULL)
		ip = strdup("192.168.2.15");
	dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
	append_variant(&dict_entry, "Address", DBUS_TYPE_STRING, ip);
	dbus_message_iter_close_container(&dict, &dict_entry);

	log_debug("Set netmask\n");
	dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
	append_variant(&dict_entry, "Netmask", DBUS_TYPE_STRING, "255.255.255.0");
	dbus_message_iter_close_container(&dict, &dict_entry);

	log_debug("set gateway\n");
	gateway = get_network_gateway();
	if(gateway)
	{
		dbus_message_iter_open_container(&dict, DBUS_TYPE_DICT_ENTRY, NULL, &dict_entry);
		append_variant(&dict_entry, "Gateway", DBUS_TYPE_STRING, gateway);
		dbus_message_iter_close_container(&dict, &dict_entry);
	}
	dbus_message_iter_close_container(&variant, &dict);
	dbus_message_iter_close_container(&msg_iter, &variant);
  }

  log_debug("Connect gadget\n");
  /* Finally we can bring it up */
  if ((msg = dbus_message_new_method_call("net.connman", service, "net.connman.Service", "Connect")) != NULL)
  {
        /* we don't care for the reply, which is empty anyway if all goes well */
        ret = !dbus_connection_send(dbus_conn_connman, msg, NULL);
        /* make sure the message is sent before cleaning up and closing the connection */
        dbus_connection_flush(dbus_conn_connman);
        dbus_message_unref(msg);
  }
  dbus_connection_unref(dbus_conn_connman);
  dbus_error_free(&error);
  free(service);
  if(ip)
	free(ip);
  if(gateway)
	free(gateway);
  return(ret);

#else
  char command[128];
  const char *interface;

  interface = get_interface(data); 
  ip = get_network_ip();
  gateway = get_network_gateway();

  if(ip == NULL)
  {
	sprintf(command,"ifconfig %s 192.168.2.15", interface);
	system(command);
	goto clean;
  }
  else if(!strcmp(ip, "dhcp"))
  {
	sprintf(command, "dhclient -d %s\n", interface);
	ret = system(command);
	if(ret != 0)
	{	
		sprintf(command, "udhcpc -i %s\n", interface);
		system(command);
	}

  }
  else
  {
	sprintf(command, "ifconfig %s %s\n", interface, ip);
	system(command);
  }

  /* TODO: Check first if there is a gateway set */
  if(gateway)
  {
	sprintf(command, "route add default gw %s\n", gateway);
        system(command);
  }

clean:
  free((char *)interface);
  free((char *)gateway);
  free((char *)ip);

  return(0);
#endif /* CONNMAN */
}
示例#3
0
/**
 * Activate the network interface
 *
 */
int usb_network_up(struct mode_list_elem *data)
{
  const char *ip, *interface, *gateway;
  char command[128];

#if CONNMAN
  DBusConnection *dbus_conn_connman = NULL;
  DBusMessage *msg = NULL, *reply = NULL;
  DBusError error;

  dbus_error_init(&error);

  if( (dbus_conn_connman = dbus_bus_get(DBUS_BUS_SYSTEM, &error)) == 0 )
  {
         log_err("Could not connect to dbus for connman\n");
  }

  if ((msg = dbus_message_new_method_call("net.connman", "/", "net.connman.Service", connect)) != NULL)
  {
        if ((reply = dbus_connection_send_with_reply_and_block(dbus_conn_connman, msg, -1, NULL)) != NULL)
        {
            dbus_message_get_args(reply, NULL, DBUS_TYPE_INT32, &ret, DBUS_TYPE_INVALID);
            dbus_message_unref(reply);
        }
        dbus_message_unref(msg);
  }
  dbus_connection_unref(dbus_conn_connman);

  log_debug("connman state = %d\n", ret);
  return(ret);

#else

  interface = get_interface(data); 
  ip = get_network_ip();
  gateway = get_network_gateway();

  if(ip == NULL)
  {
	sprintf(command,"ifconfig %s 192.168.2.15", interface);
	system(command);
	goto clean;
  }
  else if(!strcmp(ip, "dhcp"))
  {
	sprintf(command, "dhclient -d %s\n", interface);
	system(command);
  }
  else
  {
	sprintf(command, "ifconfig %s %s\n", interface, ip);
	system(command);
  }

  /* TODO: Check first if there is a gateway set */
  if(gateway)
  {
	sprintf(command, "route add default gw %s\n", gateway);
        system(command);
  }

clean:
  free((char *)interface);
  free((char *)gateway);
  free((char *)ip);

  return(0);
#endif /* CONNMAN */
}
示例#4
0
/** 
  * Write udhcpd.conf
  * @ipforward : NULL if we want a simple config, otherwise include dns info etc...
  */
static int write_udhcpd_conf(struct ipforward_data *ipforward, struct mode_list_elem *data)
{
  FILE *conffile;
  char *ip, *interface;
  char *ipstart, *ipend;
  int dot = 0, i = 0, test;
  struct stat st;

  /* /tmp is often tmpfs, so we avoid writing to flash */
  conffile = fopen("/tmp/udhcpd.conf", "w");
  if(conffile == NULL)
  {
	log_debug("Error creating /etc/udhcpd.conf!\n");
	return(1);
  }

  /* generate start and end ip based on the setting */
  ip = get_network_ip();
  if(ip == NULL)
  {
	ip = strdup("192.168.2.15");
  }
  ipstart = malloc(sizeof(char)*15);
  ipend = malloc(sizeof(char)*15);
  while(i < 15)
  {
        if(dot < 3)
        {
                if(ip[i] == '.')
                        dot ++;
                ipstart[i] = ip[i];
                ipend[i] = ip[i];
        }
        else
        {
                ipstart[i] = '\0';
                ipend[i] = '\0';
                break;
        }
        i++;
  }
  strcat(ipstart,"1");
  strcat(ipend, "10");

  interface = get_interface(data);
  /* print all data in the file */
  fprintf(conffile, "start\t%s\n", ipstart);
  fprintf(conffile, "end\t%s\n", ipend);
  fprintf(conffile, "interface\t%s\n", interface);
  fprintf(conffile, "option\tsubnet\t255.255.255.0\n");
  if(ipforward != NULL)
  {
	if(!ipforward->dns1 || !ipforward->dns2)
	{
		log_debug("No dns info!");
	}
	else
		fprintf(conffile, "opt\tdns\t%s %s\n", ipforward->dns1, ipforward->dns2);
	fprintf(conffile, "opt\trouter\t%s\n", ip);
  }

  free(ipstart);
  free(ipend);
  free(ip);
  free(interface);
  fclose(conffile);
  log_debug("/etc/udhcpd.conf written.\n");

  /* check if it is a symlink, if not remove and link, create the link if missing */
  test = stat("/etc/udhcpd.conf", &st);
  /* if stat fails there is no file or link */
  if(test == -1)
	goto link;
  /* if it is not a link we remove it, else we expect the right link to be there */
  if((st.st_mode & S_IFMT) != S_IFLNK)
  {
	unlink("/etc/udhcpd.conf");
  }
  else
	goto end;

link:
  symlink("/tmp/udhcpd.conf", "/etc/udhcpd.conf");

end:

  return(0);
}