From 3cd0ed377b749735408045c5cd68e5550c576560 Mon Sep 17 00:00:00 2001
From: David Cermak <cermak@espressif.com>
Date: Fri, 20 Dec 2024 13:47:33 +0100
Subject: [PATCH] fix(mosq): Use sock_utils instead of func stubs

---
 components/mosquitto/CMakeLists.txt           |  5 ++---
 components/mosquitto/idf_component.yml        |  2 ++
 components/mosquitto/port/ifaddrs.c           | 20 -------------------
 .../mosquitto/port/priv_include/config.h      | 13 ++----------
 .../mosquitto/port/priv_include/ifaddrs.h     | 17 ----------------
 components/mosquitto/port/priv_include/poll.h |  8 --------
 components/mosquitto/port/sysconf.c           | 18 +++++++++++++++++
 7 files changed, 24 insertions(+), 59 deletions(-)
 delete mode 100644 components/mosquitto/port/ifaddrs.c
 delete mode 100644 components/mosquitto/port/priv_include/ifaddrs.h
 delete mode 100644 components/mosquitto/port/priv_include/poll.h
 create mode 100644 components/mosquitto/port/sysconf.c

diff --git a/components/mosquitto/CMakeLists.txt b/components/mosquitto/CMakeLists.txt
index 47e5d551d9..a7e337009e 100644
--- a/components/mosquitto/CMakeLists.txt
+++ b/components/mosquitto/CMakeLists.txt
@@ -74,16 +74,15 @@ idf_component_register(SRCS ${m_srcs}
                             port/callbacks.c
                             port/config.c
                             port/signals.c
-                            port/ifaddrs.c
                             port/broker.c
                             port/files.c
                             port/net__esp_tls.c
+                            port/sysconf.c
                     PRIV_INCLUDE_DIRS port/priv_include port/priv_include/sys ${m_dir} ${m_src_dir}
                                       ${m_incl_dir} ${m_lib_dir} ${m_deps_dir}
                     INCLUDE_DIRS ${m_incl_dir} port/include
                     REQUIRES esp-tls
-                    PRIV_REQUIRES newlib
-        )
+                    PRIV_REQUIRES newlib sock_utils)
 
 target_compile_definitions(${COMPONENT_LIB} PRIVATE "WITH_BROKER")
 target_compile_options(${COMPONENT_LIB} PRIVATE "-Wno-format")
diff --git a/components/mosquitto/idf_component.yml b/components/mosquitto/idf_component.yml
index d8aee0e68a..7514a882fe 100644
--- a/components/mosquitto/idf_component.yml
+++ b/components/mosquitto/idf_component.yml
@@ -3,3 +3,5 @@ url: https://github.com/espressif/esp-protocols/tree/master/components/mosquitto
 description: The component provides a simple ESP32 port of mosquitto broker
 dependencies:
   idf: '>=5.1'
+  espressif/sock_utils:
+    version: '^0.2'
diff --git a/components/mosquitto/port/ifaddrs.c b/components/mosquitto/port/ifaddrs.c
deleted file mode 100644
index 1e746416bd..0000000000
--- a/components/mosquitto/port/ifaddrs.c
+++ /dev/null
@@ -1,20 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2024 Roger Light <roger@atchoo.org>
- *
- * SPDX-License-Identifier: EPL-2.0
- *
- * SPDX-FileContributor: 2024 Espressif Systems (Shanghai) CO LTD
- */
-#include "ifaddrs.h"
-
-// Dummy implementation of getifaddrs()
-// TODO: Implement this if we need to use bind_interface option of listener's config
-int getifaddrs(struct ifaddrs **ifap)
-{
-    return -1;
-}
-
-void freeifaddrs(struct ifaddrs *ifa)
-{
-
-}
diff --git a/components/mosquitto/port/priv_include/config.h b/components/mosquitto/port/priv_include/config.h
index 8680e88f75..bf7cb619b9 100644
--- a/components/mosquitto/port/priv_include/config.h
+++ b/components/mosquitto/port/priv_include/config.h
@@ -5,20 +5,11 @@
  */
 #pragma once
 #include <ctype.h>
+#include "net/if.h"
 
 #undef  isspace
 #define isspace(__c) (__ctype_lookup((int)__c)&_S)
 
 #include_next "config.h"
-#define VERSION "v2.0.18~0"
 
-#define _SC_OPEN_MAX_OVERRIDE                 4
-
-// used to override syscall for obtaining max open fds
-static inline long sysconf(int arg)
-{
-    if (arg == _SC_OPEN_MAX_OVERRIDE) {
-        return 64;
-    }
-    return -1;
-}
+#define VERSION "v2.0.20~1"
diff --git a/components/mosquitto/port/priv_include/ifaddrs.h b/components/mosquitto/port/priv_include/ifaddrs.h
deleted file mode 100644
index 604628cbac..0000000000
--- a/components/mosquitto/port/priv_include/ifaddrs.h
+++ /dev/null
@@ -1,17 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-#pragma once
-
-#define gai_strerror(x) "gai_strerror() not supported"
-
-struct ifaddrs {
-    struct ifaddrs  *ifa_next;    /* Next item in list */
-    char            *ifa_name;    /* Name of interface */
-    struct sockaddr *ifa_addr;    /* Address of interface */
-};
-
-int getifaddrs(struct ifaddrs **ifap);
-void freeifaddrs(struct ifaddrs *ifa);
diff --git a/components/mosquitto/port/priv_include/poll.h b/components/mosquitto/port/priv_include/poll.h
deleted file mode 100644
index 1ac76a5b4f..0000000000
--- a/components/mosquitto/port/priv_include/poll.h
+++ /dev/null
@@ -1,8 +0,0 @@
-/*
- * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
- *
- * SPDX-License-Identifier: Apache-2.0
- */
-#pragma once
-
-#include_next "sys/poll.h"
diff --git a/components/mosquitto/port/sysconf.c b/components/mosquitto/port/sysconf.c
new file mode 100644
index 0000000000..80a1d92318
--- /dev/null
+++ b/components/mosquitto/port/sysconf.c
@@ -0,0 +1,18 @@
+/*
+ * SPDX-FileCopyrightText: 2024 Espressif Systems (Shanghai) CO LTD
+ *
+ * SPDX-License-Identifier: Apache-2.0
+ */
+#include <ctype.h>
+#include <unistd.h>
+
+#define _SC_OPEN_MAX_OVERRIDE                 4
+
+// used to override syscall for obtaining max open fds
+long sysconf(int arg)
+{
+    if (arg == _SC_OPEN_MAX_OVERRIDE) {
+        return 64;
+    }
+    return -1;
+}