Ejemplo n.º 1
0
static PyObject *
Util_func_interface_addresses(PyObject *obj)
{
    int i, count;
    char ip[INET6_ADDRSTRLEN];
    uv_interface_address_t* interfaces;
    uv_err_t err;
    PyObject *result, *item, *exc_data;

    UNUSED_ARG(obj);

    err = uv_interface_addresses(&interfaces, &count);
    if (err.code == UV_OK) {
        result = PyList_New(0);
        if (!result) {
            uv_free_interface_addresses(interfaces, count);
            PyErr_NoMemory();
            return NULL;
        }
        for (i = 0; i < count; i++) {
            item = PyDict_New();
            if (!item)
                continue;
            PyDict_SetItemString(item, "name", PyString_FromString(interfaces[i].name));
            PyDict_SetItemString(item, "is_internal", PyBool_FromLong((long)interfaces[i].is_internal));
            if (interfaces[i].address.address4.sin_family == AF_INET) {
                uv_ip4_name(&interfaces[i].address.address4, ip, INET_ADDRSTRLEN);
                PyDict_SetItemString(item, "address", PyString_FromString(ip));
            } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
                uv_ip6_name(&interfaces[i].address.address6, ip, INET6_ADDRSTRLEN);
                PyDict_SetItemString(item, "address", PyString_FromString(ip));
            }
            if (PyList_Append(result, item))
                continue;
            Py_DECREF(item);
        }
        uv_free_interface_addresses(interfaces, count);
        return result;
    } else {
        exc_data = Py_BuildValue("(is)", err.code, uv_strerror(err));
        if (exc_data != NULL) {
            PyErr_SetObject(PyExc_UVError, exc_data);
            Py_DECREF(exc_data);
        }
        return NULL;
    }
}
Ejemplo n.º 2
0
static PyObject *
Util_func_interface_addresses(PyObject *obj)
{
    int i, count;
    char ip[INET6_ADDRSTRLEN];
    uv_interface_address_t* interfaces;
    uv_err_t err;
    PyObject *result, *item, *exc_data;

    UNUSED_ARG(obj);

    err = uv_interface_addresses(&interfaces, &count);
    if (err.code == UV_OK) {
        result = PyList_New(count);
        if (!result) {
            uv_free_interface_addresses(interfaces, count);
            return NULL;
        }
        for (i = 0; i < count; i++) {
            item = PyStructSequence_New(&InterfaceAddressesResultType);
            if (!item) {
                Py_DECREF(result);
                uv_free_interface_addresses(interfaces, count);
                return NULL;
            }
            PyStructSequence_SET_ITEM(item, 0, Py_BuildValue("s", interfaces[i].name));
            PyStructSequence_SET_ITEM(item, 1, PyBool_FromLong((long)interfaces[i].is_internal));
            if (interfaces[i].address.address4.sin_family == AF_INET) {
                uv_ip4_name(&interfaces[i].address.address4, ip, INET_ADDRSTRLEN);
            } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
                uv_ip6_name(&interfaces[i].address.address6, ip, INET6_ADDRSTRLEN);
            }
            PyStructSequence_SET_ITEM(item, 2, Py_BuildValue("s", ip));
            PyList_SET_ITEM(result, i, item);
        }
        uv_free_interface_addresses(interfaces, count);
        return result;
    } else {
        exc_data = Py_BuildValue("(is)", err.code, uv_strerror(err));
        if (exc_data != NULL) {
            PyErr_SetObject(PyExc_UVError, exc_data);
            Py_DECREF(exc_data);
        }
        return NULL;
    }
}
Ejemplo n.º 3
0
int main(int argc, char *argv[]) {
  char** hosts;
  char* hostname;
  char* user;
  char* name;
  int i, c = 0;
  uv_interface_address_t* addresses;
  uv_err_t err;

  srand(time(NULL));

  atexit(forza__kill);
  signal(SIGTERM, forza__on_sigterm);

  loop = uv_default_loop();

#ifdef FORZA_VERSION_HASH
  printf("forza "FORZA_VERSION_HASH"\n");
#else
  printf("forza\n");
#endif

  opt = saneopt_init(argc - 1, argv + 1);
  saneopt_alias(opt, "host", "h");
  hosts = saneopt_get_all(opt, "host");
  hostname = saneopt_get(opt, "hostname");
  user = saneopt_get(opt, "app-user");
  name = saneopt_get(opt, "app-name");
  arguments = saneopt_arguments(opt);

  if (hostname == NULL) {
    hostname = malloc(256 * sizeof(*hostname));
    err = uv_interface_addresses(&addresses, &c);
    if (err.code != UV_OK) {
      fprintf(stderr, "uv_interface_addresses: %s\n", uv_err_name(uv_last_error(loop)));
      return 1;
    }
    for (i = 0; i < c; i++) {
      /* For now, only grab the first non-internal, non 0.0.0.0 interface.
       * TODO: Make this smarter.
       */
      if (addresses[i].is_internal) continue;
      uv_ip4_name(&addresses[i].address.address4, hostname, 255 * sizeof(*hostname));
      if (strcmp(hostname, "0.0.0.0") != 0) break;
    }
    uv_free_interface_addresses(addresses, c);
  }

  forza_connect(hosts, hostname, user, name, on_connect);

  uv_run(loop, UV_RUN_DEFAULT);

  free(hosts);

  return 0;
}
Ejemplo n.º 4
0
/** List available interfaces. */
static int net_interfaces(lua_State *L)
{
    /* Retrieve interface list */
    int count = 0;
    char buf[INET6_ADDRSTRLEN]; /* http://tools.ietf.org/html/rfc4291 */
    uv_interface_address_t *info = NULL;
    uv_interface_addresses(&info, &count);
    lua_newtable(L);
    for (int i = 0; i < count; ++i) {
        uv_interface_address_t iface = info[i];
        lua_getfield(L, -1, iface.name);
        if (lua_isnil(L, -1)) {
            lua_pop(L, 1);
            lua_newtable(L);
        }

        /* Address */
        lua_getfield(L, -1, "addr");
        if (lua_isnil(L, -1)) {
            lua_pop(L, 1);
            lua_newtable(L);
        }
        if (iface.address.address4.sin_family == AF_INET) {
            uv_ip4_name(&iface.address.address4, buf, sizeof(buf));
        } else if (iface.address.address4.sin_family == AF_INET6) {
            uv_ip6_name(&iface.address.address6, buf, sizeof(buf));
        } else {
            buf[0] = '\0';
        }
        lua_pushstring(L, buf);
        lua_rawseti(L, -2, lua_rawlen(L, -2) + 1);
        lua_setfield(L, -2, "addr");

        /* Hardware address. */
        char *p = buf;
        memset(buf, 0, sizeof(buf));
        for (unsigned k = 0; k < sizeof(iface.phys_addr); ++k) {
            sprintf(p, "%.2x:", iface.phys_addr[k] & 0xff);
            p += 3;
        }
        *(p - 1) = '\0';
        lua_pushstring(L, buf);
        lua_setfield(L, -2, "mac");

        /* Push table */
        lua_setfield(L, -2, iface.name);
    }
    uv_free_interface_addresses(info, count);

    return 1;
}
Ejemplo n.º 5
0
Archivo: misc.c Proyecto: luvit/luv
static int luv_interface_addresses(lua_State* L) {
  uv_interface_address_t* interfaces;
  int count, i;
  char ip[INET6_ADDRSTRLEN];
  char netmask[INET6_ADDRSTRLEN];

  uv_interface_addresses(&interfaces, &count);

  lua_newtable(L);

  for (i = 0; i < count; i++) {
    lua_getfield(L, -1, interfaces[i].name);
    if (!lua_istable(L, -1)) {
      lua_pop(L, 1);
      lua_newtable(L);
      lua_pushvalue(L, -1);
      lua_setfield(L, -3, interfaces[i].name);
    }
    lua_newtable(L);
    lua_pushboolean(L, interfaces[i].is_internal);
    lua_setfield(L, -2, "internal");

    lua_pushlstring(L, interfaces[i].phys_addr, sizeof(interfaces[i].phys_addr));
    lua_setfield(L, -2, "mac");

    if (interfaces[i].address.address4.sin_family == AF_INET) {
      uv_ip4_name(&interfaces[i].address.address4, ip, sizeof(ip));
      uv_ip4_name(&interfaces[i].netmask.netmask4, netmask, sizeof(netmask));
    } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
      uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
      uv_ip6_name(&interfaces[i].netmask.netmask6, netmask, sizeof(netmask));
    } else {
      strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
      strncpy(netmask, "<unknown sa family>", INET6_ADDRSTRLEN);
    }
    lua_pushstring(L, ip);
    lua_setfield(L, -2, "ip");
    lua_pushstring(L, netmask);
    lua_setfield(L, -2, "netmask");

    lua_pushstring(L, luv_af_num_to_string(interfaces[i].address.address4.sin_family));
    lua_setfield(L, -2, "family");
    lua_rawseti(L, -2, lua_rawlen (L, -2) + 1);
    lua_pop(L, 1);
  }
  uv_free_interface_addresses(interfaces, count);
  return 1;
}
Ejemplo n.º 6
0
static int rava_system_interfaces(lua_State* L)
{
  int size, i;
  char buf[INET6_ADDRSTRLEN];

  uv_interface_address_t* info;
  int r = uv_interface_addresses(&info, &size);

  lua_settop(L, 0);

  if (r < 0) {
    lua_pushboolean(L, 0);
    luaL_error(L, uv_strerror(r));

    return 2;
  }

  lua_newtable(L);

  for (i = 0; i < size; i++) {
    uv_interface_address_t addr = info[i];

    lua_newtable(L);

    lua_pushstring(L, addr.name);
    lua_setfield(L, -2, "name");

    lua_pushboolean(L, addr.is_internal);
    lua_setfield(L, -2, "is_internal");

    if (addr.address.address4.sin_family == PF_INET) {
      uv_ip4_name(&addr.address.address4, buf, sizeof(buf));
    }
    else if (addr.address.address4.sin_family == PF_INET6) {
      uv_ip6_name(&addr.address.address6, buf, sizeof(buf));
    }

    lua_pushstring(L, buf);
    lua_setfield(L, -2, "address");

    lua_rawseti(L, -2, i + 1);
  }

  uv_free_interface_addresses(info, size);

  return 1;
}
Ejemplo n.º 7
0
void foreach_ip6_interface(iface_info_cb iface_cb) {
  int count, ix;
  uv_interface_address_t* addresses;

  ASSERT(0 == uv_interface_addresses(&addresses, &count));

  for (ix = 0; ix < count; ix++) {
    if (addresses[ix].address.address4.sin_family != AF_INET6)
      continue;

    call_iface_info_cb(iface_cb,
                       addresses[ix].name,
                       &addresses[ix].address.address6);
  }

  uv_free_interface_addresses(addresses, count);
}
Ejemplo n.º 8
0
int luv_interface_addresses(lua_State* L) {
  uv_interface_address_t* interfaces;
  int count, i;
  char ip[INET6_ADDRSTRLEN];

  uv_interface_addresses(&interfaces, &count);

  lua_newtable(L);

  for (i = 0; i < count; i++) {
    const char* family;

    lua_getfield(L, -1, interfaces[i].name);
    if (!lua_istable(L, -1)) {
      lua_pop(L, 1);
      lua_newtable(L);
      lua_pushvalue(L, -1);
      lua_setfield(L, -3, interfaces[i].name);
    }
    lua_newtable(L);
    lua_pushboolean(L, interfaces[i].is_internal);
    lua_setfield(L, -2, "internal");

    if (interfaces[i].address.address4.sin_family == AF_INET) {
      uv_ip4_name(&interfaces[i].address.address4,ip, sizeof(ip));
      family = "IPv4";
    } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
      uv_ip6_name(&interfaces[i].address.address6, ip, sizeof(ip));
      family = "IPv6";
    } else {
      strncpy(ip, "<unknown sa family>", INET6_ADDRSTRLEN);
      family = "<unknown>";
    }
    lua_pushstring(L, ip);
    lua_setfield(L, -2, "address");
    lua_pushstring(L, family);
    lua_setfield(L, -2, "family");
    lua_rawseti(L, -2, lua_objlen (L, -2) + 1);
    lua_pop(L, 1);
  }
  uv_free_interface_addresses(interfaces, count);
  return 1;
}
Ejemplo n.º 9
0
Archivo: util.c Proyecto: Sevenops/pyuv
static PyObject *
Util_func_interface_addresses(PyObject *obj)
{
    static char buf[INET6_ADDRSTRLEN+1];
    int i, count;
    uv_interface_address_t* interfaces;
    int err;
    PyObject *result, *item, *exc_data;

    UNUSED_ARG(obj);

    err = uv_interface_addresses(&interfaces, &count);
    if (err < 0) {
        exc_data = Py_BuildValue("(is)", err, uv_strerror(err));
        if (exc_data != NULL) {
            PyErr_SetObject(PyExc_UVError, exc_data);
            Py_DECREF(exc_data);
        }
        return NULL;
    }

    result = PyList_New(count);
    if (!result) {
        uv_free_interface_addresses(interfaces, count);
        return NULL;
    }

    for (i = 0; i < count; i++) {
        item = PyStructSequence_New(&InterfaceAddressesResultType);
        if (!item) {
            Py_DECREF(result);
            uv_free_interface_addresses(interfaces, count);
            return NULL;
        }
        PyStructSequence_SET_ITEM(item, 0, Py_BuildValue("s", interfaces[i].name));
        PyStructSequence_SET_ITEM(item, 1, PyBool_FromLong((long)interfaces[i].is_internal));
        if (interfaces[i].address.address4.sin_family == AF_INET) {
            uv_ip4_name(&interfaces[i].address.address4, buf, sizeof(buf));
        } else if (interfaces[i].address.address4.sin_family == AF_INET6) {
            uv_ip6_name(&interfaces[i].address.address6, buf, sizeof(buf));
        }
        PyStructSequence_SET_ITEM(item, 2, Py_BuildValue("s", buf));
        if (interfaces[i].netmask.netmask4.sin_family == AF_INET) {
            uv_ip4_name(&interfaces[i].netmask.netmask4, buf, sizeof(buf));
        } else if (interfaces[i].netmask.netmask4.sin_family == AF_INET6) {
            uv_ip6_name(&interfaces[i].netmask.netmask6, buf, sizeof(buf));
        }
        PyStructSequence_SET_ITEM(item, 3, Py_BuildValue("s", buf));
        PyOS_snprintf(buf, sizeof(buf), "%02x:%02x:%02x:%02x:%02x:%02x",
                                        (unsigned char)interfaces[i].phys_addr[0],
                                        (unsigned char)interfaces[i].phys_addr[1],
                                        (unsigned char)interfaces[i].phys_addr[2],
                                        (unsigned char)interfaces[i].phys_addr[3],
                                        (unsigned char)interfaces[i].phys_addr[4],
                                        (unsigned char)interfaces[i].phys_addr[5]);
        PyStructSequence_SET_ITEM(item, 4, Py_BuildValue("s", buf));
        PyList_SET_ITEM(result, i, item);
    }

    uv_free_interface_addresses(interfaces, count);
    return result;
}
Ejemplo n.º 10
0
void Settings::SetDefaultRTClistenIP(int requested_family) {
	MS_TRACE();

	int err;
	uv_interface_address_t* addresses;
	int num_addresses;
	std::string ipv4;
	std::string ipv6;
	int _bind_err;

	err = uv_interface_addresses(&addresses, &num_addresses);
	if (err)
		MS_ABORT("uv_interface_addresses() failed: %s", uv_strerror(err));

	for (int i=0; i<num_addresses; i++) {
		uv_interface_address_t address = addresses[i];

		// Ignore internal addresses.
		if (address.is_internal)
			continue;

		int family;
		MS_PORT port;
		std::string ip;
		Utils::IP::GetAddressInfo((struct sockaddr*)(&address.address.address4), &family, ip, &port);

		if (family != requested_family)
			continue;

		switch(family) {
			case AF_INET:
				// Ignore if already got an IPv4.
				if (! ipv4.empty())
					continue;

				// Check if it is bindable.
				if (! IsBindableIP(ip, AF_INET, &_bind_err)) {
					MS_DEBUG("ignoring '%s' for RTC.listenIPv4: %s", ip.c_str(), std::strerror(errno));
					continue;
				}

				MS_DEBUG("auto-discovered '%s' for RTC.listenIPv4", ip.c_str());
				ipv4 = ip;
				break;

			case AF_INET6:
				// Ignore if already got an IPv6.
				if (! ipv6.empty())
					continue;

				// Check if it is bindable.
				if (! IsBindableIP(ip, AF_INET6, &_bind_err)) {
					MS_DEBUG("ignoring '%s' for RTC.listenIPv6: %s", ip.c_str(), std::strerror(errno));
					continue;
				}

				MS_DEBUG("auto-discovered '%s' for RTC.listenIPv6", ip.c_str());
				ipv6 = ip;
				break;
		}
	}

	if (! ipv4.empty()) {
		Settings::configuration.RTC.listenIPv4 = ipv4;
		Settings::configuration.RTC.hasIPv4 = true;
	}

	if (! ipv6.empty()) {
		Settings::configuration.RTC.listenIPv6 = ipv6;
		Settings::configuration.RTC.hasIPv6 = true;
	}

	uv_free_interface_addresses(addresses, num_addresses);
}
Ejemplo n.º 11
0
int main(int argc, char *argv[]) {
  char* host;
  char* hostname;
  char* app;
  char* port_str;
  int port;
  int i, c, v = 0;
  uv_interface_address_t* addresses;
  uv_err_t err;

  srand(time(NULL));

  atexit(forza__kill);
  signal(SIGTERM, forza__on_sigterm);

  loop = uv_default_loop();

#ifdef FORZA_VERSION_HASH
  printf("forza %s\n", FORZA_VERSION_HASH);
  // saneopt doesn't allow options without input
  for (v = 0; v < argc; v++) {
    if (strcmp(argv[v], "-v") == 0 || strcmp(argv[v], "--version") == 0) {
      return 0;
    }
  }
#else
  printf("forza\n");
#endif

  opt = saneopt_init(argc - 1, argv + 1);

  saneopt_alias(opt, "host", "h");
  saneopt_alias(opt, "port", "p");

  host = saneopt_get(opt, "host");
  port_str = saneopt_get(opt, "port");
  hostname = saneopt_get(opt, "hostname");
  app = saneopt_get(opt, "app");
  arguments = saneopt_arguments(opt);

  if (host == NULL || port_str == NULL) {
    fprintf(stderr, "Host and port required\n");
    return 2;
  }

  sscanf(port_str, "%d", &port);
  if (port <= 0 || port > 65535) {
    fprintf(stderr, "Port has to be <= 0 and > 65535\n");
    return 3;
  }

  if (hostname == NULL) {
    hostname = malloc(256 * sizeof(*hostname));
    err = uv_interface_addresses(&addresses, &c);
    if (err.code != UV_OK) {
      fprintf(stderr, "uv_interface_addresses: %s\n", uv_err_name(uv_last_error(loop)));
      return 1;
    }
    for (i = 0; i < c; i++) {
      /* For now, only grab the first non-internal, non 0.0.0.0 interface.
       * TODO: Make this smarter.
       */
      if (addresses[i].is_internal) continue;
      uv_ip4_name(&addresses[i].address.address4, hostname, 255 * sizeof(*hostname));
      if (strcmp(hostname, "0.0.0.0") != 0) break;
    }
    uv_free_interface_addresses(addresses, c);
  }

  forza_connect(host, port, hostname, app, on_connect);

  uv_run(loop, UV_RUN_DEFAULT);

  free(hostname);
  free(opt);

  return 0;
}