summaryrefslogtreecommitdiff
path: root/src/socket.cpp
blob: 2b6f84cd35d3637856008c35d4d08ad56a29e0b9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
/*       +------------------------------------+
 *       | Inspire Internet Relay Chat Daemon |
 *       +------------------------------------+
 *
 *  Inspire is copyright (C) 2002-2004 ChatSpike-Dev.
 *                       E-mail:
 *                <brain@chatspike.net>
 *           	  <Craig@chatspike.net>
 *     
 * Written by Craig Edwards, Craig McLure, and others.
 * This program is free but copyrighted software; see
 *            the file COPYING for details.
 *
 * ---------------------------------------------------
 */

using namespace std;

#include "inspircd_config.h"
#include <sys/time.h>
#include <sys/resource.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string>
#include <unistd.h>
#include <fcntl.h>
#include <sstream>
#include <iostream>
#include <fstream>
#include "socket.h"
#include "inspircd.h"
#include "inspircd_io.h"
#include "inspircd_util.h"
#include "inspstring.h"
#include "helperfuncs.h"

extern FILE *log_file;
extern int boundPortCount;
extern int openSockfd[MAXSOCKS];
extern time_t TIME;
extern bool unlimitcore;
extern int MaxConn;

InspSocket::InspSocket()
{
	this->state = I_DISCONNECTED;
}

InspSocket::InspSocket(std::string host, int port, bool listening, unsigned long maxtime)
{
	if (listening) {
	} else {
		char* ip;
		this->host = host;
		hostent* hoste = gethostbyname(host.c_str());
		if (!hoste) {
			ip = (char*)host.c_str();
		} else {
			struct in_addr* ia = (in_addr*)hoste->h_addr;
			ip = inet_ntoa(*ia);
		}

                timeout_end = time(NULL)+maxtime;
                timeout = false;
                if ((this->fd = socket(AF_INET, SOCK_STREAM, 0)) == -1)
		{
			this->state = I_ERROR;
                        return;
		}
		this->port = port;
                inet_aton(ip,&addy);
                addr.sin_family = AF_INET;
                addr.sin_addr = addy;
                addr.sin_port = htons(this->port);

                int flags;
                flags = fcntl(this->fd, F_GETFL, 0);
                fcntl(this->fd, F_SETFL, flags | O_NONBLOCK);

                if(connect(this->fd, (sockaddr*)&this->addr,sizeof(this->addr)) == -1)
                {
                        if (errno != EINPROGRESS)
                        {
                                shutdown(this->fd,2);
                                close(this->fd);
                                this->fd = -1;
				this->state = I_ERROR;
                                return;
                        }
                }
                this->state = I_CONNECTING;
                return;
	}
}

void InspSocket::EngineTrigger()
{
	switch (this->state)
	{
		case I_CONNECTING:
			this->OnConnected();
		break;
		case I_LISTENING:
			this->OnIncomingConnection();
		break;
		case I_CONNECTED:
			this->OnDataReady();
		break;
		default:
		break;
	}
}

void InspSocket::SetState(InspSocketState s)
{
	this->state = s;
}

int InspSocket::OnConnected() { return 0; }
int InspSocket::OnError() { return 0; }
int InspSocket::OnDisconnect() { return 0; }
int InspSocket::OnIncomingConnection() { return 0; }
int InspSocket::OnDataReady() { return 0; }

InspSocket::~InspSocket()
{
}

/*
int BindSocket (int sockfd, struct sockaddr_in client, struct sockaddr_in server, int port, char* addr)
int OpenTCPSocket (void)
*/