Ethernet test for ECE 4180 and others to find your IP address and do a simple HTTP GET request over port 80.

Dependencies:   mbed Socket lwip-eth lwip-sys lwip

Committer:
mkersh3
Date:
Thu Apr 04 05:26:09 2013 +0000
Revision:
0:e7ca326e76ee
Ethernet Test for ECE4180 and others to find their IP Address and do a simple HTTP GET request over port 80.

Who changed what in which revision?

UserRevisionLine numberNew contents of line
mkersh3 0:e7ca326e76ee 1 /* Copyright (C) 2012 mbed.org, MIT License
mkersh3 0:e7ca326e76ee 2 *
mkersh3 0:e7ca326e76ee 3 * Permission is hereby granted, free of charge, to any person obtaining a copy of this software
mkersh3 0:e7ca326e76ee 4 * and associated documentation files (the "Software"), to deal in the Software without restriction,
mkersh3 0:e7ca326e76ee 5 * including without limitation the rights to use, copy, modify, merge, publish, distribute,
mkersh3 0:e7ca326e76ee 6 * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is
mkersh3 0:e7ca326e76ee 7 * furnished to do so, subject to the following conditions:
mkersh3 0:e7ca326e76ee 8 *
mkersh3 0:e7ca326e76ee 9 * The above copyright notice and this permission notice shall be included in all copies or
mkersh3 0:e7ca326e76ee 10 * substantial portions of the Software.
mkersh3 0:e7ca326e76ee 11 *
mkersh3 0:e7ca326e76ee 12 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING
mkersh3 0:e7ca326e76ee 13 * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
mkersh3 0:e7ca326e76ee 14 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,
mkersh3 0:e7ca326e76ee 15 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
mkersh3 0:e7ca326e76ee 16 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
mkersh3 0:e7ca326e76ee 17 */
mkersh3 0:e7ca326e76ee 18 #ifndef SOCKET_H_
mkersh3 0:e7ca326e76ee 19 #define SOCKET_H_
mkersh3 0:e7ca326e76ee 20
mkersh3 0:e7ca326e76ee 21 #include "lwip/sockets.h"
mkersh3 0:e7ca326e76ee 22 #include "lwip/netdb.h"
mkersh3 0:e7ca326e76ee 23
mkersh3 0:e7ca326e76ee 24 //DNS
mkersh3 0:e7ca326e76ee 25 inline struct hostent *gethostbyname(const char *name) {
mkersh3 0:e7ca326e76ee 26 return lwip_gethostbyname(name);
mkersh3 0:e7ca326e76ee 27 }
mkersh3 0:e7ca326e76ee 28
mkersh3 0:e7ca326e76ee 29 inline int gethostbyname_r(const char *name, struct hostent *ret, char *buf, size_t buflen, struct hostent **result, int *h_errnop) {
mkersh3 0:e7ca326e76ee 30 return lwip_gethostbyname_r(name, ret, buf, buflen, result, h_errnop);
mkersh3 0:e7ca326e76ee 31 }
mkersh3 0:e7ca326e76ee 32
mkersh3 0:e7ca326e76ee 33 class TimeInterval;
mkersh3 0:e7ca326e76ee 34
mkersh3 0:e7ca326e76ee 35 /** Socket file descriptor and select wrapper
mkersh3 0:e7ca326e76ee 36 */
mkersh3 0:e7ca326e76ee 37 class Socket {
mkersh3 0:e7ca326e76ee 38 public:
mkersh3 0:e7ca326e76ee 39 /** Socket
mkersh3 0:e7ca326e76ee 40 */
mkersh3 0:e7ca326e76ee 41 Socket();
mkersh3 0:e7ca326e76ee 42
mkersh3 0:e7ca326e76ee 43 /** Set blocking or non-blocking mode of the socket and a timeout on
mkersh3 0:e7ca326e76ee 44 blocking socket operations
mkersh3 0:e7ca326e76ee 45 \param blocking true for blocking mode, false for non-blocking mode.
mkersh3 0:e7ca326e76ee 46 \param timeout timeout in ms [Default: (1500)ms].
mkersh3 0:e7ca326e76ee 47 */
mkersh3 0:e7ca326e76ee 48 void set_blocking(bool blocking, unsigned int timeout=1500);
mkersh3 0:e7ca326e76ee 49
mkersh3 0:e7ca326e76ee 50 /** Close the socket file descriptor
mkersh3 0:e7ca326e76ee 51 */
mkersh3 0:e7ca326e76ee 52 int close();
mkersh3 0:e7ca326e76ee 53
mkersh3 0:e7ca326e76ee 54 ~Socket();
mkersh3 0:e7ca326e76ee 55
mkersh3 0:e7ca326e76ee 56 protected:
mkersh3 0:e7ca326e76ee 57 int _sock_fd;
mkersh3 0:e7ca326e76ee 58 int init_socket(int type);
mkersh3 0:e7ca326e76ee 59
mkersh3 0:e7ca326e76ee 60 int wait_readable(TimeInterval& timeout);
mkersh3 0:e7ca326e76ee 61 int wait_writable(TimeInterval& timeout);
mkersh3 0:e7ca326e76ee 62
mkersh3 0:e7ca326e76ee 63 bool _blocking;
mkersh3 0:e7ca326e76ee 64 unsigned int _timeout;
mkersh3 0:e7ca326e76ee 65
mkersh3 0:e7ca326e76ee 66 private:
mkersh3 0:e7ca326e76ee 67 int select(struct timeval *timeout, bool read, bool write);
mkersh3 0:e7ca326e76ee 68 };
mkersh3 0:e7ca326e76ee 69
mkersh3 0:e7ca326e76ee 70 /** Time interval class used to specify timeouts
mkersh3 0:e7ca326e76ee 71 */
mkersh3 0:e7ca326e76ee 72 class TimeInterval {
mkersh3 0:e7ca326e76ee 73 friend class Socket;
mkersh3 0:e7ca326e76ee 74
mkersh3 0:e7ca326e76ee 75 public:
mkersh3 0:e7ca326e76ee 76 /** Time Interval
mkersh3 0:e7ca326e76ee 77 \param ms time interval expressed in milliseconds
mkersh3 0:e7ca326e76ee 78 */
mkersh3 0:e7ca326e76ee 79 TimeInterval(unsigned int ms);
mkersh3 0:e7ca326e76ee 80
mkersh3 0:e7ca326e76ee 81 private:
mkersh3 0:e7ca326e76ee 82 struct timeval _time;
mkersh3 0:e7ca326e76ee 83 };
mkersh3 0:e7ca326e76ee 84
mkersh3 0:e7ca326e76ee 85 #endif /* SOCKET_H_ */