socket.c 3.54 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
#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;
}