socket.c 6.5 KB
Newer Older
1
#include "socket.h"
2

3

4
int PrrtSocket_create(PrrtSocket *sock_ptr, const uint16_t port, const uint8_t is_sender) {
5
    sock_ptr->seqno_source = 1;
6
    sock_ptr->seqnoRedundancy = 1;
7

8
    // Create Data Socket
9
    if ((sock_ptr->fd_data = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
10
11
12
13
14
        perror("cannot create socket");
        return -1;
    }

    // Create Feedback Socket
15
    if ((sock_ptr->fd_feedback = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
16
17
18
19
        perror("cannot create socket");
        return -1;
    }

20
    if (is_sender) {
21
22
        // Bind Feedback Socket
        struct sockaddr_in address;
23
        memset((char *) &address, 0, sizeof(address));
24
25
26
        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
27

28
        if (bind(sock_ptr->fd_feedback, (struct sockaddr *) &address, sizeof(address)) < 0) {
29
30
31
            perror("cannot bind socket");
            return -1;
        }
32

33
34
35
36
        pthread_mutex_init(&sock_ptr->out_queue_filled_mutex, NULL);
        pthread_cond_init(&sock_ptr->out_queue_filled_cv, NULL);

        sock_ptr->out_queue = List_create();
37
38

        int rc = pthread_create(&sock_ptr->receive_thread, NULL, receive_feedback_loop, (void *) sock_ptr);
39
        if (rc) {
40
41
42
43
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }

44
        rc = pthread_create(&sock_ptr->send_thread, NULL, send_data_loop, (void *) sock_ptr);
45
        if (rc) {
46
47
48
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }
49
    } else {
50
51
        // Bind Data Socket
        struct sockaddr_in address;
52
        memset((char *) &address, 0, sizeof(address));
53
54
55
56
        address.sin_family = AF_INET;
        address.sin_addr.s_addr = htonl(INADDR_ANY);
        address.sin_port = htons(port);

57
        if (bind(sock_ptr->fd_data, (struct sockaddr *) &address, sizeof(address)) < 0) {
58
59
60
            perror("cannot bind socket");
            return -1;
        }
61

62
        sock_ptr->forwardPacketTable = malloc(sizeof(PrrtForwardPacketTable));
63
        PrrtForwardPacketTable_create(sock_ptr->forwardPacketTable);
64
65
    }

66
67
68
    return 0;
}

69
int PrrtSocket_connect(PrrtSocket *sock_ptr, const char *host, const uint16_t port) {
70
    PrrtReceiver recv = {host, port};
71

72
73
    if (sock_ptr->receiver_len < PRRT_MAX_RECEIVER_COUNT) {
        sock_ptr->receivers[sock_ptr->receiver_len] = recv;
74
75
76
77
        sock_ptr->receiver_len++;
    } else {
        return -1;
    }
78
79
80
81

    return 0;
}

82
int PrrtSocket_send(PrrtSocket *sock_ptr, const void *data, const size_t data_len) {
83
    pthread_mutex_lock(&sock_ptr->out_queue_filled_mutex);
84

85
    PrrtPacket *packet = malloc(sizeof(PrrtPacket));
86
    assert(packet != NULL);
87
    PrrtPacket_create_data_packet(packet, 5, data, data_len, sock_ptr->seqno_source++);
88

89
90
91
    List_push(sock_ptr->out_queue, packet);
    pthread_cond_signal(&sock_ptr->out_queue_filled_cv);
    pthread_mutex_unlock(&sock_ptr->out_queue_filled_mutex);
92

93
    return 0;
94
95
}

96
uint32_t PrrtSocket_recv(const PrrtSocket *sock_ptr, void *buf_ptr) {
97
98
    unsigned char buffer[MAX_PAYLOAD_LENGTH];

99
    while (1) {
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
        // RECEIVE DATA
        ssize_t n;
        struct sockaddr_in remote;
        socklen_t addrlen = sizeof(remote);

        n = recvfrom(sock_ptr->fd_data, buffer, MAX_PAYLOAD_LENGTH, 0, (struct sockaddr *) &remote, &addrlen);
        uint16_t remote_port = ntohs(remote.sin_port);
        char *remote_host = inet_ntoa(remote.sin_addr);

        PrrtPacket *packet = malloc(sizeof(PrrtPacket));
        assert(packet != NULL);
        PrrtPacket_decode(buffer, (uint16_t) n, packet);

        // REPLY
        struct sockaddr_in targetaddr;
115
        memset((char *) &targetaddr, 0, sizeof(targetaddr));
116
117
118
119
120
        targetaddr.sin_family = AF_INET;
        targetaddr.sin_port = htons((uint16_t) (remote_port + 1));

        struct hostent *hp;
        hp = gethostbyname(remote_host);
121
        memcpy((void *) &targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
122

123
        PrrtPacket *feedback_pkt_ptr = PrrtPacket_create_feedback_packet(0, 19, 4715, 7, 3, 50, 4, 6, 8, 9, 5, 1);
124
125
126
        uint8_t buf[MAX_PAYLOAD_LENGTH];
        uint32_t length = PrrtPacket_size(feedback_pkt_ptr);

127
        if (PrrtPacket_encode(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) < 0) {
128
129
            perror("BUF too small.");
        } else {
130
131
            if ((sendto(sock_ptr->fd_feedback, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) <
                 0)) {
132
133
134
135
                perror("sendto failed");
                return NULL;
            }
        }
136

137
        switch (PrrtPacket_type(packet)) {
138
            case PACKET_TYPE_DATA:
139
140
                // packet.timestamp + packet.timeout < now: break

141
142
143
144
                if (PrrtForwardPacketTable_test_set_is_number_relevant(sock_ptr->forwardPacketTable, packet->seqno) ==
                    FALSE) {
                    break;
                }
145
146
147
148
149
150

                // check incomplete_prrt_blocks for this seqno: insert if found
                // else: insert in data_packet_store

                // forward to application layer

151
152
                memcpy(buf_ptr, packet->payload + PRRT_PACKET_DATA_HEADER_SIZE,
                       packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE);
153
154
                return packet->payload_len - PRRT_PACKET_DATA_HEADER_SIZE;
            default:
155
                //PrrtPacket_print(packet);
156
                break;
157
158
        }

159
        // TODO: enough packets received?
160
    }
161
    return 0;
162
163
}

164
int PrrtSocket_close(const PrrtSocket *sock_ptr) {
165
166
    // TODO: shut down threads;

167
168
    // TODO: clean up all receivers

169
170
171
    pthread_mutex_destroy(&sock_ptr->out_queue_filled_mutex);
    pthread_cond_destroy(&sock_ptr->out_queue_filled_cv);
    List_destroy(sock_ptr->out_queue);
172

173
174
    close(sock_ptr->fd_data);
    close(sock_ptr->fd_feedback);
175
176
177
    return 0;
}

178
PrrtPacket *PrrtSocket_recv_feedback(const PrrtSocket *sock_ptr, void *bufin, const size_t length) {
179
180
181
182
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

183
    n = recvfrom(sock_ptr->fd_feedback, bufin, length, 0, (struct sockaddr *) &remote, &addrlen);
184
    if (n < 0) {
185
        perror("RECVFROM FAIL");
186
187
        return NULL;
    }
188
189
    uint16_t remote_port = ntohs(remote.sin_port);
    char *remote_host = inet_ntoa(remote.sin_addr);
190
    printf("RECV FEEDBACK: %s:%d\n", remote_host, remote_port);
191

192
    PrrtPacket *packet_ptr = malloc(sizeof(PrrtPacket));
193
    assert(packet_ptr != NULL);
194
    PrrtPacket_decode(bufin, (uint16_t) n, packet_ptr);
195
    return packet_ptr;
196
}