| 1 |
/***************************************************************************
|
| 2 |
netio.c - description
|
| 3 |
-------------------
|
| 4 |
begin : Mon Oct 18 2004
|
| 5 |
copyright : (C) 2004 by Leaflet
|
| 6 |
email : leaflet@leafok.com
|
| 7 |
***************************************************************************/
|
| 8 |
|
| 9 |
/***************************************************************************
|
| 10 |
* *
|
| 11 |
* This program is free software; you can redistribute it and/or modify *
|
| 12 |
* it under the terms of the GNU General Public License as published by *
|
| 13 |
* the Free Software Foundation; either version 2 of the License, or *
|
| 14 |
* (at your option) any later version. *
|
| 15 |
* *
|
| 16 |
***************************************************************************/
|
| 17 |
|
| 18 |
#include "common.h"
|
| 19 |
#include <string.h>
|
| 20 |
#include <sys/socket.h>
|
| 21 |
|
| 22 |
char s_getc(int socket)
|
| 23 |
{
|
| 24 |
char c;
|
| 25 |
if (recv(socket, &c, 1, 0) > 0)
|
| 26 |
return c;
|
| 27 |
else
|
| 28 |
return '\0';
|
| 29 |
}
|
| 30 |
|
| 31 |
int s_putc(int socket, char c)
|
| 32 |
{
|
| 33 |
int ret;
|
| 34 |
|
| 35 |
ret = send(socket, &c, 1, 0);
|
| 36 |
|
| 37 |
return ret;
|
| 38 |
}
|
| 39 |
|
| 40 |
int s_receive(int socket, char *buffer, int buffer_length, char end_str[])
|
| 41 |
{
|
| 42 |
char buf_str[2048];
|
| 43 |
int buf_read, total_read;
|
| 44 |
int buf_len = 2047;
|
| 45 |
|
| 46 |
if (buf_len + 1 > buffer_length)
|
| 47 |
buf_len = buffer_length - 1;
|
| 48 |
|
| 49 |
total_read = 0;
|
| 50 |
strcpy(buffer, "");
|
| 51 |
while ((buf_read = recv(socket, buf_str, buf_len, 0)) > 0)
|
| 52 |
{
|
| 53 |
buf_str[buf_read] = '\0';
|
| 54 |
total_read += buf_read;
|
| 55 |
strcat(buffer, buf_str);
|
| 56 |
|
| 57 |
buf_len = buffer_length - total_read - 1;
|
| 58 |
if (buf_len + 1 > buffer_length)
|
| 59 |
buf_len = buffer_length - 1;
|
| 60 |
|
| 61 |
if (strcmp((buffer + total_read - strlen(end_str)), end_str) == 0)
|
| 62 |
break;
|
| 63 |
// different line-end symbol in different OS
|
| 64 |
if (strcmp(end_str, "\r") == 0)
|
| 65 |
{
|
| 66 |
if (strcmp((buffer + total_read - 2), "\r\n") == 0)
|
| 67 |
break;
|
| 68 |
}
|
| 69 |
}
|
| 70 |
|
| 71 |
return total_read;
|
| 72 |
}
|
| 73 |
|
| 74 |
int s_send(int socket, const char *in_str)
|
| 75 |
{
|
| 76 |
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 |
return ret;
|
| 84 |
}
|
| 85 |
|
| 86 |
// from Maple-hightman
|
| 87 |
// added by flyriver, 2001.3.3
|
| 88 |
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 |
}
|