@Dear Salem
I did as u suggested , now the good news is the program did not get segfault (maybe the reason is i had changed read_from_pipe and add those 3 line of read and also add malloc (data) in read_from_pipe which was not in origianl code as u had suggested in your last comment) but (the regression is) after this change i got black screen (which with those original (none-sence) of codes , black screen not happens)
n = 452 , the sizeof (n) is 4
P.S :
this is how i did tried in my last attempt (i put last write , in return)
Code:
int32_t write_to_pipe(int32_t fd, int32_t id, uchar *data, int32_t n)
{
if ((id<0) || (id>PIP_ID_MAX))
return(PIP_ID_ERR);
write(fd, PIP_ID_TXT[id], 3); // the magic cookie
write(fd,&n,sizeof(n)); // the length of the buffer (in n)
return( write(fd,data,n)); // n bytes of data
}
Code:
/*
* read_from_pipe():
* read all kind of data from pipe specified by fd
* special-flag redir: if set AND data is ECM: this will redirected to appr. client
*/
int32_t read_from_pipe(int32_t fd, uchar **data)
{
int32_t rc;
intptr_t hdr=0;
uchar buf[3];
memset(buf, 0, sizeof(buf));
*data=(uchar *)0;
rc=PIP_ID_NUL;
int32_t n;
read(fd, buf, sizeof(buf));
read(fd, &n, sizeof(n));
*data = malloc(n); //!! caller calls free()
read(fd, *data, n);
/*
if (read(fd, buf, sizeof(buf))==sizeof(buf)) {
memcpy(&hdr, buf+3, sizeof(void*));
} else {
cs_log("WARNING: pipe header to small !");
return PIP_ID_ERR;
}
*/
int32_t l;
for (l=0; (rc<0) && (PIP_ID_TXT[l]); l++)
if (!memcmp(buf, PIP_ID_TXT[l], 3))
rc=l;
if (rc<0) {
cs_log("WARNING: pipe garbage from pipe %i", fd);
return PIP_ID_ERR;
}
*data = (void*)hdr;
return(rc);
}
i would like also to point some functions which used write_to_pipe , and read_pipe , maybe after this change , i need some changes in other functions too .
Code:
static int32_t write_ecm_request(int32_t fd, ECM_REQUEST *er)
{
return(write_to_pipe(fd, PIP_ID_ECM, (uchar *) er, sizeof(ECM_REQUEST)));
}
Code:
int32_t process_client_pipe(struct s_client *cl, uchar *buf, int32_t l) {
if (!cl) return -1;
uchar *ptr;
uint16_t n;
int32_t pipeCmd = read_from_pipe(cl->fd_m2c_c, &ptr);
switch(pipeCmd) {
case PIP_ID_ECM:
chk_dcw(cl, (ECM_REQUEST *)ptr);
break;
case PIP_ID_UDP:
if (ptr[0]!='U') {
cs_log("INTERNAL PIPE-ERROR");
}
memcpy(&n, ptr+1, 2);
if (n+3<=l) {
memcpy(buf, ptr, n+3);
}
break;
case PIP_ID_ERR:
cs_exit(1);
break;
default:
cs_log("unhandled pipe message %d (client %s)", pipeCmd, cl->account->usr);
break;
}
if (ptr) free(ptr);
return pipeCmd;
}
highly appreciated if need any other corection to fix it (and make it run) .
Best Regards.