int main() { int lfd, cfd; struct sctp_event_subscribe events; struct sctp_initmsg init; struct sockaddr_in sin[1]; if((lfd = ext_socket(checkIPv6() ? AF_INET6 : AF_INET, SOCK_STREAM, IPPROTO_SCTP)) == -1) { perror("socket"); exit(1); } init.sinit_num_ostreams = MAX_OUTGOING; init.sinit_max_instreams = MAX_INCOMING; init.sinit_max_attempts = 3; init.sinit_max_init_timeo = 30; if(ext_setsockopt(lfd, IPPROTO_SCTP, SCTP_INITMSG, (void*)&init, sizeof(init)) < 0) { perror("setsockopt"); exit(1); } sin->sin_family = AF_INET; sin->sin_port = htons(7); sin->sin_addr.s_addr = INADDR_ANY; if(ext_bind(lfd, (struct sockaddr *)sin, sizeof (*sin)) == -1) { perror("bind"); exit(1); } /* Enable ancillary data and notifications */ memset((char*)&events,1,sizeof(events)); if (ext_setsockopt(lfd, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof(events)) < 0) { perror("setsockopt SCTP_EVENTS"); exit(1); } if(ext_listen(lfd, 1) == -1) { perror("listen"); exit(1); } /* Wait for new associations */ for (;;) { if((cfd = ext_accept(lfd, NULL, 0)) == -1) { perror("accept"); exit(1); } /* Enable ancillary data and notifications */ memset((char*)&events,1,sizeof(events)); if(ext_setsockopt(cfd, IPPROTO_SCTP, SCTP_EVENTS, &events, sizeof(events)) < 0) { perror("setsockopt SCTP_EVENTS"); exit(1); } /* Echo back any and all data */ echo(cfd,0); } }
static pthread_t init_server(struct sockaddr_in local_addr, int port, void *(connection_handler ())) { pthread_t tid; int fd; struct server_data *server_data_p; if ((fd = ext_socket(PF_INET, SOCK_STREAM, IPPROTO_SCTP)) < 0) perror("socket call failed"); local_addr.sin_port= htons(port); if(ext_bind(fd, (struct sockaddr *)&local_addr, sizeof(local_addr)) < 0) perror("bind call failed"); if(ext_listen(fd, 1) < 0) perror("listen call failed"); server_data_p = malloc(sizeof(struct server_data)); server_data_p->fd = fd; server_data_p->connection_handler = connection_handler; if (verbose) { printf("Starting server on port %u.\n", port); fflush(stdout); } pthread_create(&tid, NULL, &start_server, (void *)server_data_p); return(tid); }
int main(int argc, char **argv) { int fd, n, addr_len, len, msg_flags, close_time; size_t buffer_size; fd_set rset; char buffer[1000]; struct sctp_event_subscribe evnts; struct sctp_sndrcvinfo sri; struct sockaddr_in local_addr, remote_addr; if (argc < 4) { printf("Usage: client local_port remote_addr remote_port [autoclose]\n"); exit(-1); } if ((fd = ext_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) < 0) perror("socket"); bzero(&evnts, sizeof(evnts)); evnts.sctp_data_io_event = 1; evnts.sctp_association_event = 1; evnts.sctp_address_event = 1; evnts.sctp_send_failure_event = 1; evnts.sctp_peer_error_event = 1; evnts.sctp_shutdown_event = 1; evnts.sctp_partial_delivery_event = 1; evnts.sctp_adaption_layer_event = 1; if (ext_setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof(evnts)) < 0) perror("setsockopt"); if (argc > 4) { close_time = atoi(argv[4]); if (ext_setsockopt(fd, IPPROTO_SCTP, SCTP_AUTOCLOSE, &close_time, sizeof(close_time)) < 0) perror("setsockopt"); } bzero(&local_addr, sizeof(struct sockaddr_in)); local_addr.sin_family = AF_INET; #ifdef HAVE_SIN_LEN local_addr.sin_len = sizeof(struct sockaddr_in); #endif local_addr.sin_addr.s_addr = htonl(INADDR_ANY); local_addr.sin_port = htons(atoi(argv[1])); if (ext_bind(fd, (struct sockaddr *) &local_addr, sizeof(local_addr)) != 0) perror("bind"); if (ext_listen(fd, 1) != 0) perror("listen"); remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(atoi(argv[3])); #ifdef HAVE_SIN_LEN remote_addr.sin_len = sizeof(struct sockaddr_in); #endif remote_addr.sin_addr.s_addr = inet_addr(argv[2]); FD_ZERO(&rset); while (1) { FD_SET(fd, &rset); FD_SET(0, &rset); n = ext_select(fd + 1, &rset, NULL, NULL, NULL); if (n == 0) { printf("Timer was runnig off.\n"); } if (FD_ISSET(0, &rset)) { printf("Reading from stdin.\n"); len = ext_read(0, (void *) buffer, sizeof(buffer)); if (len == 0) break; if (ext_sendto(fd, (const void *)buffer, len, 0, (const struct sockaddr *)&remote_addr, sizeof(remote_addr)) != len) perror("sendto"); } if (FD_ISSET(fd, &rset)) { printf("Reading from network.\n"); addr_len = sizeof(struct sockaddr_in); buffer_size = sizeof(buffer); if ((len = sctp_recvmsg(fd, (void *) buffer, buffer_size , (struct sockaddr *)&remote_addr, &addr_len, &sri,&msg_flags)) < 0) perror("recvfrom"); else { if(msg_flags & MSG_NOTIFICATION) { sctp_print_notification(buffer); continue; } else { printf("Message of length %d received from %s:%u: %.*s", len, inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port), len, buffer); } } } } if (ext_close(fd) < 0) perror("close"); sleep(2); return 0; }
int main(int argc, char **argv) { int fd, n, addr_len, len; fd_set rset; char buffer[1000]; struct sctp_event_subscribe evnts; struct sockaddr_in local_addr, remote_addr; if (argc != 4) { printf("Usage: client local_port remote_addr remote_port\n"); exit(-1); } if ((fd = ext_socket(AF_INET, SOCK_SEQPACKET, IPPROTO_SCTP)) < 0) perror("socket"); bzero(&evnts, sizeof(evnts)); if (ext_setsockopt(fd, IPPROTO_SCTP, SCTP_EVENTS, &evnts, sizeof(evnts)) < 0) perror("setsockopt"); bzero(&local_addr, sizeof(struct sockaddr_in)); local_addr.sin_family = AF_INET; #ifdef HAVE_SIN_LEN local_addr.sin_len = sizeof(struct sockaddr_in); #endif local_addr.sin_addr.s_addr = htonl(INADDR_ANY); local_addr.sin_port = htons(atoi(argv[1])); if (ext_bind(fd, (struct sockaddr *) &local_addr, sizeof(local_addr)) != 0) perror("bind"); if (ext_listen(fd, 1) != 0) perror("listen"); remote_addr.sin_family = AF_INET; remote_addr.sin_port = htons(atoi(argv[3])); #ifdef HAVE_SIN_LEN remote_addr.sin_len = sizeof(struct sockaddr_in); #endif remote_addr.sin_addr.s_addr = inet_addr(argv[2]); FD_ZERO(&rset); while (1) { FD_SET(fd, &rset); FD_SET(0, &rset); puts("\n\n\n\nSELECT...\n"); n = ext_select(fd + 1, &rset, NULL, NULL, NULL); if (n == 0) { printf("Timer was runnig off.\n"); } if (FD_ISSET(0, &rset)) { printf("Reading from stdin.\n"); len = ext_read(0, (void *) buffer, sizeof(buffer)); if (len == 0) break; if (ext_sendto(fd, (const void *)buffer, len, 0, (const struct sockaddr *)&remote_addr, sizeof(remote_addr)) != len) perror("sendto"); } if (FD_ISSET(fd, &rset)) { printf("Reading from network.\n"); addr_len = sizeof(struct sockaddr_in); if ((len = ext_recvfrom(fd, (void *) buffer, sizeof(buffer),0,(struct sockaddr *) &remote_addr, &addr_len)) < 0) {perror("recvfrom"); /* puts("STOP!------------------"); exit(1); */ } else printf("Message of length %d received from %s:%u: %.*s", len, inet_ntoa(remote_addr.sin_addr), ntohs(remote_addr.sin_port), len, buffer); } } if (ext_close(fd) < 0) perror("close"); sleep(2); return 0; }