Commit 4654393e authored by Andreas Schmidt's avatar Andreas Schmidt

Rename and refactor. Proper port binding.

parent eccb98a6
......@@ -6,7 +6,7 @@ set(CMAKE_LIBRARY_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/lib)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin)
add_subdirectory(prrt)
add_library(PRRT prrt/prrt_socket.c)
add_library(PRRT prrt/socket.c)
add_executable(sender sender.c)
add_executable(receiver receiver.c)
......
//
// Created by root on 05.11.15.
//
#ifndef PRRT_DEFINES_H
#define PRRT_DEFINES_H
#endif //PRRT_DEFINES_H
#include <netdb.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#include <sys/types.h>
#include <sys/socket.h>
#include "prrt_socket.h"
int prrt_socket() {
int fd;
if((fd = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket");
return -1;
}
return fd;
}
int prrt_connect() {
return 0;
}
int prrt_bind(int fd, int port) {
struct sockaddr_in address;
memset((char*) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(port);
return bind(fd, (struct sockaddr *) &address, sizeof(address));
}
int prrt_send(int fd, const void * data, size_t data_len) {
// TODO: should iterate over receivers
struct hostent *hp;
char *host = "localhost";
struct sockaddr_in targetaddr;
memset((char*) &targetaddr, 0, sizeof(targetaddr));
targetaddr.sin_family = AF_INET;
targetaddr.sin_port = htons(5000); // TODO: make port flexible
hp = gethostbyname(host);
memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], hp->h_length);
if((sendto(fd, data, data_len, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
perror("sendto failed");
return -1;
}
return 0;
}
int prrt_recv(int fd, void * bufin, size_t length) {
int n;
struct sockaddr_in remote;
socklen_t addrlen = sizeof(remote);
n = recvfrom(fd, bufin,length, 0, (struct sockaddr *) &remote, &addrlen);
printf("got a datagram from %s port %d\n", inet_ntoa(remote.sin_addr), ntohs(remote.sin_port));
return n;
}
int prrt_close(int fd) {
close(fd);
return 0;
}
#ifndef PRRT_SOCKET
#define PRRT_SOCKET
int prrt_socket(void);
int prrt_close(int fd);
int prrt_bind(int fd, int port);
int prrt_connect();
int prrt_send(int fd, const void * data, size_t data_len);
int prrt_recv(int fd, void * bufin, size_t length);
#endif
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include "socket.h"
int prrt_create_socket(prrt_socket *sock, uint16_t port) {
// Create Data Socket
if((sock->fd_data = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket");
return -1;
}
// Bind Data Socket
struct sockaddr_in address;
memset((char*) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons(port);
if(bind(sock->fd_data, (struct sockaddr *) &address, sizeof(address)) < 0) {
perror("cannot bind socket");
return -1;
}
// Create Feedback Socket
if((sock->fd_feedback = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
perror("cannot create socket");
return -1;
}
// Bind Feedback Socket
memset((char*) &address, 0, sizeof(address));
address.sin_family = AF_INET;
address.sin_addr.s_addr = htonl(INADDR_ANY);
address.sin_port = htons((uint16_t) (port + 1)); // TODO: fail if port is 65535
if(bind(sock->fd_feedback, (struct sockaddr *) &address, sizeof(address)) < 0) {
perror("cannot bind socket");
return -1;
}
return 0;
}
int prrt_connect(prrt_socket *sock, char *host, size_t host_len, uint16_t port) {
return 0;
}
int prrt_send(prrt_socket *sock, const void *data, size_t data_len) {
// TODO: should iterate over receivers
struct hostent *hp;
char *host = "localhost"; // TODO make receiver flexible
struct sockaddr_in targetaddr;
memset((char*) &targetaddr, 0, sizeof(targetaddr));
targetaddr.sin_family = AF_INET;
targetaddr.sin_port = htons(5000); // TODO: make port flexible
hp = gethostbyname(host);
memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], hp->h_length);
if((sendto(sock->fd_data, data, data_len, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
perror("sendto failed");
return -1;
}
return 0;
}
ssize_t prrt_recv(prrt_socket *sock, void *bufin, size_t length) {
// RECEIVE DATA
ssize_t n;
struct sockaddr_in remote;
socklen_t addrlen = sizeof(remote);
n = recvfrom(sock->fd_data, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
uint16_t remote_port = ntohs(remote.sin_port);
char *remote_host = inet_ntoa(remote.sin_addr);
printf("got a datagram from %s port %d\n", remote_host, remote_port);
// REPLY
struct sockaddr_in targetaddr;
memset((char*) &targetaddr, 0, sizeof(targetaddr));
targetaddr.sin_family = AF_INET;
targetaddr.sin_port = htons(remote_port + 1);
struct hostent *hp;
hp = gethostbyname(remote_host);
memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], hp->h_length);
if((sendto(sock->fd_data, bufin, n, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
perror("sendto failed");
return -1;
}
return n;
}
int prrt_close_socket(prrt_socket *sock) {
close(sock->fd_data);
close(sock->fd_feedback);
return 0;
}
ssize_t prrt_recv_feedback(prrt_socket *sock, void *bufin, size_t length) {
printf("RECEIVING FB");
ssize_t n;
struct sockaddr_in remote;
socklen_t addrlen = sizeof(remote);
n = recvfrom(sock->fd_feedback, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
uint16_t remote_port = ntohs(remote.sin_port);
char *remote_host = inet_ntoa(remote.sin_addr);
printf("got feedback from %s port %d\n", remote_host, remote_port);
return n;
}
#ifndef PRRT_SOCKET_H
#define PRRT_SOCKET_H
#include <stdint.h>
#include <stddef.h>
#include <stdio.h>
typedef struct {
int fd_data;
int fd_feedback;
} prrt_socket;
int prrt_create_socket(prrt_socket *sock, uint16_t port);
int prrt_close_socket(prrt_socket *sock);
int prrt_connect(prrt_socket *string, char *i, size_t i1, uint16_t i2);
int prrt_send(prrt_socket *sock, const void *data, size_t data_len);
ssize_t prrt_recv(prrt_socket *sock, void *bufin, size_t length);
ssize_t prrt_recv_feedback(prrt_socket *sock, void *bufin, size_t length);
#endif // PRRT_SOCKET_H
#include <stdio.h>
#include <unistd.h>
#include "prrt/prrt_socket.h"
#include "prrt/socket.h"
#define MAXBUF 1024*1024
int main() {
int n;
prrt_socket sock;
ssize_t n;
char bufin[MAXBUF];
printf("PRRT - RECEIVER\n");
int s = prrt_socket();
if(prrt_bind(s, 5000) < 0) {
perror("bind failed");
if(prrt_create_socket(&sock, 5000) < 0) {
perror("could not create socket");
return 0;
}
while(1) {
n = prrt_recv(s,bufin,MAXBUF);
n = prrt_recv(&sock,bufin,MAXBUF);
if(n < 0) {
perror("Error receiving data");
break;
} else {
printf("GOT %d BYTES\n",n);
printf("GOT %d BYTES\n", (int) n);
}
usleep(1000*1000);
}
prrt_close_socket(&sock);
return 0;
}
#include <stdint.h>
#include <stdio.h>
#include <string.h>
#include "prrt/prrt_socket.h"
#include "prrt/socket.h"
#define MAXBUF 1024 * 1024
int main() {
int fd;
uint16_t local_port = 6000;
prrt_socket sock = {};
printf("PRRT - SENDER\n");
char *message = "this is a message";
if((fd = prrt_socket()) < 0) {
if(prrt_create_socket(&sock, local_port) < 0) {
perror("socket failed");
return 0;
}
prrt_connect();
char *remote_host = "localhost";
uint16_t remote_port = 5000;
prrt_connect(&sock, remote_host, strlen(remote_host), remote_port);
char *message = "this is a message";
prrt_send(&sock, message, strlen(message));
char bufin[MAXBUF];
prrt_send(fd, message, strlen(message));
ssize_t t = prrt_recv_feedback(&sock, bufin, MAXBUF);
printf("FEEDBACK: %d\n", (int) t);
prrt_close(fd);
prrt_close_socket(&sock);
return 0;
}
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment