socket.c 5.66 KB
Newer Older
1
2
3
4
5
#include <netdb.h>
#include <stdio.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
6
#include <stdlib.h>
7
#include <pthread.h>
8
#include "../defines.h"
9
10
#include "socket.h"

11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
void *receive_loop(void *ptr) {
    char bufin[MAX_PAYLOAD_LENGTH];

    prrt_socket *sock_ptr = ptr;
    printf("RECEIVING:\n");


    while(1) {
        memset(bufin, 0, MAX_PAYLOAD_LENGTH);
        prrt_packet *t = prrt_recv_feedback(sock_ptr, bufin, MAX_PAYLOAD_LENGTH);
        if(t != NULL) {
            print_packet(t);
            delete_packet(t);
        }
        usleep(1000);
    }
}

int prrt_create_socket(prrt_socket *sock_ptr, uint16_t port, uint8_t is_sender) {
30
    // Create Data Socket
31
    if((sock_ptr->fd_data = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
32
33
34
35
36
37
38
39
40
41
42
        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);

43
    if(bind(sock_ptr->fd_data, (struct sockaddr *) &address, sizeof(address)) < 0) {
44
45
46
47
48
        perror("cannot bind socket");
        return -1;
    }

    // Create Feedback Socket
49
    if((sock_ptr->fd_feedback = socket(AF_INET, SOCK_DGRAM, 0)) < 0) {
50
51
52
53
54
55
56
57
58
59
        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

60
    if(bind(sock_ptr->fd_feedback, (struct sockaddr *) &address, sizeof(address)) < 0) {
61
62
63
64
        perror("cannot bind socket");
        return -1;
    }

65
66
67
68
69
70
71
72
73
74
75
    if(is_sender) {
        int rc = pthread_create(&sock_ptr->receive_thread, NULL, receive_loop, (void*) sock_ptr);
        if(rc) {
            printf("ERROR; return code from pthread_create() is %d\n", rc);
            exit(-1);
        }

    } else {

    }

76
77
78
    return 0;
}

79
80
int prrt_connect(prrt_socket *sock_ptr, char *host, uint16_t port) {
    prrt_receiver recv =  { host , port};
81

82
83
84
85
86
87
    if(sock_ptr->receiver_len < PRRT_MAX_RECEIVER_COUNT) {
        sock_ptr->receivers[sock_ptr->receiver_len] =recv;
        sock_ptr->receiver_len++;
    } else {
        return -1;
    }
88
89
90
91

    return 0;
}

92
int prrt_send(prrt_socket *sock_ptr, const void *data, size_t data_len) {
93
    prrt_packet packet;
94
    create_packet_data(&packet, 5, data, data_len);
95
96

    // TODO: finally delete_packet(packet);
97
98

    uint8_t buf[MAX_PAYLOAD_LENGTH];
99
100
    uint32_t length = packet_size(&packet);
    if(encode_packet(buf, MAX_PAYLOAD_LENGTH, &packet) < 0) {
101
102
103
        perror("BUF too small.");
        return -1;
    }
104
    print_packet(&packet);
105

106
    // SENDING TO ALL RECEIVERS
107
108
109
    int i;
    for(i = 0; i < sock_ptr->receiver_len; i++) {
        prrt_receiver recv = sock_ptr->receivers[i];
110

111
        struct hostent *hp;
112

113
114
115
116
        struct sockaddr_in targetaddr;
        memset((char*) &targetaddr, 0, sizeof(targetaddr));
        targetaddr.sin_family = AF_INET;
        targetaddr.sin_port = htons(recv.port);
117

118
119
        hp = gethostbyname(recv.host_name);
        memcpy((void *)&targetaddr.sin_addr, hp->h_addr_list[0], (size_t) hp->h_length);
120

121
        if((sendto(sock_ptr->fd_data, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
122
123
124
            perror("sendto failed");
            return -1;
        }
125
126
127
128
    }
    return 0;
}

129
130
131
prrt_packet *prrt_recv(prrt_socket *sock_ptr) {
    unsigned char buffer[MAX_PAYLOAD_LENGTH];

132
133
134
135
136
    // RECEIVE DATA
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

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

141
    prrt_packet *packet = malloc(sizeof(prrt_packet));
142
    decode_packet(buffer, (uint16_t) n, packet);
143
    print_packet(packet);
144
145
146
147
148

    // REPLY
    struct sockaddr_in targetaddr;
    memset((char*) &targetaddr, 0, sizeof(targetaddr));
    targetaddr.sin_family = AF_INET;
149
    targetaddr.sin_port = htons((uint16_t) (remote_port + 1));
150
151
152

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

155
    prrt_packet* feedback_pkt_ptr = create_packet_feedback(0, 19, 4715, 5, 0, 50, 0, 0, 0, 0, 5, 1);
156
157
158
159
160
161
162
163
164
165
166
    uint8_t buf[MAX_PAYLOAD_LENGTH];
    uint32_t length = packet_size(feedback_pkt_ptr);

    if(encode_packet(buf, MAX_PAYLOAD_LENGTH, feedback_pkt_ptr) < 0) {
        perror("BUF too small.");
    } else {
        if((sendto(sock_ptr->fd_feedback, buf, length, 0, (struct sockaddr *) &targetaddr, sizeof(targetaddr)) < 0)) {
            perror("sendto failed");
            return NULL;
        }

167
    }
168
    return packet;
169
170
}

171
int prrt_close_socket(prrt_socket *sock_ptr) {
172
173
    // TODO: shut down threads;

174
175
176
177
    // TODO: clean up all receivers

    close(sock_ptr->fd_data);
    close(sock_ptr->fd_feedback);
178
179
180
    return 0;
}

181
prrt_packet * prrt_recv_feedback(prrt_socket *sock_ptr, void *bufin, size_t length) {
182
183
184
185
    ssize_t n;
    struct sockaddr_in remote;
    socklen_t addrlen = sizeof(remote);

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

196
197
198
    prrt_packet * packet_ptr = malloc(sizeof(prrt_packet));
    decode_packet(bufin, (uint16_t) n, packet_ptr);
    return packet_ptr;
199
}