I created DNS proxy in C like this:
"socket" - create socket to serve client
"bind" - bind that socket
while (true) {
"recvfrom" - a package from socket
"socket" - create socket for dns server
(1)
"sendto" - send package to dns server
"recvfrom" - respond from dns server
"sendto" - send respond to client
}
It’s working. But what if I get another request when I am at position (1)? How can I add some queue with waiting requests? How to change this that I serve every request even if I am working on another?
You need to
listen()on the server socket, andlistenallows you to specify a backlog of connections. You will be able to handle up to that number of new client arrivals per cycle.Then you cyclically run
select()on the server socket and any outstanding client sockets, maintaining a list of them:when
selectexits (unless it’s timed out) you will inspect the three return lists and handle any sockets ready for reading, writing, or in error state, and dispatch them appropriately.You will find a detailed example here (it’s for Linux, but easily portable).