SCTP socket added

This commit is contained in:
User Infidel 2022-10-05 21:04:30 +07:00
parent 02ae085aed
commit 018e1911c4
5 changed files with 112 additions and 4 deletions

View File

@ -41,6 +41,13 @@ The `kqueue_socket.c` is my attempt on create a socket watcher timer program usi
- Array of kevent - Array of kevent
### SCTP
- Listening
- Init ACK
> Find out how to make a stateless connection over SCTP.
## Goal ## Goal
- Understand the difference between c++ and C - Understand the difference between c++ and C

BIN
bin/sctp_sock Executable file

Binary file not shown.

Binary file not shown.

102
src/ksctp_socket.c Normal file
View File

@ -0,0 +1,102 @@
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/sctp.h>
#include <netinet/in.h>
#include <stdlib.h>
#include <sys/event.h>
#include <stdio.h>
int main() {
int socket_listen_fd,
socket_connection_fd,
i,
kq,
client_len,
new_events,
port_num = 36215;
struct sockaddr_in serv_addr, client_addr;
struct kevent change_event,
evList[32],
event;
socket_listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_SCTP);
if (((socket_listen_fd) < 0))
{
perror("Faild creating socket");
exit(1);
}
bzero((char *)&serv_addr, sizeof(serv_addr));
serv_addr.sin_family = AF_INET;
serv_addr.sin_addr.s_addr = INADDR_ANY;
serv_addr.sin_port = htons(port_num);
if (bind(socket_listen_fd, (struct sockaddr *)&serv_addr, sizeof(serv_addr)) < 0)
{
perror("Failed bind socket");
exit(1);
}
listen(socket_listen_fd, 3);
client_len = sizeof(client_addr);
kq = kqueue();
EV_SET(&change_event, socket_listen_fd, EVFILT_READ, EV_ADD | EV_ENABLE, 0, 0, 0);
if (kevent(kq, &change_event, 1, NULL, 0, NULL) == -1)
{
perror("Failed to bind socket to kevent");
exit(1);
}
int x = 0;
for (;;) {
printf("-----------SEQ : %d\n", x+1);
new_events = kevent(kq, NULL, 0, evList, 1, NULL);
if (new_events == -1)
{
perror("Failed to create new kevent");
exit(1);
}
for (int i = 0; new_events > i; i++)
{
printf("Listed events : %d\n", new_events);
int event_fd = evList[i].ident;
if (evList[i].flags & EV_EOF)
{
printf("Client disconnect...\n");
close(event_fd);
}
else if (event_fd == socket_listen_fd) ;
{
printf("New connection incoming...\n");
socket_connection_fd = accept(event_fd, (struct sockaddr *)&client_addr, (socklen_t *)&client_len);
if (socket_connection_fd == -1)
{
perror("Failed to accept connection...\n");
}
EV_SET(&change_event, socket_connection_fd, EVFILT_READ, EV_ADD, 0, 0, NULL);
if (kevent(kq, &change_event, 1, NULL, 0, NULL) < 0)
{
perror("kevent failed to monitor socket connection");
}
}
}
x=x+1;
}
}

View File

@ -14,12 +14,11 @@ int main() {
kq, kq,
new_events; new_events;
struct kevent change_event, event; struct kevent change_event, event;
struct kevent evList[32]; struct kevent evList[32];
struct sockaddr_in serv_addr, client_addr; struct sockaddr_in serv_addr, client_addr;
if (((socket_listen_fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)) if (((socket_listen_fd = socket(AF_INET, SOCK_STREAM, IPPROTO_TCP)) < 0))
{ {
perror("Failed creating socket"); perror("Failed creating socket");
exit(1); exit(1);