Peer-to-Peer application using java - java

I want to develop a p2p app using java and netbeans IDE. In my app i want to make two things
file sharing between users
Chat between users
I have read from different resources
Introduction to the Peer-to-Peer Sockets Project
Peer-to-Peer File Sharing
Problem is how i am able to connect two or more pcs connected to different networks. Showing all connected peers, file sharing of any type under a size limit. Any help will be appreciated.

These questions are dealing with the most well known problem of NAT traversal, dealing with the NAT traversal is not related with the language you are using rather its about the basic problem domain. First of all you need to advertise the address(es) of your both end using some signalling method (i.e. SIP, Jingle ..). Now if you are trying to connect two end point which resides behind same network or NAT then simply knowing or advertising the local host candidates will do the job, but if any of your end point resides out side of the network or having NAT between two endpoints then you need to traverse the NAT using protocols like STUN/TURN/ICE. Based on your use cases, you may interested to read the RFCs like RFC-5389, RFC-5245 and RFC-5766. Once you know what you need you might get any open source solution by google, or If you are curious enough you may start implementing your solution based on the RFCs. So the keywords like NAT traversal or STUN/TURN/ICE might help you to know your problem first, once you know your problem domain then you can find solution.

Now if you are trying to connect two end point which resides behind same network or NAT then simply knowing or advertising the local host candidates will do the job, but if any of your end point resides out side of the network or having NAT between two endpoints then you need to traverse the NAT using protocols like STUN/TURN/ICE.

Related

Secure communications between Java server and Arduino clients: need a suggestion

There is a Java server and many Arduino devices, and they are connected via TCP connection. Board that we can use: Arduino Uno or Arduino Mega 2560
Each Arduino device is actually reporter (say, it takes data from water meter and transmits data to the server) or some kind of remote control for some appliances.
Currently, this connection isn't secure. I need to make it secure, i.e. implement VPN.
I'm completely new in this field of knowledge, and I'm trying to figure out the way should I use.
I really hope that I can find existing implementation of some protocol, and build it into this system.
But which protocol?
I already found out that I can't use IPSec, since TCP/IP stack is hardware-implemented in these Arduino devices, so, we can't modify it.
Therefore, I need to use some protocol higher than TCP, and I need implementation in C (for client devices) and in Java (for server).
I'm trying to find implementations of SSL, or PPTP, or L2TP, or something else which I still don't know about.
If anyone have experience in this field, I would be glad to see your suggestions.
Response to your follow up question got too long...
VPN usually creates a "tunnel" into an infrastructure- say, the University computer system. That is, the VPN concentrator "sits st the perimeter" of the university network, and when you connect to it, you create a "pass through" into the system behind the firewall - any protocols, any IP addresses inside the firewall become accessible. Key is that anyone observing the flow of traffic from your computer to the concentrator (also called "VPN gateway") only sees you talking to the concentrator - they don't know what IP addresses inside the firewall you are talking to, with what protocol, or what data. By contrast if you don't need to hide all that stuff you just have to encrypt your data itself; a simple encryption algorithm is easy to implement, especially if you don't need to worry about people stealing your devices and getting the code. How complex the encryption needs to be will depend on your application- there is a trade off between speed, memory use, and security.
Goog. le "Arduino encryption library" for some examples; pick a key, then encrypt your data, and just POST it...
Let us know how you make out!
I very much agree with #Floris. To add some ideas to that:
What about HTTPs? If your communication is one-way (Arduino -> PC) then it should be sufficient for your needs. This issue is discussed in this post, with references to this discussion. HHTPs on Arduino is not easy, but it may have been done.
Apparently, XXTEA is another alternative, and there are several stackoverflow questions on this topic as well.
If you go the -build-it-yourself route, check out this post on electronics.SE: apparently there is a cryptography library for AVR, and also useful list of attacks to consider in the electronics.SE post: are you worried only about MitM attack? What if someone rips your device open the reads the keys, is that a problem?
Here is described an implementation of the [CHAP] (en.wikipedia.org/wiki/Challenge-Handshake_Authentication_Protocol) for Arduino/AVR devices.
And here is described a similar implementation of [HMAC] (en.wikipedia.org/wiki/Hash-based_message_authentication_code) and [SHA256] (en.wikipedia.org/wiki/SHA-2) for Arduino/AVR.
[Cryptosuite] (github.com/wgoulet/Cryptosuite) is an implementation of HMAC-SHA-256 for Arduino.
Finally, [here] (github.com/arpitchauhan/cryptographic-protocols-arduino-and-PC) are demonstrated some cryptographic procolos for Arduino, including key exchange using RSA.
So it is definitely possible to secure communication between Arduino and a server.

Simply opening a port in Java

I am coding a program that necessitates having two machines talk to each other via TCP.
I have a (very) basic familiarity with the java.net Socket classes and related things. I am worried about routers/firewalls blocking the connection. All I need to do is get a port open in order to use the Java STL Socket classes.
I was told to look into using upnp by a friend, and another stackoverflow question regarding upnp suggested using Cling. As a network programming novice, Cling seems like overkill for this (and I am not advanced enough to fully understand the manual).
I am sorry if I am asking the wrong question, looking in the wrong place, etc. All I want to do is connect two machines to send some floats back and forth without having to manually go into my router and forward ports.
Is there anything in the STL that does this automatically, or any simple libs?
First thing first: there's no such thing as "STL" in Java. You're probably referring to the basic Java SDK.
Second, you are asking for "simple means" to perform an operation that is, from a networking point of view, not trivial at all. Look at the most basic task that one of your programs (program A) will have to do: denoting the network location of its "target" (program B). If program B is behind a router, what do you know about program B's network location? nothing. All you know is the router's address.
Same with firewalls. You're concerned about firewalls blocking your connection? well, if your firewalls are any good, then you want them to block all connections except for pre-approved ones.
For programs separated by a NAT, the only sensible method to go about doing what you're looking for is to use UPnP. If Cling is an overkill, try something like weupnp.
There's no free lunch, though.
If there is no network "masking" device (such as a router) between program A and program B, then very simple TCP/IP programming using the Java SDK should just work. If there are networking devices along the ways, you'll have to cope with them by means of configuring your devices, or using UPnP.
I would take a long look at the following tutorial: http://www.giantflyingsaucer.com/blog/?p=224
The idea here is that a library called xsocket creates events that trigger when new information arrives.

Instant Messenger, basic project in Java

I'm currently working on trying to create a very basic bare bones instant messenger with Java.
I'm at a bit of a loss where to begin, was wondering if anyone here could reccommend a starting point? What I should read? What would be the easiest way for a beginner/intermediate programmer to try and achieve this?
Currently, I'll I'd want it to be able to do is to send text to one machine to another. I am so far unfamiliar with anything network related in java. Would I use a server or just direct connect between two machines?
Any pointers/direction or advice anyone has in this matter would be very helpful!
Firstly check out the official resource for Socket programming, the article on writing a Client/Server Pair should provide you with what you need to know to create a instant messenger.
Well, I am focusing the second question of yours.
If you're building this program for your local area network and if you're intended to use socket programming, you need to find the exact ip address of the machine. If you don't know the ip address ( I mean if you don't want the program to know ip address before you run it ) ,then you can ping local ip addresses until you find some peers.
Some of the p2p applications use servers as the ip matchers. This means that the servers have ip address tables which make all of the users send/receive data by using these servers. But if you just started on networking with this project, I think don't use server.
Lastly, you can take a look at a project of my own. It's a simple parallel document preparation program with a chat module inside it. It's developed in C#. It needs wireless ad hoc network to run. Link : http://code.google.com/p/parallel-docs/
I don't think this type of project is ever going to be a beginner project. I'm absolutely not discouraging you to try it, I think you should. I'm always in over my head with stuff like this but I look it up and ask questions and usually end up learning something. My advice would be no matter how simple of a program it is, start with some Class Diagrams, if nothing else just to get organized before jumping into the code. For example you know you you'll need a user account object right off the bat and you know you'll need certain attributes for that object (username, password, etc.), I encapsulate everything so then you would need methods to get and set these fields and so on. I've found that by doing that even if I don't create complete proper UML diagrams getting the easy stuff out of the way and if somewhat organized first if nothing else frees me to focus on the more complex logic and things I may not yet know how to implement.

communicate with computer that isn't on the same network

I want to communicate between two computers that aren't on the same network, i tried to use sockets, but I only found tutorials and examples for communicating in the same network.
I tried to search on stackoverflow, but I had no clear idea of what to search for.
I want to send text two-way (chat).
-edit- The thing is, it is to me vague what I have to use or do, so I can't be much clearer then to say I want to send two way text (perhaps date) for a chatapp, just to learn from.
-edit-
To communicate between two machines, they have to be on the same extended network. The only other option is to save data to external drives, e.g. USB drives, and you can attach to the second machine and up load.
You can use sockets to communicate with any other machine with an Internet connection, provided your firewall(s) and configured to allow the connection. The whole point of a firewall is to prevent unauthorised access. I suspect this is your real problem. If your firewalls prevent access, you should not be able to get around this using Java (google network hacking for alternatives ;)
Another option is to use a service such as email if the two computers cannot talk to each other directly (they still have to be connected to the same network) This is much more complex than using a direct connection but it can be done.
I suggest instead you configure your firewall(s) correctly and you will be able to use plain Sockets.
You have to get the external IPs (there are sites which while visiting give you this IP), and hope that the firewall allows communication. Typically one should use a port above 1000.
There should be peer-to-peer libraries.
Another way is via SSH on one computer (JSch is one java library for SSH).
The easiest way is email exchange: the javax.mail API is compact but not the easiest. (Especially you could easily delete all mails, as I did once.)

A Packet Blocker Software

Can any one suggest me a packet blocking software ??
My requirement is to block packets within the LAN.(Internet does not come into picture).
Supposing CLIENT_A communicating to CLIENT_B in the same network(LAN).
I want a CLIENT_C (who is in the same network) to capture(I can use jpcap library's sniffer for this purpose) and block packets sent by CLIENT_A to CLIENT_B.
How do i block packets over LAN..?
Thank you in advance.
Regards,
Veenit Shah
This kind of thing is normally done using a separate firewall (e.g. in a router or gateway box) or firewall software running on one or both of the client machines.
This is not the sort of thing that it is sensible to implement in Java.
EDIT - in response to this followup
I am aware that such a thing is not advisable in Java..but still ill have to implement it..so is there any means to do so..?
Let us assume that you are talking about implementing a client-side firewall on a Linux machine. I can think of two approaches:
You could use Process.execute() and friends to run the Linux iptables(8) admin utility which manipulates the OS kernel's network packet filters. This is the simplest Java-based approach. But it requires that your Java app runs as root.
You could reverse engineer what iptables(8) is doing to manipulate the packet filters and code the same functionality in Java. That would be more coding work, including implementing parts of the functionality in C via JNI or JNA. And your app needs to run as root.
But a far, far simpler approach is to simply run iptables(8) from the command line, or make your changes using the fancy GUI-based admin tools.
Note that in the scenarios above, the firewall itself is not implemented in Java. All you are doing is administering the firewall from a Java application. I cannot think of ANY way to actually do the filtering / blocking in Java that is even remotely practical.
You may need some arp attack things to redirect packets from CLIENT-A to Client-C instead of CLIent-B.
I don't think this is a good idea.
If you are reading the packet on the network on Client C, Client B has already received it so it is too late to block it. If you are using switched ethernet, then Client C wouldn't even see the packet that was sent to B anyway.
The only way to do this is to have A communicate to B through C then C can decide if packets should be sent. This is called a firewall. Instead of writing one, you could use iptables on Linux to do this.
But to make use of this, you need to understand how the network works and I'm not sure you do at this point based on your question. So you will need to learn a lot about ethernet (assuming you are using ethernet) and the different networking layers. I'm not sure of a resource for this.

Categories

Resources