You are viewing a plain text version of this content. The canonical link for it is here.
Posted to commits@nuttx.apache.org by GitBox <gi...@apache.org> on 2021/12/29 02:22:56 UTC

[GitHub] [incubator-nuttx-apps] normanr opened a new pull request #944: Add a system tool to get/set hostname

normanr opened a new pull request #944:
URL: https://github.com/apache/incubator-nuttx-apps/pull/944


   ## Summary
   Add a tool to get/set hostname
   
   ## Impact
   Userspace can now get and set the system hostname
   
   ## Testing
   Tested by setting hostname, and getting it back. Checked that the hostname was used in dhcp requests.


-- 
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-unsubscribe@nuttx.apache.org

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



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on a change in pull request #944: Add a system tool to get/set hostname

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on a change in pull request #944:
URL: https://github.com/apache/incubator-nuttx-apps/pull/944#discussion_r776143843



##########
File path: system/hostname/hostname_main.c
##########
@@ -0,0 +1,173 @@
+/****************************************************************************
+ * apps/system/hostname/hostname_main.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 <stdio.h>
+#include <stdlib.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+
+#include <net/if.h>
+#include <arpa/inet.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_HELLO_IPADDR     0xc0a8eacd
+#define CONFIG_HELLO_PORT       81
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Socket related variables */
+
+static int g_listen_fd;
+
+static struct sockaddr_in g_sa;
+
+int initialize_socket(void)
+{
+  /* Prepare listen address */
+
+  g_sa.sin_family      = AF_INET;
+  g_sa.sin_port        = HTONS(CONFIG_HELLO_PORT);
+  g_sa.sin_addr.s_addr = HTONL(CONFIG_HELLO_IPADDR);
+
+  /* Open listening socket */
+
+  g_listen_fd = socket(g_sa.sin_family, SOCK_DGRAM, 0);
+
+  /* Verify socket creation */
+
+  if (!(g_listen_fd > 0))
+    {
+      return -1;
+    }
+
+  return 0;
+}
+
+int get_phy_id(void)
+{
+  int ret;
+  uint16_t phy_id;
+  struct ifreq ifr;
+
+  /* Prepare ifreq */
+
+  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
+
+  ret = ioctl(g_listen_fd, SIOCGMIIPHY, (unsigned long) &ifr);
+
+  printf("call to %s() ", "ioctl");
+
+  if (ret != 0)
+    {
+      printf("failed! (value %d, errno %d)\n", ret, errno);
+      return -1;
+    }
+
+  phy_id = ifr.ifr_mii_phy_id;
+
+  return phy_id;
+}
+
+int get_phy_reg(uint16_t phy_id, uint16_t reg_num, uint16_t *val)
+{
+  int ret;
+  struct ifreq ifr;
+
+  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
+
+  ifr.ifr_mii_phy_id = phy_id;
+  ifr.ifr_mii_reg_num = reg_num;
+
+  ret = ioctl(g_listen_fd, SIOCGMIIREG, (unsigned long) &ifr);
+
+  if (ret == OK)
+    {
+      *val = ifr.ifr_mii_val_out;
+    }
+
+  return ret;
+}
+
+int set_phy_reg(uint16_t phy_id, uint16_t reg_num, uint16_t val)

Review comment:
       should we remove all unrelated function and header file?




-- 
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-unsubscribe@nuttx.apache.org

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



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 merged pull request #944: Add a system tool to get/set hostname

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 merged pull request #944:
URL: https://github.com/apache/incubator-nuttx-apps/pull/944


   


-- 
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-unsubscribe@nuttx.apache.org

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



[GitHub] [incubator-nuttx-apps] normanr commented on a change in pull request #944: Add a system tool to get/set hostname

Posted by GitBox <gi...@apache.org>.
normanr commented on a change in pull request #944:
URL: https://github.com/apache/incubator-nuttx-apps/pull/944#discussion_r776152385



##########
File path: system/hostname/hostname_main.c
##########
@@ -0,0 +1,173 @@
+/****************************************************************************
+ * apps/system/hostname/hostname_main.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 <stdio.h>
+#include <stdlib.h>
+
+#include <unistd.h>
+#include <string.h>
+#include <errno.h>
+#include <sys/socket.h>
+#include <sys/ioctl.h>
+#include <netinet/in.h>
+
+#include <net/if.h>
+#include <arpa/inet.h>
+
+/****************************************************************************
+ * Pre-processor Definitions
+ ****************************************************************************/
+
+#define CONFIG_HELLO_IPADDR     0xc0a8eacd
+#define CONFIG_HELLO_PORT       81
+
+/****************************************************************************
+ * Private Data
+ ****************************************************************************/
+
+/* Socket related variables */
+
+static int g_listen_fd;
+
+static struct sockaddr_in g_sa;
+
+int initialize_socket(void)
+{
+  /* Prepare listen address */
+
+  g_sa.sin_family      = AF_INET;
+  g_sa.sin_port        = HTONS(CONFIG_HELLO_PORT);
+  g_sa.sin_addr.s_addr = HTONL(CONFIG_HELLO_IPADDR);
+
+  /* Open listening socket */
+
+  g_listen_fd = socket(g_sa.sin_family, SOCK_DGRAM, 0);
+
+  /* Verify socket creation */
+
+  if (!(g_listen_fd > 0))
+    {
+      return -1;
+    }
+
+  return 0;
+}
+
+int get_phy_id(void)
+{
+  int ret;
+  uint16_t phy_id;
+  struct ifreq ifr;
+
+  /* Prepare ifreq */
+
+  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
+
+  ret = ioctl(g_listen_fd, SIOCGMIIPHY, (unsigned long) &ifr);
+
+  printf("call to %s() ", "ioctl");
+
+  if (ret != 0)
+    {
+      printf("failed! (value %d, errno %d)\n", ret, errno);
+      return -1;
+    }
+
+  phy_id = ifr.ifr_mii_phy_id;
+
+  return phy_id;
+}
+
+int get_phy_reg(uint16_t phy_id, uint16_t reg_num, uint16_t *val)
+{
+  int ret;
+  struct ifreq ifr;
+
+  strncpy(ifr.ifr_name, "eth0", IFNAMSIZ);
+
+  ifr.ifr_mii_phy_id = phy_id;
+  ifr.ifr_mii_reg_num = reg_num;
+
+  ret = ioctl(g_listen_fd, SIOCGMIIREG, (unsigned long) &ifr);
+
+  if (ret == OK)
+    {
+      *val = ifr.ifr_mii_val_out;
+    }
+
+  return ret;
+}
+
+int set_phy_reg(uint16_t phy_id, uint16_t reg_num, uint16_t val)

Review comment:
       Woops, left over from mdio_main that I copied as a template. Removed.




-- 
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-unsubscribe@nuttx.apache.org

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



[GitHub] [incubator-nuttx-apps] normanr commented on pull request #944: Add a system tool to get/set hostname

Posted by GitBox <gi...@apache.org>.
normanr commented on pull request #944:
URL: https://github.com/apache/incubator-nuttx-apps/pull/944#issuecomment-1002429384


   fwiw, `uname -n` can also be used to get the hostname, but it's not as intuitive, and doesn't support setting the hostname (which inspired adding the tool).


-- 
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-unsubscribe@nuttx.apache.org

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



[GitHub] [incubator-nuttx-apps] normanr commented on pull request #944: Add a system tool to get/set hostname

Posted by GitBox <gi...@apache.org>.
normanr commented on pull request #944:
URL: https://github.com/apache/incubator-nuttx-apps/pull/944#issuecomment-1002432584


   A related question: I want to automatically include a unique identifier from the board, eg network mac address or something, in the hostname. What do you think the best way to achieve that would be? Obviously writing a small userspace tool to generate the hostname which can be fed to the `hostname` tool via back quotes would work, but maybe an option to append `BOARDCTL_UNIQUEID` to `LIBC_HOSTNAME` would be viable? I'd prefer to avoid it in `board_app_initialize`. (Also I noticed `NETUTILS_DHCPC_HOST_NAME`, maybe it should just be replace with `LIBC_HOSTNAME` these days?)


-- 
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-unsubscribe@nuttx.apache.org

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



[GitHub] [incubator-nuttx-apps] xiaoxiang781216 commented on pull request #944: Add a system tool to get/set hostname

Posted by GitBox <gi...@apache.org>.
xiaoxiang781216 commented on pull request #944:
URL: https://github.com/apache/incubator-nuttx-apps/pull/944#issuecomment-1002444962


   > fwiw, `uname -n` can also be used to get the hostname, but it's not as intuitive, and doesn't support setting the hostname (which inspired adding the tool).
   
   It's fine to add hostname program since it exists on FreeBSD/Linux too.
   
   
   > A related question: I want to automatically include a unique identifier from the board, eg network mac address or something, in the hostname. What do you think the best way to achieve that would be? Obviously writing a small userspace tool to generate the hostname which can be fed to the `hostname` tool via back quotes would work, but maybe an option to append `BOARDCTL_UNIQUEID` to `LIBC_HOSTNAME` would be viable? 
   
   you can create a PR and see the feedback from other.
   
   > I'd prefer to avoid it in `board_app_initialize`. (Also I noticed `NETUTILS_DHCPC_HOST_NAME`, maybe it should just be replace with `LIBC_HOSTNAME` these days?)
   
   or let `NETUTILS_DHCPC_HOST_NAME` default value to `LIBC_HOSTNAME`.


-- 
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-unsubscribe@nuttx.apache.org

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