43
Chapter 10 SCTP Client / Server example

Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

  • View
    243

  • Download
    3

Embed Size (px)

Citation preview

Page 1: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Chapter 10

SCTP Client / Server example

Page 2: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back to the

client Iterative server

Page 3: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client

Client reads a single line of text from standard input

Text is send in [#]text format.# represents SCTP stream number where text

is sent

Page 4: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server

Reads the text from the network Increases the SCTP stream number by

one (# in [#]text) Sends the message back to the client on

this new stream

Page 5: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client

Reads the reply message and prints it to standard output

Stream number, sequence number and text string is printed

Page 6: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

#include "unp.h"

int { int sock_fd,msg_flags; char readbuf[BUFFSIZE]; struct sockaddr_in servaddr, cliaddr; main(int argc, char **argv) struct sctp_sndrcvinfo sri; struct sctp_event_subscribe evnts; int stream_increment=1; socklen_t len; size_t rd_sz;

if (argc == 2) stream_increment = atoi(argv[1]); sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT);

Bind(sock_fd, (SA *) &servaddr, sizeof(servaddr));

bzero(&evnts, sizeof(evnts)); evnts.sctp_data_io_event = 1; Setsockopt(sock_fd, IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof(evnts));

Listen(sock_fd, LISTENQ); for ( ; ; ) { len = sizeof(struct sockaddr_in); rd_sz = Sctp_recvmsg(sock_fd, readbuf, sizeof(readbuf), (SA *)&cliaddr, &len, &sri,&msg_flags); if(stream_increment) { sri.sinfo_stream++; if(sri.sinfo_stream >= sctp_get_no_strms(sock_fd,(SA *)&cliaddr, len)) sri.sinfo_stream = 0; } Sctp_sendmsg(sock_fd, readbuf, rd_sz, (SA *)&cliaddr, len, sri.sinfo_ppid, sri.sinfo_flags, sri.sinfo_stream, 0, 0); } }

Page 7: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Stream increment

Stream increment option If command line option is passed to

program, program decides whether to increment stream number of incoming messages if (argc == 2) stream_increment = atoi(argv[1]);

Page 8: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Create an SCTP socket

sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP);

Page 9: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Bind address

Bind any incoming address to certain server port. bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr =

htonl(INADDR_ANY); servaddr.sin_port =

htons(SERV_PORT); Bind(sock_fd, (SA *) &servaddr,

sizeof(servaddr));

Page 10: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Set up for notification

Server changes its notification subscription for the one-to-many SCTP socket.

This allows server to see the stream number where the message arrived. bzero(&evnts, sizeof(evnts)); evnts.sctp_data_io_event = 1; Setsockopt(sock_fd, IPPROTO_SCTP,

SCTP_EVENTS, &evnts, sizeof(evnts));

Page 11: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Incoming associations

Server enables incoming associations so it can listen the server socket for incoming messages. Listen(sock_fd, LISTENQ);

After which server will enter the main loop

Page 12: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Wait for messages

Initialize size of client socket address structure.

Block until message arrives. len = sizeof(struct sockaddr_in);rd_sz = Sctp_recvmsg(sock_fd, readbuf,

sizeof(readbuf), (SA *)&cliaddr, &len,

&sri,&msg_flags);

Page 13: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Increment stream number

Check increment flag and increase stream stream number is flag is set.

If number is too large then number is set to 0. if(stream_increment) { sri.sinfo_stream++; if(sri.sinfo_stream >=

sctp_get_no_strms(sock_fd, (SA *)&cliaddr, len))

sri.sinfo_stream = 0; }

Page 14: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Send message back

Sending back the message to the clientSctp_sendmsg(sock_fd, readbuf, rd_sz, (SA *)&cliaddr, len, sri.sinfo_ppid, sri.sinfo_flags, sri.sinfo_stream, 0, 0);

Page 15: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Server: Finish?

Program runs forever until it is shutdown with an external signal. for ( ; ; ) { … }

Page 16: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

#include "unp.h"

int main(int argc, char **argv) { int sock_fd; struct sockaddr_in servaddr; struct sctp_event_subscribe evnts; int echo_to_all=0;

if(argc < 2) err_quit("Missing host argument - use '%s host [echo]'\n", argv[0]); if(argc > 2) { printf("Echoing messages to all streams\n"); echo_to_all = 1; } sock_fd = Socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP); bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

bzero(&evnts, sizeof(evnts)); evnts.sctp_data_io_event = 1; Setsockopt(sock_fd,IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof(evnts)); if(echo_to_all == 0) sctpstr_cli(stdin,sock_fd,(SA *)&servaddr,sizeof(servaddr)); else sctpstr_cli_echoall(stdin,sock_fd,(SA *)&servaddr,sizeof(servaddr)); Close(sock_fd); return(0); }

Page 17: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Validate arguments

Arguments: host and echo flag. if(argc < 2) err_quit("Missing host argument - use '%s

host [echo]'\n", argv[0]); if(argc > 2) { printf("Echoing messages to all streams\

n"); echo_to_all = 1;}

Page 18: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Create a socket

Create an SCTP socketsock_fd = Socket(AF_INET,

SOCK_SEQPACKET, IPPROTO_SCTP);

Page 19: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Set up server address

Use given server address and well known port number to construct server address structure using Inet_pton(). bzero(&servaddr, sizeof(servaddr)); servaddr.sin_family = AF_INET; servaddr.sin_addr.s_addr = htonl(INADDR_ANY); servaddr.sin_port = htons(SERV_PORT); Inet_pton(AF_INET, argv[1], &servaddr.sin_addr);

Page 20: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Set up for notifications

Explicitly setting notification subscription for one-to-many socket. bzero(&evnts, sizeof(evnts)); evnts.sctp_data_io_event = 1; Setsockopt(sock_fd,IPPROTO_SCTP,

SCTP_EVENTS, &evnts, sizeof(evnts));

Page 21: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Echo processing

If flag is not set then sctpstrcli() is called. Else sctpstr_cli_echoall() is called.

if(echo_to_all == 0) sctpstr_cli(stdin,sock_fd,(SA

*)&servaddr,sizeof(servaddr)); else sctpstr_cli_echoall(stdin,sock_fd,(SA

*)&servaddr,sizeof(servaddr));

These functions are shown later.

Page 22: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Finish

When all is done, client closes socket and returns zero. Close(sock_fd); return(0);

Page 23: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: sctpstr_cli() #include "unp.h"

void sctpstr_cli(FILE *fp, int sock_fd, struct sockaddr *to, socklen_t tolen) { struct sockaddr_in peeraddr; struct sctp_sndrcvinfo sri; char sendline[MAXLINE], recvline[MAXLINE]; socklen_t len; int out_sz,rd_sz; int msg_flags;

bzero(&sri,sizeof(sri)); while (fgets(sendline, MAXLINE, fp) != NULL) { if(sendline[0] != '[') { printf("Error, line must be of the form '[streamnum]text'\n"); continue; } sri.sinfo_stream = strtol(&sendline[1],NULL,0); out_sz = strlen(sendline); Sctp_sendmsg(sock_fd, sendline, out_sz, to, tolen, 0, 0, sri.sinfo_stream, 0, 0);

len = sizeof(peeraddr); rd_sz = Sctp_recvmsg(sock_fd, recvline, sizeof(recvline), (SA *)&peeraddr, &len, &sri,&msg_flags); printf("From str:%d seq:%d (assoc:0x%x):", sri.sinfo_stream,sri.sinfo_ssn, (u_int)sri.sinfo_assoc_id); printf("%.*s",rd_sz,recvline); } }

Page 24: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Initialize

Initialize structure and enter loop. Block until user inputs line of text.

bzero(&sri,sizeof(sri)); while (fgets(sendline, MAXLINE, fp) !=

NULL) {

Page 25: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Validate input

Validate given input from the user. if(sendline[0] != '[') { printf("Error, line must be of the form

'[streamnum]text'\n"); continue;}

Page 26: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Stream number

Stream number is translated from the user input and put into the structure.sri.sinfo_stream = strtol(&sendline[1],NULL,0);

Page 27: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Send message

Message is sent.out_sz = strlen(sendline);Sctp_sendmsg(sock_fd, sendline, out_sz, to, tolen, 0, 0, sri.sinfo_stream, 0, 0);

Page 28: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Wait for response

Program blocks until message is received back from the server. len = sizeof(peeraddr);rd_sz = Sctp_recvmsg(sock_fd, recvline,

sizeof(recvline), (SA *)&peeraddr, &len, &sri,&msg_flags);

Page 29: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Display message

Received message is displayed to standard output.printf("From str:%d seq:%d (assoc:0x%x):", sri.sinfo_stream,sri.sinfo_ssn, (u_int)sri.sinfo_assoc_id);printf("%.*s",rd_sz,recvline);

Page 30: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Finish?

Program reads lines from user until it is terminated by an external signal.

Page 31: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Head-of-Line blocking

Head-of-Line blocking occurs when TCP segment is lost and latter segments arrive out of order.

All the subsequent segments are held until the lost segment is retransmitted.

This ensures data arrives to the application in the order it was sent.

Page 32: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

SCTP blocking of streams

SCTP allows multiple streams over single connection.

Each stream can be transmitted independent of other streams.

Blocking of one stream for retransmission does not block other streams.

Page 33: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: sctpstr_cli_echoall() #include "unp.h"

#define SCTP_MAXLINE 800

void sctpstr_cli_echoall(FILE *fp, int sock_fd, struct sockaddr *to, socklen_t tolen) { struct sockaddr_in peeraddr; struct sctp_sndrcvinfo sri; char sendline[SCTP_MAXLINE], recvline[SCTP_MAXLINE]; socklen_t len; int rd_sz,i,strsz; int msg_flags;

bzero(sendline,sizeof(sendline)); bzero(&sri,sizeof(sri)); while (fgets(sendline, SCTP_MAXLINE - 9, fp) != NULL) { strsz = strlen(sendline); if(sendline[strsz-1] == '\n') { sendline[strsz-1] = '\0'; strsz--; } for(i=0;i<SERV_MAX_SCTP_STRM;i++) { snprintf(sendline + strsz, sizeof(sendline) - strsz, ".msg.%d", i); Sctp_sendmsg(sock_fd, sendline, sizeof(sendline), to, tolen, 0, 0, i, 0, 0); } for(i=0;i<SERV_MAX_SCTP_STRM;i++) { len = sizeof(peeraddr); rd_sz = Sctp_recvmsg(sock_fd, recvline, sizeof(recvline), (SA *)&peeraddr, &len, &sri,&msg_flags); printf("From str:%d seq:%d (assoc:0x%x):", sri.sinfo_stream,sri.sinfo_ssn, (u_int)sri.sinfo_assoc_id); printf("%.*s\n",rd_sz,recvline); } } }

Page 34: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Initialize and read input

Initialize data structures. Enter loop and read user input.

bzero(sendline,sizeof(sendline)); bzero(&sri,sizeof(sri)); while (fgets(sendline, SCTP_MAXLINE -

9, fp) != NULL) {

Page 35: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Process user input

Process given user input.strsz = strlen(sendline); if(sendline[strsz-1] == '\n') { sendline[strsz-1] = '\0'; strsz--;}

Page 36: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Send message

Send message to each stream. Append .msg. and stream number to each

message for visual help of tracking messages. for(i=0;i<SERV_MAX_SCTP_STRM;i++) { snprintf(sendline + strsz, sizeof(sendline) - strsz, ".msg.%d", i); Sctp_sendmsg(sock_fd, sendline, sizeof(sendline), to, tolen, 0, 0, i, 0, 0); }

Page 37: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Read, display and loop

Block until incoming data comes from streams. Display message and move to read next.

for(i=0;i<SERV_MAX_SCTP_STRM;i++) { len = sizeof(peeraddr); rd_sz = Sctp_recvmsg(sock_fd, recvline, sizeof(recvline), (SA *)&peeraddr, &len, &sri,&msg_flags); printf("From str:%d seq:%d (assoc:0x%x):", sri.sinfo_stream,sri.sinfo_ssn,

(u_int)sri.sinfo_assoc_id); printf("%.*s\n",rd_sz,recvline); }

After last message is read, loop back to user input.

Page 38: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Client: Modifications

Send two messages to each stream to see messages are being held for reordering. for(i=0;i<SERV_MAX_SCTP_STRM;i++) { snprintf(sendline + strsz, sizeof(sendline) - strsz, ".msg.%d 1",

i); Sctp_sendmsg(sock_fd, sendline, sizeof(sendline), to, tolen, 0, 0, i, 0, 0); snprintf(sendline + strsz, sizeof(sendline) - strsz, ".msg.%d 2",

i); Sctp_sendmsg(sock_fd, sendline, sizeof(sendline), to, tolen, 0, 0, i, 0, 0); } for(i=0;i<SERV_MAX_SCTP_STRM*2;i++) { len = sizeof(peeraddr);

Page 39: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Number of streams

FreeBSD KAME implementation: default 10 streams at start.

Change must be made on the socket before an association is made. if (argc == 2) stream_increment = atoi(argv[1]); sock_fd = Socket(AF_INET, SOCK_SEQPACKET,

IPPROTO_SCTP); bzero(&initm,sizeof(initm)); initm.sinit_num_ostreams = SERV_MORE_STRMS_SCTP; Setsockopt(sock_fd, IPPROTO_SCTP, SCTP_INITMSG, &initm, sizeof(initm));

Page 40: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Termination

Two possible methods for closing one-to-many association:Close after sending message (eof)Close immediately (abort)

Page 41: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Termination: eof

Server wishes to shutdown an association after sending message

Set flag MSG_EOF in the reply message structure.Sctp_sendmsg(sock_fd, readbuf, rd_sz, (SA *)&cliaddr, len, sri.sinfo_ppid, (sri.sinfo_flags | MSG_EOF),

sri.sinfo_stream, 0, 0); }

Page 42: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Termination: abort

Server wishes to shutdown an association immediately. Set flag MSG_ABORT in the reply message structure. Forces immediate termination with an ABORT chunk. Any data not transmitted will be discarded.

strcpy(byemsg,"goodbye"); Sctp_sendmsg(sock_fd, byemsg, strlen(byemsg), (SA *)&servaddr, sizeof(servaddr), 0, MSG_ABORT, 0, 0, 0); Close(sock_fd);

Even if association has been aborted, socket descriptor still needs to be freed with Close().

Page 43: Chapter 10 SCTP Client / Server example. Simple echo server using SCTP protocol Send line of text from client to server Server sends the same line back

Summary

Simple SCTP client and iterative server Head-of-Line blocking problem Number of streams Shutdown and abort associations More on SCTP on chapter 23