1

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?

7
  • 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. Commented 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. Commented 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.) Commented Jul 12, 2013 at 1:14
  • 1
    If 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. Commented Jul 12, 2013 at 1:15
  • 1
    @millimoose How should I restructure my question and what specific things should I include? Commented Jul 12, 2013 at 1:20

3 Answers 3

3

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.

Sign up to request clarification or add additional context in comments.

Comments

2

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.)

2 Comments

Should I use this for my server and this for my client in my protocol?
Those are reasonable starting points, perhaps, but they are (a) ancient, and (b) not very robust. I say they are ancient, because they use the bzero() and bcopy() functions, which were deprecated a fair long time ago (and aren't even listed in the current POSIX documentation) and should be replaced by memset() and memcpy()/memmove(). I say they aren't robust because they're using atoi() to convert the command-line argument to a numeric value, and there isn't any handling of the case of atoi() failing (which is Undefine Behavior anyway...) Always use strtol() and handle errors.
0

In practice, it is much better to use an existing TCP stack (e.g. tcp(7) socket implementation, at least on Linux), then use some HTTP server library above it, like e.g. libonion. Reinventing your TCP stack and your HTTP server layer would take you more than a year of work.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.