I want to write an Application layer protocol that uses TCP to return certain ASCII Text when a GET request is sent. I read the first HTTP specification and the SMTP specification but am still unclear on how to write the protocol connecting the two computers itself. How could I write this in C? Are there any tutorials or examples I could look at?
-
1"how could I reverse engineer HTTP 0.9 using C?" - I'm not really sure what this question means. The HTTP spec is a description of data that has to travel accross a wire to accomplish something. You don't "reverse-engineer" a specification, you just make your program conform to the rules the specification outlines.millimoose– millimoose2013-07-12 01:10:18 +00:00Commented Jul 12, 2013 at 1:10
-
I want to make a protocol that can request data from another computer and the computer can return ASCII text.user2574729– user25747292013-07-12 01:12:29 +00:00Commented Jul 12, 2013 at 1:12
-
Then you should start by specifying the protocol. How does a single "message" look like? What data can it contain, how do you tell if it's valid or not? Are there several "types" of messages? Do they have to follow in a specific order? (E.g. authentication / setup handshakes like in SMTP or SSL.)millimoose– millimoose2013-07-12 01:14:39 +00:00Commented Jul 12, 2013 at 1:14
-
1If all that sounds vague, that's because this comes with the territory of doing something where you're allowed arbitrary flexibility. There isn't a concise guide that tells you "how to invent a protocol from scratch", there's many concerns to consider, which is why it's almost always better to just use an existing one that a bunch of people already hammered out.millimoose– millimoose2013-07-12 01:15:58 +00:00Commented Jul 12, 2013 at 1:15
-
1@millimoose How should I restructure my question and what specific things should I include?user2574729– user25747292013-07-12 01:20:28 +00:00Commented Jul 12, 2013 at 1:20
3 Answers
The heart of any communication protocol is the interface control document(ICD), which will describe the message structures that are allowed, like what is the size of your header, data, crc field etc. It is from this document you create the C structures. Usually people use bit fields to encapsulate the message fields appropriately. When you use existing communication methods, for example Ethernet you have the TCP or UDP sockets to send and receive the data. You can encode your messages in them. If you want to develop a new communication protocol then you have to make a logic of your own and embed it over existing media and proceed.
Comments
You're asking two questions. Your first question is "How can I create a new communications protocol", and your second question is "How can I implement this in C".
These are both far too generic to be good questions per the charter of this forum.
The answer to "How can I create a new communications protocol" is, as millimoose already pointed out, simple: A protocol is a document specifying the set of rules for how entities can communicate. Decide what a conversation should look like, starting from the "hello" or equivalent, specifying every possible request and every possible response, and every possible error response, through to how to say goodbye (and how to deal with a connection that gets dropped with saying goodbye), and write that all down. The SMTP protocol is actually a fairly good example of exactly that, in fact. (A TCP-based internet protocol will also typically specify a default TCP port to operate the protocol over.)
The answer to "How can I implement this in C", now that you have a fully specified protocol, is the same as the answer to "How can I implement this in Java", "How can I implement this in REBOL", or "How can I implement this in TCL": Write a basic server app that speaks the server half of the protocol and a basic client app that speaks the client half of the protocol.
(Of course, you might actually have been intending to ask "Regardless of the specific protocol, how can I write in C a server and client that communicate with each other?". This is also an excessively generic question, which can be answered through judicious searching on google.)