Commit 277f7d72 authored by Anna Feiler's avatar Anna Feiler
Browse files

first version

parents
File added
ip.png

22.1 KB

public class TCPFIN extends base{
final int FIN_WAIT_1 = 1;
final int FIN_WAIT_2 = 2;
final int CLOSING = 3;
final int TIME_WAIT = 4;
final int CLOSED = 5;
int state; //Ich haette das hier gleich mit FIN_WAIT_1 initialisiert.
void close() throws IOException{
state = FIN_WAIT_1;
send(FIN);
}
void receive(int flag) throws IOException{
if(flag == FIN){
if(state == FIN_WAIT_1){
state = CLOSING;
}
else if(state == FIN_WAIT_2){
state = TIME_WAIT;
startTimer();
}
send(ACK);
}
else if(flag == ACK){
if(state == FIN_WAIT_1){
state = FIN_WAIT_2;
}
else if(state == CLOSING){
state = TIME_WAIT;
startTimer();
}
}
}
void timeout() throws WrongStateException{
if(state != TIME_WAIT){
throw new WrongStateException();
}
state = CLOSED;
}
}
\ No newline at end of file
public class SendingHost extends CommonHost
{
private volatile int base = 1;
private volatile int nextSQN = 1;
private volatile Pkt[] pkt = new Pkt[Math.pow(2,31)-1]; //one does not simply make arrays volatile // Integer.MAX_VALUE ?
private Pkt genPkt(int SQN, Pkt pkt)
{
pkt.seqnum=SQN;
return pkt;
}
boolean fromAbove(Pkt pkt)
{
if(nextSQN<base+W)
{
transmit(pkt[nextSQN]=genPkt(nextSQN,pkt));
if(base==nextSQN++) startTimer();
return true;
}
return false;
}
private void transmitAll()
{
for(int i = base; i < nextSQN; i++)
transmit(pkt[i]);
}
void receive (Pkt ack)
{
if(!bitError(ack))
{
if(ack.seqnum==base-1)
transmitAll();
else if(base<=ack.seqnum && ack.seqnum<nextSQN)
{
base = ack.seqnum+1;
if(nextSQN==base) stopTimer();
else startTimer();
}
}
}
void timeout(Pkt pkt)
{
transmitAll();
startTimer();
}
}
\ No newline at end of file
import java.io.*;
import java.net.*;
public class RKMailClient {
private final static String CRLF = "\r\n";
private static Socket socket;
private static BufferedReader rx;
private static BufferedWriter tx;
public static void main(String[] args) throws Exception {
sendEmail(getEmailsCount());
}
public static void exit() {
System.out.println("Fehler aufgetreten");
System.exit(-1);
}
private static void check(String s) {
if(!rx.readLine().startsWith(s)) {
tx.close();
rx.close();
socket.close();
exit();
}
}
private static int getEmailsCount() throws Exception {
socket = new Socket("mail.rk.org", 110);
rx = new BufferedReader(new InputStreamReader(socket.getInputStream()));
tx = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
check("+OK");
tx.write("user rk" + CRLF);
check("+OK");
tx.write("pass rK!23" + CRLF);
check("+OK");
tx.write("list" + CRLF);
int count = 0;
boolean fin = false;
do {
if(rx.readLine().startsWith(".")) {
fin = true;
} else {
count++;
}
} while(!fin);
tx.write("quit" + CRLF);
check("+OK");
tx.close();
rx.close();
socket.close();
return count;
}
private static void sendEmail(int count) throws Exception {
socket = new Socket("cipmail.cs.fau.de", 25);
rx = new BufferedReader(new InputStreamReader(socket.getInputStream()));
tx = new BufferedWriter(new OutputStreamWriter(socket.getOutputStream()));
check("220");
tx.write("HELO rk.org" + CRLF);
check("250");
tx.write("MAIL FROM: <status@rk.org>" + CRLF);
check("250");
tx.write("RCPT TO: <mailCheck.cs.fau.de>" + CRLF);
check("250");
tx.write("DATA" + CRLF);
check("354");
tx.write("RK hat " + count + " E-Mails." + CRLF + "." + CRLF);
check("250");
tx.write("QUIT" + CRLF);
check("221");
tx.close();
rx.close();
socket.close();
}
}
public class BehaviorImpl extends Behavior {
private final int WAIT_FOR_DATA = 1;
private final int WAIT_FOR_ACK = 2;
private final int BACKOFF = 3;
private int state = WAIT_FOR_DATA;
private String d;
public void fromAbove(String data) throws NotReadyException {
if(state == WAIT_FOR_DATA) {
d = data;
state = WAIT_FOR_ACK;
transmit(d);
startTimer(TAU);
} else throw new NotReadyException("nudel");
}
public void receive(String data) {
if(data.equals(ACK) && state == WAIT_FOR_ACK) {
state = WAIT_FOR_DATA;
stopTimer();
m = 0;
} else {
toAbove(data);
transmit(ACK);
}
}
public void timeout() {
if(state == WAIT_FOR_DATA) {
m++;
state = BACKOFF;
startTimer(rnd.nextInt(Math.pow(2, m)));
return;
}
if(state == BACKOFF) {
transmit(d);
startTimer(TAU);
state = WAIT_FOR_ACK;
return;
}
}
}
public class SendingHost extends CommonHost {
private int base = 1;
private int nextSQN = 1;
private Pkt buffer[] = new Pkt[Integer.MAX_VALUE];
private Pkt genPkt(int sqn, Pkt p) {
p.seqnum = sqn;
p.checksum = checkSum(p);
return p;
}
public boolean fromAbove(Pkt pkt) {
if(pkt.seqnum >= base + W) {
return false;
}
buffer[nextSQN] = genPkt(nextSQN, Pkt);
transmit(buffer[nextSQN]);
if(base == nextSQN) {
startTimer();
}
nextSQN++;
return true;
}
public void receive(Pkt pkt) {
if(bitError(pkt)) {
return;
}
if(pkt.seqnum == base - 1) {
for(int i = base; i < nextSQN; i++)) {
transmit(pkt[i]);
}
return;
}
if(base <= pkt.seqnum && pkt.seqnum < nextSQN) {
base = pkt.seqnum + 1;
if(nextSQN == base) {
stopTimer();
} else {
startTimer();
}
return;
}
}
public void timeout() {
for(int i = base; i < nextSQN; i++)) {
transmit(buffer[i]);
}
startTimer();
}
}
public class BehaviorImpl extends Behavior
{
public enum State {WAIT_FOR_DATA, WAIT_FOR_ACK, BACKOFF }
State aktuellerZustand=WAIT_FOR_DATA;
String d;
void fromAbove ( String data ) throws NotReadyException
{
if(aktuellerZustand==WAIT_FOR_DATA)
{
d=data;
transmit(data);
startTimer(TAU);
aktuellerZustand=WAIT_FOR_ACK;
}
else
{
throw new NotReadyException();
}
}
void receive ( String data )
{
if(data.equals(ACK) && aktuellerZustand==WAIT_FOR_ACK)
{
stopTimer();
m=0;
aktuellerZustand=WAIT_FOR_DATA;
}
else
{
toAbove(data);
transmit(ACK);
}
}
void timeout ( )
{
if(aktuellerZustand==WAIT_FOR_DATA)
{
m++;
aktuellerZustand=BACKOFF;
startTimer(rnd(0,1<<m-1)*TAU);
}
else if(aktuellerZustand==BACKOFF)
{
transmit(d);
startTimer(TAU);
aktuellerZustand=WAIT_FOR_ACK;
}
}
}
\ No newline at end of file
public class Saloha
{
public enum STATES {WAIT_FOR_DATA, WAIT_FOR_SLOT_START, TRANSMISSION, WAIT_FOR_ACK, BACKOFF };
States aktuellerZustand=WAIT_FOR_DATA;
Packet p;
abstract void fromAbove ( Packet pkt ) throws WrongStateException
{
if(aktuellerZustand==WAIT_FOR_DATA)
{
aktuellerZustand=WAIT_FOR_SLOT_START;
p=pkt;
}
else
{
throw new WrongStateException("Falscher Zustand");
}
}
abstract void timeout ( )
{
if(aktuellerZustand==WAIT_FOR_ACK)
{
m++;
aktuellerZustand=BACKOFF;
startTimer(rnd(0, Math.pow(2, m)-1)*TAU);
}
else if(aktuellerZustand==BACKOFF)
{
aktuellerZustand=WAIT_FOR_SLOT_START;
}
}
abstract void newSlot ( ) throws IOException
{
if(aktuellerZustand==WAIT_FOR_SLOT_START)
{
aktuellerZustand=TRANSMISSION;
send(p);
aktuellerZustand=WAIT_FOR_ACK;
startTimer(TAU);
}
}
abstract void rcvACK ( )
{
if(aktuellerZustand==WAIT_FOR_ACK)
{
stopTimer();
m=0;
aktuellerZustand=WAIT_FOR_DATA;
}
}
}
\ No newline at end of file
public class POP3Server extends Base
{
public void run() throws IOException
{
while(true)
{
acceptConnection();
send(OK + "example.com POP3-Server" + NL);
string user = receive();
send(OK + "Please enter password" + NL);
string pass = receive();
if(authentificate(user, pass))
{
send(OK + "mailbox locked and ready" + NL);
}
else
{
send(ERR + "wrong password or user" +NL);
continue;
}
boolean ready = true;
while(ready)
{
string order = receive();
if(order.Equals("STAT"))
{
try
{
var sta = stat();
send(OK + sta + NL);
}
catch
{
throws new POP3Exception();
}
}
else if (order.Equals("LIST"))
{
try
{
String mails[] = list();
send(OK + "mailbox has " + mails.length + " messages\n");
for(int i = 0; i < mails.length; i++)
{
send(mails[i] + "\n");
}
send(NL);
}
catch
{
throws new POP3Exception();
}
}
else if (order.Equals("QUIT"))
{
send(OK + "bye" + NL);
ready = false;
}
}
}
}
}
\ No newline at end of file
class Pop3 extends Base{
void init() throws Exception{
socket = new Socket(POP3_SERVER, 110);
out = new DataOutputStream(socket.getOutputStream());
in = new BufferedReader(new InputStreamReader(socket.getInputStream()));
authOK = authentificate();
}
boolean authentificate() throws Exception{
out.writeBytes(USER);
string s = in.readLine();
if(!s.beginsWIth("+OK"))
return false;
out.writeBytes(PASSWORD);
s = in.readLine();
if(!s.beginsWIth("+OK"))
return false;
return true;
}
int countMails() throws Exception{
out.writeBytes("list");
int counter = 0;
while(!in.readline().equals(".")){
in.readLine();
counter++;
}
return counter;
}
void test() throws Exception;{
if(authOK == true){
print(countMails());
}else{
end();
}
}
}
public class POP3Server extends Base{
public void run() throws IOException{
while(true){
acceptConnection();
send(OK + "example.com POP3-Server" + NL);
string user = receive();
send(OK + "Please enter password" + NL);
string pass = receive();
if(authentificate(user, pass)){
send(OK + "mailbox locked and ready" + NL);
}else{
send(ERR + "wrong password or user" +NL);
continue;
}
boolean ready = true;
while(ready){
string order = receive();
if(order.equals("STAT")){
try{
String sta = stat();
send(OK + sta + NL);
}catch{
throws new POP3Exception();
}
} else if (order.equals("LIST")){
try{
String mails[] = list();
send(OK + "mailbox has " + mails.length + " messages\n");
for(int i = 0; i < mails.length; i++){
send(mails[i] + "\n");
}
send(NL);
}catch{
throws new POP3Exception();
}
} else if (order.equals("QUIT")){
send(OK + "bye" + NL);
ready = false;
}
}
}
}
public class TCPFIN extends Base{
public enum STATE ={FIN-WAIT-1, CLOSING, TIME-WAIT, FIN-WAIT-2, CLOSED, init};
private volatile STATE state = init;
public void timeout() throws WrongStateException{
if(state!=TIME-WAIT{
throw new WrongStateException();
}
state = CLOSED;
}
public void close() throws IOException{
if(!state==init)
throw new IOException();
send(FIN);
state =FIN-WAIT-1;
}
public void receive(int flag) throws IOE{
switch(state){
case FIN-WAIT-1:
if(flag == FIN){
state = CLOSING;
send(ACK);
}else{
state=FIN-WAIT-2;
}
break;
case CLOSING:
if(flag==ACK){
state =TIME_WAIT;
startTimer();
}else{
throw new IOExceptio();
}
break;
case FIN-WAIT-2:
if(flag==FIN){
send(ACK);
state =TIME-WAIT;
startTimer();
}else{
throw new IOException();
}
break;
default:
throw new IOExcpetion();
}
}
}
public class Saloah extends Base {
private final int WAIT_FOR_DATA = 1;
private final int WAIT_FOR_SLOT_START = 2;
private final int TRANSMISSION = 3;
private final int WAIT_FOR_ACK = 4;
private final int BACKOFF = 5;
private int state = WAIT_FOR_DATA;
private Packet p;
public void fromAbove(Packet pkt) throws WrongStateException {
if(state != WAIT_FOR_DATA) {