Skip to content

Conversation

@zs39
Copy link
Contributor

@zs39 zs39 commented Jan 9, 2026

Summary

Add netlib_check_httpconnectivity() to verify HTTP service connectivity by sending GET request and validating status code.

Impact

The new features have no impact.

Testing

Use the demo below for testing. Test passed.

/****************************************************************************
 * apps/examples/hello/hello_main.c
 *
 * SPDX-License-Identifier: Apache-2.0
 *
 * Licensed to the Apache Software Foundation (ASF) under one or more
 * contributor license agreements.  See the NOTICE file distributed with
 * this work for additional information regarding copyright ownership.  The
 * ASF licenses this file to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance with the
 * License.  You may obtain a copy of the License at
 *
 *   http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
 * WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the
 * License for the specific language governing permissions and limitations
 * under the License.
 *
 ****************************************************************************/

/****************************************************************************
 * Included Files
 ****************************************************************************/

#include <nuttx/config.h>
#include <stdio.h>
#include <string.h>
#include <errno.h>

#include <netutils/netlib.h>

/****************************************************************************
 * Public Functions
 ****************************************************************************/

/****************************************************************************
 * hello_main
 ****************************************************************************/

int main(int argc, FAR char *argv[])
{
  const char *host = "www.example.com";
  const char *path = "";
  int port = 80;
  int expect_code = 200;
  int ret;

  printf("Hello, World!!\n");
  printf("\n");
  printf("========================================\n");
  printf("HTTP Connectivity Test\n");
  printf("========================================\n");
  printf("\n");

  printf("Testing: http://%s:%d/%s\n", host, port, path);
  printf("Expected HTTP status code: %d\n", expect_code);
  printf("\n");

  printf("Checking HTTP connectivity...\n");
  ret = netlib_check_httpconnectivity(host, path, port, expect_code);

  printf("\n");
  printf("========================================\n");
  if (ret == 0)
    {
      printf("SUCCESS: HTTP connectivity check passed!\n");
      printf("Server returned expected status code: %d\n", expect_code);
    }
  else
    {
      printf("FAILED: HTTP connectivity check failed!\n");
      if (ret < 0)
        {
          if (ret > -1000)
            {
              printf("HTTP Status Code: %d (expected %d)\n",
                     -ret, expect_code);
            }
          else
            {
              printf("Error: %s\n", strerror(-ret));
            }
        }
    }
  printf("========================================\n");

  return (ret == 0) ? 0 : 1;
}

The results are as follows

nsh> hello
Hello, World!!

========================================
HTTP Connectivity Test
========================================

Testing: http://www.example.com:80/
Expected HTTP status code: 200

Checking HTTP connectivity...

========================================
SUCCESS: HTTP connectivity check passed!
Server returned expected status code: 200
========================================

@zs39
Copy link
Contributor Author

zs39 commented Jan 9, 2026

Documentation supplement here
apache/nuttx#17812

xiaoxiang781216
xiaoxiang781216 previously approved these changes Jan 9, 2026
simbit18
simbit18 previously approved these changes Jan 9, 2026
@simbit18 simbit18 requested review from acassis and linguini1 January 9, 2026 16:28
acassis
acassis previously requested changes Jan 9, 2026
Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @zs39 :-)

  • Are you sure use or return -errno; is always valid? Please take a look at code comments. I think there are functions that does not set global errno and thus return value may be undefined?
  • Would it be possible to add example code from the PR to apps/examples please? :-)

@cederom
Copy link
Contributor

cederom commented Jan 11, 2026

@cederom: Where is errno defined and what sets its value?

Looking at https://github.com/apache/nuttx/blob/fac76746eae2afe91a5cb9948e3843596c9f941f/include/errno.h#L43 there is no set_errno(e) in the https://github.com/apache/nuttx/blob/fac76746eae2afe91a5cb9948e3843596c9f941f/libs/libc/netdb/lib_getaddrinfo.c#L133.

If getaddrinfo() returns error code by return then why not this?

int ret = getaddrinfo(hostname, NULL, &hint, &info);
id ( ret != OK ) return ret;

@xiaoxiang781216: @cederom the usage is right, please read https://man7.org/linux/man-pages/man3/errno.3.html, to understand when errno get set.

I am putting this discussion here so it does not disappear after code comments are closed ;-)

@zs39 please answer my questions about errno handling:

  1. Where is errno defined and what sets its value? There are places in getaddrinfo() that will return an error code but does not set errno with set_errno() that is required to use errno as valid error code that you may reference in the caller function.
  2. If getaddrinfo() returns error code by return then why not return ret instead return -errno?

Lets take a looks at your proposition netutils/netlib/netlib_checkhttpconnectivity.c line 76:

 if (getaddrinfo(hostname, NULL, &hint, &info) != OK)
    {
      return -errno;
    }

You are not getting and returning code of getaddrinfo(). Instead when getaddrinfo() fails you return -errno that does not reflect error code returned.

Let's go to that function (https://github.com/apache/nuttx/blob/fac76746eae2afe91a5cb9948e3843596c9f941f/libs/libc/netdb/lib_getaddrinfo.c#L133) and see first check (https://github.com/apache/nuttx/blob/fac76746eae2afe91a5cb9948e3843596c9f941f/libs/libc/netdb/lib_getaddrinfo.c#L151):

  if (hostname == NULL && servname == NULL)
    {
      return EAI_NONAME;
    }

I cannot see set_errno() here (https://github.com/apache/nuttx/blob/fac76746eae2afe91a5cb9948e3843596c9f941f/include/errno.h#L44) thus in the call above you will not forward EAI_NONAME but some random errno that was probably set before by something else.

If you look at existing code use of getaddrinfo() in the apps (https://github.com/search?q=repo%3Aapache%2Fnuttx-apps%20getaddrinfo&type=code) there are use cases like:

  1. Return ERROR=-1 when getaddrinfo() fails, see:
    if (getaddrinfo(hostname, NULL, &hint, &info) != OK)
  2. Convert ret = getaddrinfo() to get string error, see:
    ret = getaddrinfo(host, port, &hints, &info);
  3. Both (1) and (2) above ret is used to get error strting and ERROR=-1 is returned, see:
    ret = getaddrinfo(argv[1], NULL, NULL, &info);

Once again, your implementation is prone to returning errno set somewhere else instead valid return code from getaddrinfo().

I would advise update to something like existing examples (1,2,3) above use:

int ret = getaddrinfo(hostname, NULL, &hint, &info);
if ( ret != OK ) return ret;

The only valid use of errno may be for socket() [1] as it known to set it explicitly. But it's not for getaddrinfo() [2].

[1] https://man.freebsd.org/cgi/man.cgi?query=socket&apropos=0&sektion=0
[2] https://man.freebsd.org/cgi/man.cgi?query=getaddrinfo&apropos=0&sektion=3

@zs39 zs39 dismissed stale reviews from simbit18 and xiaoxiang781216 via baf1631 January 12, 2026 11:10
@zs39 zs39 force-pushed the master05 branch 2 times, most recently from baf1631 to ad51026 Compare January 12, 2026 11:19
Add netlib_check_httpconnectivity() to verify HTTP service connectivity by sending GET request and validating status code.

Signed-off-by: meijian <meijian@xiaomi.com>
@zs39
Copy link
Contributor Author

zs39 commented Jan 12, 2026

You are correct. Some error cases in getaddrinfo do not set errno, so I mapped it to a standard error code and returned it. Both inet_pton and getaddrinfo use errno for error codes. :)
@cederom

@zs39 zs39 requested review from acassis and cederom January 12, 2026 11:30
@cederom
Copy link
Contributor

cederom commented Jan 12, 2026

Perfect @zs39 no more hazards thanks :-)

Copy link
Contributor

@cederom cederom left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Looks good now thanks @zs39 :-) Lets just make CI builds fine :-)

@xiaoxiang781216 xiaoxiang781216 merged commit b0b90ac into apache:master Jan 13, 2026
40 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants