| 1 |
/*************************************************************************** |
/*************************************************************************** |
| 2 |
netio.c - description |
netio.c - description |
| 3 |
------------------- |
------------------- |
| 4 |
begin : Mon Oct 18 2004 |
begin : Mon Oct 18 2004 |
| 5 |
copyright : (C) 2004 by Leaflet |
copyright : (C) 2004 by Leaflet |
| 6 |
email : leaflet@leafok.com |
email : leaflet@leafok.com |
| 7 |
***************************************************************************/ |
***************************************************************************/ |
| 8 |
|
|
| 9 |
/*************************************************************************** |
/*************************************************************************** |
| 16 |
***************************************************************************/ |
***************************************************************************/ |
| 17 |
|
|
| 18 |
#include "common.h" |
#include "common.h" |
| 19 |
|
#include <string.h> |
| 20 |
|
#include <sys/socket.h> |
| 21 |
|
|
| 22 |
char |
char s_getc(int socket) |
|
s_getc(int socket) |
|
| 23 |
{ |
{ |
| 24 |
char c; |
char c; |
| 25 |
if (recv(socket,&c,1,0)>0) |
if (recv(socket, &c, 1, 0) > 0) |
| 26 |
return c; |
return c; |
| 27 |
else |
else |
| 28 |
return '\0'; |
return '\0'; |
| 29 |
} |
} |
| 30 |
|
|
| 31 |
int |
int s_putc(int socket, char c) |
|
s_putc(int socket, char c) |
|
| 32 |
{ |
{ |
| 33 |
int ret; |
int ret; |
| 34 |
|
|
| 35 |
ret = send (socket, &c, 1, 0); |
ret = send(socket, &c, 1, 0); |
| 36 |
|
|
| 37 |
return ret; |
return ret; |
| 38 |
} |
} |
| 39 |
|
|
| 40 |
int |
int s_receive(int socket, char *buffer, int buffer_length, char end_str[]) |
|
s_receive(int socket, char* buffer, int buffer_length, char end_str[]) |
|
| 41 |
{ |
{ |
| 42 |
char buf_str[2048]; |
char buf_str[2048]; |
| 43 |
int buf_read,total_read; |
int buf_read, total_read; |
| 44 |
int buf_len = 2047; |
int buf_len = 2047; |
| 45 |
|
|
| 46 |
if (buf_len+1 > buffer_length) |
if (buf_len + 1 > buffer_length) |
| 47 |
buf_len = buffer_length -1; |
buf_len = buffer_length - 1; |
| 48 |
|
|
| 49 |
total_read = 0; |
total_read = 0; |
| 50 |
strcpy(buffer,""); |
strcpy(buffer, ""); |
| 51 |
while((buf_read = recv(socket,buf_str,buf_len,0)) > 0) |
while ((buf_read = recv(socket, buf_str, buf_len, 0)) > 0) |
| 52 |
{ |
{ |
| 53 |
buf_str[buf_read] = '\0'; |
buf_str[buf_read] = '\0'; |
| 54 |
total_read += buf_read; |
total_read += buf_read; |
| 55 |
strcat(buffer,buf_str); |
strcat(buffer, buf_str); |
| 56 |
|
|
| 57 |
buf_len = buffer_length - total_read - 1; |
buf_len = buffer_length - total_read - 1; |
| 58 |
if (buf_len+1 > buffer_length) |
if (buf_len + 1 > buffer_length) |
| 59 |
buf_len = buffer_length -1; |
buf_len = buffer_length - 1; |
| 60 |
|
|
| 61 |
if (strcmp((buffer + total_read - strlen(end_str)), end_str) == 0) |
if (strcmp((buffer + total_read - strlen(end_str)), end_str) == 0) |
| 62 |
break; |
break; |
| 63 |
//different line-end symbol in different OS |
// different line-end symbol in different OS |
| 64 |
if (strcmp(end_str,"\r") == 0) |
if (strcmp(end_str, "\r") == 0) |
| 65 |
{ |
{ |
| 66 |
if (strcmp((buffer + total_read - 2), "\r\n") == 0) |
if (strcmp((buffer + total_read - 2), "\r\n") == 0) |
| 67 |
break; |
break; |
| 68 |
} |
} |
| 69 |
} |
} |
| 70 |
|
|
| 71 |
return total_read; |
return total_read; |
| 72 |
} |
} |
| 73 |
|
|
| 74 |
int |
int s_send(int socket, const char *in_str) |
|
s_send(int socket, const char* in_str) |
|
| 75 |
{ |
{ |
| 76 |
int ret; |
int ret; |
| 77 |
|
|
| 78 |
|
if (in_str != NULL && strlen(in_str) > 0) |
| 79 |
|
{ |
| 80 |
|
ret = send(socket, in_str, strlen(in_str), 0); |
| 81 |
|
} |
| 82 |
|
|
| 83 |
if (in_str != NULL && strlen(in_str) > 0) |
return ret; |
| 84 |
{ |
} |
| 85 |
ret = send(socket, in_str, strlen(in_str), 0); |
|
| 86 |
} |
// from Maple-hightman |
| 87 |
|
// added by flyriver, 2001.3.3 |
| 88 |
return ret; |
int telnetopt(int fd, char *buf, int max) |
| 89 |
|
{ |
| 90 |
|
unsigned char c, d, e; |
| 91 |
|
int pp = 0; |
| 92 |
|
unsigned char tmp[30]; |
| 93 |
|
|
| 94 |
|
while (pp < max) |
| 95 |
|
{ |
| 96 |
|
c = buf[pp++]; |
| 97 |
|
if (c == 255) |
| 98 |
|
{ |
| 99 |
|
d = buf[pp++]; |
| 100 |
|
e = buf[pp++]; |
| 101 |
|
iflush(); |
| 102 |
|
if ((d == 253) && (e == 3 || e == 24)) |
| 103 |
|
{ |
| 104 |
|
tmp[0] = 255; |
| 105 |
|
tmp[1] = 251; |
| 106 |
|
tmp[2] = e; |
| 107 |
|
write(fd, tmp, 3); |
| 108 |
|
continue; |
| 109 |
|
} |
| 110 |
|
if ((d == 251 || d == 252) && (e == 1 || e == 3 || e == 24)) |
| 111 |
|
{ |
| 112 |
|
tmp[0] = 255; |
| 113 |
|
tmp[1] = 253; |
| 114 |
|
tmp[2] = e; |
| 115 |
|
write(fd, tmp, 3); |
| 116 |
|
continue; |
| 117 |
|
} |
| 118 |
|
if (d == 251 || d == 252) |
| 119 |
|
{ |
| 120 |
|
tmp[0] = 255; |
| 121 |
|
tmp[1] = 254; |
| 122 |
|
tmp[2] = e; |
| 123 |
|
write(fd, tmp, 3); |
| 124 |
|
continue; |
| 125 |
|
} |
| 126 |
|
if (d == 253 || d == 254) |
| 127 |
|
{ |
| 128 |
|
tmp[0] = 255; |
| 129 |
|
tmp[1] = 252; |
| 130 |
|
tmp[2] = e; |
| 131 |
|
write(fd, tmp, 3); |
| 132 |
|
continue; |
| 133 |
|
} |
| 134 |
|
if (d == 250) |
| 135 |
|
{ |
| 136 |
|
while (e != 240 && pp < max) |
| 137 |
|
e = buf[pp++]; |
| 138 |
|
tmp[0] = 255; |
| 139 |
|
tmp[1] = 250; |
| 140 |
|
tmp[2] = 24; |
| 141 |
|
tmp[3] = 0; |
| 142 |
|
tmp[4] = 65; |
| 143 |
|
tmp[5] = 78; |
| 144 |
|
tmp[6] = 83; |
| 145 |
|
tmp[7] = 73; |
| 146 |
|
tmp[8] = 255; |
| 147 |
|
tmp[9] = 240; |
| 148 |
|
write(fd, tmp, 10); |
| 149 |
|
} |
| 150 |
|
} |
| 151 |
|
else |
| 152 |
|
outc(c); |
| 153 |
|
} |
| 154 |
|
iflush(); |
| 155 |
|
return 0; |
| 156 |
} |
} |