Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /*
- Archvile
- Copyright [c] 2011 tm512, All Rights Reserved
- This program is free software: you can redistribute it and/or modify
- it under the terms of the GNU General Public License as published by
- the Free Software Foundation, either version 3 of the License, or
- (at your option) any later version.
- This program is distributed in the hope that it will be useful,
- but WITHOUT ANY WARRANTY; without even the implied warranty of
- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
- GNU General Public License for more details.
- You should have received a copy of the GNU General Public License
- along with this program. If not, see <http://www.gnu.org/licenses/>.
- */
- /* net - Generic, low-level networking */
- #include <iostream>
- #include <fcntl.h>
- #include <netdb.h>
- #include <errno.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <string.h>
- #include "net.h"
- using std::cout;
- using std::cerr;
- using std::endl;
- // select () nonsense :P
- int highestSocket = 0;
- fd_set fdread, fdwrite;
- int InitNet ()
- {
- // cout << "InitNet: Initializing Network" << endl;
- FD_ZERO (&fdread);
- FD_ZERO (&fdwrite);
- return 0;
- }
- // Initialize socket (no connect)
- CSocket::CSocket ()
- {
- init ();
- }
- // Initialize socket (connect to host:port)
- CSocket::CSocket (char *host, char *port)
- {
- init ();
- connect (host, port);
- }
- CSocket::~CSocket ()
- {
- cout << "Destroying socket " << this << endl;
- }
- // initialize a socket for use
- void CSocket::init ()
- {
- cout << "Opening a socket at " << this << endl;
- fcntl (sock, F_SETFL, O_NONBLOCK);
- sock = -1;
- }
- // Return 1 if this instance has a valid socket opened
- bool CSocket::connected ()
- {
- return sock >= 0;
- }
- int CSocket::connect (const char *host, const char *port)
- {
- addrinfo hints, *res, *r;
- timeval conn_timeout = { 10, 0 };
- fd_set fdtmp; // Temporary fd_set for timing out of select
- // util_info("connecting to %s:%s.\n", host, port);
- cout << "connecting to " << host << ":" << port << "." << endl;
- sock = 0;
- this->host = host;
- this->port = port;
- memset (&hints, 0, sizeof hints);
- hints.ai_family = AF_UNSPEC;
- hints.ai_socktype = SOCK_STREAM; // Tee Cee Pee
- int ret;
- if ((ret = getaddrinfo (host, port, &hints, &res)) != 0)
- {
- // fprintf (stderr, "[error] could not resolve host %s:%s.\n", host, port);
- cout << "[error] could not resolve host " << host << ":" << port << "." << endl;
- return sock = -1;
- }
- // Kind of messy. Keep looping until we get a good connection
- for (r = res; r; r = r->ai_next)
- {
- if ((sock = socket (r->ai_family, r->ai_socktype, r->ai_protocol)) < 0)
- continue;
- FD_ZERO (&fdtmp);
- FD_SET (sock, &fdtmp);
- if (::connect (sock, r->ai_addr, r->ai_addrlen) < 0)
- if (errno != EWOULDBLOCK)
- continue;
- if (select (sock + 1, NULL, &fdtmp, NULL, &conn_timeout) == 1)
- {
- int serror;
- socklen_t slen = sizeof (serror);
- getsockopt (sock, SOL_SOCKET, SO_ERROR, &serror, &slen);
- if (!serror) // Success!
- {
- cout << "Success!" << endl;
- break;
- }
- }
- ::close (sock);
- }
- freeaddrinfo (res);
- if (!r)
- {
- // fprintf (stderr, "[error] could not connect to host %s:%s.\n", host, port);
- cout << "[error] could not connect to host " << host << ":" << port << "." << endl;
- return sock = -1;
- }
- FD_SET (sock, &fdread);
- FD_SET (sock, &fdwrite);
- if (sock > highestSocket)
- highestSocket = sock;
- return sock;
- }
- int CSocket::reconnect ()
- {
- return connect (host.c_str (), port.c_str ());
- }
- #define MAXTRIES 5
- // Send up to len bytes from tosend to the socket.
- // Returns the amount of bytes that failed to send or -1 on invalid target/len
- int CSocket::send (char *tosend, int len)
- {
- int totalSent = 0, tempSent = 0, sendTries = 0;
- cout << "sending: " << tosend << " (" << len << " bytes)." << endl;
- if (!tosend || len <= 0)
- return -1;
- while (totalSent < len)
- {
- if ((tempSent = ::send (sock, tosend + totalSent, len - totalSent, 0)) < 0)
- {
- if (sendTries ++ < MAXTRIES)
- continue;
- else
- break;
- }
- totalSent += tempSent;
- }
- return len - totalSent;
- }
- // Read up to len bytes into target
- // Hardly any sanity checks, use carefully
- // Returns the amount of bytes that failed to be read or -1 on invalid target/len
- int CSocket::recv (char *target, int len)
- {
- int totalRecvd = 0, tempRecvd = 0, recvTries = 0;
- cout << "attempting to receive " << len << " bytes." << endl;
- // Check for invalid buffer or length (to some degree)
- if (!target || len <= 0)
- return -1;
- while (totalRecvd < len)
- {
- if ((tempRecvd = ::recv (sock, target + totalRecvd, len - totalRecvd, 0)) < 0)
- {
- if (recvTries ++ < MAXTRIES)
- continue;
- else
- break;
- }
- totalRecvd += tempRecvd;
- }
- return len - totalRecvd;
- }
- // Close the socket
- int CSocket::close ()
- {
- int ret = ::close (sock);
- sock = -1;
- return ret;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement