Skip to content

Instantly share code, notes, and snippets.

@robbmanes
Created February 2, 2016 21:49
Show Gist options
  • Save robbmanes/25f413b1c6ab228390b3 to your computer and use it in GitHub Desktop.
Save robbmanes/25f413b1c6ab228390b3 to your computer and use it in GitHub Desktop.
# sctp_init_timeou_test
Quick method of testing timeouts and connectivity of basic SCTP sockets.
APP=sctp_init_timeo_test
all:
gcc -o $(APP) $(APP).c -Wall -lsctp
clean:
rm $(APP)
#include <stdio.h>
#include <stdlib.h>
#include <errno.h>
#include <time.h>
#include <string.h>
#include <unistd.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <sys/types.h>
#define TIMEOUT_IN_MS 10000
int sctp_serve_and_ignore(int listen_port);
int sctp_connect();
int main(int argc, char **argv);
void print_usage_and_exit();
int sctp_serve_and_ignore(int listen_port)
{
int listenfd = 0;
struct sockaddr_in serv_addr;
listenfd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = htonl(INADDR_ANY);
serv_addr.sin_port = htons(listen_port);
if(bind(listenfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)))
return -1;
if(listen(listenfd, 10))
return -1;
printf("Server now listening on %d - will not accept connections.\n", listen_port);
while(1)
{
sleep(10);
}
return 0;
}
int sctp_connect(char *addr, int port)
{
int connfd;
struct sockaddr_in serv_addr;
struct sctp_initmsg initmsg;
connfd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
serv_addr.sin_family = AF_INET;
serv_addr.sin_port = htons(port);
serv_addr.sin_addr.s_addr = inet_addr(addr);
initmsg.sinit_num_ostreams = 10;
initmsg.sinit_max_instreams = 10;
initmsg.sinit_max_attempts = 5;
initmsg.sinit_max_init_timeo = TIMEOUT_IN_MS;
setsockopt(connfd, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg));
printf("Attempting to connect...\n");
if(connect(connfd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)))
return -1;
printf("Connect sequence completed.\n");
return 0;
}
int main(int argc, char **argv)
{
if(argc < 2)
print_usage_and_exit();
char match = argv[1][0];
switch(match)
{
case 's':
if(argc > 3)
print_usage_and_exit();
sctp_serve_and_ignore(atoi(argv[2]));
break;
case 'c':
if(argc > 4)
print_usage_and_exit();
sctp_connect(argv[2], atoi(argv[3]));
break;
default:
print_usage_and_exit();
}
return 0;
}
void print_usage_and_exit()
{
printf("Usage:\n");
printf(" # sctp_init_timeo_test s <listen port>\n");
printf(" # sctp_init_timeo_test c <server IP> <server port>\n");
exit(1);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment