Network Socket Homework

CS 441 Homework, Dr. Lawlor

I've started a set of TCP network socket servers for you to communicate with.  These servers are all running on sandy.cs.uaf.edu, IP address 137.229.25.153, ports 6661 through 6669.  Sadly, the UAF firewall won't allow you to connect to these from outside the UAF network, so you'll have to either connect from inside the UAF network, or write your clients from inside NetRun (on Sandy, Q6600, or Phenom; most of the old machines are behind their own firewall). 

Feel free to use my osl/socket.h library, or write your own code from scratch.  (I use it, but then, it's my library!). You can download osl/socket code and a demo for HTTP as a .zip or a tar-gzip.

For this homework, "int32" means a big-endian 32-bit binary integer, like my "Big32" class.

TCP Port
Purpose
Problem #
Server protocol
6661
Basic test
None
Connect, and receive a "Hello!" string.  Terminated by exclamation point (or EOF).
6662
Newline string echo
1 Send one newline-terminated string.  You'll receive the same string back again.
6663
Addition 2 Send two int32 numbers, get back one int32 number, the sum.
6664
Counted string
3
Send one int32 string length, then a string of that length.  Get back the string length (only, no data).
6665
Simple callback
4
Create your own listening TCP port.
Send the server an int32 with your port number.
The server will connect on this port.  On this new connection, you then send the server a newline-terminated string, and the server will respond with the int32 length of the string you sent.

UPDATE!  Connecting back to you seems to just hang from the UAF wireless network.  So you need to be hardwired into the 137.229.25.x subnet (inside Chapman), or use NetRun, for this or 6666 to work!
6666
Authenticated callback
5
Create your own listening TCP port.
Send the server an int32 with your port number.
The server will connect on this port, and perform these steps: 
Authenticate:
        Server sends 0xBEEF as an int32.
        You send back 0xF00D as an int32.
        Server sends a challenge int32.
        You add 0xF00D to the challenge and send that int32 back.
Communicate:
        You send a newline-terminated string.  We store this string.
        Server sends back client's string, or an error message, followed by newline.
Close connection
6669
Log reader
(debugging)
None
Send two int32's: your student ID, and the server port you want the logs for.  Returns an int32 log length, and the logs from that server matching your ID.

The problems are:
  1. Send a string containing your student ID to server 6662.
  2. Pick two integers that add up to your student ID, and send them to server 6663.
  3. Send a string containing your student ID formatted for server 6664.
  4. Get server 6665 to connect back to you, and send it a string containing your student ID.
  5. Get server 6666 to connect back to you, authenticate, and send it a string containing your student ID.
The "string containing your student ID" should be in ordinary ASCII decimal, but can be anywhere in the string, for example:

Turning in == Logs

There's nothing for you to turn in for this homework, instead I can see if you've done it by looking at the server logs.  You can extract this same data using server 6669, the log reader.  Here's an example 6669 client, runnable from NetRun:
#include "osl/socket.h"
#include "osl/socket.cpp"

int foo(void) {
skt_ip_t ip=skt_lookup_ip("137.229.25.153"); // sandy
int port=6669; // the log reading demo server
int timeout=1; // seconds until timeout (usually 60)
SOCKET s=skt_connect(ip,port,timeout);
std::cout<<"Connected.\n";

// Send off my ID
Big32 id=36661313;
skt_sendN(s,&id,sizeof(id));

// Send off the requested server number
Big32 server=6669;
skt_sendN(s,&server,sizeof(server));

// Receive a string with all occurrences in the logfiles.
Big32 len=0;
skt_recvN(s,&len,sizeof(len));
unsigned int slen=len;
std::cout<<"Server is sending back "<<slen<<" bytes.\n";
if (slen>0 && slen<100000) {
std::string str(slen,'x');
skt_recvN(s,&str[0],slen);
std::cout<<"Back from server: \n"<<str<<"\n";
}
skt_close(s);

return 0;
}

(Try this in NetRun now!)

Of course, you need to use your own student ID and server number.  The 6669 server itself logs your request, so every time you run the above, you should see another entry with your ID and the date.

Debugging

Keeping servers up is hard, and it's likely at least one of these servers will find some creative new way to hang or crash.  If you can't even connect to one of the servers, please send me an email, and I will restart it!

If you discover an exploitable security hole (other than a denial of service) in one of the servers, I will pay you $50 to report it promptly.  Exploiting the hole without reporting it may result in academic, civil, criminal, and moral penalties (plus there's not really anything valuable on sandy).