Starting my first game - general questions

It depends on whether there is any Network Address Translation (NAT) going on. Most corporate nets use this to save on the number of public IP addresses they need. Cable & DSL routers for connecting multiple home computers to a single broadband connection also use it.

NAT exposes only a single IP address to the internet. You may wonder what happens if more then one computer behind the NAT-router puts out a request from the same port to a server on the internet. How does the remote server send data back and ensure it is routed to the correct computer behind the router? This is achieved by a process called port mapping. When a computer sends a packet out through the NAT router, the senders IP address is replaced with that of the NAT router. The senders port number is replaced with an unused port number on the NAT router. The router places this mapping in a table and will maintain it in that table for an unspecified but large number of minutes. Meanwhile the packet goes to the server, which processes it & then sends it back to the exact IP:port that it came from. The NAT-router receives it, looks up the mapping in it’s routing table and replaced the ‘to’ IP:port combo with that from the table. It can then forward the packet to the computer than originally sent the request.

That is how an HTTP request originating behind a NAT router gets to a webserver & how the reply gets back to the sending computer. Note that it is only the webserver that receives requests on port 80. The client uses any port above 1024 for the outgoing request & this in turn is modified by the router.

The key thing about this protocol is that for data to get from the internet to a particular computer behind the NAT-router, there must be an entry in the routing table. An entry can only get there if:
i) The interchange was initiated by the client (as discussed above)
ii) The user has manually made a permanent entry in the table. This is called port forwarding.
iii) The application running on the client makes an entry in the routing table. This uses Universal Plug and Play (UPnP)

This means that if you have two clients, each behind a NAT-router then they cannot directly initiate communications. This is why a NAT-router functions as a basic firewall. It protects the computers behind it from unsolicited connections. However this really breaks the peer-peer (client to client) communication model. There are four basic solutions:

i) You can run all communication through a server which isn’t behind a NAT router (or if it is, use port forwarding to force an entry in the routing table). For turn based games, you could run this on a webserver using PHP & mySQL, clients would have to poll the server regularly (say once a second) to see if it was their turn yet. A dedicated server would allow custom server software which could maintain a continuous connection, which is more flexible, but costs more. NB. Real time games need a higher polling rate, which means the PHP/mySQL combo is not a realistic option. You might also look at java servlets. These are mostly used as a backend to serving webpages, but you might be able to do more with them. Cost is more than PHP, but less than a dedicated server.

ii) You can run peer-peer using UDP packets (TCP doesn’t work with this) by implementing a simple server which just keeps a list of IP addresses, port numbers for each client & sends the list to any other client that requests it. This is called UDP punchthrough. The key thing with this simple server (called an Introducer) is that it must also listen using UDP. Thus you cannot use a normal webserver to run this service. That potentially puts the cost up of the implementation as you need a dedicated server.

iii) The client uses UPnP to automatically configure the NAT-router to do port forwarding. However this is a very complex protocol to implement and most routers have it switched off anyway as it is a security risk. If a virus got onto your computer, it could use uPnP to open a port on your NAT-router to allow incoming connections, making it useless as a firewall. This is therefore a non-starter.

iv) Accept that your program doesn’t work with clients behind routers. You can ask the user to do manual port forwarding on his or her router (if they have one), to get round the problem.

If you want your application to “just work” it really comes down to option i) or ii).

If you are running a business with a permanently on internet connection, then these are easily possible (especially i) ). However if this is a home project, then to keep costs down, you either need to implement a server using PHP/mySQL and use polling to get game state (only suitable for turn based games) or you need a dedicated server (either to do i) or ii) ). A cheap (but not 100% reliable) solution to getting a dedicated server is to run it on your own broadband connection at home. However you have to watch the bandwidth usage. If there is only a small amount of client-client data communication then the client-server architecture works well and allows you to implement game logic in a central location as well. However if there is a lot of client-client communication, routing it through your server is slower & uses a lot of your bandwidth, in which case peer-peer using UDP Punchthrough is more attractive.

Note that in this case you lack a central server, which means all the game logic has to be in the clients. This can be a problem in some sorts of games. Also note that if you do multiplayer peer-peer, the bandwidth required by the client rises more quickly as more players are added, compared to a client-server architecture. Shouldn’t be a problem with one-on-one type games though.

As you noted, UDP is not a reliable protocol. Thus if you use UDP punchthrough & need reliable transfer, you need to write your own layer on top to do the queuing and retries.

For a simple turn based game, you are probably best off with option i) above. A dedicated server would be easiest, but you might be able to do something using PHP/mySQL (or a servlet) by having each client poll at a given rate (not to fast) to get current game state.

Alan

What about JXTA ? (java peer to peer)

has someone investigated it from a gaming point of view ?

I know there are some IM soft based on it, so it might be suited for non-realtime games…

any insight is welcome.

Lilian

Thanks all for taking the time to reply!

The game is only something I’m doing as a learning experience (I’ve programmed in Java but not done any network programming) but at the same time I’d like to make it my first proper game, play it with my friends and put it on my website for people to download and play if they wish to.

Real-time is really not an issue; even it it took several seconds to update each player’s board it would not be the end of the world. As much I as I understand how I could store the data in MySQL and serve the data to the clients that way I’m trying not to do it this way because I’d like to try something new! I’m not too bothered about the games “just working” - if the user needs to do a little configuration themselves then so be it. Since this is my first venture in Java network programming I think it would be a little too ambitious to attempt option three.

So, let’s go with option four. Does this mean that clients that aren’t going through a router just need to make sure that their firewall will let the connection through on whatever port I use? And if they are behind a router they must manually set up port forwarding?

How are turn-based games usually implemented? Do you usually have a server? Do realtime games generally use UDP?

Thanks again for your replies and for helping out a newbie - it is appreciated!

Yes, although you’ve still got the problem of matching players (and their IP addresses). If you’re testing it with friends you can manually provide the data while in IRC chat, email or even over the phone. Otherwise you need some sort of web based player matching system.

TCP with a server would be a good choice, as the server can control who’s turn it is next. TCP/IP gives reliable transfer, which is needed otherwise commands can get lost resulting in the game hanging. Also you only need to store the game state in one place.

Usually. In realtime you care more about timely arrival of data rather than reliable transfer.

Short answer… interesting for turn-based games.

Too much overhead for anything else.

Generally agree with thsi, a few odd comments,…

Doesnt have tobe web based. A simple matchmaking server is pretty easy to whip up in straight Java code.

As mentioned above, you need a way to find each other. Sometiems thi is done with matchmaking, sometiems other ways. (Ive seen turn based games that actually do all their communication through email!)

You have a lot of options since latency isnt a factor for you.

[quote][quote]
Do realtime games generally use UDP?
[/quote]
Usually. In realtime you care more about timely arrival of data rather than reliable transfer.
[/quote]
There is a lot of debate over how much of this is really technical necssity and how much of this is just prejudice and misunderstanding.

Back at TEN we got great DukeNukem3D play over pure TCP on 14.4 modems!

TCP is mreo compelx then UDP and requires you to udnertsnad mroe abotu the ent to tune it, which is oen of the reasons why game develoerps have often shied away from it.

UDP is definitely faster for unreliable communciation. Once you start needing to communicate reliably though there are strong arguments that TCP and/or TCP/UDP hybrids are goign to perform better the n tryign to reinvent TCP over UDP.

If the virus code is already running on your computer, what is left to lose?

I think disabling UPnP is a little paranoid. If you have a virus scanner that works disabling the UPnP feature isn’t going to offer you much in the way of additional protection. These days it is much more risky to simply launch Internet Explorer or Outlook Express… and if you aren’t running Windows - What virus? :-).

Your bak account.

Your credit card numbers.

Your pay pal password.

etc

A maleficient program that cannot contact its creator is not a security risk. One that can, is.

One that can take incoming contact can also become a paltform for the launching of attacks on other systems. If you really want the FBI confusicating your computer that was used as a jump-point to attack a bank’s system fine. I dont.

All these are reasons why ZoneALarm has been so successful.

Thats good for 3-5 percent of the computing world. I wouldnt want to limit my marekt that much.

But that has nothing to do with enabling UPnP. Once the code is running on your PC nothing is going to stop it from phoning home to port 80 on some server with an HTTP request.

Disabling UPnP is a paranoid way to pretend you are reducing risk, after the fact.

A valid point, but again addressing already compromised systems. Run a virus scanner and avoid the initial problem in the first place - I know they aren’t perfect, but combined with good surfing practices that is really the ONLY thing that will save you. The second malicious code gets to run on your machine it is usually too late to protect your security. You might stop your machine from being a zombie to attack others.

A good firewall can, depending on how you configure it.

I will grant you that many users don’t have that good a firewall, which is a crime since Zonealarm is free.

Sorry I dont agree. Disabling UPNP reduces risks by limiting what a program can do

Enabling UPNP remvoes ANY outbound proitection. And every securiy expert Ive ever talked to agrees.

A valid point, but again addressing already compromised systems. Run a virus scanner and avoid the initial problem in the first place - I know they aren’t perfect,

They are far from perfect. If you hecvnt tried the experiment in a while I suggest you put a virus scanner on an average user’s system and count the minutes til you see your first infection. I gave up and moved my wife to LInux to solve the problem as it was the only solution I ever found that worked for any lenght of time.

And again, with UPNP disabled once you have that virus it can (and has in the past) be used to launch DOS attacks on arbitrary systems at arbitrary ports. WIth UPNP disabled, the worst it can do is attack web servers.

Ok, I agree diabling UPnP reduces “risks” by some amount. I’m only arguing that the risks that it reduces are very insignifcant relatively speaking, since it is offering protection to a system that already has malicous code running on it and so the BIG risk has already happened. Your system is screwed, your data is already lost or stolen and now you are locking doors after knowing that the burgler is already inside.

That might buy you something, but at that point I’m already mad and can’t get much madder :-).

They are far from perfect. If you hecvnt tried the experiment in a while I suggest you put a virus scanner on an average user’s system and count the minutes til you see your first infection.
[/quote]
Sure, but disabling UPnP isn’t going to help you not get that virus.

Key words “once you have that virus” - i.e. it is useful AFTER a succesful attack to possibly limit damage to other systems. I could be altruistic and say I want to protect every other system in the world, while causing a pain in the butt for myself by disabling a useful feature… but I dont’ care that much about the other systems :slight_smile: they have their own protection.

Also UPnP, at least on my system and using the default config of the firewalls that I have used, is not going to prevent OUTGOING connections. That’s already allowed. Only incoming connections are blocked by default. So the worst case is that a zombie can use UPnP to open a port to allow incoming connections to the zombie can receive instructions. The problem being of course, that the Zombie could easily get instructions by polling some other server with outgoing requests, so you are simply locking the back door while leaving the front door wide open.

I still believe that in the grand scheme of things disabling UPnP isn’t helpful. Virus scanners that catch email attachments and scan downloads are certainly not perfect, but much more likely to protect your system from an actual threat than disabling UPnP.

The only good protection is a careful user. How many times have you downloaded a game from these forums and fired it up? Unless you ran it in the Web Start sandbox* each one could easily have installed a virus or deleted your files. Your only real protection would be to disassemble it and check what it really does.

*another great reason to use Web Start and one that Sun should be promoting to end users more.

I fall into the leaving uPNP off camp, since it stops your computer being configured as an open smtp relay. I agree that a virus can poll for instructions, but this location to poll must be hardcoded in the virus & hence can be shutdown once the virus is discovered.

However, more importantly, routers are usually configured with uPnP off by default, so you can’t assume it’s available for publicly distributed applications, which is really where it would be most useful. Asking the user to configure uPNP on their router is likely the flumux the majority of joe public. The minority who can, probably won’t want to for security reasons.

Alan :slight_smile:

;D ;D ;D

(I LOVE linux…)

You mean “a good interactive firewall”. There is no way for a “normal” firewall to do this. IMHO you’re talking about a small subset of firewalling.

Not according to the field of Security Engineering:

Threat: someone wants to get info from inside your LAN out
Measure: you have to stop info being read, or stop it from being transmitted

Evaluation: UPNP is only one of many ways that it can be transmitted. Others that are arguably easier include:

  • piggyback on some well-known program that is bound to get run sooner or later. IM clients are good for this, assuming you are too lazy to go read up some of the Outlook hacks
  • set your process name to “internet explorer” and dial-out. I’d consider this the easiest by far. ZoneAlarm says “internet explorer is trying to access a site”. What do you think most people will do?

You didn’t have any in the first place; be diligent and methodical, look at the attacks and the counter-measures and you’ll see this is de facto true, unless you have put many other things in place (several of which are going to prevent the UPNP class of attacks anyway, at which point the whole UPNP issue becomes moot. )

I don’t want to put a fine point on this, but I can only say your security expert friends differ substantially in their opinions from my “security expert” friends, people such as Ross Anderson.

By that argument, to put it simply, since your going to die anyway if something, you migth as well smoke and drink to excess. Or more directly ,noone shoudl have a firewall, period, because there are other security risks besides the one it covers.

Pretty clearly and obviously faulty reasoning.

No, that’s twisting my words. I was saying “You are already dead. A few more bullets aren’t going to make a difference.”

But part of it rings true… a chain is only as strong as the weakest link. Firewalls DO help, but in my opinion they are far overrated. People treat them like some magic bullet but they are no better than the virus scanners and that you found so ineffective…

I disagree to some degree. yes there are limits to firewalls but the big advantage they have is that they run on seprate uncompromisable hardware (a dedicated box.)

The same is NOT true of any virus scanner or firewall that runs on the target machine.

That probably makes my oibjection to UPnP the clearest as with UPnP the firewall CAN now be compromised by mal-ware running on the target machine. SO I agree with you they are no better then the virus scanner IF you allow UPnP, but they are a lot more secure if you dont.

So there is malicious code on my machine and it uses http: and connects to port 80 of an external server to share my secrets (hardly a limitation). Getting an outbound connection from behind the firewall is rarely a problem. Once the connection is made the information flows in BOTH directions. How will UPnP protect me from that?

Depends on how tight your fiewalll is.

You dont have to let any 80 tcp connection out. You can proxy web connections through a proxy server (which is what we do at SSSun for our corporate network). Besides, you are still ignoring the use of your machine to launmch attacks on OTHER systems. That generally requires an inbound connection. UPnP makes it possible for malware to open op your firewall and allow that inbound connection.

Nothing ia 100% secure, the question is how many threats do you want to be secure against? If you enable UPnP youve made the answer to that “none”.

Not many home users use a proxy server. And even if they did, I’m not sure it would solve the problem without making a lot of sacrifices to the whole surfing experience.

[quote] Besides, you are still ignoring the use of your machine to launmch attacks on OTHER systems.
[/quote]
Yes, intentionally. That’s a secondary effect that only comes about AFTER my computer is compromised. If I protect my computer from being compromised, then I eliminate that threat as well.

[quote]That generally requires an inbound connection.
[/quote]
Keyword “generally”, that is, until someone takes four or five minutes and writes some code to find instructions (and lists of servers in case the main one gets taken down) using polling, it wouldn’t have to poll frequently.

[quote]UPnP makes it possible for malware to open op your firewall and allow that inbound connection.
[/quote]
Not JUST malware, useful stuff that I WANT to run. I tend to avoid running malware :slight_smile: so that leaves UPnP as a useful feature for the stuff I want to run.

[quote]Nothing ia 100% secure, the question is how many threats do you want to be secure against? If you enable UPnP youve made the answer to that “none”.
[/quote]
“none” ?

Enabling UPnP means you will be secure against absolutely NO threats whatsoever? Disabling UPnP is the ONLY thing protecting your computer?

A bit of a stretch don’t you think?

You are more likely to get your machine taken over by visiting a web site with I.E., or opening an email in Outlook Express that has HTML content. Unless you’ve configured I.E. and Outlook to not be able to access the internet… but that is hardly useful.

Are you saying that most OS’s come with malware pre-installed that will use UPnP to open up the ports and turn my machine into a zombie? You DO have to get that malware on your machine in the first place of course, and that is the basis of my point. You need to protect your system as much as you can so that the malware can’t get installed. Once the system is compromised, whatever barriers you have left are going to offer you significantly diminishing returns… to the point where I feel disabling UPnP is not worth the inconvenience.