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
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
157
/*----------------------------------------------------------------------------*
* *
* M I N I M A L I S T I C U D P E N T I T Y *
* *
* Copyright (C) 2010 Amit Chawre. *
* *
*----------------------------------------------------------------------------*/
/**
* @file NwMiniUdpEntity.c
* @brief This file contains example of a minimalistic ULP entity.
*/
#include <stdio.h>
#include <string.h>
#include <assert.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include <fcntl.h>
#include "NwEvt.h"
#include "NwGtpv1u.h"
#include "NwMiniLogMgrEntity.h"
#include "NwMiniUdpEntity.h"
#ifndef NW_ASSERT
#define NW_ASSERT assert
#endif
#define MAX_UDP_PAYLOAD_LEN (4096)
#ifdef __cplusplus
extern "C" {
#endif
/*---------------------------------------------------------------------------
* Private functions
*--------------------------------------------------------------------------*/
static
void NW_TMR_CALLBACK(nwUdpDataIndicationCallbackData)
{
NwGtpv1uRcT rc;
uint8_t udpBuf[MAX_UDP_PAYLOAD_LEN];
NwS32T bytesRead;
uint32_t peerLen;
struct sockaddr_in peer;
NwMiniUdpEntityT *thiz = (NwMiniUdpEntityT *) arg;
peerLen = sizeof(peer);
bytesRead = recvfrom(thiz->hSocket, udpBuf, MAX_UDP_PAYLOAD_LEN , 0,
(struct sockaddr *) &peer,(socklen_t *) &peerLen);
if(bytesRead) {
NW_LOG(NW_LOG_LEVEL_DEBG, "Received UDP message of length %u from %X:%u",
bytesRead, ntohl(peer.sin_addr.s_addr), ntohs(peer.sin_port));
rc = nwGtpv1uProcessUdpReq(thiz->hGtpv1uStack, udpBuf, bytesRead,
peer.sin_port, peer.sin_addr.s_addr);
} else {
NW_LOG(NW_LOG_LEVEL_ERRO, "%s", strerror(errno));
}
}
/*---------------------------------------------------------------------------
* Public functions
*--------------------------------------------------------------------------*/
NwGtpv1uRcT nwMiniUdpInit(NwMiniUdpEntityT *thiz,
NwGtpv1uStackHandleT hGtpv1uStack, uint8_t *ipAddr)
{
int sd;
struct sockaddr_in addr;
sd = socket(AF_INET, SOCK_DGRAM, 0);
if (sd < 0) {
NW_LOG(NW_LOG_LEVEL_ERRO, "%s", strerror(errno));
NW_ASSERT(0);
}
addr.sin_family = AF_INET;
addr.sin_port = htons(2152);
addr.sin_addr.s_addr = inet_addr(ipAddr);
memset(addr.sin_zero, '\0', sizeof (addr.sin_zero));
if(bind(sd, (struct sockaddr *)&addr, sizeof(addr)) < 0) {
NW_LOG(NW_LOG_LEVEL_ERRO, "%s", strerror(errno));
NW_ASSERT(0);
}
event_set(&(thiz->ev), sd, EV_READ|EV_PERSIST, nwUdpDataIndicationCallbackData,
thiz);
event_add(&(thiz->ev), NULL);
thiz->hSocket = sd;
thiz->hGtpv1uStack = hGtpv1uStack;
return NW_GTPV1U_OK;
}
NwGtpv1uRcT nwMiniUdpDestroy(NwMiniUdpEntityT *thiz)
{
close(thiz->hSocket);
}
NwGtpv1uRcT nwMiniUdpDataReq(NwGtpv1uUdpHandleT udpHandle,
uint8_t *dataBuf,
uint32_t dataSize,
uint32_t peerIpAddr,
uint32_t peerPort)
{
struct sockaddr_in peerAddr;
NwS32T bytesSent;
NwMiniUdpEntityT *thiz = (NwMiniUdpEntityT *) udpHandle;
peerAddr.sin_family = AF_INET;
peerAddr.sin_port = htons(peerPort);
peerAddr.sin_addr.s_addr = (peerIpAddr);
memset(peerAddr.sin_zero, '\0', sizeof (peerAddr.sin_zero));
NW_LOG(NW_LOG_LEVEL_DEBG,
"Sending %u bytes on handle 0x%x to peer %u.%u.%u.%u:%u", dataSize, udpHandle,
(peerIpAddr & 0x000000ff),
(peerIpAddr & 0x0000ff00) >> 8,
(peerIpAddr & 0x00ff0000) >> 16,
(peerIpAddr & 0xff000000) >> 24,
peerPort);
bytesSent = sendto (thiz->hSocket, dataBuf, dataSize, 0,
(struct sockaddr *) &peerAddr, sizeof(peerAddr));
if(bytesSent < 0) {
NW_LOG(NW_LOG_LEVEL_ERRO, "%s", strerror(errno));
} else {
NW_LOG(NW_LOG_LEVEL_DEBG, "Sent %u bytes on handle 0x%x to peer %u.%u.%u.%u",
dataSize, udpHandle,
(peerIpAddr & 0x000000ff),
(peerIpAddr & 0x0000ff00) >> 8,
(peerIpAddr & 0x00ff0000) >> 16,
(peerIpAddr & 0xff000000) >> 24);
}
return NW_GTPV1U_OK;
}
#ifdef __cplusplus
}
#endif