0

I have written a client server sctp program in c. on sctp client side I create two client threads and each thread binds with local port 8888 and 9999 port respectively. However, when I connect client to server and see the client ports on sever by stripping client tcp header. i receive completly different port and both client ports seems to be same in server log. I want to connect two different ports from sctp client to the same sctp server port. Is it possible ? it server side multiplexing is possible in scpt? Is many to one connection from client to server is possible in sctp?

client code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>
#include <pthread.h>


static void die(const char *s) {
        perror(s);
        exit(1);
}

void* client_thread(void* arg) 
{
    int clientSocket;
    struct sockaddr_in clientAddress;
    struct sockaddr_in serverAddress;
    const char *msg = "Hello, Server!";
    int ret;
    int clientPort = *((int*)arg);


    // Create the client socket
    clientSocket = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
    if (clientSocket == -1) {
        perror("Failed to create socket");
        exit(EXIT_FAILURE);
    }

    // Set up the client address and port
    clientAddress.sin_family = AF_INET;
    clientAddress.sin_port = htons(clientPort);  // Specify the source port number here
    clientAddress.sin_addr.s_addr = INADDR_ANY;

    // Bind the client socket to the local address and port
    if (bind(clientSocket, (struct sockaddr *)&clientAddress, sizeof(clientAddress)) < 0) {
        perror("Failed to bind socket");
        exit(EXIT_FAILURE);
    }

    // Set up the server address and port
    serverAddress.sin_family = AF_INET;
    serverAddress.sin_port = htons(62325);  // Specify the destination port number here
    serverAddress.sin_addr.s_addr = inet_addr("127.0.0.1");  // Specify the server IP address here

    // Connect to the server
    if (connect(clientSocket, (struct sockaddr *)&serverAddress, sizeof(serverAddress)) < 0) {
        perror("Failed to connect to server");
        exit(EXIT_FAILURE);
    }
        
    char buffer[45];

    int length = snprintf(buffer, sizeof(buffer), "Hello, Server! The value of client port %d", clientPort);
    ret = sctp_sendmsg(clientSocket, (void *) buffer, length + 1, NULL, 0, 0, 0, 0, 0, 0 );
    if (ret < 0)
            die("sctp_sendmsg");

    close(clientSocket);
    return NULL;
}


int main(int argc, char **argv) 
{
    pthread_t thread1, thread2;
    int client_port1 = 8888;
    int client_port2 = 9999;

    // Create thread 1
    if (pthread_create(&thread1, NULL, client_thread, &client_port1) != 0) {
        perror("Failed to create thread 1");
        exit(EXIT_FAILURE);
    }

    // Create thread 2
    if (pthread_create(&thread2, NULL, client_thread, &client_port2) != 0) {
        perror("Failed to create thread 2");
        exit(EXIT_FAILURE);
    }

    // Wait for threads to finish
    if (pthread_join(thread1, NULL) != 0) {
        perror("Failed to join thread 1");
        exit(EXIT_FAILURE);
    }
    if (pthread_join(thread2, NULL) != 0) {
        perror("Failed to join thread 2");
        exit(EXIT_FAILURE);
    }

    return 0;
}

server code

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <libgen.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <netinet/in.h>
#include <netinet/sctp.h>
#include <arpa/inet.h>

#define MY_PORT_NUM 62325

static void die(const char *s) {
        perror(s);
        exit(1);
}

static void server(void) {
        int listen_fd, conn_fd, flags, ret, in;        
        struct sctp_rcvinfo rcvinfo;
        struct sockaddr_in servaddr = {
                .sin_family = AF_INET,
                .sin_addr.s_addr = htonl(INADDR_ANY),
                .sin_port = htons(MY_PORT_NUM),
        };
        struct sctp_initmsg initmsg = {
                .sinit_num_ostreams = 5,
                .sinit_max_instreams = 5,
                .sinit_max_attempts = 4,
        };

        listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
        if (listen_fd < 0)
                die("socket");

        ret = bind(listen_fd, (struct sockaddr *) &servaddr, sizeof(servaddr));
        if (ret < 0)
                die("bind");

        ret = setsockopt(listen_fd, IPPROTO_SCTP, SCTP_INITMSG, &initmsg, sizeof(initmsg));
        if (ret < 0)
                die("setsockopt");

        ret = listen(listen_fd, initmsg.sinit_max_instreams);
        if (ret < 0)
                die("listen");

        for (;;) {
                char buffer[1024];

                printf("Waiting for connection\n");
                fflush(stdout);

                conn_fd = accept(listen_fd, (struct sockaddr *) NULL, NULL);
                if(conn_fd < 0)
                        die("accept()");

                printf("New client connected\n");
                fflush(stdout);
               
                in = sctp_recvmsg(conn_fd, buffer, sizeof(buffer), NULL, 0, &rcvinfo, &flags);
                if (in > 0) {
                        printf("Received data: %s\n", buffer);
                        // Extract the source port from the SCTP header
                        uint16_t sourcePort = ntohs(rcvinfo.rcv_sid);
                        printf("Source port: %u\n", sourcePort);                        
                        fflush(stdout);
                }

                close(conn_fd);
        }
}


int main(int argc, char **argv) 
{
   server();
   return 0;
}

output

New client connected
Received data: Hello, Server! The value of client port 9999
Source port: 61608         ------------------------------------> using 9999 client port but shows 61608
Waiting for connection
New client connected
Received data: Hello, Server! The value of client port 8888
Source port: 61608         ------------------------------------> using 8888 client port but shows 61608
Waiting for connection
Harith
  • 4,663
  • 1
  • 5
  • 20

0 Answers0