# Cannot Resolve Host with libcurl

**URL:** <https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897>\
**Category:** Legato Application Framework\
**Created:** [December 18, 2017, 7:58pm UTC](https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897 "2017-12-18T19:58:15Z")\
**Posts on this page:** 7\
**Page:** 1

<div class="post-metadata">

**Author:** ![coffeebean](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@coffeebean](https://forum.legato.io/u/coffeebean)\
**Post date:** [December 18, 2017, 7:58pm UTC](https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897/1 "2017-12-18T19:58:15Z")

</div>

Hi everyone,

I’m having a strange issue with the use of libcurl. I’m attempting to connect to mobile data, then use libcurl to get the contents of a website. However, I always receive the error:

`curl_easy_perform() failed: Couldn't resolve host name`

I’m using the Modem Data Control service to connect to data. My code is as follows (somewhat truncated, error handling and such excluded):

`le_mdc_ConnectService();`  
`defaultModemProfile = le_mdc_GetProfile(LE_MDC_DEFAULT_PROFILE);`  
`le_mdc_SetAPN(defaultModemProfile, "att.mvno");`  
`le_mdc_SetPDP(defaultModemProfile, LE_MDC_PDP_IPV4);`  
`le_mdc_ConState_t state;`  
`le_mdc_GetSessionState(defaultModemProfile, &state);`  
`le_mdc_StartSession(defaultModemProfile);`

The connection seems to work fine using this logic. Then, via libcurl, I’m doing the following:

`curl_global_init(CURL_GLOBAL_ALL);`  
`CURL *myHandle;`  
`CURLcode result;`  
`myHandle = curl_easy_init();`  
`char *destUrl = "http://example.com";`  
`curl_easy_setopt(myHandle, CURLOPT_URL, destUrl);`  
`curl_easy_setopt(myHandle, CURLOPT_HTTPGET, 1);`  
`curl_easy_setopt(myHandle, CURLOPT_FOLLOWLOCATION, 1L);`  
`result = curl_easy_perform(myHandle);`  
`curl_easy_cleanup(myHandle);`

However, it always returns the `Couldn't resolve host name` error.

Interestingly enough, if I comment out the above code (so my application isn’t establishing the connection), and instead use the `cm data connect` command through SSH, then run my application, libcurl returns the page with no issues.

Based on that, I’m thinking maybe this is some sort of DNS issue, although I can’t say for sure. Is there some means by which I’m supposed to configuring DNS through the Modem Data Control service?

Thanks much in advance!

---

<div class="post-metadata">

**Author:** ![dfrey](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/dfrey/32/563_2.png) [@dfrey](https://forum.legato.io/u/dfrey)\
**Post date:** [December 18, 2017, 8:02pm UTC](https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897/2 "2017-12-18T20:02:27Z")

</div>

- Which module are you running this code on?
- Which modem firmware are you running?
- Which Legato framework version are you running?
- Is the app that uses libcurl sandboxed?

---

<div class="post-metadata">

**Author:** ![coffeebean](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@coffeebean](https://forum.legato.io/u/coffeebean)\
**Post date:** [December 18, 2017, 8:13pm UTC](https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897/3 "2017-12-18T20:13:38Z")

</div>

Hi @dfrey

Here’s my setup:  
`Board: MangOH Red`  
`Device: WP8548`  
`Device Firmware: SWI9X15Y_07.12.09.00`  
`Legato Version: 16.10.3`

The app is not sandboxed (I have `sandboxed:false` in my `adef` file).

---

<div class="post-metadata">

**Author:** ![coffeebean](https://avatars.discourse-cdn.com/v4/letter/c/8e8cbc/32.png) [@coffeebean](https://forum.legato.io/u/coffeebean)\
**Post date:** [December 20, 2017, 6:34pm UTC](https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897/4 "2017-12-20T18:34:19Z")

</div>

I’ve been diagnosing this problem the past few days, and I’ve found the source of the issue (albeit not a solution). For some reason, when I establish a data connection through my application, the routing table fails to populate correctly.

Here’s the routing table (via `"route -n"`) when I connect to data via `"cm data connect"` in SSH:  
 ![routing_ssh](https://us1.discourse-cdn.com/flex019/uploads/legato1/original/1X/3a03c7eb1c34ef8ee5c572915b1ec527dee5575c.png)

Meanwhile, here it is when I connect through my application:  
 ![routing_app](https://us1.discourse-cdn.com/flex019/uploads/legato1/original/1X/64f1f6f3c775bb53268eaf4ad0c5ab0cdf76621f.png)

Notice how the entry for the gateway is completely missing. Interestingly enough, however, if I run `cm data info` on both instances, a gateway IP is provided.

Is this some sort of permissions issues? Or perhaps something wrong with my Legato installation? I’ve tried resetting the userspace and reinstalling the platform to no avail. Any insight would be greatly appreciated.

---

<div class="post-metadata">

**Author:** ![lotam](https://avatars.discourse-cdn.com/v4/letter/l/958977/32.png) [@lotam](https://forum.legato.io/u/lotam)\
**Post date:** [December 21, 2017, 1:22am UTC](https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897/5 "2017-12-21T01:22:24Z")

</div>

When app use le\_mdc\_StartSession() to start data connection, it has to setup the route table as well, same is done by “cm data connect” itself, refer to below link for sample:

> **[Sample code for network interface settings - Legato Docs](http://docs.legato.io/latest/c_mdcDataSessions.html)**
>
> legato™ is an open source Linux-based embedded platform designed to simplify connected IoT application development

Reminded, the app needs to be unsandboxed before it can alter the route table.

Optionally, you may consider using le\_data\_Request() which should take care route table setting automatically.

Thx

---

<div class="post-metadata">

**Author:** ![1240371187](https://avatars.discourse-cdn.com/v4/letter/1/9de053/32.png) [@1240371187](https://forum.legato.io/u/1240371187)\
**Post date:** [July 21, 2021, 7:16am UTC](https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897/6 "2021-07-21T07:16:00Z")

</div>

Have you solved the problem now?  
I have a similar problem now.[quote=“coffeebean, post:1, topic:2897, full:true”]  
🥺

---

<div class="post-metadata">

**Author:** ![jyijyi](https://sea1.discourse-cdn.com/flex019/user_avatar/forum.legato.io/jyijyi/32/822_2.png) [@jyijyi](https://forum.legato.io/u/jyijyi)\
**Post date:** [July 21, 2021, 7:19am UTC](https://forum.legato.io/t/cannot-resolve-host-with-libcurl/2897/7 "2021-07-21T07:19:25Z")

</div>

I don’t see problem WP77:

> **[HttpGet example via modem does not work on WP7702](https://forum.mangoh.io/t/httpget-example-via-modem-does-not-work-on-wp7702/4825/5)**
>
> I just tried on WP77 R12, no problem is found, maybe you can use my httpget sample attached. http\_get\_WP77.rar (414.5 KB) Please attach data connection by “cm data connect &” before running the app. root@swi-mdm9x28-wp:~# cm info Device: ...

Have you tried if command line to use curl is working fine first?
