I’ve been working with a LAMP server with Ubuntu on a virtual PC. I came to the part where I had to identify the grid of the object that was calling my API. Although the llHTTPRequest method passes a few headers that help identify objects, owners, and locations, they do not provide any information as to what grid they are on. They do identify the difference between the preview grid, and the production grid through the shard header, stating “Production” or “Testing”. How do I know if it is a region on the official Second Life™ grid? By default, Open Simulator uses “OpenSim” as the value for shard, but it too does not identify the grid.
After much research and hacking away at a few hours of code, I believe I have found the answer. With PHP, I can capture the simulators IP address. If the simulators are ever put behind a proxy, or start using IPv6 addresses, my solution breaks. So now I also have a list of IP addresses that Linden Lab has ownership of.
I created one table that identifies grids that have access to my API. I created one entry to represent the official grid, Agni. I then created a second table to store all addresses associated with the grid. Rather than storing an individual address for each valid IP address (over 20 thousand of them), I got smart and decided to use the IP and Network Mask to represent a range of IP addresses. MySQL offers a method to convert IPv4 addresses to their numeric equivalent and back again. Rather than going through the trouble of datatype problems between PHP’s signed integers, the nature of IPv4 as unsigned, and MySQL unsigned integers, I decided to let MySQL do all the work for me.
In the end, I have a simple query to ask my database, “What grid does the IP 220.127.116.11 belong to?”. This is how I can rephrase the question that the database can understand.
select GridId from GridAddress where INET_ATON('18.104.22.168') between GridAddressNumber and (GridAddressNumber + pow(2, (32-GridAddressMask))-1)
With only a handful of records to search through, the answer comes back quick. “Hey, this is for the Agni grid”.
With this capability, I can then build upon the features over time. If a grid gets a new IP range, or just one, it would be easy to add it to the database. If I wanted to add the address 192.168.0.1, then I use a simple query:
insert into GridAddress (GridId, GridAddressNumber, GridAddressMask) values (1, INET_ATON(‘192.168.0.1’), 32)
This is great for the official grid. However, other grids will start to pose a problem. Most grids, such as OsGrid, have many servers with seemingly random IP addresses since the simulators are at random places on the internet instead of one companies ip range. To represent an Open Simulator grid with a ton of regions would require thousands of records instead of maybe 10. The other problem is that someone may have a server on one grid, and then switch the sim to use another grid. They may even be running two simulators behind the same IP address, but on two different grids. This instability causes havoc in trying to identify which grid a particular script is running from.
This brings me back to the original problem of llHTTPRequest headers. If it became a standard to also include the grid name, then this would be less of a problem. Sure, OpenSim hosts could choose “Agni” as the name of their grid. I don’t know where I’m going with this, but I would like to see an additional header for X-SecondLife-Grid (SVC-4883) that would have a value of “Agni” on the main grid. For anyone specifying Agni, I can validate that against the network addresses for Second Life. As for everyone else, I can choose either validation by IP, Grid Name, or to simply let it go through but with minimal trust.