struct telnetd_line {
char line[TELNETD_CONF_LINELEN];
};
MEMB(linemem, struct telnetd_line, TELNETD_CONF_NUMLINES);
memb_init(&linemem);
/*---------------------------------------------------------------------------*/
void
memb_init(struct memb_blocks *m)
{
memset(m->count, 0, m->num);
memset(m->mem, 0, m->size * m->num);
}
/*---------------------------------------------------------------------------*/
void *
memb_alloc(struct memb_blocks *m)
{
int i;
for(i = 0; i < m->num; ++i) {
if(m->count[i] == 0) {
/* If this block was unused, we increase the reference count to
indicate that it now is used and return a pointer to the
memory block. */
++(m->count[i]);
return (void *)((char *)m->mem + (i * m->size));
}
}
/* No free block was found, so we return NULL to indicate failure to
allocate block. */
return NULL;
}
/*---------------------------------------------------------------------------*/
char
memb_free(struct memb_blocks *m, void *ptr)
{
int i;
char *ptr2;
/* Walk through the list of blocks and try to find the block to
which the pointer "ptr" points to. */
ptr2 = (char *)m->mem;
for(i = 0; i < m->num; ++i) {
if(ptr2 == (char *)ptr) {
/* We've found to block to which "ptr" points so we decrease the
reference count and return the new value of it. */
if(m->count[i] > 0) {
/* Make sure that we don't deallocate free memory. */
--(m->count[i]);
}
return m->count[i];
}
ptr2 += m->size;
}
return -1;
}
static void
acked(void)
{
static unsigned int i;
while(s.numsent > 0) {
dealloc_line(s.lines[0]);
for(i = 1; i < TELNETD_CONF_NUMLINES; ++i) {
s.lines[i - 1] = s.lines[i];
}
s.lines[TELNETD_CONF_NUMLINES - 1] = NULL;
--s.numsent;
}
udp_dadt_len=0;
}
void senddata(void)//static
{
/*static char *bufptr, *lineptr;
static int buflen, linelen;
bufptr = uip_appdata;
buflen = 0;
for(s.numsent = 0; s.numsent < 1300 &&
s.lines[s.numsent] != NULL ; ++s.numsent) {
lineptr = s.lines[s.numsent];
linelen = strlen(lineptr);
if(linelen > 1300) {
linelen = 1300;
}
if(buflen + linelen < uip_mss()) {
memcpy(bufptr, lineptr, linelen);
bufptr += linelen;
buflen += linelen;
} else {
break;
}
}
uip_send(uip_appdata, buflen);*/
static char *bufptr, *lineptr;
static int buflen, linelen;
bufptr = uip_appdata;
buflen = 0;
linelen =udp_dadt_len+1;
for(s.numsent = 0; s.numsent < 1300&&s.numsent<udp_dadt_len ;//&& s.lines[s.numsent] != NULL ;
++s.numsent) {
lineptr = s.lines[s.numsent];
// linelen = strlen(lineptr);
linelen--;
if(linelen > 1300) {
linelen = 1300;
}
if(buflen + linelen < uip_mss()) {
memcpy(bufptr, lineptr, linelen);
bufptr += linelen;
buflen += linelen;
} else {
break;
}
}
uip_send(uip_appdata, udp_dadt_len);
/*static char *lineptr;
lineptr = s.lines[0]; //s.numsent获取数据最后的指针地址
uip_send(lineptr,udp_dadt_len);// buflen
udp_dadt_len=0;*/
}
/*---------------------------------------------------------------------------*/
static void
closed(void)
{
static unsigned int i;
for(i = 0; i < TELNETD_CONF_NUMLINES; ++i) {
if(s.lines[i] != NULL) {
dealloc_line(s.lines[i]);
}
}
}
static void
sendline(char *line)
{
static unsigned int i;
for(i = 0; i < TELNETD_CONF_NUMLINES; ++i) {
if(s.lines[i] == NULL) {
s.lines[i] = line;
break;
}
}
if(i == TELNETD_CONF_NUMLINES) {
dealloc_line(line);
}
}
void
shell_output(char *str1, int out_len)
{
static unsigned len;
char *line;
udp_dadt_len=out_len;
line = alloc_line();//分配内存
if(line != NULL) {
memcpy(line, str1,out_len);
}
sendline(line);//释放内存
#define MEMB_CONCAT2(s1, s2) s1##s2#define MEMB_CONCAT(s1, s2) MEMB_CONCAT2(s1, s2)
}
#define MEMB(name, structure, num) \
//第一个 参数是分配内存块起名字
//第2个参数是分配 内存的块大小子块的数据结构
//第3个参数是分配内存的块数量
static char MEMB_CONCAT(name,_memb_count)[num]; \
static structure MEMB_CONCAT(name,_memb_mem)[num]; \
static struct memb_blocks name = {sizeof(structure), num, \
MEMB_CONCAT(name,_memb_count), \
(void *)MEMB_CONCAT(name,_memb_mem)}
struct memb_blocks {
unsigned short size;//子块的大小
unsigned short num;//子块的数目
char *count;//每个子块引用计数,表示正在使用还是未被使用
void *mem;//内存块首地址
};