| 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 |
#include <stdlib.h> |
| 41 |
#include <unistd.h> |
| 42 |
#include <stdio.h> |
| 43 |
#include <netinet/in.h> |
| 44 |
#include <netinet/tcp.h> |
| 45 |
#include <string.h> |
| 46 |
#include <time.h> |
| 47 |
#include <sys/types.h> |
| 48 |
#include <sys/stat.h> |
| 49 |
#include <sys/socket.h> |
| 50 |
#include <sys/time.h> |
| 51 |
#include <arpa/inet.h> |
| 52 |
#include <netdb.h> |
| 53 |
#include <fcntl.h> |
| 54 |
#include <errno.h> |
| 55 |
#include <ctype.h> |
| 56 |
#include <pthread.h> |
| 57 |
|
| 58 |
|
| 59 |
#define SIGNAL_PORT 8418 |
| 60 |
#define COMMAND_PORT 8417 |
| 61 |
#define SEND_ENVIORNMENT |
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
#define BUFSIZE 1024 |
| 66 |
char buf[BUFSIZE]; |
| 67 |
|
| 68 |
#define WINSIZE 400 |
| 69 |
char window[WINSIZE]; |
| 70 |
|
| 71 |
#define WINBUF_NUM 2400 |
| 72 |
char* winbuf; |
| 73 |
char *end, *bs, *be; |
| 74 |
|
| 75 |
|
| 76 |
|
| 77 |
|
| 78 |
|
| 79 |
|
| 80 |
int command_desc; |
| 81 |
int speech_desc; |
| 82 |
char connected=1; |
| 83 |
|
| 84 |
int connect_to_host(char* host, int port); |
| 85 |
|
| 86 |
void read_full(int file, char* buffer, int num); |
| 87 |
int read_some(int file, char* buffer, int size); |
| 88 |
|
| 89 |
void write_buf(int file, char* buffer, int num); |
| 90 |
int write_amap(int file, char* buffer, int num); |
| 91 |
|
| 92 |
void setnonblocking(int desc); |
| 93 |
|
| 94 |
void finalize(); |
| 95 |
|
| 96 |
pthread_mutex_t command_mutex; |
| 97 |
pthread_t stdin_thread,signal_thread; |
| 98 |
void* readStdin(void* ptr); |
| 99 |
void* readSignal(void* ptr); |
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
|
| 107 |
int main() |
| 108 |
{ |
| 109 |
int ret; |
| 110 |
|
| 111 |
atexit(finalize); |
| 112 |
|
| 113 |
setlinebuf(stdin); |
| 114 |
setlinebuf(stdout); |
| 115 |
|
| 116 |
winbuf=(char*)malloc(WINSIZE*WINBUF_NUM); |
| 117 |
end=winbuf+WINSIZE*WINBUF_NUM; |
| 118 |
bs=be=winbuf; |
| 119 |
|
| 120 |
speech_desc=connect_to_host("localhost",SIGNAL_PORT); |
| 121 |
if(speech_desc<0) |
| 122 |
{ |
| 123 |
perror("signal socket"); |
| 124 |
return -1; |
| 125 |
} |
| 126 |
|
| 127 |
|
| 128 |
command_desc=connect_to_host("localhost",COMMAND_PORT); |
| 129 |
if(command_desc<0) |
| 130 |
{ |
| 131 |
perror("command socket"); |
| 132 |
return -1; |
| 133 |
} |
| 134 |
|
| 135 |
pthread_mutex_init(&command_mutex,NULL); |
| 136 |
pthread_create(&stdin_thread,NULL,readStdin,NULL); |
| 137 |
pthread_create(&signal_thread,NULL,readSignal,NULL); |
| 138 |
|
| 139 |
while(connected) |
| 140 |
{ |
| 141 |
pthread_mutex_lock(&command_mutex); |
| 142 |
ret=read_some(command_desc,buf,BUFSIZE); |
| 143 |
pthread_mutex_unlock(&command_mutex); |
| 144 |
if(ret>0) |
| 145 |
{ |
| 146 |
buf[ret]=0; |
| 147 |
printf("%s",buf); |
| 148 |
} |
| 149 |
} |
| 150 |
|
| 151 |
return 0; |
| 152 |
} |
| 153 |
|
| 154 |
void finalize() |
| 155 |
{ |
| 156 |
close(command_desc); |
| 157 |
close(speech_desc); |
| 158 |
free(winbuf); |
| 159 |
} |
| 160 |
|
| 161 |
void* readStdin(void* ptr) |
| 162 |
{ |
| 163 |
while(1) |
| 164 |
{ |
| 165 |
fgets(buf,BUFSIZE,stdin); |
| 166 |
#ifdef SEND_ENVIORNMENT |
| 167 |
pthread_mutex_lock(&command_mutex); |
| 168 |
write_buf(command_desc,buf,strlen(buf)); |
| 169 |
pthread_mutex_unlock(&command_mutex); |
| 170 |
#endif |
| 171 |
if(feof(stdin) || buf[0]=='\n') |
| 172 |
{ |
| 173 |
break; |
| 174 |
} |
| 175 |
} |
| 176 |
|
| 177 |
while(connected) |
| 178 |
{ |
| 179 |
fgets(buf,BUFSIZE,stdin); |
| 180 |
pthread_mutex_lock(&command_mutex); |
| 181 |
write_buf(command_desc,buf,strlen(buf)); |
| 182 |
pthread_mutex_unlock(&command_mutex); |
| 183 |
} |
| 184 |
|
| 185 |
pthread_exit(NULL); |
| 186 |
} |
| 187 |
|
| 188 |
void* readSignal(void* ptr) |
| 189 |
{ |
| 190 |
while(connected) |
| 191 |
{ |
| 192 |
read_full(3,window,WINSIZE); |
| 193 |
write_buf(speech_desc,window,WINSIZE); |
| 194 |
} |
| 195 |
|
| 196 |
pthread_exit(NULL); |
| 197 |
} |
| 198 |
|
| 199 |
|
| 200 |
void read_full(int file, char* buffer, int num) |
| 201 |
{ |
| 202 |
int count,pos=0; |
| 203 |
|
| 204 |
while(num) |
| 205 |
{ |
| 206 |
count=read(file,buffer+pos,num); |
| 207 |
if(count==0 || (count<0 && errno!=EAGAIN)) |
| 208 |
{ |
| 209 |
connected=0; |
| 210 |
return; |
| 211 |
} |
| 212 |
num-=count; |
| 213 |
pos+=count; |
| 214 |
} |
| 215 |
} |
| 216 |
|
| 217 |
int connect_to_host(char* name, int port) |
| 218 |
{ |
| 219 |
int address; |
| 220 |
struct hostent* host_entity; |
| 221 |
int res,desc; |
| 222 |
int opts; |
| 223 |
struct sockaddr_in host; |
| 224 |
|
| 225 |
|
| 226 |
|
| 227 |
if(!strcmp(name,"localhost")) |
| 228 |
address=htonl(2130706433); |
| 229 |
else |
| 230 |
{ |
| 231 |
address=inet_addr(name); |
| 232 |
if(address==(in_addr_t)-1) |
| 233 |
{ |
| 234 |
host_entity = gethostbyname(name); |
| 235 |
|
| 236 |
if(!host_entity) |
| 237 |
{ |
| 238 |
fprintf(stderr,"EAGI proxy: Wrong address!\n"); |
| 239 |
return -1; |
| 240 |
} |
| 241 |
address=*((int*)host_entity->h_addr); |
| 242 |
} |
| 243 |
} |
| 244 |
|
| 245 |
desc=socket(AF_INET,SOCK_STREAM,IPPROTO_TCP); |
| 246 |
if(desc<0) |
| 247 |
{ |
| 248 |
fprintf(stderr,"EAGI proxy: Cannot create socket!\n"); |
| 249 |
return -1; |
| 250 |
} |
| 251 |
|
| 252 |
memset((void*)&host,0,sizeof(struct sockaddr_in)); |
| 253 |
|
| 254 |
host.sin_family=AF_INET; |
| 255 |
host.sin_port=htons(port); |
| 256 |
host.sin_addr.s_addr=address; |
| 257 |
|
| 258 |
res=connect(desc,(struct sockaddr*)&host,sizeof(host)); |
| 259 |
if(res<0) |
| 260 |
{ |
| 261 |
fprintf(stderr,"EAGI proxy: Cannot connect!\n"); |
| 262 |
return -1; |
| 263 |
} |
| 264 |
|
| 265 |
|
| 266 |
opts = fcntl(desc,F_GETFL); |
| 267 |
if (opts < 0) { |
| 268 |
perror("fcntl(F_GETFL)"); |
| 269 |
exit(EXIT_FAILURE); |
| 270 |
} |
| 271 |
opts = (opts | O_NONBLOCK); |
| 272 |
if (fcntl(desc,F_SETFL,opts) < 0) { |
| 273 |
perror("fcntl(F_SETFL)"); |
| 274 |
exit(EXIT_FAILURE); |
| 275 |
} |
| 276 |
|
| 277 |
|
| 278 |
return desc; |
| 279 |
} |
| 280 |
|
| 281 |
int read_some(int desc, char* buffer, int size) |
| 282 |
{ |
| 283 |
unsigned char c; |
| 284 |
int res,i=0; |
| 285 |
|
| 286 |
for(;;) |
| 287 |
{ |
| 288 |
res=read(desc,&c,1); |
| 289 |
if(res<1) |
| 290 |
{ |
| 291 |
if(errno!=EAGAIN) |
| 292 |
{ |
| 293 |
perror("Error reading"); |
| 294 |
connected=0; |
| 295 |
} |
| 296 |
break; |
| 297 |
} |
| 298 |
if(res==0) |
| 299 |
{ |
| 300 |
connected=0; |
| 301 |
break; |
| 302 |
} |
| 303 |
|
| 304 |
buffer[i]=c; |
| 305 |
i++; |
| 306 |
} |
| 307 |
|
| 308 |
return i; |
| 309 |
} |
| 310 |
|
| 311 |
|
| 312 |
void write_buf(int desc, char* buf, int size) |
| 313 |
{ |
| 314 |
int ret; |
| 315 |
|
| 316 |
|
| 317 |
|
| 318 |
if(be!=bs) |
| 319 |
{ |
| 320 |
if(be>bs) |
| 321 |
{ |
| 322 |
ret=write_amap(desc,bs,be-bs); |
| 323 |
bs+=ret; |
| 324 |
} |
| 325 |
else |
| 326 |
{ |
| 327 |
ret=write_amap(desc,bs,end-bs); |
| 328 |
if(ret==end-bs) |
| 329 |
{ |
| 330 |
ret=write_amap(desc,winbuf,be-winbuf); |
| 331 |
bs=winbuf+ret; |
| 332 |
} |
| 333 |
else bs+=ret; |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
if(be==bs) |
| 338 |
{ |
| 339 |
ret=write_amap(desc,buf,size); |
| 340 |
buf+=ret; |
| 341 |
size-=ret; |
| 342 |
} |
| 343 |
|
| 344 |
if(size) |
| 345 |
{ |
| 346 |
if(be>=bs) |
| 347 |
{ |
| 348 |
if(size>end-be) |
| 349 |
{ |
| 350 |
size-=end-be; |
| 351 |
memcpy(be,buf,end-be); |
| 352 |
be=winbuf; |
| 353 |
buf+=end-be; |
| 354 |
} |
| 355 |
else |
| 356 |
{ |
| 357 |
memcpy(be,buf,size); |
| 358 |
be+=size; |
| 359 |
if(be>=end) |
| 360 |
be=winbuf; |
| 361 |
size=0; |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
if(size) |
| 366 |
{ |
| 367 |
if(size>=bs-be) |
| 368 |
{ |
| 369 |
fprintf(stderr,"Buffer overflow!\n"); |
| 370 |
size=bs-be-1; |
| 371 |
} |
| 372 |
|
| 373 |
if(size) |
| 374 |
{ |
| 375 |
memcpy(be,buf,size); |
| 376 |
be+=size; |
| 377 |
} |
| 378 |
} |
| 379 |
} |
| 380 |
} |
| 381 |
|
| 382 |
int write_amap(int desc, char* buf, int size) |
| 383 |
{ |
| 384 |
int ret; |
| 385 |
ret=write(desc,buf,size); |
| 386 |
if(ret<0) |
| 387 |
{ |
| 388 |
if(errno!=EAGAIN) |
| 389 |
{ |
| 390 |
perror("Error writing"); |
| 391 |
connected=0; |
| 392 |
} |
| 393 |
return 0; |
| 394 |
} |
| 395 |
if(ret==0) |
| 396 |
connected=0; |
| 397 |
|
| 398 |
return ret; |
| 399 |
} |
| 400 |
|
| 401 |
|
| 402 |
void setnonblocking(int desc) |
| 403 |
{ |
| 404 |
int opts; |
| 405 |
|
| 406 |
opts = fcntl(desc,F_GETFL); |
| 407 |
if(opts < 0) |
| 408 |
{ |
| 409 |
perror("fcntl(F_GETFL)"); |
| 410 |
exit(-1); |
| 411 |
} |
| 412 |
|
| 413 |
opts = (opts | O_NONBLOCK ); |
| 414 |
if(fcntl(desc,F_SETFL,opts) < 0) |
| 415 |
{ |
| 416 |
perror("fcntl(F_SETFL)"); |
| 417 |
exit(-1); |
| 418 |
} |
| 419 |
} |