in this project we will implement basic implementation of netcat.
we will support the following features:
- -e flag: execute a shell command
- -i flag: get input from a socket
- -o flag: write output to a socket
- -b flag: input and output go to the same socket
- -t flag: set timeout for the execution of the program
if we don't send the -e
flag, the program will act as a chat between two terminals. (if we send -i
or -o
flags, the chat will be one-way channel)
we will support the following socket types:
- TCP - as client & server. can get input, output or both from a socket.
- UDP - as client & server. can get input, output or both from a socket.
- UNIX Domain Socket Stream - as client & server. can get input, output or both from a socket.
- UNIX Domain Socket Datagram - as client & server. server can get input, client will send output.
to use the socket we will send them after the flag, in this format:
- TCP server:
TCPS<PORT>
- TCP client:
TCPC<HOST>,<PORT>
- UDP server:
UDPS<PORT>
- UDP client:
UDPC<HOST>,<PORT>
- UDS stream server:
UDSSS<PATH>
- UDS stream client:
UDSCS<PATH>
- UDS datagram server:
UDSSD<PATH>
(go only with -i flag) - UDS datagram client:
UDSCD<PATH>
(go only with -o flag)
NOTE: all datagram sockets (UDP and UDS datagram) will be wait for a dummy input from the client for accepting the connection, and then start the execution of the program.
in all of the examples below, when we use the -e flag, we will execute the command a tik tak toe game, that we implemented in the here
we simulate a server & client with nc
command,and for the unix domain socket datagram we use socat
command.
all of the commands that you can use are in the run_commands file.
- open a TCP server and wait for a input from a client. output go to stdout
# server
./mync -e "./ttt 123456789" -i TCPS4269
# client
nc localhost 4269
- open a TCP server and wait for a input from a client. output go to client
# server
./mync -e "./ttt 123456789" -b TCPS4269
# client
nc localhost 4269
- open a UDP server and wait for a input from a client, the output go to TCP server that listen on port 6699
# TCP server
nc -l -p 6699
# UDP server & TCP client
./mync -e "./ttt 123456789" -i UDPS4269 -o TCPClocalhost,6699
# UDP client
nc -u localhost 4269
- open a UDS stream server and wait for a input from a client, the output go to TCP server that listen on port 6699
# TCP server
nc -l -p 6699
# UDS stream server & TCP client
./mync -e "./ttt 123456789" -i UDSSShoi.socket -o TCPClocalhost,6699
# UDS stream client
nc -U hoi.socket
- open a UDS datagram server & UDP server, the input is from the UDS client and the output go to the UDP client
# servers
./mync -e "./ttt 123456789" -i UDSSDhoi.socket -o UDPS4269
# UDS client
socat - UNIX-SENDTO:hoi.socket
# UDP client
nc -u localhost 4269
- chat between two terminals
# terminal 1
./mync -b TCPS4269
# terminal 2
./mync -b TCPClocalhost,4269
- input from UDS server and output to UDS client (datagram)
#open the UDS server (the output will go here)
socat UNIX-RECVFROM:hoi1.socket,fork -
# open the UDS client for sending the output, and the UDS server for input
./mync -e "./ttt 123456789" -o UDSCDhoi1.socket -i UDSSDhoi2.socket
# open the UDS client for sending the input
socat - UNIX-SENDTO:hoi2.socket
- Unix domain socket stream server and client
# example 1
#open the UDS stream server
nc -lU hoi.socket
# open the UDS stream client
./mync -e "./ttt 123456789" -b UDSCShoi.socket
# example 2
./mync -e "./ttt 123456789" -b UDSSShoi.socket
nc -U hoi.socket
we checked the code coverage of the code using gcov. most of the code is covered, except errors that are related to the socket library.