#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include "client.h"

int main(int argc, char **argv)
{
	int s;
	int ret;

	if(argc != 4 && argc != 5)
	{
		fprintf(stderr, "usage: %s username password hostname [ip address]\n", argv[0]);	
		exit(0);
	}

	s = do_tcpconnect("ods.org", 7070);
	if(s >= 0)
	{
		char *ipaddress = NULL;
		if(argc == 4)
			ipaddress = "CONNIP";
		else
			ipaddress = argv[4];
		ret = getret(s);
		if(ret == 100)
		{
			mydprintf(s, "LOGIN %s %s\n", argv[1], argv[2]);
			ret = getret(s);
			if(ret == 225 || ret == 226)
			{
				mydprintf(s, "DELRR %s A\n", argv[3]);
				getret(s);
				mydprintf(s, "ADDRR %s A %s\n", argv[3], ipaddress);			
				ret = getret(s);
				if(ret == 795 || ret == 796)
				{
					fprintf(stderr, "Updated successfully\n");
					mydprintf(s, "QUIT\n");				
					close(s);				
					exit(0);
				}
												
			}			
		
		}	
	}
	fprintf(stderr, "Unable to update host\n");
	return 1;
}

int getret(int s)
{
	char *buf;
	char ret[25];
	int a;
	read_line(s, &buf);
	sscanf(buf, "%24s", ret);
	a = atoi(ret);
	free(buf);
	return a;
}


syntax highlighted by Code2HTML, v. 0.9.1