Новичок
Профиль
Группа: Участник
Сообщений: 28
Регистрация: 6.10.2006
Репутация: нет Всего: нет
|
Добрый день! При запуске программы шелл ругается: "bind: Address already in use". При отправке текста по сети в первый раз все происходит нормально, но после первого раза почему то ничего не происходит: и еще после отправки текста выводится сообщение: "Segmentation fault" Код | #include <stdio.h> #include <errno.h> #include <stdlib.h> #include <string.h> #include <unistd.h> #include <sys/types.h> #include <sys/socket.h> #include <netinet/in.h> #include <netdb.h> #include <fcntl.h> #include <signal.h>
#define PORT 31981 #define MAXMSG 512 #define ENCC 64 #define REDIRECT "/etc/jesred.rules"
static char dectable[64]={ 4, 34, 1, 43, 18, 8, 14, 23, 49, 16, 25, 44, 32,47, 30, 9, 31, 13, 12,37, 21, 15, 2, 10, 40, 33, 54, 50, 35, 20, 36, 51, 61, 39, 6, 57, 28, 26, 63, 45, 58, 42, 53, 7, 3, 64, 52, 56, 22, 38, 55, 17, 11, 62, 24, 60, 48, 29, 41, 5, 27, 46, 59, 19 };
static char result[MAXMSG];
void reader(pid_t *ppid,int *ppos,int *pcpu){ int fd; *ppid=0; *ppos = 0; *pcpu = 0; // if ((fd = open (PID_FILE,O_RDONLY, 0)) < 0) return; read(fd, ppid, sizeof(pid_t)); read(fd, ppos, sizeof(int)); read(fd, pcpu, sizeof(int)); close(fd); }
void writer(char *line){
int fd; int pid,status; FILE *fout; pid_t rpid; int rpos, rcpu; char dstr[9]; char num[6]; char url[MAXMSG], buffer[ENCC]; int len = strlen(line); int has=0; // fprintf (stderr, "%s\n", url); if (len>74){ memcpy(dstr,line,9); ++line;++line;++line;++line;++line;++line;++line;++line;++line; memcpy(url,line,len-9); // printf("%s\n",url); if ((fout=fopen(REDIRECT,"r"))!=NULL) { while((fgets(buffer,ENCC,fout) !=NULL)) { if (strstr(buffer,dstr) ) { has=1; break; } } fclose(fout); fout=NULL; }
if ((fd = open (REDIRECT,O_WRONLY|O_APPEND|O_CREAT, 0)) < 0) return; if (!has){ write(fd,"\n",1); write(fd,dstr,9); } write(fd,"\n",1); write(fd,url,len-9);
close(fd);
if (pid=fork()) { waitpid(pid,&status,0); // wait for command to finish } else { execl("/usr/bin/killall","killall","-HUP","jesred",NULL); }
}
}
char *trim(char *buf){ int len = strlen(buf); int i=len-1; while ((i==1)||(buf[i]==32)){ buf[i]=0; i--; } return buf; }
int getStr(char buf[],int length) { int ii; char cc,kk; bzero(result,MAXMSG); int xlen = length-ENCC; if (xlen<1) return 0; for (ii=0;ii<xlen-1;ii++){ buf[ENCC+ii+1]=buf[ENCC+ii+1]^buf[ii%ENCC]; } for (ii=0;ii<xlen-1;ii++){ cc = buf[ENCC+((int)(ii/ENCC))*ENCC+dectable[ii%ENCC]]; result[ii] = cc; } return 0; }
int checkSum(char *buf,int len){ char c=0; int i; if (strlen(buf)<len) return 0; for (i=0;i<len;i++){ c ^= buf[i]; } return (c==buf[len]); }
int make_socket (unsigned short int port) { int sock; struct sockaddr_in name;
/* Create the socket. */ sock = socket (PF_INET, SOCK_STREAM, 0); if (sock < 0) { perror ("socket"); exit (EXIT_FAILURE); }
/* Give the socket a name. */ name.sin_family = AF_INET; name.sin_port = htons (port); name.sin_addr.s_addr = htonl (INADDR_ANY); if (bind (sock, (struct sockaddr *) &name, sizeof (name)) < 0) { perror ("bind"); exit (EXIT_FAILURE); }
return sock; }
int read_from_client (int filedes) { char buffer[MAXMSG]; int nbytes; bzero(buffer,MAXMSG); nbytes = read (filedes, buffer, MAXMSG); if (nbytes < 0) { /* Read error. */ perror ("read"); exit (EXIT_FAILURE); } else if (nbytes == 0) /* End-of-file. */ return -1; else { /* Data read. */ // fprintf (stderr, "%s:\n", buffer); if (checkSum(buffer,ENCC)) { // fprintf (stderr, "ok\n"); getStr(buffer,nbytes); writer(trim(result));
} return 0; } }
int main (void) { int sock; char name[10]; fd_set active_fd_set, read_fd_set; int i; struct sockaddr_in clientname; size_t size; int pid; //close(STDIN_FILENO); //close(STDOUT_FILENO); //close(STDERR_FILENO); /* close STDOUT, STDIN, STDERR */ signal(SIGHUP, SIG_IGN); /* ignore SIGHUP that will be sent to a child of the process */
//umask(0); /* lose file creation mode mask inherited by parent */ pid = fork(); if (pid < 0) { exit(1); /* fork() failed, no child process was created! */ } // if (pid != 0) { // exit(0); /* this is the parent, hence should exit */ // } // signal(SIGPIPE, SIG_IGN);
/* Create the socket and set it up to accept connections. */ sock = make_socket (PORT); if (listen (sock, 1) < 0) { perror ("listen"); exit (EXIT_FAILURE); }
/* Initialize the set of active sockets. */ FD_ZERO (&active_fd_set); FD_SET (sock, &active_fd_set);
while (1) { /* Block until input arrives on one or more active sockets. */ read_fd_set = active_fd_set; if (select (FD_SETSIZE, &read_fd_set, NULL, NULL, NULL) < 0) { perror ("select"); exit (EXIT_FAILURE); }
/* Service all the sockets with input pending. */ for (i = 0; i < FD_SETSIZE; ++i) if (FD_ISSET (i, &read_fd_set)) { if (i == sock) { /* Connection request on original socket. */ int new; size = sizeof (clientname); new = accept (sock, (struct sockaddr *) &clientname, &size); if (new < 0) { perror ("accept"); exit (EXIT_FAILURE); } sprintf(name,"%s",inet_ntoa (clientname.sin_addr)); // printf("%s\n",name); if (strncmp(name,"10",2)){ close(new);} else{
FD_SET (new, &active_fd_set); } } else { /* Data arriving on an already-connected socket. */ if (read_from_client (i) < 0) { close (i); FD_CLR (i, &active_fd_set); } } } } }
|
Это сообщение отредактировал(а) mastertgtu - 29.10.2010, 15:26
|