-
Notifications
You must be signed in to change notification settings - Fork 3
/
util.c
111 lines (82 loc) · 2.67 KB
/
util.c
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
/*
This file is part of macSSH
Copyright 2016 Daniel Machon
SSH 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/>.
*/
#include "includes.h"
#include "util.h"
#include "ssh-session.h"
#define h_addr h_addr_list[0]
int init_tcp_socket(char* ip, int port, int t_out);
int init_tcp_listen_socket(int port);
int connect_to_remote_host()
{
int sock;
sock = init_tcp_socket("194.255.39.141", 6666, 0);
if(sock < 0)
return -1;
else
ses.sock_out = ses.sock_in = sock;
return 0;
}
int init_tcp_socket(char *ip, int port, int t_out)
{
/* Needs to be declared outside while loop */
int sock;
struct hostent *host;
struct sockaddr_in server_addr;
struct timeval rcv_timeval;
struct timeval snd_timeval;
/* TextSpeak IP Address is defined as 192.168.0.23 */
host = gethostbyname(ip);
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return -1;
/* Set TCP socket options */
if (t_out) {
rcv_timeval.tv_sec = t_out;
rcv_timeval.tv_usec = 0;
snd_timeval.tv_sec = t_out;
snd_timeval.tv_usec = 0;
int buffersize = 2;
setsockopt(sock, SOL_SOCKET, SO_RCVTIMEO, (char *) &rcv_timeval, sizeof(struct timeval));
setsockopt(sock, SOL_SOCKET, SO_SNDTIMEO, &snd_timeval, sizeof(struct timeval));
//setsockopt(sock, SOL_SOCKET, SO_SNDBUF, &buffersize, 4);
}
server_addr.sin_family = AF_INET;
server_addr.sin_port = htons(port);
server_addr.sin_addr = *((struct in_addr *) host->h_addr);
bzero(&(server_addr.sin_zero), 8);
if (connect(sock, (struct sockaddr *) &server_addr, sizeof(struct sockaddr)) < 0)
return -1;
return sock;
}
int init_tcp_listen_socket(int port)
{
int sock;
struct hostent *host;
struct sockaddr_in si_me;
struct timeval rcv_timeval;
struct timeval snd_timeval;
if ((sock = socket(AF_INET, SOCK_STREAM, 0)) < 0)
return -1;
si_me.sin_family = AF_INET;
si_me.sin_port = htons(port);
si_me.sin_addr.s_addr = htonl(INADDR_ANY);
if (bind(sock, (struct sockaddr*) &si_me, sizeof(si_me)) == -1)
return -1;
listen(sock, 5);
return sock;
}
void get_ip(struct in_addr *addr, char *ip)
{
inet_ntop(AF_INET, addr, ip, INET_ADDRSTRLEN);
}