Stream Control Transmission Protocol (SCTP) Associations
where the field sinfo_stream of sinfo has been set.
The call to read is, conversely:
ssize_t sctp_recvmsg(int sd,
void *msg,
size_t len,
struct sockaddr *from,
socklen_t *fromlen
struct sctp_sndrcvinfo *sinfo
int *msg_flags)
The stream number is then available in sinfo.sinfo_stream.
The SCTP stack keeps a lot of information about each message that passes between peers. It also keeps information about the state of each association. To avoid overloading applications, most of this information is suppressed and is not passed to the application. In particular, by default, the structure sctp_sndrcvinfo is not filled in, so a reader cannot tell on which stream a read occurred! To enable this to be filled, a socket option must be called first as:
struct sctp_event_subscribe events;
bzero(&events, sizeof(events));
events.sctp_data_io_event = 1;
setsockopt(sockfd, IPPROTO_SCTP,
SCTP_EVENTS, &events, sizeof(events));
(More details on SCTP events will be given in the next article.) See Listings 4 (streamsend_echo_client.c) and 5 (streamsend_echo_server.c) for an example of a client and server using a specific stream for communication. .
Listing 4. streamsend_echo_client.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#define SIZE 1024
char buf[SIZE];
#define ECHO_PORT 2013
char *usage_msg = "usage: streamsend_echo_client ip-addr istreams
ostreams stream";
void usage() {
fprintf(stderr, "%s\n", usage_msg);
exit(1);
}
int main(int argc, char *argv[]) {
int sockfd;
int len;
struct sockaddr_in serv_addr;
struct sockaddr_in *addresses;
int addr_size = sizeof(struct sockaddr_in);
int addr_count = argc - 1;
int port = ECHO_PORT;
char *message = "hello\n";
struct sctp_initmsg initmsg;
struct sctp_status status;
struct sctp_sndrcvinfo sinfo;
int ochannel;
if (argc != 5) usage();
/* create endpoint */
sockfd = socket(AF_INET, SOCK_STREAM,
IPPROTO_SCTP
);
if (sockfd < 0) {
perror(NULL);
exit(2); }
/* connect to server */
addresses = malloc(addr_size);
if (addresses == NULL) {
exit(1);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(port);
memcpy(addresses, &serv_addr, addr_size);
memset(&initmsg, 0, sizeof(initmsg));
initmsg.sinit_max_instreams = atoi(argv[2]);
initmsg.sinit_num_ostreams = atoi(argv[3]);
printf("Asking for: input streams: %d, output streams: %d\n",
initmsg.sinit_max_instreams,
initmsg.sinit_num_ostreams);
if (setsockopt(sockfd, IPPROTO_SCTP,
SCTP_INITMSG, &initmsg, sizeof(initmsg))) {
perror("set sock opt\n");
}
if (sctp_connectx(sockfd, (struct sockaddr *) addresses, 1) < 0)
{
perror("connectx");
exit(3);
}
memset(&status, 0, sizeof(status));
len = sizeof(status);
status.sstat_assoc_id = 1;
if (getsockopt(sockfd, IPPROTO_SCTP,
SCTP_STATUS, &status, &len) == -1) {
perror("get sock opt\n");
}
printf("Got: input streams: %d, output streams: %d\n",
status.sstat_instrms,
status.sstat_outstrms);
/* sanity check channel */
ochannel = atoi(argv[4]);
if (ochannel >= status.sstat_outstrms)
printf("Writing on illegal channel %d\n", ochannel);
/* transfer data */
bzero(&sinfo, sizeof(sinfo));
sinfo.sinfo_stream = ochannel;
sctp_send(sockfd, message, strlen(message),
&sinfo, 0);
sinfo.sinfo_flags = SCTP_EOF;
sctp_send(sockfd, NULL, 0,
&sinfo, 0);
close(sockfd);
exit(0);
}
Listing 5. streamsend_echo_server.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#define SIZE 1024
char buf[SIZE];
#define TIME_PORT 2013
char *usage_msg = "usage: app ip-addr istreams ostreams";
void usage() {
fprintf(stderr, "%s\n", usage_msg);
exit(1);
}
int main(int argc, char *argv[]) {
int sockfd, client_sockfd;
int nread, len;
struct sockaddr_in serv_addr, client_addr;
time_t t;
struct sockaddr_in *addresses;
int addr_size = sizeof(struct sockaddr_in);
int addr_count = 1;
int port = TIME_PORT;
int n;
struct sctp_initmsg initmsg;
struct sctp_status status;
sctp_assoc_t associd;
struct sctp_sndrcvinfo sinfo;
struct sctp_event_subscribe events;
int flags;
if (argc != 4) usage();
/* create endpoint */
sockfd = socket(AF_INET, SOCK_STREAM,
IPPROTO_SCTP
);
if (sockfd < 0) {
perror(NULL);
exit(2);
}
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = inet_addr(argv[1]);
serv_addr.sin_port = htons(port);
if (sctp_bindx(sockfd, (struct sockaddr *) &serv_addr, addr_count,
SCTP_BINDX_ADD_ADDR) == -1) {
perror("sctp bindx");
exit(2);
}
memset(&initmsg, 0, sizeof(initmsg));
initmsg.sinit_max_instreams = atoi(argv[2]);
initmsg.sinit_num_ostreams = atoi(argv[3]);
printf("Asking for: input streams: %d, output streams: %d\n",
initmsg.sinit_max_instreams,
initmsg.sinit_num_ostreams);
if (setsockopt(sockfd, IPPROTO_SCTP,
SCTP_INITMSG, &initmsg, sizeof(initmsg))) {
perror("set sock opt\n");
}
/* specify queue */
listen(sockfd, 5);
for (;;) {
len = sizeof(client_addr);
client_sockfd = accept(sockfd, (struct sockaddr *) &client_addr,
&len);
if (client_sockfd == -1) {
perror(NULL); continue;
}
memset(&status, 0, sizeof(status));
len = sizeof(status);
status.sstat_assoc_id = 0;
if (getsockopt(client_sockfd, IPPROTO_SCTP,
SCTP_STATUS, &status, &len) == -1) {
perror("get sock opt\n");
}
printf("Got: input streams: %d, output streams: %d\n",
status.sstat_instrms,
status.sstat_outstrms);
for(;;) {
/* transfer data */
len = sizeof(client_addr);
bzero(&sinfo, sizeof(sinfo));
nread = sctp_recvmsg(client_sockfd, buf, SIZE,
(struct sockaddr *) &client_addr, &len,
&sinfo, &flags);
if (nread == 0) {
break;
}
printf("read %d bytes on channel %hd\n", nread,
sinfo.sinfo_stream);
printf("sinfo flags: %d\n", sinfo.sinfo_flags);
write(1, buf, nread);
}
close(client_sockfd);
}
}
There is no way to specify from which stream to read. This is deliberate; the intention is that when data is ready on any stream, then you read it. Otherwise, data could be blocked on a stream with no one to read it, which eventually could fill up system buffers. So, you can't restrict reading to any particular stream. But, once a read is done, you can tell which stream it has come from by using the mechanism above.
Typically, a server that reads and handles a message will have (pseudocode) that looks like this:
while (true) {
nread = sctp_recvmsg(..., msg, ..., &sinfo, ...)
if (nread <= 0) break;
assoc_id = sinfo.sinfo_assoc_id;
stream = sinfo.sinfo_stream;
handle_mesg(assoc_id, stream, msg, nread);
}
This is a single-threaded read loop. It ensures that information is read, no matter what association or stream it is sent on. The application function handle_mesg() can, of course, dispatch the message to different threads if it wants. Writes, on the other hand can be sent from multiple threads if desired.
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Sponsored by AMD
Built-in forensics, incident response, and security with Red Hat Enterprise Linux 6
Every security policy provides guidance and requirements for ensuring adequate protection of information and data, as well as high-level technical and administrative security requirements for a system in a given environment. Traditionally, providing security for a system focuses on the confidentiality of the information on it. However, protecting the data integrity and system and data availability is just as important. For example, when processing United States intelligence information, there are three attributes that require protection: confidentiality, integrity, and availability.
Learn more about catching the bad guy in this free white paper.
Sponsored by DLT Solutions
| Designing Electronics with Linux | May 22, 2013 |
| Dynamic DNS—an Object Lesson in Problem Solving | May 21, 2013 |
| Using Salt Stack and Vagrant for Drupal Development | May 20, 2013 |
| Making Linux and Android Get Along (It's Not as Hard as It Sounds) | May 16, 2013 |
| Drupal Is a Framework: Why Everyone Needs to Understand This | May 15, 2013 |
| Home, My Backup Data Center | May 13, 2013 |
- Designing Electronics with Linux
- Making Linux and Android Get Along (It's Not as Hard as It Sounds)
- Dynamic DNS—an Object Lesson in Problem Solving
- Using Salt Stack and Vagrant for Drupal Development
- New Products
- Validate an E-Mail Address with PHP, the Right Way
- Build a Skype Server for Your Home Phone System
- Why Python?
- A Topic for Discussion - Open Source Feature-Richness?
- Tech Tip: Really Simple HTTP Server with Python
Enter to Win an Adafruit Pi Cobbler Breakout Kit for Raspberry Pi

It's Raspberry Pi month at Linux Journal. Each week in May, Adafruit will be giving away a Pi-related prize to a lucky, randomly drawn LJ reader. Winners will be announced weekly.
Fill out the fields below to enter to win this week's prize-- a Pi Cobbler Breakout Kit for Raspberry Pi.
Congratulations to our winners so far:
- 5-8-13, Pi Starter Pack: Jack Davis
- 5-15-13, Pi Model B 512MB RAM: Patrick Dunn
- 5-21-13, Prototyping Pi Plate Kit: Philip Kirby
- Next winner announced on 5-27-13!
Free Webinar: Hadoop
How to Build an Optimal Hadoop Cluster to Store and Maintain Unlimited Amounts of Data Using Microservers
Realizing the promise of Apache® Hadoop® requires the effective deployment of compute, memory, storage and networking to achieve optimal results. With its flexibility and multitude of options, it is easy to over or under provision the server infrastructure, resulting in poor performance and high TCO. Join us for an in depth, technical discussion with industry experts from leading Hadoop and server companies who will provide insights into the key considerations for designing and deploying an optimal Hadoop cluster.
Some of key questions to be discussed are:
- What is the “typical” Hadoop cluster and what should be installed on the different machine types?
- Why should you consider the typical workload patterns when making your hardware decisions?
- Are all microservers created equal for Hadoop deployments?
- How do I plan for expansion if I require more compute, memory, storage or networking?




3 hours 24 min ago
3 hours 32 min ago
5 hours 47 min ago
8 hours 17 min ago
18 hours 20 min ago
22 hours 47 min ago
1 day 2 hours ago
1 day 2 hours ago
1 day 5 hours ago
1 day 5 hours ago