Free (GPLv2) TCP/IP stack developed by TASS Belgium

Dependents:   lpc1768-picotcp-demo ZeroMQ_PicoTCP_Publisher_demo TCPSocket_HelloWorld_PicoTCP Pico_TCP_UDP_Test ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers Endpoint.cpp Source File

Endpoint.cpp

00001 /* 
00002  *
00003  * PicoTCP Socket interface for mbed.
00004  * Copyright (C) 2013 TASS Belgium NV
00005  * 
00006  * Released under GPL v2
00007  *
00008  * Other licensing models might apply at the sole discretion of the copyright holders.
00009  *
00010  *
00011  * This software is based on the mbed.org EthernetInterface implementation:
00012  * Copyright (C) 2012 mbed.org, MIT License
00013  *
00014  * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
00015  * and associated documentation files (the "Software"), to deal in the Software without restriction,
00016  * including without limitation the rights to use, copy, modify, merge, publish, distribute,
00017  * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
00018  * furnished to do so, subject to the following conditions:
00019  *
00020  * The above copyright notice and this permission notice shall be included in all copies or
00021  * substantial portions of the Software.
00022  *
00023  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
00024  * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
00025  * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
00026  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00027  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00028  */
00029 #include "Socket/Socket.h"
00030 #include "Socket/Endpoint.h"
00031 #include "wrapper.h"
00032 #include "proxy_endpoint.h"
00033 
00034 extern "C"
00035 {
00036 #include "pico_ipv4.h"
00037 }
00038 #include <cstring>
00039 #include <cstdio>
00040 
00041 Endpoint::Endpoint()  {
00042     reset_address();
00043 }
00044 Endpoint::~Endpoint() {}
00045 
00046 void Endpoint::reset_address(void) {
00047     memset(&_remoteHost,0,sizeof(_remoteHost));
00048 }
00049 
00050 #include "stdio.h"
00051 
00052 int Endpoint::set_address(const char* host, const int port) {
00053     _remoteHost.sin_port = short_be(port);
00054     if(pico_string_to_ipv4(host,&_remoteHost.sin_addr.s_addr) < 0)
00055     {
00056         // execute a dns query because this is a name
00057         struct hostent * _host = gethostbyname(host);
00058         if(_host && _host->h_addr_list[0])
00059         {
00060             memcpy(_ipAddress,_host->h_addr_list[0],strlen(_host->h_addr_list[0])+1);
00061             mbed_dbg("Dns result : %s\n",_ipAddress);
00062             pico_string_to_ipv4(_ipAddress,&_remoteHost.sin_addr.s_addr);
00063             delete _host->h_name;
00064             delete _host->h_addr_list;
00065             delete _host;
00066         }
00067         else 
00068             return -1;
00069     }
00070     else
00071         memcpy(_ipAddress,host, strlen(host)+1);
00072         
00073     return 0;
00074 }
00075 
00076 char* Endpoint::get_address() {
00077     return _ipAddress;
00078 }
00079 
00080 int   Endpoint::get_port() {
00081     return short_be(_remoteHost.sin_port);
00082 }