static void ip_config_ready (MMBaseModem *modem, GAsyncResult *res, GetIpConfig3gppContext *ctx) { MMBearerIpConfig *ip_config = NULL; const gchar *response; GError *error = NULL; gchar **items; gchar *dns[3] = { 0 }; guint i; guint dns_i; guint32 tmp; response = mm_base_modem_at_command_full_finish (MM_BASE_MODEM (modem), res, &error); if (error) { g_simple_async_result_take_error (ctx->result, error); get_ip_config_context_complete_and_free (ctx); return; } /* TODO: use a regex to parse this */ /* Check result */ if (!g_str_has_prefix (response, OWANDATA_TAG)) { g_simple_async_result_set_error (ctx->result, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Couldn't get IP config: invalid response '%s'", response); get_ip_config_context_complete_and_free (ctx); return; } response = mm_strip_tag (response, OWANDATA_TAG); items = g_strsplit (response, ", ", 0); for (i = 0, dns_i = 0; items[i]; i++) { if (i == 0) { /* CID */ glong num; errno = 0; num = strtol (items[i], NULL, 10); if (errno != 0 || num < 0 || (gint) num != ctx->cid) { error = g_error_new (MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Unknown CID in OWANDATA response (" "got %d, expected %d)", (guint) num, ctx->cid); break; } } else if (i == 1) { /* IP address */ if (!inet_pton (AF_INET, items[i], &tmp)) break; ip_config = mm_bearer_ip_config_new (); mm_bearer_ip_config_set_method (ip_config, MM_BEARER_IP_METHOD_STATIC); mm_bearer_ip_config_set_address (ip_config, items[i]); } else if (i == 3 || i == 4) { /* DNS entries */ if (!inet_pton (AF_INET, items[i], &tmp)) { g_clear_object (&ip_config); break; } dns[dns_i++] = items[i]; } } if (!ip_config) { if (error) g_simple_async_result_take_error (ctx->result, error); else g_simple_async_result_set_error (ctx->result, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Couldn't get IP config: couldn't parse response '%s'", response); } else { /* If we got DNS entries, set them in the IP config */ if (dns[0]) mm_bearer_ip_config_set_dns (ip_config, (const gchar **)dns); g_simple_async_result_set_op_res_gpointer (ctx->result, ip_config, (GDestroyNotify)g_object_unref); } get_ip_config_context_complete_and_free (ctx); g_strfreev (items); }
gboolean mm_mbm_parse_e2ipcfg_response (const gchar *response, MMBearerIpConfig **out_ip4_config, MMBearerIpConfig **out_ip6_config, GError **error) { MMBearerIpConfig **ip_config = NULL; gboolean got_address = FALSE, got_gw = FALSE, got_dns = FALSE; GRegex *r; GMatchInfo *match_info = NULL; GError *match_error = NULL; gchar *dns[3] = { 0 }; guint dns_idx = 0; int family = AF_INET; MMBearerIpMethod method = MM_BEARER_IP_METHOD_STATIC; g_return_val_if_fail (out_ip4_config, FALSE); g_return_val_if_fail (out_ip6_config, FALSE); if (!response || !g_str_has_prefix (response, E2IPCFG_TAG)) { g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Missing " E2IPCFG_TAG " prefix"); return FALSE; } response = mm_strip_tag (response, "*E2IPCFG: "); if (strchr (response, ':')) { family = AF_INET6; ip_config = out_ip6_config; method = MM_BEARER_IP_METHOD_DHCP; } else if (strchr (response, '.')) { family = AF_INET; ip_config = out_ip4_config; method = MM_BEARER_IP_METHOD_STATIC; } else { g_set_error (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Failed to detect " E2IPCFG_TAG " address family"); return FALSE; } /* *E2IPCFG: (1,<IP>)(2,<gateway>)(3,<DNS>)(3,<DNS>) * * *E2IPCFG: (1,"46.157.32.246")(2,"46.157.32.243")(3,"193.213.112.4")(3,"130.67.15.198") * *E2IPCFG: (1,"fe80:0000:0000:0000:0000:0000:e537:1801")(3,"2001:4600:0004:0fff:0000:0000:0000:0054")(3,"2001:4600:0004:1fff:0000:0000:0000:0054") * *E2IPCFG: (1,"fe80:0000:0000:0000:0000:0027:b7fe:9401")(3,"fd00:976a:0000:0000:0000:0000:0000:0009") */ r = g_regex_new ("\\((\\d),\"([0-9a-fA-F.:]+)\"\\)", 0, 0, NULL); g_assert (r != NULL); if (!g_regex_match_full (r, response, -1, 0, 0, &match_info, &match_error)) { if (match_error) { g_propagate_error (error, match_error); g_prefix_error (error, "Could not parse " E2IPCFG_TAG " results: "); } else { g_set_error_literal (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Couldn't match " E2IPCFG_TAG " reply"); } goto done; } *ip_config = mm_bearer_ip_config_new (); mm_bearer_ip_config_set_method (*ip_config, method); while (g_match_info_matches (match_info)) { char *id = g_match_info_fetch (match_info, 1); char *str = g_match_info_fetch (match_info, 2); switch (atoi (id)) { case 1: if (validate_address (family, str)) { mm_bearer_ip_config_set_address (*ip_config, str); mm_bearer_ip_config_set_prefix (*ip_config, (family == AF_INET6) ? 64 : 28); got_address = TRUE; } break; case 2: if ((family == AF_INET) && validate_address (family, str)) { mm_bearer_ip_config_set_gateway (*ip_config, str); got_gw = TRUE; } break; case 3: if (validate_address (family, str)) { dns[dns_idx++] = g_strdup (str); got_dns = TRUE; } break; default: break; } g_free (id); g_free (str); g_match_info_next (match_info, NULL); } if (got_dns) { mm_bearer_ip_config_set_dns (*ip_config, (const gchar **) dns); g_free (dns[0]); g_free (dns[1]); } if (!got_address || (family == AF_INET && !got_gw)) { g_object_unref (*ip_config); *ip_config = NULL; g_set_error_literal (error, MM_CORE_ERROR, MM_CORE_ERROR_FAILED, "Got incomplete IP configuration from " E2IPCFG_TAG); } done: if (match_info) g_match_info_free (match_info); g_regex_unref (r); return !!*ip_config; }