r/C_Programming • • 2d ago

Building a user space network stack from scratch

I dont know much about networking as i am just now studying it seriously but i am thinking of building what i mentioned in the title from scratch, if anyone could help me with the direction im supposed to go in to start i would really really appreciate it. whenever i try looking up a place to start all i get is github repos with many many files of stuff im not yet familiar with and it all assumes prior knowledge of some stuff, i know you cant really just go into this thing blind but what im saying is i dont really know where to begin learning about all of this stuff to build my target. also i am on a 2-week time limit

0 Upvotes

12 comments sorted by

7

u/Due-Mortgage-6854 2d ago

thats a huge project for a beginner😭

Ik you said you don't really love the github repos but did you try this one : https://github.com/TanayK07/networking-from-scratch ?

0

u/Soft-Bedroom8708 2d ago

that's actually one of the repos that had me thinking about putting this project on hold because of the size of it.
I have also looked briefly at the following repos
https://github.com/kdkasad/userspace-network-stack/tree/master
i said repos plural but i cant seem to find the other one or two i looked at so i will leave it at this

since you say this is a huge project for a beginner would you please recommend something maybe less huge but not introduction-level project.
im kinda getting overly driven by ego because one professor of mine at uni saw that im performing much much better than my peer students and he wanted me to write a project of my choice to give him a basic idea of where i am in regards to clean code, comfort with using git/github and how much im able to do in a limited amount of time, he does teach us networking so that's one reason i went with this but recommending even non networking related projects would be helpful, any guidance is appreciated as i get lost trying to find ideas but do exceedingly well when any resemblance of a path is in front of me.
thank you in advance

3

u/Due-Mortgage-6854 2d ago

for a beginner it's indeed a larger project. Beej and Saminiir teached the basic well but if you can't understand i would shift to something else... Maybe

  • Custom HTTP/1.1 Server (over raw sockets)
  • DNS Resolver & Forwarder
  • Mini Chat or File-Transfer Protocol
  • Simple Packet Sniffer / Analyzer

0

u/Soft-Bedroom8708 2d ago

assuming i build one of these with not much complexity, how much can you expand on it after the simple version of it exists?
the dns resolver is most tempting for me followed by the packet sniffer so if you could keep that in mind when answering
also thank you a lot this is immense help

1

u/Due-Mortgage-6854 2d ago

For the DNS one, you can expand your 1st module by adding the following :

  • caching & TTL handling
  • true recursion, walking from root servers to Top-Level Domain (TLD) and authoritative name servers instead of only forwarding
  • more record types like AAAA (IPv6), CNAME, MX, TXT, and PTR (reverse lookups)
  • encrypted upstream transport
  • filtering & local overrides
  • TCP fallback

For the Packet Sniffer :

  • deep protocol dissection
  • BPF-style filtering
  • PCAP export & replay
  • traffic stats & anomaly detection
  • live TUI or dashboard

You can find more info on these on google. As for the Network stack, if you're really willing to make it, i would advise you to use AI. It will help you greatly in learning each part of it and guide you well. It will benefit you more than the resources you are looking for. A lil upvote would be appreciated btw😭

1

u/Soft-Bedroom8708 2d ago

thank you a lot!!!
Im assuming this answers my question with a "yes they can be expanded"
I would really like to make the network stack thing but i think im too overwhelmed and will end up wasting my time for the project by being afraid of "what if i waste my time for the project"
and i was trying to avoid ai because i was starting to depend on it for searching stuff about such topics a little too much but i guess it would have been handy the time i tried to avoid it lol
so i guess i will just head towards the dns one instead
thanks a lot again

1

u/Soft-Bedroom8708 2d ago

i should add 2 things, the first being yes the repo i linked is written in rust but i thought that could be easier to understand before moving to pure c
the second being, i was really excited about this network stack thing specifically because it could have given me an easier time with some uni stuff because the professor we have for networking holds a higher position in our department so him seeing that im doing well enough to implement such a project would help me with some other stuff that i would rather not get into

1

u/deaddodo 2d ago

What do you mean by a "user space network stack"? Are you referring to a client/server architecture over the OS' network interface? Or are you trying to implement your own TCP/IP / UDP/IP / other networking interface that runs via a user space daemon?

For the former, it's tedious but overall fairly simple. You can just follow Beej's guide for a run down of sockets. For the latter, you're getting into black magic territory, OSes like to lock down the raw network interface for various reasons. You would either need to break your "user space" requirement and write some sort of kernel or driver shim, or go straight to embedded development.

That being said, I've never had to actually write a user space network protocol/interface, so others here might have better advice for you.

1

u/Soft-Bedroom8708 2d ago

i fear i know too little but i was trying to implement the latter, but you describing it this way may make me put this project on hold for now on go focus on simpler stuff.
I have looked at beej's guide but due to my limited knowledge i wasn't able to tell if the stuff in it are the useful stuff that i will need to know for the project, again it all comes down to limited knowledge about the topic and I apologize for possibly getting too excited and going immediately to such stuff.

3

u/sciencekm 2d ago edited 2d ago

Look up the book "Computer Networks: A Systems Approach" by Larry Peterson, 1st Edition.

As part of teaching the fundamentals, it implements a complete single allocation, zero copy TCP/IP stack in C.

https://people.inf.elte.hu/toke/halozatok-jegyzet/Peterson-Davie/X-kernel/

1

u/yel50 9h ago

not sure how much of the stack you want to do, but a good user space project would be to implement an FEC (Forward Error Correction) protocol. then write a proxy that corrupts the packets and verify that the receiver can still recover the original data.