Advertisement
tm512

Archvile net.cpp 2

Oct 25th, 2011
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.91 KB | None | 0 0
  1. /*
  2.    Archvile
  3.    Copyright [c] 2011 tm512, All Rights Reserved
  4.  
  5.    This program is free software: you can redistribute it and/or modify
  6.    it under the terms of the GNU General Public License as published by
  7.    the Free Software Foundation, either version 3 of the License, or
  8.    (at your option) any later version.
  9.  
  10.    This program is distributed in the hope that it will be useful,
  11.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  12.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  13.    GNU General Public License for more details.
  14.  
  15.    You should have received a copy of the GNU General Public License
  16.    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  17. */
  18.  
  19. /* net - Generic, low-level networking */
  20.  
  21. #include <iostream>
  22. #include <fcntl.h>
  23. #include <netdb.h>
  24. #include <errno.h>
  25. #include <netinet/in.h>
  26. #include <sys/socket.h>
  27. #include <string.h>
  28.  
  29. #include "net.h"
  30.  
  31. using std::cout;
  32. using std::cerr;
  33. using std::endl;
  34.  
  35. // select () nonsense :P
  36. int highestSocket = 0;
  37. fd_set fdread, fdwrite;
  38.  
  39. int InitNet ()
  40. {
  41. //  cout << "InitNet: Initializing Network" << endl;
  42.     FD_ZERO (&fdread);
  43.     FD_ZERO (&fdwrite);
  44.  
  45.     return 0;
  46. }
  47.  
  48. // Initialize socket (no connect)
  49. CSocket::CSocket ()
  50. {
  51.     init ();
  52. }
  53.  
  54. // Initialize socket (connect to host:port)
  55. CSocket::CSocket (char *host, char *port)
  56. {
  57.     init ();
  58.     connect (host, port);
  59. }
  60.  
  61. CSocket::~CSocket ()
  62. {
  63.     cout << "Destroying socket " << this << endl;
  64. }
  65.  
  66. // initialize a socket for use
  67. void CSocket::init ()
  68. {
  69.     cout << "Opening a socket at " << this << endl;
  70.     fcntl (sock, F_SETFL, O_NONBLOCK);
  71.     sock = -1;
  72. }
  73.  
  74. // Return 1 if this instance has a valid socket opened
  75. bool CSocket::connected ()
  76. {
  77.     return sock >= 0;
  78. }
  79.  
  80. int CSocket::connect (const char *host, const char *port)
  81. {
  82.     addrinfo hints, *res, *r;
  83.     timeval conn_timeout = { 10, 0 };
  84.     fd_set fdtmp; // Temporary fd_set for timing out of select
  85.  
  86.     // util_info("connecting to %s:%s.\n", host, port);
  87.     cout << "connecting to " << host << ":" << port << "." << endl;
  88.     sock = 0;
  89.     this->host = host;
  90.     this->port = port;
  91.  
  92.     memset (&hints, 0, sizeof hints);
  93.     hints.ai_family = AF_UNSPEC;
  94.     hints.ai_socktype = SOCK_STREAM; // Tee Cee Pee
  95.  
  96.     int ret;
  97.     if ((ret = getaddrinfo (host, port, &hints, &res)) != 0)
  98.     {
  99.         // fprintf (stderr, "[error] could not resolve host %s:%s.\n", host, port);
  100.         cout << "[error] could not resolve host " << host << ":" << port << "." << endl;
  101.         return sock = -1;
  102.     }
  103.  
  104.     // Kind of messy. Keep looping until we get a good connection
  105.     for (r = res; r; r = r->ai_next)
  106.     {
  107.         if ((sock = socket (r->ai_family, r->ai_socktype, r->ai_protocol)) < 0)
  108.             continue;
  109.  
  110.         FD_ZERO (&fdtmp);
  111.         FD_SET (sock, &fdtmp);
  112.  
  113.         if (::connect (sock, r->ai_addr, r->ai_addrlen) < 0)
  114.             if (errno != EWOULDBLOCK)
  115.                 continue;
  116.  
  117.         if (select (sock + 1, NULL, &fdtmp, NULL, &conn_timeout) == 1)
  118.         {
  119.             int serror;
  120.             socklen_t slen = sizeof (serror);
  121.             getsockopt (sock, SOL_SOCKET, SO_ERROR, &serror, &slen);
  122.  
  123.             if (!serror) // Success!
  124.             {
  125.                 cout << "Success!" << endl;
  126.                 break;
  127.             }
  128.         }
  129.  
  130.         ::close (sock);
  131.     }
  132.  
  133.     freeaddrinfo (res);
  134.  
  135.     if (!r)
  136.     {
  137.         // fprintf (stderr, "[error] could not connect to host %s:%s.\n", host, port);
  138.         cout << "[error] could not connect to host " << host << ":" << port << "." << endl;
  139.         return sock = -1;
  140.     }
  141.  
  142.     FD_SET (sock, &fdread);
  143.     FD_SET (sock, &fdwrite);
  144.  
  145.     if (sock > highestSocket)
  146.         highestSocket = sock;
  147.  
  148.     return sock;
  149. }
  150.  
  151. int CSocket::reconnect ()
  152. {
  153.     return connect (host.c_str (), port.c_str ());
  154. }
  155.  
  156. #define MAXTRIES 5
  157.  
  158. // Send up to len bytes from tosend to the socket.
  159. // Returns the amount of bytes that failed to send or -1 on invalid target/len
  160. int CSocket::send (char *tosend, int len)
  161. {
  162.     int totalSent = 0, tempSent = 0, sendTries = 0;
  163.     cout << "sending: " << tosend << " (" << len << " bytes)." << endl;
  164.  
  165.     if (!tosend || len <= 0)
  166.         return -1;
  167.  
  168.     while (totalSent < len)
  169.     {
  170.         if ((tempSent = ::send (sock, tosend + totalSent, len - totalSent, 0)) < 0)
  171.         {
  172.             if (sendTries ++ < MAXTRIES)
  173.                 continue;
  174.             else
  175.                 break;
  176.         }
  177.         totalSent += tempSent;
  178.     }
  179.  
  180.     return len - totalSent;
  181. }
  182.  
  183. // Read up to len bytes into target
  184. // Hardly any sanity checks, use carefully
  185. // Returns the amount of bytes that failed to be read or -1 on invalid target/len
  186. int CSocket::recv (char *target, int len)
  187. {
  188.     int totalRecvd = 0, tempRecvd = 0, recvTries = 0;
  189.     cout << "attempting to receive " << len << " bytes." << endl;
  190.  
  191.     // Check for invalid buffer or length (to some degree)
  192.     if (!target || len <= 0)
  193.         return -1;
  194.  
  195.     while (totalRecvd < len)
  196.     {
  197.         if ((tempRecvd = ::recv (sock, target + totalRecvd, len - totalRecvd, 0)) < 0)
  198.         {
  199.             if (recvTries ++ < MAXTRIES)
  200.                 continue;
  201.             else
  202.                 break;
  203.         }
  204.         totalRecvd += tempRecvd;
  205.     }
  206.  
  207.     return len - totalRecvd;
  208. }
  209.  
  210. // Close the socket
  211. int CSocket::close ()
  212. {
  213.     int ret = ::close (sock);
  214.     sock = -1;
  215.  
  216.     return ret;
  217. }
  218.  
  219.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement