forked from afarhan/sbitx
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtelnet.c
223 lines (192 loc) · 5.18 KB
/
telnet.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
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
#include <stdio.h>
#include <unistd.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <string.h>
#include <arpa/inet.h>
#include <stdlib.h>
#include <errno.h>
#include <fcntl.h>
#include <complex.h>
#include <ctype.h>
#include <sys/time.h>
#include <unistd.h>
#include <netdb.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <pthread.h>
#include "sdr_ui.h"
void telnet_open(char *server);
int telnet_write(char *text);
void telnet_close();
static int telnet_sock = -1;
static pthread_t telnet_thread;
long get_address(char *host)
{
int i, dotcount=0;
char *p = host;
struct hostent *pent;
/* struct sockaddr_in addr; */ /* see the note on portabilit at the end of the routine */
/*try understanding if this is a valid ip address
we are skipping the values of the octets specified here.
for instance, this code will allow 952.0.320.567 through*/
while (*p)
{
for (i = 0; i < 3; i++, p++)
if (!isdigit(*p))
break;
if (*p != '.')
break;
p++;
dotcount++;
}
/* three dots with upto three digits in before, between and after ? */
if (dotcount == 3 && i > 0 && i <= 3)
return inet_addr(host);
/* try the system's own resolution mechanism for dns lookup:
required only for domain names.
inspite of what the rfc2543 :D Using SRV DNS Records recommends,
we are leaving it to the operating system to do the name caching.
this is an important implementational issue especially in the light
dynamic dns servers like dynip.com or dyndns.com where a dial
ip address is dynamically assigned a sub domain like farhan.dynip.com
although expensive, this is a must to allow OS to take
the decision to expire the DNS records as it deems fit.
*/
printf("Looking up %s\n", host);
pent = gethostbyname(host);
if (!pent){
printf("Failed to resolve %s\n", host);
return 0;
}
/* PORTABILITY-ISSUE: replacing a costly memcpy call with a hack, may not work on
some systems.
memcpy(&addr.sin_addr, (pent->h_addr), pent->h_length);
return addr.sin_addr.s_addr; */
return *((long *)(pent->h_addr));
}
void *telnet_thread_function(void *server){
struct sockaddr_storage serverStorage;
socklen_t addr_size;
struct sockaddr_in serverAddr;
char buff[200], host[100], port[7];
if (strlen(server) > sizeof(host) - 1)
return NULL;
int i;
char *p = server;
char *q = host;
for(i = 0; *p && *p != ':' && i < sizeof(host)-1; i++)
*q++ = *p++;
*q = 0;
q = port;
p++;
for(i = 0; *p && *p && i < sizeof(port)-1; i++)
*q++ = *p++;
*q = 0;
if(!strlen(host)){
write_console(FONT_TELNET, "Telnet : specify host and port\nex:'\topen dxc.g3lrs.org.uk:7300\n'");
return NULL;
}
if(!strlen(port)){
write_console(FONT_TELNET, "Telnet port is missing\n");
return NULL;
}
if (telnet_sock >= 0)
close(telnet_sock);
sprintf(buff, "Finding %s:%s\n", host, port);
write_console(FONT_TELNET, buff);
memset(serverAddr.sin_zero, '\0', sizeof serverAddr.sin_zero);
serverAddr.sin_family = AF_INET;
serverAddr.sin_port = htons(atoi(port));
serverAddr.sin_addr.s_addr = get_address(host);
sprintf(buff, "Opening %s:%s\n", host, port);
write_console(FONT_TELNET, buff);
telnet_sock = socket(AF_INET, SOCK_STREAM, 0);
if (connect(telnet_sock,
(struct sockaddr *)&serverAddr, sizeof(serverAddr)) < 0) {
printf("Telnet: failed to connect to %s\n", server);
write_console(FONT_TELNET, "Failed to open telnet, check hostname and port?\n");
close(telnet_sock);
telnet_sock = -1;
return NULL;
}
//send our callsign
char mycallsign[100];
get_field_value("#mycallsign", mycallsign);
telnet_write(mycallsign);
int e;
while((e = recv(telnet_sock, buff, sizeof(buff), 0)) >= 0){
if (e > 0){
buff[e] = 0;
//here we will try stripping too many spaces
char *p, *q, buff2[201]; // bigger than the original buff
int n_spaces = 0;
int tab_space = 3;
p = buff; q = buff2;
for (p = buff;*p;p++){
if (*p != ' '){
//add spaces for a shorter tab
if (n_spaces > tab_space){
int l = strlen(buff2);
int next_tab = (l/tab_space + 1)* tab_space;
for (int i = 0; i < next_tab - l; i++){
*q++ = ' ';
*q = 0;
}
}
*q++ = *p;
*q = 0;
n_spaces = 0;
}
else { // *p is a space
if (n_spaces < tab_space){
*q++ = *p;
*q = 0;
}
n_spaces++;
}
}
*q = 0;
// printf("compressed [%s] to [%s]\n", buff, buff2);
write_console(FONT_TELNET, buff2);
}
}
close(telnet_sock);
telnet_sock = -1;
}
int telnet_write(char *text){
if (telnet_sock < 0){
return -1;
}
char nl[] = "\n";
send (telnet_sock, text, strlen(text), 0);
send (telnet_sock, nl, strlen(nl), 0);
}
void telnet_close(){
puts("Telnet socket is closing");
close(telnet_sock);
telnet_sock = 0;
}
char telnet_server_name[100];
void telnet_open(char *server){
//close any existing telnet sessions
if (telnet_sock > 1){
close(telnet_sock);
telnet_sock = 0;
}
strcpy(telnet_server_name, server);
pthread_create( &telnet_thread, NULL, telnet_thread_function,
(void*)telnet_server_name);
}
/*
int main(int argc, char *argv[]){
telnet_open(argv[1]);
sleep(10);
telnet_write("vu2ese\n");
sleep(20);
telnet_close();
sleep(10);
}
*/