cederom commented on code in PR #3304: URL: https://github.com/apache/nuttx-apps/pull/3304#discussion_r2677376445
########## netutils/netlib/netlib_checkhttpconnectivity.c: ########## @@ -0,0 +1,199 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_checkhttpconnectivity.c + * + * 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 <sys/socket.h> +#include <sys/ioctl.h> +#include <stdint.h> +#include <stdio.h> +#include <stdbool.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <debug.h> + +#ifdef CONFIG_LIBC_NETDB +# include <netdb.h> +#endif + +#include <arpa/inet.h> +#include <netinet/in.h> + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: httpget_gethostip + * + * Description: + * Check the http status code + * + * Parameters: + * hostname The host name to use in the nslookup. + * dest The location to return the IPv4 address. + * + * Return: + * 0 on success; a nagtive on failure. + * + ****************************************************************************/ + +static int httpget_gethostip(FAR const char *hostname, + FAR struct in_addr *dest) +{ +#ifdef CONFIG_LIBC_NETDB + /* Netdb DNS client support is enabled */ + + FAR struct addrinfo hint; + FAR struct addrinfo *info; + FAR struct sockaddr_in *addr; + + memset(&hint, 0, sizeof(hint)); + hint.ai_family = AF_INET; + + if (getaddrinfo(hostname, NULL, &hint, &info) != OK) + { + return -errno; Review Comment: 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 why not this? ``` int ret = getaddrinfo(hostname, NULL, &hint, &info); id ( ret != OK ) return ret; ``` ########## netutils/netlib/netlib_checkhttpconnectivity.c: ########## @@ -0,0 +1,199 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_checkhttpconnectivity.c + * + * 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 <sys/socket.h> +#include <sys/ioctl.h> +#include <stdint.h> +#include <stdio.h> +#include <stdbool.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <debug.h> + +#ifdef CONFIG_LIBC_NETDB +# include <netdb.h> +#endif + +#include <arpa/inet.h> +#include <netinet/in.h> + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: httpget_gethostip + * + * Description: + * Check the http status code + * + * Parameters: + * hostname The host name to use in the nslookup. + * dest The location to return the IPv4 address. + * + * Return: + * 0 on success; a nagtive on failure. + * + ****************************************************************************/ + +static int httpget_gethostip(FAR const char *hostname, + FAR struct in_addr *dest) +{ +#ifdef CONFIG_LIBC_NETDB + /* Netdb DNS client support is enabled */ + + FAR struct addrinfo hint; + FAR struct addrinfo *info; + FAR struct sockaddr_in *addr; + + memset(&hint, 0, sizeof(hint)); + hint.ai_family = AF_INET; + + if (getaddrinfo(hostname, NULL, &hint, &info) != OK) + { + return -errno; + } + + addr = (FAR struct sockaddr_in *)info->ai_addr; + memcpy(dest, &addr->sin_addr, sizeof(struct in_addr)); + + freeaddrinfo(info); + return OK; + +#else /* CONFIG_LIBC_NETDB */ + /* No host name support */ + + /* Convert strings to numeric IPv4 address */ + + int ret = inet_pton(AF_INET, hostname, dest); + + if (ret < 0) + { + return -errno; Review Comment: Similar as above why not `return ret` ? ########## netutils/netlib/netlib_checkhttpconnectivity.c: ########## @@ -0,0 +1,199 @@ +/**************************************************************************** + * apps/netutils/netlib/netlib_checkhttpconnectivity.c + * + * 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 <sys/socket.h> +#include <sys/ioctl.h> +#include <stdint.h> +#include <stdio.h> +#include <stdbool.h> +#include <unistd.h> +#include <string.h> +#include <errno.h> +#include <debug.h> + +#ifdef CONFIG_LIBC_NETDB +# include <netdb.h> +#endif + +#include <arpa/inet.h> +#include <netinet/in.h> + +/**************************************************************************** + * Private Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: httpget_gethostip + * + * Description: + * Check the http status code + * + * Parameters: + * hostname The host name to use in the nslookup. + * dest The location to return the IPv4 address. + * + * Return: + * 0 on success; a nagtive on failure. + * + ****************************************************************************/ + +static int httpget_gethostip(FAR const char *hostname, + FAR struct in_addr *dest) +{ +#ifdef CONFIG_LIBC_NETDB + /* Netdb DNS client support is enabled */ + + FAR struct addrinfo hint; + FAR struct addrinfo *info; + FAR struct sockaddr_in *addr; + + memset(&hint, 0, sizeof(hint)); + hint.ai_family = AF_INET; + + if (getaddrinfo(hostname, NULL, &hint, &info) != OK) + { + return -errno; + } + + addr = (FAR struct sockaddr_in *)info->ai_addr; + memcpy(dest, &addr->sin_addr, sizeof(struct in_addr)); + + freeaddrinfo(info); + return OK; + +#else /* CONFIG_LIBC_NETDB */ + /* No host name support */ + + /* Convert strings to numeric IPv4 address */ + + int ret = inet_pton(AF_INET, hostname, dest); + + if (ret < 0) + { + return -errno; + } + + /* The inet_pton() function returns 1 if the conversion succeeds. It will + * return 0 if the input is not a valid IPv4 dotted-decimal string or -1 + * with errno set to EAFNOSUPPORT if the address family argument is + * unsupported. + */ + + return OK; + +#endif /* CONFIG_LIBC_NETDB */ +} + +/**************************************************************************** + * Public Functions + ****************************************************************************/ + +/**************************************************************************** + * Name: netlib_check_httpconnectivity + * + * Description: + * Check the http status code + * + * Parameters: + * host The remote host to check + * getmsg The http GET message + * port The port of remote host + * expect_code The except http code of http code + * + * Return: + * 0 on success; a nagtive on failure. + * + ****************************************************************************/ + +int netlib_check_httpconnectivity(FAR const char *host, + FAR const char *getmsg, + int port, int expect_code) +{ + int sock; + int ret; + int status_code = -1; + struct sockaddr_in server_addr; + char buf[256]; + + ret = httpget_gethostip(host, &server_addr.sin_addr); + if (ret < 0) + { + nerr("Failed to get host ip"); + return ret; + } + + sock = socket(AF_INET, SOCK_STREAM, 0); + if (sock < 0) + { + nerr("Failed to create socket"); + return -errno; Review Comment: `return -errno;` here is valud because `socket()` sets global `errno` :-) https://github.com/apache/nuttx/blob/fac76746eae2afe91a5cb9948e3843596c9f941f/fs/socket/socket.c#L261 -- This is an automated message from the Apache Git Service. To respond to the message, please log on to GitHub and use the URL above to go to the specific comment. To unsubscribe, e-mail: [email protected] For queries about this service, please contact Infrastructure at: [email protected]
