pkarashchenko commented on code in PR #7820:
URL: https://github.com/apache/nuttx/pull/7820#discussion_r1043056846


##########
include/netdb.h:
##########
@@ -222,6 +222,8 @@ struct protoent
                             * alternative protocol names, terminated by a
                             * null pointer. */
   int        p_proto;      /* The protocol number. */
+  int        idx;          /* Index used by the local database, required

Review Comment:
   Maybe we can make it more compact like
   ```
   uint8_t p_proto;
   uint16_t idx;
   ```
   ?



##########
libs/libc/netdb/lib_protor.c:
##########
@@ -0,0 +1,138 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_protor.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 <errno.h>
+#include <netdb.h>
+#include <string.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const unsigned char g_protos[] =
+{
+  "\000ip\0"
+  "\001icmp\0"
+  "\002igmp\0"
+  "\003ggp\0"
+  "\004ipencap\0"
+  "\005st\0"
+  "\006tcp\0"
+  "\010egp\0"
+  "\014pup\0"
+  "\021udp\0"
+  "\024hmp\0"
+  "\026xns-idp\0"
+  "\033rdp\0"
+  "\035iso-tp4\0"
+  "\044xtp\0"
+  "\045ddp\0"
+  "\046idpr-cmtp\0"
+  "\051ipv6\0"
+  "\053ipv6-route\0"
+  "\054ipv6-frag\0"
+  "\055idrp\0"
+  "\056rsvp\0"
+  "\057gre\0"
+  "\062esp\0"
+  "\063ah\0"
+  "\071skip\0"
+  "\072ipv6-icmp\0"
+  "\073ipv6-nonxt\0"
+  "\074ipv6-opts\0"
+  "\111rspf\0"
+  "\121vmtp\0"
+  "\131ospf\0"
+  "\136ipip\0"
+  "\142encap\0"
+  "\147pim\0"
+  "\377raw"
+};
+
+static const char *g_aliases;
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void setprotoent_r(int stayopen, FAR struct protoent * result_buf)

Review Comment:
   ```suggestion
   void setprotoent_r(int stayopen, FAR struct protoent *result_buf)
   ```



##########
libs/libc/netdb/lib_protor.c:
##########
@@ -0,0 +1,138 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_protor.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 <errno.h>
+#include <netdb.h>
+#include <string.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const unsigned char g_protos[] =
+{
+  "\000ip\0"

Review Comment:
   I'm not sure which is better, but just an idea that maybe `\x0` (hex) format 
may bring more readability.



##########
libs/libc/netdb/lib_protor.c:
##########
@@ -0,0 +1,138 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_protor.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 <errno.h>
+#include <netdb.h>
+#include <string.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const unsigned char g_protos[] =
+{
+  "\000ip\0"
+  "\001icmp\0"
+  "\002igmp\0"
+  "\003ggp\0"
+  "\004ipencap\0"
+  "\005st\0"
+  "\006tcp\0"
+  "\010egp\0"
+  "\014pup\0"
+  "\021udp\0"
+  "\024hmp\0"
+  "\026xns-idp\0"
+  "\033rdp\0"
+  "\035iso-tp4\0"
+  "\044xtp\0"
+  "\045ddp\0"
+  "\046idpr-cmtp\0"
+  "\051ipv6\0"
+  "\053ipv6-route\0"
+  "\054ipv6-frag\0"
+  "\055idrp\0"
+  "\056rsvp\0"
+  "\057gre\0"
+  "\062esp\0"
+  "\063ah\0"
+  "\071skip\0"
+  "\072ipv6-icmp\0"
+  "\073ipv6-nonxt\0"
+  "\074ipv6-opts\0"
+  "\111rspf\0"
+  "\121vmtp\0"
+  "\131ospf\0"
+  "\136ipip\0"
+  "\142encap\0"
+  "\147pim\0"
+  "\377raw"
+};
+
+static const char *g_aliases;
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void setprotoent_r(int stayopen, FAR struct protoent * result_buf)
+{
+  result_buf->idx = 0;
+}
+
+void endprotoent_r(FAR struct protoent * result_buf)

Review Comment:
   ```suggestion
   void endprotoent_r(FAR struct protoent *result_buf)
   ```



##########
libs/libc/netdb/lib_protor.c:
##########
@@ -0,0 +1,138 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_protor.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 <errno.h>
+#include <netdb.h>
+#include <string.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const unsigned char g_protos[] =
+{
+  "\000ip\0"
+  "\001icmp\0"
+  "\002igmp\0"
+  "\003ggp\0"
+  "\004ipencap\0"
+  "\005st\0"
+  "\006tcp\0"
+  "\010egp\0"
+  "\014pup\0"
+  "\021udp\0"
+  "\024hmp\0"
+  "\026xns-idp\0"
+  "\033rdp\0"
+  "\035iso-tp4\0"
+  "\044xtp\0"
+  "\045ddp\0"
+  "\046idpr-cmtp\0"
+  "\051ipv6\0"
+  "\053ipv6-route\0"
+  "\054ipv6-frag\0"
+  "\055idrp\0"
+  "\056rsvp\0"
+  "\057gre\0"
+  "\062esp\0"
+  "\063ah\0"
+  "\071skip\0"
+  "\072ipv6-icmp\0"
+  "\073ipv6-nonxt\0"
+  "\074ipv6-opts\0"
+  "\111rspf\0"
+  "\121vmtp\0"
+  "\131ospf\0"
+  "\136ipip\0"
+  "\142encap\0"
+  "\147pim\0"
+  "\377raw"
+};
+
+static const char *g_aliases;
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void setprotoent_r(int stayopen, FAR struct protoent * result_buf)
+{
+  result_buf->idx = 0;
+}
+
+void endprotoent_r(FAR struct protoent * result_buf)
+{
+  result_buf->idx = 0;
+}
+
+int getprotoent_r(FAR struct protoent *result_buf, FAR char *buf,

Review Comment:
   so `buf` is not used for now?



##########
libs/libc/netdb/lib_protor.c:
##########
@@ -0,0 +1,138 @@
+/****************************************************************************
+ * libs/libc/netdb/lib_protor.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 <errno.h>
+#include <netdb.h>
+#include <string.h>
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+static const unsigned char g_protos[] =
+{
+  "\000ip\0"
+  "\001icmp\0"
+  "\002igmp\0"
+  "\003ggp\0"
+  "\004ipencap\0"
+  "\005st\0"
+  "\006tcp\0"
+  "\010egp\0"
+  "\014pup\0"
+  "\021udp\0"
+  "\024hmp\0"
+  "\026xns-idp\0"
+  "\033rdp\0"
+  "\035iso-tp4\0"
+  "\044xtp\0"
+  "\045ddp\0"
+  "\046idpr-cmtp\0"
+  "\051ipv6\0"
+  "\053ipv6-route\0"
+  "\054ipv6-frag\0"
+  "\055idrp\0"
+  "\056rsvp\0"
+  "\057gre\0"
+  "\062esp\0"
+  "\063ah\0"
+  "\071skip\0"
+  "\072ipv6-icmp\0"
+  "\073ipv6-nonxt\0"
+  "\074ipv6-opts\0"
+  "\111rspf\0"
+  "\121vmtp\0"
+  "\131ospf\0"
+  "\136ipip\0"
+  "\142encap\0"
+  "\147pim\0"
+  "\377raw"
+};
+
+static const char *g_aliases;
+
+/****************************************************************************
+ * Public Functions
+ ****************************************************************************/
+
+void setprotoent_r(int stayopen, FAR struct protoent * result_buf)
+{
+  result_buf->idx = 0;
+}
+
+void endprotoent_r(FAR struct protoent * result_buf)
+{
+  result_buf->idx = 0;
+}
+
+int getprotoent_r(FAR struct protoent *result_buf, FAR char *buf,
+                  size_t buflen, FAR struct protoent **result)
+{
+  int idx = result_buf->idx;
+  if (idx >= sizeof(g_protos))
+    {
+      return ENOENT;
+    }
+
+  result_buf->p_proto = g_protos[idx];
+  result_buf->p_name = (char *)&g_protos[idx + 1];
+  result_buf->p_aliases = (char **)&g_aliases;
+  idx += strlen(result_buf->p_name) + 2;
+  result_buf->idx = idx;
+  *result = result_buf;
+
+  return OK;

Review Comment:
   ```suggestion
     if (idx >= sizeof(g_protos))
       {
         *result = NULL;
       }
     else
       {
         result_buf->p_proto = g_protos[idx];
         result_buf->p_name = (char *)&g_protos[idx + 1];
         result_buf->p_aliases = (char **)&g_aliases;
         idx += strlen(result_buf->p_name) + 2;
         result_buf->idx = idx;
         *result = result_buf;
       }
     return *result == NULL ? ENOENT : OK;
   ```
   or just set `*result` to `NULL` before `return ENOENT;`



-- 
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: commits-unsubscr...@nuttx.apache.org

For queries about this service, please contact Infrastructure at:
us...@infra.apache.org

Reply via email to