Chinaunix首页 | 论坛 | 博客
  • 博客访问: 1834773
  • 博文数量: 283
  • 博客积分: 10141
  • 博客等级: 上将
  • 技术积分: 2931
  • 用 户 组: 普通用户
  • 注册时间: 2005-12-21 14:33
文章分类

全部博文(283)

文章存档

2013年(2)

2012年(2)

2011年(17)

2010年(36)

2009年(17)

2008年(18)

2007年(66)

2006年(105)

2005年(20)

分类: C/C++

2006-05-11 20:39:43

匆忙写完了MPI版的,发现bug的xdjms说一声,多谢了。
环境:
Redhat 9.0
MPI 1.0
LSF or pbs
 
下一步的计划是写穷举的串行和并行程序。。。
 
傍晚时候测试了一下crypt()的效率,比我想像的好。不过根据师兄的建议,还有优化的余地,但这不是现在的主要问题。
 
/* ****************************************************
 * $ID: icymoon                                       *
 * $Date: 2006.05.10.                                 *
 * Just a practise                                    *
 * Complie: mpicc pcrack_dic.c -o pcrack_dic -lcrypt  *
 * ****************************************************/
#define _XOPEN_SOURCE
#include
#include
#include
#include
#include "mpi.h"
char sh_file[256];
char dic_file[256];
char shadow_line[256];
char dic_line[256];
char salt[12];
char crypt_passwd[35];
char username[32];
int flag = 0;
char *check;
FILE * sh_fp;
FILE * dic_fp;
void usage(char * programme)
{
        printf("Usage: %s       -s shadow_file -d dictionary_file\n", programme);
        exit(0);
}
void MPI_myexit(char * msg, int quit)
{
        MPI_Finalize();
        printf("%s\n", msg);
        exit(quit);
}
void master(int proc_id, int proc_num)
{
        printf("proc %d begin\n", proc_id);
        if((sh_fp = fopen(sh_file, "r")) == NULL)
                MPI_myexit("Can't open shadow_file!", 99);
        while(1)
        {
                memset(shadow_line, '\0', sizeof(shadow_line));
                if(fgets(shadow_line, 256, sh_fp) == NULL)
                {
                        flag = 0;
                        MPI_Bcast(&flag, 1, MPI_INT, 0, MPI_COMM_WORLD);
                        break;
                }
                else
                {
                        char * tmp = strchr(shadow_line,'$');
                        char * tmp1;
                        char * tmp2;
                        flag = 1;
                        MPI_Bcast(&flag, 1, MPI_INT, 0, MPI_COMM_WORLD);
                        memset(salt, '\0', sizeof(salt));
                        memset(username, '\0', sizeof(username));
                        if(tmp == NULL)
                        {
                                printf("Error shadow format: %s\n", shadow_line);
                                continue;
                        }
                        tmp1 = strchr(shadow_line, ':');
                        tmp2 = strchr(tmp1+1, ':');
                        *tmp1 = '\0';
                        *tmp2 = '\0';
                        strncpy(username, shadow_line, sizeof(username));
                        username[strlen(username)] = '\0';
                        strncpy(salt, tmp, 12);
                        strncpy(crypt_passwd, tmp, 34);
                        crypt_passwd[34] = '\0';
                        MPI_Bcast(&username, 32, MPI_CHAR, 0, MPI_COMM_WORLD);
                        MPI_Bcast(&salt, 12, MPI_CHAR, 0, MPI_COMM_WORLD);
                        MPI_Bcast(&crypt_passwd, 35, MPI_CHAR, 0, MPI_COMM_WORLD);
                }
        }
        fclose(sh_fp);
}
void slave(int proc_id, int proc_num)
{
        int line_num = 0;
        if((dic_fp = fopen(dic_file, "r")) == NULL)
                MPI_myexit("Can't open dictionary_file!", 98);
        else
        {
                fclose(dic_fp);
                while(1)
                {
                        dic_fp = fopen(dic_file, "r");
                        MPI_Bcast(&flag, 1, MPI_INT, 0, MPI_COMM_WORLD);
                        if(flag == 0)
                                return;
                        MPI_Bcast(&username, 32, MPI_CHAR, 0, MPI_COMM_WORLD);
                        MPI_Bcast(&salt, 12, MPI_CHAR, 0, MPI_COMM_WORLD);
                        MPI_Bcast(&crypt_passwd, 35, MPI_CHAR, 0, MPI_COMM_WORLD);
                        memset(dic_line, '\0', sizeof(dic_line));
                        while(1)
                        {
                                if(fgets(dic_line, 256, dic_fp) ==  NULL)
                                {
                                        printf("line: %d\n, proc: %d,break\n", line_num, proc_id);
                                        break;
                                }
                                else
                                {
                                        line_num ++;
                                        if((line_num % (proc_num - 1) ==  proc_id) || (line_num % (proc_num - 1) ==  0))
                                        {
                                        //      check = (char *)malloc(35*sizeof(char));
                                                dic_line[strlen(dic_line)-1] = '\0';
                                                check = (char *)crypt(dic_line, salt);
                                                if(!strcmp(crypt_passwd, check))
                                                {
                                                        printf("proc %d, line: %d :%s      %s\n", proc_id, line_num, username, dic_line);
                                        //              free(check);
                                                        break;
                                                }
                                        //      free(check);
                                        }
                                        else
                                        {
                                                memset(dic_line, '\0', sizeof(dic_line));
                                                continue;
                                        }
                                        memset(dic_line, '\0', sizeof(dic_line));
                                }
                        }
                        fclose(dic_fp);
                }
        }
}
int main(int argc, char * argv[])
{
        int opt;
        int myid, numprocs;
        MPI_Status status;
        MPI_Request request;
        //check optiongs
        if((argc != 5) && (argc != 2))
        {
                usage(argv[0]);
        }
        while((opt = getopt(argc, argv, "s:d:h")) != -1)
        {
                switch(opt)
                {
                        case 's':
                                memset(sh_file, '\0', sizeof(sh_file));
                                strncpy(sh_file, optarg, sizeof(sh_file));
                                sh_file[sizeof(sh_file) - 1] = '\0';
                                break;
                        case 'd':
                                memset(dic_file, '\0', sizeof(sh_file));
                                strncpy(dic_file, optarg, sizeof(optarg));
                                dic_file[sizeof(dic_file) - 1] = '\0';
                                break;
                        case 'h':
                        default:
                                usage(argv[0]);
                                break;
                }
        }
        //MPI Initail, get the rank id and number of processes
        MPI_Init(&argc,&argv);
        MPI_Comm_size(MPI_COMM_WORLD,&numprocs);
        MPI_Comm_rank(MPI_COMM_WORLD,&myid);
        if(myid == 0)
        {
                master(myid, numprocs);
        }
        else
        {
                slave(myid, numprocs);
        }
        MPI_Finalize();
        return 1;
}
阅读(2465) | 评论(0) | 转发(0) |
给主人留下些什么吧!~~