Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ public static void OpenWindow()

GameObject prefabObj;
ROSConnection prefab;

protected virtual void OnGUI()
{
if (prefab == null)
Expand Down Expand Up @@ -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(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
using System.Text;
using System.Threading.Tasks;
using Unity.Robotics.ROSTCPConnector.MessageGeneration;
using System.Globalization;
using UnityEngine;
using UnityEngine.Serialization;

Expand Down Expand Up @@ -224,11 +225,15 @@ void OnEnable()

private void Start()
{
if(!IPFormatIsCorrect(rosIPAddress))
Debug.LogError("ROS IP address is not correct");
InitializeHUD();
Subscribe<MRosUnityError>(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
}

Expand Down Expand Up @@ -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]))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is this looking for a hostname instead of an IP? Can you please add a comment.

{
foreach(Char subChar in ipAddress)
{
if(!(Char.IsLetterOrDigit(subChar) || subChar == '-'|| subChar == '.'))
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would you mind adding a comment describing why these characters are invalid?

return false;
}

if(!Char.IsLetterOrDigit(ipAddress[ipAddress.Length - 1]))
return false;
return true;
}

string[] subAdds = ipAddress.Split('.');
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok, now let's remove this and the if statement, and I think we're done

if(subAdds.Length != 4)
{
return false;
}
IPAddress parsedipAddress;
return IPAddress.TryParse(ipAddress, out parsedipAddress);
}
}
}