NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

27
NTU CSIE Computer Networks 2009 Spring Project 2 Project 2 Internet Relay Chat Internet Relay Chat (IRC) (IRC)

Transcript of NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Page 1: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

NTU CSIE Computer Networks 2009 Spring

Project 2Project 2Internet Relay Chat Internet Relay Chat

(IRC)(IRC)

Page 2: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Project GoalProject Goal In this Project, students are asked to get

familiar with building a multi-sockets system

Students are requested to implement a client/server and P2P coexisting system

Page 3: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

System Structure System Structure

Server

Reject if the server is too busy

Server/client connection

P2P connection for file transfer

Page 4: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Scenario 1Scenario 1- Broadcast text messages- Broadcast text messages

Server

1. Send msg2. Broadcast msg

Page 5: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Scenario 2Scenario 2- Exchange files- Exchange files

Server

1. Request to send file to user C

5. Create p2p connectionand transfer the file

C

A

B

2. Ask if user C wants to receive it

3. Respond to the server

4. Forward the answer and C’s ip:port

Page 6: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

1. Create a listening socket2. Create a socket queue with size 103. Use select() to handle incoming connection

If the queue is already overflow, notify the client and close the connection

4. Lookup your own IP address Run on a machine with a public IP Hint: Use “ioctl(int socket, SIOCGIFADDR, struct ifreq)

5. Bind the interface via an available port Randomly generate your own port (larger than 1024) Bind() via the selected port If the address is already used, repeat the above procedure

Goal 1: Goal 1: Build a Listening Socket and Use Build a Listening Socket and Use Select()Select()

Page 7: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

1. Create a client socket◦ Connect to bsd6.csie.ntu.edu.tw port 5000

2. Read user id

3. Reply your IP address and port to the server

Goal 2: Goal 2: Login to IRC ServerLogin to IRC Server

Page 8: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Goal 3: Goal 3: Send Text Message to IRC ServerSend Text Message to IRC Server1. Read messages from stdin

2. Send the message to the server

3. Print the received message

Page 9: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Goal 4: Goal 4: File TransferFile Transfer1. Send a file to the other user2. Negotiate via four-way handshaking

◦ The sender sends a request to the server◦ The server forwards the request to the receiver◦ The receiver responses the answer to the server◦ The server relays the answer along with the IP

and port to the sender

3. Sender: create a socket to connect to the receiver and forward the file

4. Receiver: handle the incoming connection to receive the data

Page 10: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Message Format Login Terminate Message broadcast File transfer

ProtocolProtocol

Page 11: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Massage size limit: 1024 NICKNAMEname;USER_MSGmsg + EOTEOT

◦ “__NICKNAME__server;__USER_MSG____ACCEPT_LOGIN__EOT” Sender: server Msg: __ACCEPT_LOGIN__

◦ “__NICKNAME__testuser;__USER_MSG__this is a message EOT” Sender: testuser Msg: this is a message

See default message name in irc_util.h◦ Don’t send messages starting by /quit, /send, /from, /list,

/acceptf, /rejectf, /help

Protocol:Protocol:Message FormatMessage Format

Page 12: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

#define MSG_SIZE 1024

#define CONN_NUM 3

#define NICKNAME "__NICKNAME__"

#define LOGIN_ID "__LOGIN_ID__"

#define REQUEST_ADDR "__REQUEST_ADDR__"

#define REPLY_ADDR "__REPLY_ADDR__"

#define REJECT_EMPTYID "__REJECT_EMPTYID__"

#define REJECT_DUPID "__REJECT_DUPID__"

#define ACCEPT_LOGIN "__ACCEPT_LOGIN__"

#define REJECT_OVERFLOW "__REJECT_OVERFLOW__"

#define USER_MSG "__USER_MSG__"

#define REJECT_FTRAN_NULL_USER "__REJECT_FTRAN_NULL_USER__"

#define REJECT_FTRAN_BY_USER "__REJECT_FTRAN_BY_USER__"

Protocol:Protocol:Control Message (1)Control Message (1)

Page 13: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

#define TERMINATE "/quit"

#define GETLIST "/list"

#define GETHELP "/help"

#define REQUEST_FTRAN "/send"

#define RESPOND_FTRAN "/from"

#define FILENAME "/file"

#define FILESIZE "/fsize"

#define ADDRESS "/addr"

#define CONNSEQ "/connseq"

#define ACCEPT_FTRAN "/acceptf"

#define REJECT_FTRAN "/rejectf"

Protocol:Protocol:Control Message (2)Control Message (2)

Page 14: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Protocol:Protocol:LoginLogin

client server1. connect(host, port)

REJECT_OVERFLOW

3. LOGIN_ID

REJECT_DUPID || REJECT_EMPTYID

4. LOGIN_ID

REQUEST_ADDR

5. REPLY_ADDR

1. Connect() to the server Bsd6.csie.ntu.edu.tw port 5000

2. Print “the server is too busy” and close the socket

3. Send login id as getting ACCEPT_LOGIN NICKNAMEid;USER_MSGLOGIN_ID Ex:

“__NICKNAME__test;__USER_MSG____LOGIN_ID__EOT”

4. Re-select id as getting REJECT_DUPID or REJECT_EMPTYID

5. Report the address and port NICKNAMEid:USER_MSGREPLY_ADDRid:port ex.:

“__NICKNAME__test;__USER_MSG____REPLY_ADDR__140.112.22.22:5001EOT”

2. Close sock

1. connect(host, port)

ACCEPT_LOGIN

Page 15: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

1. Type /quit or ctrl-c #include <signal.h> Signal(SIGINT, yourfunction);

2. Send ctl message TERMINATE NICKNAMEmyid;USER_MSGTERMINATE Ex:

“__NICKNAME__myid;__USER_MSG____TERMINATE__EOT”

3. Receive TERMINATE Print “userid: has left”

4. Receive TERMINATE from the server Close all sockets, and terminate the

program

Protocol:Protocol:TerminateTerminate

client server

1. /quit or ctrl-c2. TERMINATE

client

broadcast

3. Print msg

TERMINATE4. Close all sock

Page 16: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

1. Read the message from stdin Exclude “/quit”

2. Send the message NICKNAMEmyid;USER_MSGmsg Ex:

“__NICKNAME__myid;__USER_MSG__hihi…EOT”

3. Receive the message Print the message

userid: message

Protocol:Protocol:Message BroadcastMessage Broadcast

client server

1. Read message2. Send

client

broadcast

3. recv

Page 17: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Cmd: /send recvid /file filepath◦ Check if filepath exists◦ Extract filename from the file path

Sender sends◦ NICKNAMEsendid;USER_MSGREQUEST_FTRA

N recvid FILENAME filename CONNSEQ conn_id

Receiver receives◦ NICKNAMEserver;USER_MSGRESPOND_FTRA

N sendid FILENAME filename CONNSEQ conn_id

◦ Ask if the receiver wants to accept it. (block)

◦ NICKNAMErecvid;USER_MSGACCEPT_FTRAN sendid FILENAME filename CONNSEQ conn_id

◦ NICKNAMErecvid;USER_MSGREJECT_FTRAN sendid FILENAME filename CONNSEQ conn_id

Protocol:Protocol:File Transfer (1)File Transfer (1)

sender server

2. REQUEST_FTRAN

receiver1. Type cmd

RESPOND_FTRAN

3. ACCEPT_FTRAN || REJECT_FTRAN

ACCEPT_FTRAN || REJECT_FTRAN

5. Create connection

4. Get ip:port

All messages should include the tail “EOT”

Page 18: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Sender receives◦ Accept by receiver◦ NICKNAMEserver;USER_MSGACCEPT_FTRA

N recvid FILENAME filename CONNSEQ conn_id ADDRESS ip:port

◦ Reject by receiverNICKNAMEserver;USER_MSGREJECT_FTRAN recvid FILENAME filename CONNSEQ conn_id REJECT_FTRAN_BY_USER

◦ Reject due to the wrong user name NICKNAMEserver;USER_MSGREJECT_FTRAN recvid FILENAME filename CONNSEQ conn_id REJECT_FTRAN_NULL_USER

◦ Create connection to ip:port

Protocol:Protocol:File Transfer (2)File Transfer (2)

sender server

2. REQUEST_FTRAN

receiver1. Type cmd

RESPOND_FTRAN

3. ACCEPT_FTRAN || REJECT_FTRAN

ACCEPT_FTRAN || REJECT_FTRAN

5. Create connection

4. Get ip:port

Page 19: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

5. sender: send file size to the receiver◦ NICKNAMEsendid;USER_MSGRESPNOND_FTR

AN sendid FILENAME filename FILESIZE size

6. Send the file◦ Sender: Send() ◦ Receiver: Recv() and save as a new file

7. Close the connection as file transfer is finished or the remote connection is lost

Receiver saves the file in its local directory

Receiver only needs to overwrite the file if the file name already exists

◦ Don’t need to process if the file can not be accessed

Protocol:Protocol:File Transfer (3)File Transfer (3)

sender server receiver

5. FILESIZE

6. Send the file

7. Close connection

Page 20: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

connection = accept(listen_sock, NULL, NULL)

if (queue is not overflowed)sock_list[i] = connection

else {1. send msg: NICKNAMEid;USER_MSGREJECT_OVERFLOW2. close connection

}

Protocol:Protocol:File Transfer (4) Handle new File Transfer (4) Handle new connectionconnection

Page 21: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

RequirementsRequirements Write your program on R217 workstations

◦ You can not use multi-process. Please use select() to realize the requirements of Project 2

Demo◦ To be announced

Page 22: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Intro. to Socket Intro. to Socket ProgrammingProgramming Socket programming

Page 23: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

ScoreScore Basic function – 60%

Manage connection: 10% Text: 30% File transfer: 30%

Makefile and command – 10%◦ make clean  // clean obj(*.o) and executable◦ make        // compile source code◦ ./irc_client hostname server_port // Run

client Bonus

Synchronous file transfer 20%

Page 24: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

File formatFile format Max. 2 people in one group

◦ Group member is the same as Project 1◦ Please tell TA if you want to change your group by mail:

[email protected] Tar your file

◦ tar zcvf bXXXXXXXX_bOOOOOOOO_prj1.tar.gz Makefile file1 file2 …

◦ Use “bXXXXXXXX_prj2.tar.gz” if you do not team with others

◦ DO NOT have any directory in .tar file; all files should be extract to current directory when using “tar zxvf bXXXXXXXX_prj2.tar.gz”

◦ Send your file tohttp://www.csie.ntu.edu.tw/~artoo/CN2009/hws.htm

Page 25: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

DeadlineDeadline

2009/5/20 24:00 There WOULD BE penalty for late

submission. The penalty for the first day is 10 points, the second day is 20 points, etc. Please turn in your code in time.

Page 26: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

ReferencesReferences

Beej's Guide to Network Programming◦ http://beej.us/guide/bgnet/output/html/multipage/

index.html Linux cross reference

◦ http://lxr.linux.no/

Page 27: NTU CSIE Computer Networks 2009 Spring Project 2 Internet Relay Chat (IRC)

Login◦ Normal login◦ Dup id

Server is busy Text message File transfer

◦ Accept request◦ Reject request◦ Request, but the recv id is wrong◦ Request, but the other side is gone◦ The remote site leaves under transmission◦ Normal transmission

Terminate by /quit, ctrl-c, server

DemoDemo