~ps10gel/ubuntu/xenial/trafficserver/6.2.0

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
/** @file

  A brief file description

  @section license License

  Licensed to the Apache Software Foundation (ASF) under one
  or more contributor license agreements.  See the NOTICE file
  distributed with this work for additional information
  regarding copyright ownership.  The ASF licenses this file
  to you under the Apache License, Version 2.0 (the
  "License"); you may not use this file except in compliance
  with the License.  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.
 */

#include "UDPAPITest.h"
#include <arpa/inet.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>

char ACK[] = "I got it.";

FILE *fp;

void
UDPTestInit()
{
  TSCont cont;
  TSMutex readMutexp;
  unsigned long ip;

  ip         = inet_addr("209.131.48.79");
  readMutexp = TSMutexCreate();
  cont       = TSContCreate(&handle_callbacks, readMutexp);
  //      INKUDPBind(cont, INADDR_ANY,1813);
  INKUDPBind(cont, ip, 1813);
}

void
printN(const char *start, int length)
{
  int i;
  for (i = 0; i < length; i++)
    fprintf(fp, "%c", *(start + i));
  fprintf(fp, "\n");
}

int
handle_callbacks(TSCont cont, TSEvent event, void *e)
{
  INKUDPacketQueue packetQueue;
  INKUDPPacket packet;
  TSIOBufferBlock recvBuffBlock;
  TSIOBufferReader reader;
  TSIOBuffer iobuffer;
  INKUDPConn UDPConn;
  unsigned int ip;
  int port;
  int *sizep;
  int size;
  char sendBuff[32];
  const char *buf;
  int avail, total_len;
  char recv_buffer[4096];

  fp = fopen("UDPServer.log", "a+");

  switch (event) {
  case TS_NET_EVENT_DATAGRAM_OPEN:
    fprintf(fp, "open event called\n");
    UDPConn = (INKUDPConn)e;
    INKUDPRecvFrom(cont, UDPConn);
    break;

  case TS_NET_EVENT_DATAGRAM_READ_READY:
    fprintf(fp, "read ready event called\n");
    packetQueue = (INKUDPacketQueue)e;
    total_len   = 0;
    while ((packet = INKUDPPacketGet(packetQueue)) != NULL) {
      recvBuffBlock = INKUDPPacketBufferBlockGet(packet);
      iobuffer      = TSIOBufferCreate();
      reader        = TSIOBufferReaderAlloc(iobuffer);
      TSIOBufferAppend(iobuffer, recvBuffBlock);
      buf = TSIOBufferBlockReadStart(recvBuffBlock, reader, &avail);

      if (avail > 0) {
        fprintf(fp, "Received message is\n");
        printN(buf, avail);
        fprintf(fp, "message length = %i\n", avail);
        memcpy((char *)&recv_buffer + total_len, buf, avail);
        TSIOBufferReaderConsume(reader, avail);
        total_len += avail;
      }

      ip   = INKUDPPacketFromAddressGet(packet);
      port = INKUDPPacketFromPortGet(packet);
      fprintf(fp, "port = %d\n", port);

      UDPConn = (INKUDPConn)INKUDPPacketConnGet(packet);
      INKUDPSendTo(cont, UDPConn, ip, port, ACK, strlen(ACK));

      /* INKqa10255: we'd free the memory  - jinsheng */
      INKUDPPacketDestroy(packet);
      TSIOBufferReaderFree(reader);
      TSIOBufferDestroy(iobuffer);
    }

    break;

  case TS_NET_EVENT_DATAGRAM_WRITE_COMPLETE:
    break;
  }
  fclose(fp);
  return TS_EVENT_CONTINUE;
}