diff --git a/com.unity.robotics.ros-tcp-connector/Editor/ROSSettingsEditor.cs b/com.unity.robotics.ros-tcp-connector/Editor/ROSSettingsEditor.cs index 8d9ae9a5..e70e8048 100644 --- a/com.unity.robotics.ros-tcp-connector/Editor/ROSSettingsEditor.cs +++ b/com.unity.robotics.ros-tcp-connector/Editor/ROSSettingsEditor.cs @@ -17,7 +17,6 @@ public static void OpenWindow() GameObject prefabObj; ROSConnection prefab; - protected virtual void OnGUI() { if (prefab == null) @@ -48,6 +47,15 @@ protected virtual void OnGUI() new GUIContent("Override Unity IP Address", "If blank, determine IP automatically."), prefab.overrideUnityIP); prefab.unityPort = EditorGUILayout.IntField("Unity Port", prefab.unityPort); + if ((prefab.overrideUnityIP != "" && !ROSConnection.IPFormatIsCorrect(prefab.overrideUnityIP))) + { + EditorGUILayout.HelpBox("Unity Override IP invalid", MessageType.Warning); + } + + if(!ROSConnection.IPFormatIsCorrect(prefab.rosIPAddress)) + { + EditorGUILayout.HelpBox("ROS IP is invalid", MessageType.Warning); + } EditorGUILayout.Space(); EditorGUILayout.LabelField("If awaiting a service response:", EditorStyles.boldLabel); prefab.awaitDataMaxRetries = EditorGUILayout.IntField( diff --git a/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs b/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs index 2dbd2918..eebc18e9 100644 --- a/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs +++ b/com.unity.robotics.ros-tcp-connector/Runtime/TcpConnector/ROSConnection.cs @@ -8,6 +8,7 @@ using System.Text; using System.Threading.Tasks; using Unity.Robotics.ROSTCPConnector.MessageGeneration; +using System.Globalization; using UnityEngine; using UnityEngine.Serialization; @@ -224,11 +225,15 @@ void OnEnable() private void Start() { + if(!IPFormatIsCorrect(rosIPAddress)) + Debug.LogError("ROS IP address is not correct"); InitializeHUD(); Subscribe(ERROR_TOPIC_NAME, RosUnityErrorCallback); if (overrideUnityIP != "") { + if(!IPFormatIsCorrect(overrideUnityIP)) + Debug.LogError("Override Unity IP address is not correct"); StartMessageServer(overrideUnityIP, unityPort); // no reason to wait, if we already know the IP } @@ -588,5 +593,33 @@ private void WriteDataStaggered(NetworkStream networkStream, string rosTopicName networkStream.Write(segmentData, 0, segmentData.Length); } } + + public static bool IPFormatIsCorrect(string ipAddress) + { + if(ipAddress == null || ipAddress == "") + return false; + + // If IP address is set using static lookup tables https://man7.org/linux/man-pages/man5/hosts.5.html + if(Char.IsLetter(ipAddress[0])) + { + foreach(Char subChar in ipAddress) + { + if(!(Char.IsLetterOrDigit(subChar) || subChar == '-'|| subChar == '.')) + return false; + } + + if(!Char.IsLetterOrDigit(ipAddress[ipAddress.Length - 1])) + return false; + return true; + } + + string[] subAdds = ipAddress.Split('.'); + if(subAdds.Length != 4) + { + return false; + } + IPAddress parsedipAddress; + return IPAddress.TryParse(ipAddress, out parsedipAddress); + } } } \ No newline at end of file