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/>.
- */
- using namespace std;
- #include <iostream>
- #include <fcntl.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <string.h>
- #include "net.h"
- // 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;
- }
- CSocket::CSocket ()
- {
- cout << "Opening a socket at " << this << endl;
- fcntl (sock, F_SETFL, O_NONBLOCK);
- }
- CSocket::CSocket (string &host, string &port)
- {
- cout << "Opening a socket to " << host << ":" << port << " at " << this << endl;
- fcntl (sock, F_SETFL, O_NONBLOCK);
- connect (host, port);
- }
- CSocket::~CSocket ()
- {
- cout << "Destroying socket " << this << endl;
- }
- bool CSocket::connected ()
- {
- return !(sock < 0);
- }
- int CSocket::connect (string &host, string &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
- if (getaddrinfo (host.c_str (), port.c_str (), &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);
- fcntl (sock, F_SETFL, O_NONBLOCK);
- ::connect (sock, r->ai_addr, r->ai_addrlen);
- if (select (sock + 1, &fdread, &fdwrite, NULL, &conn_timeout) == 1)
- {
- int serror;
- socklen_t slen = sizeof (serror);
- getsockopt (sock, SOL_SOCKET, SO_ERROR, &serror, &slen);
- if (!serror) // Success!
- 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::send (char *tosend)
- {
- int totalToSend = strlen (tosend), totalSent = 0, tempSent = 0;
- cout << "sending: " << tosend << " (" << totalToSend << " bytes." << endl;
- while (totalSent < totalToSend)
- {
- tempSent = ::send (sock, tosend + totalSent, totalToSend - totalSent, 0);
- if (tempSent < 0)
- {
- cout << "[error] failed on send: " << tosend << endl;
- return 1;
- }
- totalSent += tempSent;
- }
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement