Telnet Clients and Their Treatment of EOL - c

This is a rather convoluted question, and for that I apologize. I wrote a Linux C sockets application, a basic framework for a simplistic chat server. The server is running on my laptop. The client is Telnet at the moment until I write a designated client application (that'll be more secure, hopefully). There are better applications for sending generic network data from a client end, I know, but I got interested about why a certain thing happens on one Telnet client but not another.
The first Telnet client test was on another Linux laptop. It works as expected. The next, however, was a Blackberry app called BBSSH that allows Telnet and SSH connections. I went via the Telnet option, and it works too. Except, it doesn't exactly.
The server code does the usual read call to retrieve a block of data, which gets treated as a string, i.e. a message. The former client reads until I hit enter, and then it sends one string of characters. The BB app, however, sends every single character as if I've been pressing enter after each of them, which I haven't. Obviously this is something to do with buffering, what certain clients class as a EOL from the user input, etc. I just can't pinpoint it.
To illustrate, here is the server outputting messages it's received from the clients.
First, the message from the Linux client:
client name: this is a test
Now, for BBSSH:
client name: t
client name: h
client name: i
client name: s
client name:
client name: i
client name: s
client name:
client name: a
client name:
client name: t
client name: e
client name: s
client name: t
Any help?

Telnet clients can operate in line-mode or character-mode. The BBSSH client seems to be operating in character-mode for some reason.
It's possible that your server could force the client into line-mode by sending the client an instruction to that effect during the negotiation that takes place at the start of a telnet connection.
The byte sequence your server would need to send to the client is 0x255 0x253 0x34, which translates as "Interpret As Command, Do, Linemode". If the client is willing/able to operate in line-mode, it should reply with 0x255 0x251 0x34 ("Interpret As Command, Will, Linemode").
If this is all new to you (i.e. your telnet server currently doesn't do any negotiation at all), google for terms like "telnet negotiation" or take a look at some of the relevant RFCS (RFC 854 is Telnet itself, RFC 1184 covers the Linemode option).

TCP is stream-orientated, and so there's no such thing as a "message". You might receive all the data together, or just a fragment of it at a time.
In your case, you might want to buffer anything received until you hit an EOL marker.

Related

How let telnet server mandates telnet client to run in passive mode?

I have written a basic Telnet Server in C language, and I am testing it against some telnet clients.
However, depending on the client I use (PuTTY, for example) then the client acts as Active by default.
Is there a way for the server to always mandate the client to act in passive mode?
I am asking this because, in my understanding, if the server mandates it, then I do not need to implement any protocol-specific details, only the basics to handle this mandatory passive mode. So, my Telnet Server will be at the end a simple TCP socket.
Update 1:
Here I describe the data received when a client connects to the server:
Launch the Telnet Server
Telnet Client Connects (and sends telnet protocol data)
received = {ffff1fffff20ffff18ffff27ffff01ffff03ffff03}
The data is hex encoded, and by decoding it according to the Telnet specification, the client is trying to establish some configuration before any data starts to be exchanged.
On the Telnet Server side, I simply ignore received data starting with 0xff (IAC code).
That solves the problem for a while.
Type some data on the Client side and hit enter
Telnet Server receives the data
Type some data on the Client side and hit enter
Telnet Server receives the data + telnet protocol data
Here is the received data (I sent "123456"):
received = {616263646566ffff18ffff27ffff01ffff03ffff03}
As you can see, I received what I sent, but I also got some telnet protocol stuff. That is because the Telnet Client is in Active mode.
I wish that at some intermediate step between 2 and 3, the Telnet Server could set the Telnet Client to run in Passive mode, so, I would not receive any Telnet Protocol data at step 6.
Update 2:
As requested in the comments for more details about the "Passive mode", I mean the Telnet negotiation mode: (1) Passive mode: where the Telnet Client/Server will not send any negotiation data, only user data; (2) Active mode: where the Telnet Client/Server will send the negotiation data to configure/handshake what are the features to be used/set.
And yes, I also could not find that to be specific to the protocol, only on PuTTY documentation
TL;DR: The Telnet protocol and well-known options offer no support for what you describe.
I mean the Telnet negotiation mode: (1) Passive mode: where the Telnet Client/Server will not send any negotiation data, only user data; (2) Active mode: where the Telnet Client/Server will send the negotiation data to configure/handshake what are the features to be used/set.
The Telnet specifications do not define a "passive mode" by any name. There is no defined way for one side to request that the other refrain from attempting to negotiate protocol options, much less to refrain from sending any protocol commands at all.
On the Telnet Server side, I simply ignore received data starting with 0xff (IAC code).
There are many more Telnet commands than those related to option negotiation. Ignoring all Telnet commands makes your server not only impolite, but deficient. You reference PuTTY's "passive mode", but even in that mode, PuTTY still emits Telnet commands for purposes other than option negotiation, and it likely still performs option negotiation too, albeit after allowing the server to negotiate first.
Additionally,
Option negotiation and other protocol commands are asynchronous, which is why the approach you describe doesn't stall communication altogether, but that does not make it valid to ignore protocol commands. The server should emit a response to each option negotiation command received, even if that response is negative.
And that will make it less likely for clients to make renewed attempts to negotiate the same options, but the server cannot say "never" to any option request, only "no".
Although it is not obligated to accept requests or offers to enable non-default options, the server is obligated to honor requests to disable options:
Clearly, a party may always refuse
a request to enable, and must never refuse a request to disable some option
(RFC 854, p.2)
The only way for a Telnet endpoint to receive a data byte with value 255 (decimal) is via the IAC mechanism.
If a Telnet endpoint does not respond to AYT commands, then the other endpoint may sometimes conclude that the session has been dropped when in fact it is still active.
A Telnet endpoint that ignores protocol commands does not support standard, expected terminal operations including break signals, interrupt process signals, erase character and erase line commands, and terminal synch. If yours is a special-purpose Telnet implementation then perhaps some of these do not actually require any server-side action, but the synch, at least, is about the data stream between server and client, and I don't see any way that a conforming Telnet implementation can fail to provide for it.

How do apps like LogMeIn and TeamViewer work?

There's already a question How exactly does a remote program like team viewer work which gives a basic description, but I'm interested in how the comms works once the client has registered with the server. If the client is behind a NAT then it won't have its own IP address so how can the server (or another client) send a message to it? Or does the client just keep polling the server to see if its got any requests?
Are there any open source equivalents of LogMeIn or TeamViewer?
The simplest and most reliable way (although not always the most efficient) is to have each client make an outgoing TCP connection to a well-known server somewhere and keep that connection open. As long as the TCP connection is open, data can pass over that TCP connection in either direction at any time. It appears that both LogMeIn and TeamViewer use this method, at least as a fall-back. The main drawbacks for this technique are that all data has to pass through a TeamViewer/LogMeIn company server (which can become a bottleneck), and that TCP doesn't handle dropped packets very well -- it will stall and wait for the dropped packets to be resent, rather than giving up on them and sending newer data instead.
The other technique that they can sometimes use (in order to get better performance) is UDP hole-punching. That technique relies on the fact that many firewalls will accept incoming UDP packets from remote hosts that the firewalled-host has recently sent an outgoing UDP packet to. Given that, the TeamViewer/LogMeIn company's server can tell both clients to send an outgoing packet to the IP address of the other client's firewall, and after that (hopefully) each firewall will accept UDP packets from the other client's Internet-facing IP address. This doesn't always work, though, since different firewalls work in different ways and may not include the aforementioned UDP-allowing logic.

Telnet enable linemode from server

I am working on a line-oriented telnet server, not a client.
Currently, to set up the connection, I am using:
IAC DONT ECHO
IAC DONT SURPRESS-GO-AHEAD
IAC DO LINEMODE
IAC DO NAWS
Currently, the server only handles IAC AYT, and NAWS-related stuff. Anything else is rejected, like so:
IAC DONT <OPTION>
However, the server still gets characters one-at-a-time.
According to here, ECHO and SGA (at the same time) enable character-at-a-time mode. However, I have both disabled.
How can I make the client send data one line at a time?
I am using the telnet from GNU inetutils to test this.
This question's title is similar but, contrary to its title, concerns writing a telnet client.
I just checked my old telnet server source and I send WILL and WONT from the server side. I handled DO and DONT from the client.
Are you actually trying to code a true telnet server, or just a server that is compatible with telnet that does sends data a line at a time (like a text based chat or game server)? If the latter, you can do away with most of your options because in my experience, telnet clients default to line at a time.
For my game server (MUD++) all I ever did in standard game play mode was to disable / enable echo during password state in login, or kick into character mode and start a pseudo tty when the user invoked the PICO editor.
UPDATE: If you plan to seriously write a telnet server or any other network software, I really like the classic W. Richard Stevens series. TCP/IP Illustrated Volume 1, The Protocols covers telnet in a practical sense, and coupled with the RFCs like 1184 (if I recall) you'll have everything you need.

ftp server in C

I have to develop 1 FTP (simple) server in C.
What do i mean with simple??
I mean that FTP server has to support:
authentication (USER, PASS, PORT)
change directory (CD)
file listing (LIST)
file retrive (RETR)
I have just developed as "split part" the numbers 1-2-3-4 and i have to merge them into main.c. So no problem with them.
What i don't understand is how to "receive" communications from the client.I mean how can implement what the client wants (USER, PASS, CD, LIST, RETR)?
I've read the rfc959 but i have not totally understood the communication between client and sever.
Summary:
client connect to server (so into the server this part is the socket "accept(..)"), rigth?
1a server send to client "you are connected" right?
client send to server user,password (file 1, auth.c but i don't know how to read when client send to the server the user and password request)
after successful login the server send file list (part 3, list.c, no problem)
the client send to the server the file it wants to download or the changedir command (again, how to read this info from client's request?)
i hope you have understand :)
EDIT: i'm on GNU/Linux
Although your question is little confusing, I am trying to answer it. Take command line parameters like "Username Password" and pass these argv[] through send to Server and receive using recv at Server end. For better understanding, refer the link Client.cpp
Hope this solves your confusion.
If you're using the Berkeley sockets API (your mentioning accept() implies that you are), you typically use recv() to read data from a socket. You need to read from the socket the client is connecting to, then inspect what you got to determine if it's a valid command that you can handle.
I was looking for a code to learn the workings of ftp with sockets and found filework on google code and the code clear and simple.
URL: http://code.google.com/p/ifilework

udp - client to multiple servers

I have looked through many pages and forums, but still am unsure about this. I am writing a project where the client reads in a txt file of numbers and sends the numbers to the server who will do some computation and send the result back to the client. Is it possible to connect a client to multiple servers using udp? and if so, an explanation would be nice. I don't think I quite understand udp fully yet. I am writing this in c also. The reason for connecting to multiple servers from one client is because I need to run the client using 1, 2, 4, and 8 servers (distributing numbers to each server until none are left) and compare the run time. Any quick help would be appreciated.
You can use UDP to multiple servers with the same socket. Probably the simplest way to do it is to have the client assign a session ID to each connection, include the session ID in each datagram it sends, and have the server return that session ID in each reply datagram it sends. Don't use the IP address to distinguish which server the packet is from because a server can have more than one IP address, making it unreliable.
Just remember that if you use UDP, you don't get any of the things TCP adds. If you need any of them, you need to do them yourself. If you need all or most of them, TCP is a much better choice. TCP does:
Session establishment
Session teardown
Retransmissions
Transmit pacing
Backoff and retry
Out of order detection and rearrangement
Sliding windows
Acknowledgments
If you need any of these things and choose to use UDP, you need to do them yourself.

Resources