Skip to content

Commit 03c5855

Browse files
committed
Add 'features/net/network-socket/' from commit 'e09565474188b905cada21166118a82e66359217'
git-subtree-dir: features/net/network-socket git-subtree-mainline: db60d6a git-subtree-split: e095654
2 parents db60d6a + e095654 commit 03c5855

21 files changed

+3135
-0
lines changed
Lines changed: 53 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,53 @@
1+
/* CellularInterface
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef CELLULAR_INTERFACE_H
18+
#define CELLULAR_INTERFACE_H
19+
20+
#include "network-socket/NetworkInterface.h"
21+
22+
23+
/** CellularInterface class
24+
*
25+
* Common interface that is shared between ethernet hardware
26+
*/
27+
class CellularInterface : public NetworkInterface
28+
{
29+
public:
30+
/** Start the interface
31+
*
32+
* @param apn Optional name of the network to connect to
33+
* @param username Optional username for your APN
34+
* @param password Optional password for your APN
35+
* @return 0 on success, negative error code on failure
36+
*/
37+
virtual int connect(const char *apn = 0, const char *username = 0, const char *password = 0) = 0;
38+
39+
/** Stop the interface
40+
*
41+
* @return 0 on success, negative error code on failure
42+
*/
43+
virtual int disconnect() = 0;
44+
45+
/** Get the local MAC address
46+
*
47+
* @return Null-terminated representation of the local MAC address
48+
*/
49+
virtual const char *get_mac_address() = 0;
50+
};
51+
52+
53+
#endif
Lines changed: 216 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,216 @@
1+
/* Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
5+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
6+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7+
* furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or
10+
* substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
#include "DnsQuery.h"
19+
#include <stdio.h>
20+
#include <string.h>
21+
22+
23+
#define DNS_COUNT (sizeof DNS_IPS / sizeof DNS_IPS[0])
24+
const char *DNS_IPS[] = {
25+
"8.8.8.8",
26+
"209.244.0.3",
27+
"84.200.69.80",
28+
"8.26.56.26",
29+
"208.67.222.222"
30+
};
31+
32+
static bool isIP(const char *host)
33+
{
34+
int i;
35+
36+
for (i = 0; host[i]; i++) {
37+
if (!(host[i] >= '0' && host[i] <= '9') && host[i] != '.') {
38+
return false;
39+
}
40+
}
41+
42+
// Ending with '.' garuntees host
43+
if (i > 0 && host[i-1] == '.') {
44+
return false;
45+
}
46+
47+
return true;
48+
}
49+
50+
51+
static bool parseRR(uint8_t *resp, int &c, char *adr)
52+
{
53+
int n = 0;
54+
while((n=resp[c++]) != 0) {
55+
if ((n & 0xc0) != 0) {
56+
// This is a link
57+
c++;
58+
break;
59+
} else {
60+
// skip this segment, not interested in string domain names
61+
c+= n;
62+
}
63+
}
64+
65+
int TYPE = (((int)resp[c])<<8) + resp[c+1];
66+
int CLASS = (((int)resp[c+2])<<8) + resp[c+3];
67+
int RDLENGTH = (((int)resp[c+8])<<8) + resp[c+9];
68+
69+
c+= 10;
70+
if ((CLASS == 1) && (TYPE == 1)) {
71+
sprintf(adr,"%d.%d.%d.%d", resp[c], resp[c+1], resp[c+2], resp[c+3]);
72+
c+= RDLENGTH;
73+
return true;
74+
}
75+
c+= RDLENGTH;
76+
77+
return false;
78+
}
79+
80+
81+
static bool resolve(unsigned char *resp, char *ipaddress)
82+
{
83+
84+
int ID = (((int)resp[0]) <<8) + resp[1];
85+
int QR = resp[2] >>7;
86+
int Opcode = (resp[2]>>3) & 0x0F;
87+
int RCODE = (resp[3] & 0x0F);
88+
int ANCOUNT = (((int)resp[6])<<8)+ resp[7];
89+
90+
if ((ID != 1) || (QR != 1) || (Opcode != 0) || (RCODE != 0)) {
91+
return false;
92+
}
93+
94+
int c = 12;
95+
int d;
96+
// Skip domain question
97+
while( (d=resp[c++]) != 0) {
98+
c+=d;
99+
}
100+
c+= 4; // skip QTYPE and QCLASS
101+
102+
// Here comes the resource record
103+
for (int ans = 0 ; ans < ANCOUNT; ans++) {
104+
if (parseRR(resp, c, ipaddress)) {
105+
return true;
106+
}
107+
}
108+
109+
return false;
110+
}
111+
112+
static int32_t query(UDPSocket *socket, const SocketAddress &addr, const char *hostname, char *ipaddress)
113+
{
114+
int len = 0;
115+
if (hostname == NULL) {
116+
return false;
117+
}
118+
len = strlen(hostname);
119+
if ((len > 128) || (len == 0)) {
120+
return false;
121+
}
122+
123+
int packetlen = /* size of HEADER structure */ 12 + /* size of QUESTION Structure */5 + len + 1;
124+
uint8_t *packet = new uint8_t[packetlen]; /* this is the UDP packet to send to the DNS */
125+
if (packet == NULL) {
126+
return false;
127+
}
128+
129+
// Fill the header
130+
memset(packet, 0, packetlen);
131+
packet[1] = 1; // ID = 1
132+
packet[5] = 1; // QDCOUNT = 1 (contains one question)
133+
packet[2] = 1; // recursion requested
134+
135+
int c = 13; // point to NAME element in question section or request
136+
int cnt = 12; // points to the counter of
137+
packet[cnt] = 0;
138+
for (int i = 0 ; i < len ; i++) {
139+
if (hostname[i] != '.') {
140+
// Copy the character and increment the character counter
141+
packet[cnt]++;
142+
packet[c++] = hostname[i];
143+
} else {
144+
// Finished with this part, so go to the next
145+
cnt = c++;
146+
packet[cnt] = 0;
147+
}
148+
}
149+
150+
// Terminate this domain name with a zero entry
151+
packet[c++] = 0;
152+
153+
// Set QTYPE
154+
packet[c++] = 0;
155+
packet[c++] = 1;
156+
// Set QCLASS
157+
packet[c++] = 0;
158+
packet[c++] = 1;
159+
160+
161+
if (socket->sendto(addr, packet, packetlen) < 0) {
162+
delete packet;
163+
return false;
164+
}
165+
delete packet;
166+
167+
packet = new uint8_t [1024];
168+
169+
// Receive the answer from DNS
170+
int response_length = 0;
171+
response_length = socket->recvfrom(NULL, packet, 1024);
172+
173+
if (response_length > 0 ) {
174+
if (!resolve(packet, ipaddress)) {
175+
delete packet;
176+
return NSAPI_ERROR_DNS_FAILURE;
177+
}
178+
179+
// cleanup and return
180+
delete packet;
181+
return 0;
182+
}
183+
184+
delete packet;
185+
return NSAPI_ERROR_DNS_FAILURE;
186+
}
187+
188+
int32_t dnsQuery(NetworkStack *iface, const char *host, char *ip)
189+
{
190+
if (isIP(host)) {
191+
strcpy(ip, host);
192+
return 0;
193+
}
194+
195+
UDPSocket sock(iface);
196+
197+
for (unsigned i = 0; i < DNS_COUNT; i++) {
198+
return query(&sock, SocketAddress(DNS_IPS[0], 53), host, ip);
199+
}
200+
201+
return NSAPI_ERROR_DNS_FAILURE;
202+
}
203+
204+
int32_t dnsQuery(UDPSocket *socket, const char *host, char *ip)
205+
{
206+
if (isIP(host)) {
207+
strcpy(ip, host);
208+
return 0;
209+
}
210+
211+
for (unsigned i = 0; i < DNS_COUNT; i++) {
212+
return query(socket, SocketAddress(DNS_IPS[0], 53), host, ip);
213+
}
214+
215+
return NSAPI_ERROR_DNS_FAILURE;
216+
}
Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
*
3+
* Permission is hereby granted, free of charge, to any person obtaining a copy of this software
4+
* and associated documentation files (the "Software"), to deal in the Software without restriction,
5+
* including without limitation the rights to use, copy, modify, merge, publish, distribute,
6+
* sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
7+
* furnished to do so, subject to the following conditions:
8+
*
9+
* The above copyright notice and this permission notice shall be included in all copies or
10+
* substantial portions of the Software.
11+
*
12+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
13+
* BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
14+
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
15+
* DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
16+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
17+
*/
18+
#ifndef __DNSQUERY_H__
19+
#define __DNSQUERY_H__
20+
21+
#include "NetworkStack.h"
22+
#include "UDPSocket.h"
23+
24+
25+
/** Function dnsQuery implements the functionality to query a domain name
26+
* server for an IP-Address of a given hostname.
27+
* @param iface : Network interface to use for DNS resolution.
28+
* @param sock : Previously opened socket to use for DNS resolution.
29+
* @param hostname : The hostname of interest as a string.
30+
* Format must be without http:// or www. IE google.com, mbed.org, etc.
31+
* If a standard IP Address is passed, it will be copied into ip unmodified.
32+
* @param ipaddress : A reference to a IPADDRESS_t object which will receive
33+
* the resolved IP Address of the host in question.
34+
* @returns 0 on succes, NS_DNS_FAILURE if host is not found,
35+
* or a negative value for other errors.
36+
*/
37+
int32_t dnsQuery(NetworkStack *iface, const char *host, char *ip);
38+
int32_t dnsQuery(UDPSocket *sock, const char *host, char *ip);
39+
40+
41+
#endif // __DNSQUERY_H__
Lines changed: 50 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
/* EthInterface
2+
* Copyright (c) 2015 ARM Limited
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#ifndef ETH_INTERFACE_H
18+
#define ETH_INTERFACE_H
19+
20+
#include "network-socket/NetworkInterface.h"
21+
22+
23+
/** EthInterface class
24+
*
25+
* Common interface that is shared between ethernet hardware.
26+
*/
27+
class EthInterface : public NetworkInterface
28+
{
29+
public:
30+
/** Start the interface
31+
*
32+
* @return 0 on success, negative error code on failure
33+
*/
34+
virtual int connect() = 0;
35+
36+
/** Stop the interface
37+
*
38+
* @return 0 on success, negative error code on failure
39+
*/
40+
virtual int disconnect() = 0;
41+
42+
/** Get the local MAC address
43+
*
44+
* @return Null-terminated representation of the local MAC address
45+
*/
46+
virtual const char *get_mac_address() = 0;
47+
};
48+
49+
50+
#endif

0 commit comments

Comments
 (0)