udpServer.cc 18.5 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
#include <iostream>
#include <stdlib.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
#include <stdio.h>
#include <unistd.h> 
#include <string.h> 
#include <sys/time.h>
#include <pcap.h>
#include <errno.h>
#include <netinet/in.h>
#include <netinet/ip.h> 
#include <netinet/udp.h>
#include <netinet/if_ether.h>
#include <net/ethernet.h>
#include <netinet/ether.h>
#include <string>
#include <vector>
#include <map>
#include <list>
Pramod R Sanaga's avatar
Pramod R Sanaga committed
24
#include <fcntl.h>
25
26
27


#define LOCAL_SERVER_PORT 9831
Pramod R Sanaga's avatar
Pramod R Sanaga committed
28
#define MAX_MSG 150000
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
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156

pcap_t *pcapDescriptor = NULL;

char appAck[120];
unsigned long long milliSec = 0;

int sd, rc, n, flags;
socklen_t cliLen;
struct sockaddr_in cliAddr, servAddr, destAddr;

using namespace std;

void handleUDP(struct pcap_pkthdr const *pcap_info, struct udphdr const *udpHdr, u_char *const udpPacketStart, struct ip const *ipPacket);

struct tcp_info {
    vector<unsigned long > recvdPackets;

    unsigned long lastInOrderPacket;
    list< pair<unsigned long, unsigned long> > sackRanges;

    unsigned long highSeq;
    unsigned long missingPacketTotal;
};

map<string , struct tcp_info*> connectionMap;

bool sackCompare( pair<unsigned long, unsigned long> pair1, pair<unsigned long, unsigned long> pair2)
{
    if(pair1.first <= pair2.first && pair1.second <= pair2.first)
        return true;
    else
        return false;
}

unsigned long long getTimeMilli()
{
    struct timeval tp;
    gettimeofday(&tp, NULL);

    long long tmpSecVal = tp.tv_sec;
    long long tmpUsecVal = tp.tv_usec;

    return (tmpSecVal*1000 + tmpUsecVal/1000);
}


int getLinkLayer(struct pcap_pkthdr const *pcap_info, const u_char *pkt_data)
{
    unsigned int caplen = pcap_info->caplen;

    if (caplen < sizeof(struct ether_header))
    {
        printf("A captured packet was too short to contain "
                "an ethernet header");
        return -1;
    }
    else
    {
        struct ether_header * etherPacket = (struct ether_header *) pkt_data;
        return ntohs(etherPacket->ether_type);
    }
}

void pcapCallback(u_char *user, const struct pcap_pkthdr *pcap_info, const u_char *pkt_data)
{
    int packetType = getLinkLayer(pcap_info, pkt_data);

    if(packetType != ETHERTYPE_IP)
    {
        printf("Unknown link layer type: %d\n", packetType);
        return;
    }

    struct ip const *ipPacket;
    size_t bytesLeft = pcap_info->caplen - sizeof(struct ether_header);


    if(bytesLeft < sizeof(struct ip))
    {
        printf("Captured packet was too short to contain an IP header.\n");
        return;
    }

    ipPacket = (struct ip const *)(pkt_data + sizeof(struct ether_header));
    int ipHeaderLength = ipPacket->ip_hl;
    int ipVersion = ipPacket->ip_v;


    if(ipVersion != 4)
    {
        printf("Captured IP packet is not IPV4.\n");
        return;
    }

    if(ipHeaderLength < 5)
    {
        printf("Captured IP packet has header less than the minimum 20 bytes.\n");
        return;
    }

    if(ipPacket->ip_p != IPPROTO_UDP)
    {
        printf("Captured packet is not a UDP packet.\n");
        return;
    }

    // Ignore the IP options for now - but count their length.
    /////////////////////////////////////////////////////////
    u_char *const udpPacketStart = (u_char *const)(pkt_data + sizeof(struct ether_header) + ipHeaderLength*4);

    struct udphdr const *udpPacket;

    udpPacket = (struct udphdr const *)udpPacketStart;

    bytesLeft -= ipHeaderLength*4;

    if(bytesLeft < sizeof(struct udphdr))
    {
        printf("Captured packet is too small to contain a UDP header.\n");
        return;
    }

    handleUDP(pcap_info,udpPacket,udpPacketStart, ipPacket);
}


void init_pcap( char *ipAddress)
{
157
    char interface[] = "eth4";
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
    struct bpf_program bpfProg;
    char errBuf[PCAP_ERRBUF_SIZE];
    char filter[128] = " udp ";

    // IP Address and sub net mask.
    bpf_u_int32 maskp, netp;
    struct in_addr localAddress;

    pcap_lookupnet(interface, &netp, &maskp, errBuf);
    //pcapDescriptor = pcap_open_live(interface, BUFSIZ, 0, 0, errBuf);
    pcapDescriptor = pcap_open_live(interface, 64, 0, 0, errBuf);
    localAddress.s_addr = netp;
    printf("IP addr = %s\n", ipAddress);
    sprintf(filter," udp and dst host %s and dst port 9831 ", ipAddress);

    if(pcapDescriptor == NULL)
    {
        printf("Error opening device %s with libpcap = %s\n", interface, errBuf);
        exit(1);
    }

    pcap_compile(pcapDescriptor, &bpfProg, filter, 1, netp);
    pcap_setfilter(pcapDescriptor, &bpfProg);
Pramod R Sanaga's avatar
Pramod R Sanaga committed
181
    pcap_setnonblock(pcapDescriptor, 1, errBuf);
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
}

void UpdateSACKs(unsigned long seqNum, bool consecutiveSeq , struct tcp_info *connInfo)
{
    // Update the SACK ranges.
    pair<unsigned long, unsigned long> tmpRange;
    tmpRange.first = seqNum;
    tmpRange.second = seqNum;

    if(!consecutiveSeq) // We have a confirmed hole in the received packets.
    {
        if(connInfo->sackRanges.size() == 0)
            connInfo->sackRanges.push_back(tmpRange);
        else
        {
            // Find the SACK-range that this packet fits into/extends, merging
            // two ranges if necessary.
            bool extendsBlock = false;

            // If this packet is after the right end of our last SACK block,
            // then it is indicating new packet loss.
            if(seqNum > connInfo->sackRanges.back().second)
                connInfo->missingPacketTotal += (seqNum - connInfo->sackRanges.back().second - 1);
            else
                connInfo->missingPacketTotal -= 1;
            

            list<pair<unsigned long, unsigned long> >::iterator sackIter = connInfo->sackRanges.begin();
            while(sackIter != connInfo->sackRanges.end())
            {
                // This packet is the next seqnum after the right most edge
                // in this sack block.
                if(seqNum == (*sackIter).second + 1)
                {
                    (*sackIter).second++;
                    extendsBlock = true;
                    break;
                }

                // This packet is the seqnum right before the left most edge
                // in this sack block.
                if(seqNum == (*sackIter).first - 1)
                {
                    (*sackIter).first--;
                    extendsBlock = true;
                    break;
                }

                sackIter++;
            }

            if(!extendsBlock) // Create a new SACK block.
            {

                connInfo->sackRanges.push_back(tmpRange);
                connInfo->sackRanges.sort(sackCompare);
            }
            else if(connInfo->sackRanges.size() > 1)// Merge sack blocks if possible.
            {
                list<pair<unsigned long, unsigned long> >::iterator sackIter = connInfo->sackRanges.begin();
                list<pair<unsigned long, unsigned long> >::iterator advanceIter = connInfo->sackRanges.begin();

                advanceIter = ++sackIter;
                sackIter = connInfo->sackRanges.begin();

                while(advanceIter != connInfo->sackRanges.end())
                {
                    if((*sackIter).second == ((*advanceIter).first - 1))
                    {
                        (*advanceIter).first = (*sackIter).first;
                        connInfo->sackRanges.erase(sackIter++);
                    }
                    else
                        sackIter++;

                    advanceIter++;
                }
            }
        }
    }
    else // We received a packet that is advancing the left edge of the window.
    {
        // If we don't have any outstanding SACK blocks, do nothing.
        if(connInfo->sackRanges.size() != 0)
        {
            if(connInfo->sackRanges.size() == 1)
            {
                if(connInfo->sackRanges.front().second == connInfo->lastInOrderPacket)
                {
                    // Delete this lone SACK block, all the packets have been ACKed.
                    connInfo->sackRanges.clear();

                    connInfo->missingPacketTotal = 0;
                }
            }
            else
            {
                list<pair<unsigned long, unsigned long> >::iterator sackIter = connInfo->sackRanges.begin();
                // Remove the first SACK block if possible.
                {
                    if(connInfo->lastInOrderPacket == connInfo->sackRanges.front().second)
                    {
                        // Delete this SACK block because all packets before its left edge
                        // have been now ACKed.
                        connInfo->missingPacketTotal -= 1;
                        connInfo->sackRanges.erase(sackIter);
                    }
                }
            }
        }
    }
}

void handleUDP(struct pcap_pkthdr const *pcap_info, struct udphdr const *udpHdr, u_char *const udpPacketStart, struct ip const *ipPacket)
{

    u_char *dataPtr = udpPacketStart + 8;
    unsigned char packetType = *(unsigned char *)(dataPtr);

    if(packetType == '0')// This is a udp data packet arriving here. Send an
        // application level acknowledgement packet for it.
    {
        unsigned long seqNum = *(unsigned long *)(dataPtr + 1);
        unsigned long ackedSeq = 0;
        struct tcp_info *connInfo;
        string src_ip_port;

        src_ip_port = inet_ntoa(ipPacket->ip_src) ; 
        src_ip_port += ":";
        src_ip_port += ntohs(udpHdr->source);

Pramod R Sanaga's avatar
Pramod R Sanaga committed
313
//        cout << "Received packet #"<<seqNum<<endl;
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425

        if(connectionMap.find(src_ip_port) != connectionMap.end()) // Established connection.
        {
            // Look up the connection info.
            connInfo = connectionMap[src_ip_port];

            if(seqNum == connInfo->lastInOrderPacket+1) // Received the next packet in window
                // and ACK as many packets as possible.
            {

                if(connInfo->recvdPackets.size() != 0) // There are future seq. numbered packets
                    // in the buffer.
                {
                    unsigned long curSeq = seqNum;

                    // Remove all contiguous packets from the buffer.
                    while(connInfo->recvdPackets.front() == curSeq+1)
                    {
                        curSeq++;
                        pop_heap(connInfo->recvdPackets.begin(), connInfo->recvdPackets.end(), greater<int>());
                        connInfo->recvdPackets.pop_back();
                    }

                    connInfo->lastInOrderPacket = curSeq;
                }
                else // This packet advances the right edge of the window by one.
                    connInfo->lastInOrderPacket++;

                ackedSeq = connInfo->lastInOrderPacket+1;
                UpdateSACKs(seqNum, true, connInfo);
            }
            else if(seqNum <= connInfo->lastInOrderPacket) // Redundant packet.
                ackedSeq = connInfo->lastInOrderPacket+1;
            else
            {
                vector<unsigned long >::iterator seqNumIter = connInfo->recvdPackets.end();

                seqNumIter = find(connInfo->recvdPackets.begin(), connInfo->recvdPackets.end(), seqNum);

                if(seqNumIter == connInfo->recvdPackets.end()) // We haven't seen this packet
                    // before, put it in the received packet list.
                {
                    connInfo->recvdPackets.push_back(seqNum);
                    push_heap(connInfo->recvdPackets.begin(), connInfo->recvdPackets.end(), greater<int>());
                    ackedSeq = connInfo->lastInOrderPacket+1;
                    //printf("Initializing SACK blocks for packet %d, ack = %d\n", seqNum,ackedSeq);
                    UpdateSACKs(seqNum, false, connInfo);
                }
                else // Redundant packet, doesn't advance the window.
                    ackedSeq = connInfo->lastInOrderPacket+1;
            }

        }
        else // New connection
        {
            connInfo = new struct tcp_info;

            connInfo->recvdPackets.resize(0);
            connInfo->lastInOrderPacket = 0;
            connInfo->sackRanges.resize(0);
            connectionMap[src_ip_port] = connInfo;

            connInfo->highSeq = seqNum;
            connInfo->missingPacketTotal = 0;

            if(seqNum != connInfo->lastInOrderPacket+1) // This isn't the first packet.
            {
                connInfo->recvdPackets.push_back(seqNum);
                push_heap(connInfo->recvdPackets.begin(), connInfo->recvdPackets.end(), greater<int>());
                connInfo->lastInOrderPacket = 0;
                ackedSeq = connInfo->lastInOrderPacket + 1;

                UpdateSACKs(seqNum, false, connInfo);

            }
            else // Received the first packet.
            {
                connInfo->lastInOrderPacket = 1;
                ackedSeq = connInfo->lastInOrderPacket + 1;

            }
        }

        // Keep track of the highest sequence number received from the sender.
        if(seqNum > connInfo->highSeq)
            connInfo->highSeq = seqNum;

        // Send an ACK for the expected sequence number.
        appAck[0] = '1';
        char numSackRanges = (char)0;
        int packetSize = 2 + 3*sizeof(unsigned long);

        // Limit the maximum number of sack blocks to 5.
        if(connInfo->sackRanges.size() != 0)
        {
            if(connInfo->sackRanges.size() >= 5)
                numSackRanges = (char)5;
            else
                numSackRanges = (char)connInfo->sackRanges.size();
        }
        memcpy(&appAck[1], &numSackRanges, sizeof(char));
        memcpy(&appAck[2], &ackedSeq, sizeof(unsigned long));
        //cout<<"Sending ACK for seqNum = " << ackedSeq<<endl;

        // Send the total of missing packets.
        memcpy(&appAck[2 + sizeof(unsigned long)], &connInfo->missingPacketTotal, sizeof(unsigned long ));

        // Also the highest sequence number seen.
        memcpy(&appAck[2 + 2*sizeof(unsigned long)], &connInfo->highSeq, sizeof(unsigned long ));

        if((int)numSackRanges != 0)
        {
Pramod R Sanaga's avatar
Pramod R Sanaga committed
426
            printf("Creating a SACK for packet = %d ^ ", ackedSeq);
427
428
429
430
431
432
433
            packetSize += (2*(int)numSackRanges*sizeof(unsigned long));

            unsigned long rangeBegin, rangeEnd;

            list<pair<unsigned long, unsigned long> >::iterator sackIter = connInfo->sackRanges.begin();
            int i = 0;

Pramod R Sanaga's avatar
Pramod R Sanaga committed
434
435
436
437
438
439
440
441
442
443
444
445
            // Send the 'n' most recent SACK blocks.
            if(connInfo->sackRanges.size() != numSackRanges)
            {
                int index = connInfo->sackRanges.size() - numSackRanges;

                while(index != 0)
                {
                    sackIter++;
                    index--;
                }
            }

446
447
448
449
450
451

            while(i < (int)numSackRanges && (sackIter != connInfo->sackRanges.end()))
            {
                rangeBegin = (*sackIter).first;
                rangeEnd = (*sackIter).second;

Pramod R Sanaga's avatar
Pramod R Sanaga committed
452
                printf("%d %d: ", rangeBegin, rangeEnd);
453
454
455
456
457
458
459

                memcpy(&appAck[2 + 3*sizeof(unsigned long) + 2*i*sizeof(unsigned long)], &rangeBegin, sizeof(unsigned long ));
                memcpy(&appAck[2 + 3*sizeof(unsigned long) + (2*i+1)*sizeof(unsigned long)], &rangeEnd, sizeof(unsigned long ));

                i++;
                sackIter++;
            }
Pramod R Sanaga's avatar
Pramod R Sanaga committed
460
            printf("\n");
461

Pramod R Sanaga's avatar
Pramod R Sanaga committed
462
            printf("HighSeq = %u, lostNum = %u\n", connInfo->highSeq, connInfo->missingPacketTotal);
463
464
465
        }
        else
        {
Pramod R Sanaga's avatar
Pramod R Sanaga committed
466
            printf("Sending ACK for packet = %u, received = %u\n", ackedSeq,seqNum);
467
468
469
470
471
472
473
474
475

        }

        // Fill in the IP address & port number.
        memcpy((char *) &destAddr.sin_addr.s_addr,
                (char *) &ipPacket->ip_src.s_addr, sizeof(ipPacket->ip_src.s_addr));
        destAddr.sin_port = udpHdr->source;

        int retval = sendto(sd,appAck,packetSize,flags,(struct sockaddr *)&destAddr,sizeof(destAddr));
476
        if(retval <= 0)
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
        {
            cout<<"Send failed for ACK "<<ackedSeq<<endl;
            exit(1);

        }
    }
    else if(packetType == '1') // TODO:This is an udp ACK packet. If it is being sent
        // out from this host, do nothing.
    {

    }
    else
    {
        printf("ERROR: Unknown UDP packet received from remote agent\n");
        return;
    }
}

int main(int argc, char *argv[]) {

    char msg[MAX_MSG];
    struct hostent *localHost;

    /* socket creation */
    sd=socket(AF_INET, SOCK_DGRAM, 0);
    if(sd<0) {
        printf("%s: cannot open socket \n",argv[0]);
        exit(1);
    }



    localHost = gethostbyname(argv[1]);
    if(localHost == NULL) 
    {
        printf("ERROR: Unknown host %s\n", argv[1]);
        exit(1);
    }

    /* bind local server port */
    servAddr.sin_family = AF_INET;
    servAddr.sin_family = localHost->h_addrtype;
    memcpy((char *) &servAddr.sin_addr.s_addr,
            localHost->h_addr_list[0], localHost->h_length);
    servAddr.sin_port = htons(LOCAL_SERVER_PORT);
    rc = bind (sd, (struct sockaddr *) &servAddr,sizeof(servAddr));
    if(rc<0) {
        printf("%s: cannot bind port number %d \n", 
                argv[0], LOCAL_SERVER_PORT);
        exit(1);
    }

    // Prepare the structure to be used for ACKs.
    destAddr.sin_family = AF_INET;
    destAddr.sin_family = localHost->h_addrtype;


    printf("%s: waiting for data on port UDP %u\n", 
            argv[0],LOCAL_SERVER_PORT);

    flags = 0;


    init_pcap(inet_ntoa(servAddr.sin_addr));
Pramod R Sanaga's avatar
Pramod R Sanaga committed
541
    int pcapfd = pcap_get_selectable_fd(pcapDescriptor);
542
    struct pcap_stat pcapStatObj;
Pramod R Sanaga's avatar
Pramod R Sanaga committed
543
544
545
546
547
548
549
550
551
    fd_set socketReadSet;

    struct timeval timeoutStruct;

    timeoutStruct.tv_sec = 0;
    timeoutStruct.tv_usec = 50000;

    memset(msg,0x0,MAX_MSG);
    fcntl(sd, F_SETFL, flags | O_NONBLOCK);
552
553
554

    /* server infinite loop */
    while(1) {
Pramod R Sanaga's avatar
Pramod R Sanaga committed
555
556
557
        FD_ZERO(&socketReadSet);
        FD_SET(sd,&socketReadSet);
        FD_SET(pcapfd,&socketReadSet);
558

Pramod R Sanaga's avatar
Pramod R Sanaga committed
559
        select(sd+pcapfd+1,&socketReadSet,0,0,&timeoutStruct);
560
561


Pramod R Sanaga's avatar
Pramod R Sanaga committed
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
        if (FD_ISSET(sd,&socketReadSet) )
        {   
            /* receive message */
            cliLen = sizeof(cliAddr);

            while(true)
            {
                if(recvfrom(sd, msg, MAX_MSG, flags,(struct sockaddr *) &cliAddr, &cliLen))
                {
                    pcap_dispatch(pcapDescriptor, 300, pcapCallback, NULL);
                }
                else
                    break;
            }
        }

        if (FD_ISSET(pcapfd,&socketReadSet) )
        {           
            while(pcap_dispatch(pcapDescriptor, 10000, pcapCallback, NULL) != 0);
        }
582
583
584
585
586
587
588


        if(n<0) {
            printf("%s: cannot receive data \n",argv[0]);
            continue;
        }
    }
Pramod R Sanaga's avatar
Pramod R Sanaga committed
589
590
591
    pcap_stats(pcapDescriptor, &pcapStatObj);
    if(pcapStatObj.ps_drop > 0)
        printf("pcap: Packets received %d, dropped %d\n", pcapStatObj.ps_recv, pcapStatObj.ps_drop);
592
593
594
595
596

    return 0;

}