[4] | 1 | /**
|
---|
| 2 | * @file network.cpp
|
---|
| 3 | * @author myusgun@gmail.com
|
---|
| 4 | * @brief network
|
---|
| 5 | */
|
---|
| 6 | #include "cf/network.h"
|
---|
| 7 |
|
---|
| 8 | #include <iostream>
|
---|
| 9 | #include <sstream>
|
---|
| 10 | #include <string.h>
|
---|
| 11 |
|
---|
| 12 | #ifdef _ON_WINDOWS
|
---|
| 13 | # include <WinSock2.h>
|
---|
| 14 | # pragma comment(lib, "ws2_32.lib")
|
---|
| 15 | #else
|
---|
| 16 | # include <netinet/in.h>
|
---|
| 17 | # include <sys/socket.h>
|
---|
| 18 | # include <arpa/inet.h>
|
---|
| 19 | # include <netdb.h>
|
---|
| 20 | # include <unistd.h>
|
---|
| 21 | # include <sys/un.h>
|
---|
| 22 | # include <fcntl.h>
|
---|
| 23 | # include <errno.h>
|
---|
| 24 | # include <sys/types.h>
|
---|
| 25 | # include <sys/time.h>
|
---|
| 26 | #endif
|
---|
| 27 |
|
---|
| 28 | #ifdef _ON_WINDOWS
|
---|
| 29 | typedef cf::int32_t socklen_t;
|
---|
| 30 | # define sa_family_t cf::uint16_t
|
---|
| 31 | # define ERROR_INTR WSAEINTR
|
---|
| 32 | # define ERROR_CONNECTING WSAEWOULDBLOCK
|
---|
[6] | 33 | # define SOCKET_API_CALL __stdcall
|
---|
[4] | 34 | #else
|
---|
| 35 | # define closesocket(__sock) ::close(__sock)
|
---|
| 36 | # define ERROR_INTR EINTR
|
---|
| 37 | # define ERROR_CONNECTING EINPROGRESS
|
---|
[6] | 38 | # define SOCKET_API_CALL
|
---|
[4] | 39 | #endif
|
---|
| 40 |
|
---|
| 41 | /*--------------------------------------------------------------*/
|
---|
| 42 | /**
|
---|
| 43 | * do not include "cf/macaddr.[ch]pp"
|
---|
| 44 | * just declare and call
|
---|
| 45 | */
|
---|
| 46 | long GetMACAddress(cf::char_t *addr);
|
---|
| 47 |
|
---|
[5] | 48 | static std::string convertAddressToString(struct sockaddr_in & addr)
|
---|
| 49 | {
|
---|
| 50 | cf::uint8_t * bytes = NULL;
|
---|
| 51 | cf::char_t str[32] = {0x00};
|
---|
| 52 |
|
---|
| 53 | bytes = reinterpret_cast<cf::uint8_t *>(&addr.sin_addr.s_addr);
|
---|
| 54 | snprintf(str, sizeof(str) - 1, "%u.%u.%u.%u", bytes[0],
|
---|
| 55 | bytes[1],
|
---|
| 56 | bytes[2],
|
---|
| 57 | bytes[3]);
|
---|
| 58 |
|
---|
| 59 | return str;
|
---|
| 60 | }
|
---|
| 61 |
|
---|
[4] | 62 | static cf::void_t waitForTimeout(const cf::socket_t sock,
|
---|
| 63 | const cf::int32_t timeout,
|
---|
| 64 | const cf::bool_t checkWriteFD)
|
---|
| 65 | throw (cf::exception)
|
---|
| 66 | {
|
---|
| 67 | cf::int32_t result = 0;
|
---|
| 68 | fd_set rfds;
|
---|
| 69 | fd_set wfds;
|
---|
| 70 | fd_set * wfdsPtr = checkWriteFD ? &wfds : NULL;
|
---|
| 71 |
|
---|
| 72 | struct timeval tv;
|
---|
| 73 |
|
---|
| 74 | if (timeout <= 0)
|
---|
| 75 | return;
|
---|
| 76 |
|
---|
| 77 | FD_ZERO(&rfds);
|
---|
| 78 | FD_SET(sock, &rfds);
|
---|
| 79 |
|
---|
| 80 | FD_ZERO(&wfds);
|
---|
| 81 | FD_SET(sock, &wfds);
|
---|
| 82 |
|
---|
| 83 | tv.tv_sec = timeout;
|
---|
| 84 | tv.tv_usec = 0;
|
---|
| 85 |
|
---|
| 86 | result = select(sock + 1, &rfds, wfdsPtr, NULL, &tv);
|
---|
| 87 | if (result < 0)
|
---|
| 88 | THROW_EXCEPTION("select error");
|
---|
| 89 | else if (result == 0)
|
---|
| 90 | THROW_EXCEPTION("socket timed out");
|
---|
| 91 |
|
---|
| 92 | if (!FD_ISSET(sock, &rfds))
|
---|
| 93 | {
|
---|
| 94 | if (!checkWriteFD)
|
---|
| 95 | THROW_EXCEPTION("read fd is not set");
|
---|
| 96 | else if (!FD_ISSET(sock, &wfds))
|
---|
| 97 | THROW_EXCEPTION("write fd is not set");
|
---|
| 98 | }
|
---|
| 99 | }
|
---|
| 100 |
|
---|
[5] | 101 | static cf::void_t setReuseAddress(const cf::network::tcp & tcp)
|
---|
[4] | 102 | throw (cf::exception)
|
---|
| 103 | {
|
---|
| 104 | try
|
---|
| 105 | {
|
---|
[5] | 106 | cf::int32_t reuseaddr = 1;
|
---|
[4] | 107 |
|
---|
[5] | 108 | tcp.setOption(SO_REUSEADDR, &reuseaddr, sizeof(reuseaddr));
|
---|
[4] | 109 | }
|
---|
| 110 | catch (cf::exception & e)
|
---|
| 111 | {
|
---|
| 112 | FORWARD_EXCEPTION(e);
|
---|
| 113 | }
|
---|
| 114 | }
|
---|
| 115 |
|
---|
[6] | 116 | typedef cf::int32_t (SOCKET_API_CALL * getSocketNameAPI)(cf::socket_t sock,
|
---|
| 117 | struct sockaddr * addr,
|
---|
| 118 | socklen_t *len);
|
---|
[5] | 119 |
|
---|
| 120 | static cf::network::host getSocketNameFromFunction(const cf::socket_t sock,
|
---|
| 121 | getSocketNameAPI api)
|
---|
[4] | 122 | throw (cf::exception)
|
---|
| 123 | {
|
---|
[5] | 124 | cf::int32_t result = 0;
|
---|
| 125 | struct sockaddr_in addr;
|
---|
| 126 | socklen_t len = sizeof(struct sockaddr_in);
|
---|
[4] | 127 |
|
---|
[5] | 128 | result = api(sock, (struct sockaddr *)&addr, &len);
|
---|
| 129 | if (result < 0)
|
---|
| 130 | THROW_EXCEPTION("cannot get sockket or peer name ("
|
---|
| 131 | << cf::exception::systemCode()
|
---|
| 132 | << ")");
|
---|
| 133 |
|
---|
| 134 | return cf::network::host(convertAddressToString(addr), addr.sin_port);
|
---|
[4] | 135 | }
|
---|
| 136 | /*--------------------------------------------------------------*/
|
---|
| 137 |
|
---|
[5] | 138 | cf::network::host::host(const std::string & address,
|
---|
| 139 | const cf::uint16_t port)
|
---|
| 140 | : mAddress(address),
|
---|
| 141 | mPort(port)
|
---|
| 142 | {
|
---|
| 143 | }
|
---|
| 144 |
|
---|
| 145 | std::string cf::network::host::address() const
|
---|
| 146 | {
|
---|
| 147 | return mAddress;
|
---|
| 148 | }
|
---|
| 149 |
|
---|
| 150 | cf::uint16_t cf::network::host::port() const
|
---|
| 151 | {
|
---|
| 152 | return mPort;
|
---|
| 153 | }
|
---|
| 154 |
|
---|
| 155 | cf::bool_t cf::network::host::empty() const
|
---|
| 156 | {
|
---|
| 157 | return (address().empty() || port() <= 0) ? true : false;
|
---|
| 158 | }
|
---|
| 159 |
|
---|
[4] | 160 | cf::network::tcp::tcp(const cf::socket_t attachedSocket)
|
---|
| 161 | throw (cf::exception)
|
---|
| 162 | : mSocket (attachedSocket),
|
---|
| 163 | mTimeout(0)
|
---|
| 164 | {
|
---|
| 165 | static cf::bool_t isInitialized = false;
|
---|
| 166 |
|
---|
| 167 | /* initialize socket */
|
---|
| 168 | if (!isInitialized)
|
---|
| 169 | {
|
---|
| 170 | #if defined(_WIN32) || defined(_WIN64)
|
---|
| 171 | WSADATA winsockData;
|
---|
| 172 | cf::int32_t result = WSAStartup(MAKEWORD(2, 0), &winsockData);
|
---|
| 173 | if (result)
|
---|
| 174 | THROW_EXCEPTION("cannot start-up winsock");
|
---|
| 175 | #endif
|
---|
| 176 |
|
---|
| 177 | isInitialized = true;
|
---|
| 178 | }
|
---|
| 179 |
|
---|
[6] | 180 | if (mSocket == UNUSED_SOCKET)
|
---|
[4] | 181 | mSocket = ::socket(PF_INET, SOCK_STREAM, IPPROTO_TCP);
|
---|
| 182 |
|
---|
| 183 | if (mSocket < 0)
|
---|
| 184 | THROW_EXCEPTION("cannot create a socket");
|
---|
| 185 | }
|
---|
| 186 |
|
---|
| 187 | cf::network::tcp::~tcp()
|
---|
| 188 | {
|
---|
| 189 | close();
|
---|
| 190 | }
|
---|
| 191 |
|
---|
[7] | 192 | cf::socket_t cf::network::tcp::descriptor() const
|
---|
| 193 | {
|
---|
| 194 | return mSocket;
|
---|
| 195 | }
|
---|
| 196 |
|
---|
[4] | 197 | cf::void_t cf::network::tcp::close()
|
---|
| 198 | {
|
---|
[6] | 199 | if (mSocket == UNUSED_SOCKET)
|
---|
[4] | 200 | return;
|
---|
| 201 |
|
---|
| 202 | closesocket(mSocket);
|
---|
| 203 |
|
---|
[6] | 204 | mSocket = UNUSED_SOCKET;
|
---|
[4] | 205 | }
|
---|
| 206 |
|
---|
[5] | 207 | cf::void_t cf::network::tcp::connect(const cf::network::host & peer,
|
---|
[4] | 208 | const cf::int32_t timeout)
|
---|
| 209 | throw (cf::exception)
|
---|
| 210 | {
|
---|
[5] | 211 | if (peer.empty())
|
---|
[4] | 212 | THROW_EXCEPTION("invalid host info");
|
---|
| 213 |
|
---|
| 214 | cf::int32_t result = 0;
|
---|
| 215 | cf::int32_t retval = 0;
|
---|
| 216 | cf::int32_t length = 0;
|
---|
| 217 | struct sockaddr_in addr;
|
---|
| 218 | struct hostent * hostEnt;
|
---|
| 219 |
|
---|
[5] | 220 | const cf::char_t * host = peer.address().c_str();
|
---|
| 221 | cf::uint16_t port = peer.port();
|
---|
| 222 |
|
---|
[4] | 223 | /* 1. set data */
|
---|
| 224 | addr.sin_family = AF_INET;
|
---|
| 225 | addr.sin_port = htons(port);
|
---|
| 226 | addr.sin_addr.s_addr = inet_addr(host);
|
---|
| 227 |
|
---|
| 228 | /* 2. get ip from hostname if inet_addr() is failed */
|
---|
| 229 | if (addr.sin_addr.s_addr == (unsigned int)-1)
|
---|
| 230 | {
|
---|
| 231 | hostEnt = gethostbyname(host);
|
---|
| 232 | if (hostEnt == NULL)
|
---|
| 233 | THROW_EXCEPTION("cannot get host by name");
|
---|
| 234 |
|
---|
| 235 | addr.sin_family = (sa_family_t)hostEnt->h_addrtype;
|
---|
| 236 | memcpy(&(addr.sin_addr.s_addr),
|
---|
| 237 | hostEnt->h_addr,
|
---|
| 238 | (size_t)hostEnt->h_length);
|
---|
| 239 | }
|
---|
| 240 |
|
---|
| 241 | /* 3. set options */
|
---|
| 242 | try
|
---|
| 243 | {
|
---|
| 244 | setReuseAddress(*this);
|
---|
| 245 | }
|
---|
| 246 | catch (cf::exception & e)
|
---|
| 247 | {
|
---|
| 248 | FORWARD_EXCEPTION(e);
|
---|
| 249 | }
|
---|
| 250 |
|
---|
[5] | 251 | setTimeout(timeout);
|
---|
[4] | 252 |
|
---|
| 253 | /* 4. connect */
|
---|
| 254 | result = ::connect(mSocket, (struct sockaddr *)&addr, sizeof(addr));
|
---|
| 255 | if (result < 0)
|
---|
| 256 | {
|
---|
| 257 | if (timeout > 0)
|
---|
| 258 | {
|
---|
| 259 | if (cf::exception::systemCode() != ERROR_CONNECTING)
|
---|
| 260 | THROW_EXCEPTION("socket connect error");
|
---|
| 261 |
|
---|
| 262 | try
|
---|
| 263 | {
|
---|
| 264 | length = sizeof(retval);
|
---|
| 265 | waitForTimeout(mSocket, timeout, true);
|
---|
| 266 | getOption(SO_ERROR, &retval, &length);
|
---|
| 267 | }
|
---|
| 268 | catch (cf::exception & e)
|
---|
| 269 | {
|
---|
| 270 | FORWARD_EXCEPTION(e);
|
---|
| 271 | }
|
---|
| 272 |
|
---|
| 273 | if (retval)
|
---|
| 274 | THROW_EXCEPTION("cannot get error from socket option");
|
---|
| 275 | }
|
---|
| 276 | else
|
---|
| 277 | {
|
---|
| 278 | THROW_EXCEPTION("cannot connect to " << host << ":" << port);
|
---|
| 279 | }
|
---|
| 280 | }
|
---|
| 281 | }
|
---|
| 282 |
|
---|
| 283 | cf::void_t cf::network::tcp::listen(const cf::uint16_t port,
|
---|
[5] | 284 | const cf::int32_t backlog) const
|
---|
[4] | 285 | throw (cf::exception)
|
---|
| 286 | {
|
---|
| 287 | if (!port)
|
---|
| 288 | THROW_EXCEPTION("invalid port number");
|
---|
| 289 |
|
---|
| 290 | cf::int32_t result = 0;
|
---|
| 291 | struct sockaddr_in addr;
|
---|
| 292 |
|
---|
| 293 | /* 1. set data */
|
---|
| 294 | addr.sin_family = AF_INET;
|
---|
| 295 | addr.sin_addr.s_addr = htonl(INADDR_ANY);
|
---|
| 296 | addr.sin_port = htons(port);
|
---|
| 297 |
|
---|
| 298 | /* 2. set options */
|
---|
| 299 | try
|
---|
| 300 | {
|
---|
| 301 | setReuseAddress(*this);
|
---|
| 302 | }
|
---|
| 303 | catch (cf::exception & e)
|
---|
| 304 | {
|
---|
| 305 | FORWARD_EXCEPTION(e);
|
---|
| 306 | }
|
---|
| 307 |
|
---|
| 308 | result = ::bind(mSocket, (struct sockaddr *)&addr, sizeof(struct sockaddr));
|
---|
| 309 | if (result < 0)
|
---|
| 310 | THROW_EXCEPTION("cannot bind to " << port);
|
---|
| 311 |
|
---|
| 312 | result = ::listen(mSocket, backlog);
|
---|
| 313 | if (result < 0)
|
---|
| 314 | THROW_EXCEPTION("cannot listen");
|
---|
| 315 | }
|
---|
| 316 |
|
---|
[5] | 317 | cf::network::tcp cf::network::tcp::accept() const
|
---|
[4] | 318 | throw (cf::exception)
|
---|
| 319 | {
|
---|
| 320 | cf::socket_t sock = 0;
|
---|
| 321 | struct sockaddr_in addr;
|
---|
| 322 | socklen_t len = sizeof(addr);
|
---|
| 323 |
|
---|
| 324 | sock = ::accept(mSocket, (struct sockaddr *)&addr, /* in/out */&len);
|
---|
| 325 | if (sock < 0)
|
---|
| 326 | THROW_EXCEPTION("cannot accept client");
|
---|
| 327 |
|
---|
| 328 | return cf::network::tcp(sock).detach();
|
---|
| 329 | }
|
---|
| 330 |
|
---|
| 331 | cf::void_t cf::network::tcp::attach(const cf::socket_t sock)
|
---|
| 332 | throw (exception)
|
---|
| 333 | {
|
---|
[6] | 334 | if (sock == UNUSED_SOCKET)
|
---|
[4] | 335 | THROW_EXCEPTION("has invalid socket");
|
---|
| 336 |
|
---|
| 337 | mSocket = sock;
|
---|
| 338 | }
|
---|
| 339 |
|
---|
| 340 | cf::socket_t cf::network::tcp::detach()
|
---|
| 341 | throw (exception)
|
---|
| 342 | {
|
---|
[6] | 343 | if (mSocket == UNUSED_SOCKET)
|
---|
[4] | 344 | THROW_EXCEPTION("has invalid socket");
|
---|
| 345 |
|
---|
| 346 | cf::socket_t sock = mSocket;
|
---|
| 347 |
|
---|
[6] | 348 | mSocket = UNUSED_SOCKET;
|
---|
[4] | 349 |
|
---|
| 350 | return sock;
|
---|
| 351 | }
|
---|
| 352 |
|
---|
[5] | 353 | cf::void_t cf::network::tcp::send(const cf::bin & in) const
|
---|
[4] | 354 | throw (cf::exception)
|
---|
| 355 | {
|
---|
| 356 | if (in.empty())
|
---|
| 357 | THROW_EXCEPTION("send data is null or zero-bytes");
|
---|
| 358 |
|
---|
| 359 | cf::char_t * buf = reinterpret_cast<cf::char_t *>(in.buffer());
|
---|
| 360 | cf::int32_t size = static_cast<cf::int32_t>(in.size());
|
---|
| 361 |
|
---|
| 362 | cf::int32_t sentSize = (cf::int32_t)::send (mSocket, buf, size, 0);
|
---|
| 363 | if (sentSize != size)
|
---|
| 364 | THROW_EXCEPTION("cannot send (" << cf::exception::systemCode() << ")");
|
---|
| 365 | }
|
---|
| 366 |
|
---|
[5] | 367 | cf::void_t cf::network::tcp::receive(cf::bin & out) const
|
---|
[4] | 368 | throw (cf::exception)
|
---|
| 369 | {
|
---|
| 370 | if (out.size() == 0)
|
---|
| 371 | THROW_EXCEPTION("binary buffer is not created");
|
---|
| 372 |
|
---|
| 373 | try
|
---|
| 374 | {
|
---|
| 375 | waitForTimeout(mSocket, mTimeout, false);
|
---|
| 376 | }
|
---|
| 377 | catch (cf::exception & e)
|
---|
| 378 | {
|
---|
| 379 | FORWARD_EXCEPTION(e);
|
---|
| 380 | }
|
---|
| 381 |
|
---|
| 382 | cf::char_t * buf = reinterpret_cast<cf::char_t *>(out.buffer());
|
---|
| 383 | cf::int32_t size = static_cast<cf::int32_t>(out.size());
|
---|
| 384 |
|
---|
| 385 | cf::int32_t receivedSize = (cf::int32_t)::recv(mSocket, buf, size, 0);
|
---|
| 386 | if (receivedSize < 0)
|
---|
| 387 | THROW_EXCEPTION("cannot receive (" << cf::exception::systemCode() << ")");
|
---|
| 388 | else if (receivedSize == 0)
|
---|
| 389 | out.clear();
|
---|
| 390 | else if (receivedSize < size)
|
---|
| 391 | out.resize(receivedSize);
|
---|
| 392 | }
|
---|
| 393 |
|
---|
[5] | 394 | cf::bin cf::network::tcp::receive(const cf::int32_t size) const
|
---|
[4] | 395 | throw (cf::exception)
|
---|
| 396 | {
|
---|
| 397 | cf::bin out;
|
---|
| 398 |
|
---|
| 399 | out.resize(size);
|
---|
| 400 | receive(out);
|
---|
| 401 |
|
---|
| 402 | return out;
|
---|
| 403 | }
|
---|
| 404 |
|
---|
[5] | 405 | cf::bin cf::network::tcp::receive() const
|
---|
[4] | 406 | throw (cf::exception)
|
---|
| 407 | {
|
---|
| 408 | const cf::size_t bufferSize = 1024;
|
---|
| 409 |
|
---|
| 410 | cf::bin buffer;
|
---|
| 411 | cf::bin out;
|
---|
| 412 |
|
---|
| 413 | buffer.resize(bufferSize);
|
---|
| 414 |
|
---|
| 415 | do
|
---|
| 416 | {
|
---|
| 417 | receive(buffer);
|
---|
| 418 | out.append(buffer);
|
---|
| 419 | } while (buffer.size() == bufferSize);
|
---|
| 420 |
|
---|
| 421 | return out;
|
---|
| 422 | }
|
---|
| 423 |
|
---|
| 424 | cf::void_t cf::network::tcp::getOption(const cf::int32_t optname,
|
---|
| 425 | cf::void_t * optval,
|
---|
[5] | 426 | cf::int32_t * optlen) const
|
---|
[4] | 427 | throw (cf::exception)
|
---|
| 428 | {
|
---|
| 429 | cf::int32_t result = getsockopt(mSocket,
|
---|
| 430 | SOL_SOCKET,
|
---|
| 431 | optname,
|
---|
| 432 | #ifdef _ON_WINDOWS
|
---|
| 433 | (cf::char_t *)optval,
|
---|
| 434 | #else
|
---|
| 435 | optval,
|
---|
| 436 | #endif
|
---|
| 437 | (socklen_t *)optlen);
|
---|
| 438 | if (result < 0)
|
---|
| 439 | THROW_EXCEPTION("cannot get socket option ("
|
---|
| 440 | << cf::exception::systemCode()
|
---|
| 441 | << ")");
|
---|
| 442 | }
|
---|
| 443 |
|
---|
| 444 | cf::void_t cf::network::tcp::setOption(const cf::int32_t optname,
|
---|
| 445 | const cf::void_t * optval,
|
---|
[5] | 446 | const cf::int32_t optlen) const
|
---|
[4] | 447 | throw (cf::exception)
|
---|
| 448 | {
|
---|
| 449 | cf::int32_t result = setsockopt(mSocket,
|
---|
| 450 | SOL_SOCKET,
|
---|
| 451 | optname,
|
---|
| 452 | #ifdef _ON_WINDOWS
|
---|
| 453 | (cf::char_t *)optval,
|
---|
| 454 | #else
|
---|
| 455 | optval,
|
---|
| 456 | #endif
|
---|
| 457 | (socklen_t)optlen);
|
---|
| 458 | if (result < 0)
|
---|
| 459 | THROW_EXCEPTION("cannot set socket option ("
|
---|
| 460 | << cf::exception::systemCode()
|
---|
| 461 | << ")");
|
---|
| 462 | }
|
---|
| 463 |
|
---|
[7] | 464 | cf::void_t cf::network::tcp::setNonBlocking(const cf::bool_t flag)
|
---|
| 465 | {
|
---|
| 466 | #ifdef _ON_WINDOWS
|
---|
| 467 | cf::ulong_t mode = flag ? 1 : 0;
|
---|
| 468 | ioctlsocket(mSocket, FIONBIO, &mode);
|
---|
| 469 | #else
|
---|
| 470 | cf::int32_t mode = fcntl(mSocket, F_GETFL, 0);
|
---|
| 471 |
|
---|
| 472 | if (flag) mode |= O_NONBLOCK;
|
---|
| 473 | else mode &= ~O_NONBLOCK;
|
---|
| 474 |
|
---|
| 475 | fcntl(mSocket, F_SETFL, mode);
|
---|
| 476 | #endif
|
---|
| 477 | }
|
---|
| 478 |
|
---|
[5] | 479 | cf::void_t cf::network::tcp::setTimeout(const cf::int32_t seconds)
|
---|
[4] | 480 | {
|
---|
[7] | 481 | setNonBlocking(seconds > 0 /*? true : false*/);
|
---|
[5] | 482 | mTimeout = seconds;
|
---|
| 483 | }
|
---|
[4] | 484 |
|
---|
[5] | 485 | cf::network::host cf::network::tcp::peer() const
|
---|
| 486 | throw (cf::exception)
|
---|
| 487 | {
|
---|
| 488 | try
|
---|
| 489 | {
|
---|
| 490 | return getSocketNameFromFunction(mSocket, getpeername);
|
---|
| 491 | }
|
---|
| 492 | catch (cf::exception & e)
|
---|
| 493 | {
|
---|
| 494 | FORWARD_EXCEPTION(e);
|
---|
| 495 | }
|
---|
[4] | 496 | }
|
---|
| 497 |
|
---|
[5] | 498 | cf::network::host cf::network::tcp::local() const
|
---|
| 499 | throw (cf::exception)
|
---|
[4] | 500 | {
|
---|
[5] | 501 | try
|
---|
| 502 | {
|
---|
| 503 | return getSocketNameFromFunction(mSocket, getsockname);
|
---|
| 504 | }
|
---|
| 505 | catch (cf::exception & e)
|
---|
| 506 | {
|
---|
| 507 | FORWARD_EXCEPTION(e);
|
---|
| 508 | }
|
---|
[4] | 509 | }
|
---|
| 510 |
|
---|
| 511 | std::string cf::network::nic::getMACAddress()
|
---|
| 512 | throw (cf::exception)
|
---|
| 513 | {
|
---|
| 514 | static const cf::int32_t macSize = 6; /* = 48-bits */
|
---|
| 515 | static cf::bool_t isDone = false;
|
---|
| 516 | static cf::uint8_t bytes[macSize] = {0x00,};
|
---|
| 517 | static cf::char_t asciiz[macSize * 2 + 1] = {0x00,};
|
---|
| 518 |
|
---|
| 519 | if (!isDone)
|
---|
| 520 | {
|
---|
| 521 | long result = 0;
|
---|
| 522 |
|
---|
| 523 | result = GetMACAddress(reinterpret_cast<cf::char_t *>(bytes));
|
---|
| 524 | if (result)
|
---|
| 525 | THROW_EXCEPTION("cannot get mac-address");
|
---|
| 526 |
|
---|
| 527 | for (cf::int32_t iter = 0 ; iter < macSize ; iter++)
|
---|
| 528 | snprintf(asciiz + (iter * 2), sizeof(asciiz) - 1,
|
---|
| 529 | "%02x",
|
---|
| 530 | bytes[iter]);
|
---|
| 531 |
|
---|
| 532 | isDone = true;
|
---|
| 533 | }
|
---|
| 534 |
|
---|
| 535 | return std::string(asciiz);
|
---|
| 536 | }
|
---|
[7] | 537 |
|
---|
| 538 |
|
---|
| 539 | cf::uint32_t cf::network::byteOrder::htonl(cf::uint32_t in)
|
---|
| 540 | {
|
---|
[8] | 541 | return ::htonl(in);
|
---|
[7] | 542 | }
|
---|
| 543 |
|
---|
| 544 | cf::uint16_t cf::network::byteOrder::htons(cf::uint16_t in)
|
---|
| 545 | {
|
---|
[8] | 546 | return ::htons(in);
|
---|
[7] | 547 | }
|
---|
| 548 |
|
---|
| 549 | cf::uint32_t cf::network::byteOrder::ntohl(cf::uint32_t in)
|
---|
| 550 | {
|
---|
[8] | 551 | return ::ntohl(in);
|
---|
[7] | 552 | }
|
---|
| 553 |
|
---|
| 554 | cf::uint16_t cf::network::byteOrder::ntohs(cf::uint16_t in)
|
---|
| 555 | {
|
---|
[8] | 556 | return ::ntohs(in);
|
---|
[7] | 557 | }
|
---|