[root@localhost tcpip]# cat ./news_sender.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <unistd.h>
#define TTL 60
#define BUF_SIZE 30

int main(int argc, char *argv[]){
        int send_sock;
        struct sockaddr_in mul_addr;
        int time_live=TTL;
        FILE *fp;
        char buf[BUF_SIZE];

        if(argc!=3){
                printf("Usage : %s <GroupIP> <PORT> \n", argv[0]);
                exit(1);
        }
        send_sock=socket(PF_INET, SOCK_DGRAM, 0);
        memset(&mul_addr, 0, sizeof(mul_addr));
        mul_addr.sin_family=AF_INET;
        mul_addr.sin_addr.s_addr=inet_addr(argv[1]);
        mul_addr.sin_port=htons(atoi(argv[2]));

        setsockopt(send_sock, IPPROTO_IP, IP_MULTICAST_TTL, (void*)&time_live, sizeof(time_live));
        if((fp=fopen("/tmp/tcpip/news.txt","r"))==NULL){
                perror("fopen");
        }

        while(!feof(fp))
        {
                fgets(buf, BUF_SIZE, fp);
                sendto(send_sock, buf, strlen(buf), 0, (struct sockaddr*)&mul_addr, sizeof(mul_addr));
                sleep(5);
        }
        fclose(fp);
        close(send_sock);
        return 0;
}

まず、ソースコードを見てみる

ヒントに演算子の優先順位だと書かれている。

if(fd=open("/home/mistake/password",O_RDONLY,0400) < 0)

このコードを見るとfdと0を比較するように見えるが、比較演算子(<)が=より優先順位が高いため、open関数の戻り値と0を比較した結果がfdに設定される。

なので、open関数でファイルが正常に開けたら0より大きい整数を返還するから、0と比較するとFalseなのでfdには0が入る。

 

次は

if(!(len=read(fd,pw_buf,PW_LEN) > 0))

このコードも上と同じ。

fdには0が入っているからstdinだ。つまり、ユーザーから入力される。

入力した値がpw_bufに入るからpw_bufは私のもの!

 

次は

scanf("%10s", pw_buf2);

pw_buf2の値を入力するので、pw_bufとpw_buf2両方私のもの!!

 

その後、xor(pw_buf2, 10);でpw_buf2を

#define XORKEY 1

void xor(char* s, int len){
        int i;
        for(i=0; i<len; i++){
                s[i] ^= XORKEY;
        }
}

こうやって1とxor演算をした値とpw_bufの値が同じであればOK!!

成功!!

[root@localhost tcpip]# cat ./echo_mpserver.c
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <arpa/inet.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <signal.h>

#define BUF_SIZE 1024
void error_handling(char *message);
void read_childproc(int sig);

int main(int argc, char *argv[]){
        int serv_sock, clnt_sock;
        struct sockaddr_in serv_addr, clnt_addr;
        socklen_t addr_size;
        int str_len, state;
        struct sigaction act;
        pid_t pid;
        char message[BUF_SIZE];

        if(argc!=2){
                printf("Usage : %s <PORT> \n",argv[0]);
                exit(1);
        }
        act.sa_handler=read_childproc;
        sigemptyset(&act.sa_mask);
        act.sa_flags=0;
        state=sigaction(SIGCHLD, &act, 0);

        serv_sock=socket(PF_INET, SOCK_STREAM, 0);
        memset(&serv_addr, 0, sizeof(serv_addr));
        serv_addr.sin_family=AF_INET;
        serv_addr.sin_addr.s_addr=htonl(INADDR_ANY);
        serv_addr.sin_port=htons(atoi(argv[1]));

        if(bind(serv_sock, (struct sockaddr*)&serv_addr, sizeof(serv_addr))==-1){
                error_handling("bind() error");
        }
        if(listen(serv_sock, 5)==-1){
                error_handling("listen() error");
        }

        while(1)
        {
                addr_size=sizeof(clnt_addr);
                clnt_sock=accept(serv_sock, (struct sockaddr*)&clnt_addr, &addr_size);
                if(clnt_sock==-1){
                        continue;
                }
                else{
                        puts("New Client connected...\n");
                }
                pid=fork();
                if(pid==-1){
                        close(clnt_sock);
                        continue;
                }
                if(pid==0){
                        close(serv_sock);
                        while((str_len=read(clnt_sock, message, BUF_SIZE))!=0)
                        {
                                write(clnt_sock, message, str_len);
                        }
                        close(clnt_sock);
                        return 0;
                }
                else{
                        close(clnt_sock);
                }
        }
        close(serv_sock);
        return 0;
}

void error_handling(char *message){
        fputs(message, stderr);
        fputc('\n', stderr);
        exit(1);
}

void read_childproc(int sig){
        pid_t pid;
        int status;
        pid=waitpid(-1, &status, WNOHANG);
        printf("removed child process : %d \n", pid);
        printf("child process send : %d \n", WEXITSTATUS(status));
}

#include <stdio.h>

#include <stdlib.h>

#include <string.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netdb.h>

void error_handling(char *message);

 

int main(int argc, char *argv[]){

int i;

struct hostent *host;

struct sockaddr_in addr addr;

if(argc!=2){

printf("Usage : %s <IP> \n", argv[0]);

exit(1);

}

 

memset(&addr, 0, sizeof(addr));

addr.sin_addr.s_addr=inet_addr(argv[1]);

 

host=gethostbyaddr((char*)&addr.sin_addr, 4, AF_INET);

if(!host){

error_handling("gethost…error");

}

 

printf("Official name : %s \n", host->h_name);

for(i=0; host->h_aliases[i]; i++)

{

printf("Aliases %d : %s \n", i+1, host->h_aliases[i]);

}

printf("Address type : %s \n", host->h_addrtype==AF_INET? "AF_INET" : "AF_INET6");

for(i=0; host->h_addr_list[i]; i++)

{

printf("IP addr %d : %s \n", i+1, inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));

}

return 0;

}

 

void error_handling(char *message){

fputs(message, stderr);

fputc('\n', stderr);

exit(1);

}

#include <stdio.h>

#include <stdlib.h>

#include <unistd.h>

#include <arpa/inet.h>

#include <netdb.h>

void error_handling(char *message);

 

int main(int argc, char *argv[]){

int i;

struct hostent *host;

if(argc!=2){

printf("Usage : %s <addr> \n", argv[0]);

exit(1);

}

 

host=gethostbyname(argv[1]);

if(!host){

error_handling("gethost…error")

}

 

printf("Official name : %s \n", host->h_name);

 

for(i=0; host->h_aliases[i]; i++)

{

printf("Aliases %d : %s \n", i+1, host->h_aliases[i]);

}

 

printf("Address type : %s \n", host->h_addrtype==AF_INET? "AF_INET" : "AF_INET6");

for(i=0; host->h_addr_list[i]; i++)

{

printf("IP addr %d : %s \n", i+1, inet_ntoa(*(struct in_addr*)host->h_addr_list[i]));

}

return 0;

}

 

void error_handling(char *message){

fputs(message, stderr);

fputc('\n', stderr);

exit(1);

}