- 论坛徽章:
- 5
|
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <unistd.h>
- #include <sys/stat.h>
- #include <sys/types.h>
- #include <sys/mount.h>
- #include <dirent.h>
- #include <fcntl.h>
- #include <sys/wait.h>
- #include <sys/vfs.h>
- #include <pthread.h>
- #include <netdb.h>
- #include <netinet/in.h>
- #include <sys/socket.h>
- #include <errno.h>
- #include <dirent.h>
- #include <iconv.h>
- //#define TEMP_OUT_SMBCLIENT "./pvrNeighborhood"
- #define TEMP_OUT_SMBCLIENT_UTF8 "./pvrNeighborhoodutf8"
- #define MAX_FILE_READ_BUFFER_SIZE 512
- #define SMB_MAX 20
- #define DEFAULT_NETWORK_NEIGHBORHOOD_MOUNTPOINT "/mnt"
- #define TEMP_NEIGHBORHOOD_SEARCH_FILE "/mnt/network"
- #define TEMP_NMBLOOKUP_FILE "/mnt/nmblookupfile"
- #define MAX_NAME_LEN (256*2)
- #define MAX_PER_PAGE 6
- static char computerName[256][MAX_NAME_LEN];
- static int number;
- static char *host_name[256];
- static char pcIpAddress[MAX_NAME_LEN];
- static int path_count;
- static char path[SMB_MAX][MAX_FILE_READ_BUFFER_SIZE];
- //static char utf8path[SMB_MAX][MAX_FILE_READ_BUFFER_SIZE];
- int code_convert(char *from_charset,char *to_charset,char *inbuf,int inlen,char *outbuf,int outlen)
- {
- iconv_t cd;
- int rc;
- char **pin = &inbuf;
- char **pout = &outbuf;
- cd = iconv_open(to_charset,from_charset);
- if (cd==0)
- {
- return -1;
- }
-
- memset(outbuf,0,outlen);
- if (iconv(cd,pin,&inlen,pout,&outlen)==-1)
- {
- return -1;
- }
-
- iconv_close(cd);
-
- return 0;
- }
- int u2g(char *inbuf,int inlen,char *outbuf,int outlen)
- {
- return code_convert("utf-8","gb2312",inbuf,inlen,outbuf,outlen);
- }
- int g2u(char *inbuf,size_t inlen,char *outbuf,size_t outlen)
- {
- return code_convert("gb2312","utf-8",inbuf,inlen,outbuf,outlen);
- }
- int smb_attache_mounted(const char *ipaddr, const char *sharename)
- {
- char service_path[256];
- char mountpoint[256];
- char stream[MAX_FILE_READ_BUFFER_SIZE];
- FILE *pfile;
- int result = 0;
- if (ipaddr == NULL || sharename == NULL)
- return 0;
- pfile = fopen("/proc/mounts", "r");
- if(!pfile)
- return 0;
- while (fgets(stream, MAX_FILE_READ_BUFFER_SIZE, pfile))
- {
- sscanf(stream, "%s %s ", service_path, mountpoint);
- if (strstr(service_path, ipaddr) != 0 && strstr(service_path, sharename) != 0)
- {
- result = 1;
- fprintf(stderr, "have smb mount\n");
- break;
- }
- }
- fclose(pfile);
- return result;
- }
- /** smb_mount
- * return : 0, mount failed, need password
- * -1: no shared file or dir
- >0: shared file or dir's count
- */
- int smb_mount(const char *ipaddr, const char *username, const char *password, char *ret_path[])
- {
- pid_t pid;
- char username_and_passwd[64];
- char stream[MAX_FILE_READ_BUFFER_SIZE];
- char connection_error_message[MAX_FILE_READ_BUFFER_SIZE];
- char srcPath[MAX_FILE_READ_BUFFER_SIZE];
- char data[MAX_FILE_READ_BUFFER_SIZE];
- char diskShareTypeChr[64];
- char shareName[64];
- char *pType;
- DIR *tmp_dir;
- int writefile = 0;
- int status = 0;
- int count = 0;
- FILE *pfile;
- memset(username_and_passwd, 0, 64);
- memset(stream, 0, MAX_FILE_READ_BUFFER_SIZE);
- memset(connection_error_message, 0, MAX_FILE_READ_BUFFER_SIZE);
- memset(path, 0, MAX_FILE_READ_BUFFER_SIZE*SMB_MAX);
- //remove(TEMP_OUT_SMBCLIENT);
- remove(TEMP_OUT_SMBCLIENT_UTF8);
- char smbclientCmd[1024] = { 0 };
- if(username == NULL)
- sprintf(smbclientCmd, "smbclient -L %s -N > %s", ipaddr, TEMP_OUT_SMBCLIENT_UTF8);
- else
- sprintf(smbclientCmd, "smbclient -L %s -U %s%%%s > %s", ipaddr, username, password, TEMP_OUT_SMBCLIENT_UTF8);
- printf("smbclientCmd : %s\n", smbclientCmd);
- system(smbclientCmd);
-
- FILE *utf8file = fopen(TEMP_OUT_SMBCLIENT_UTF8, "r");
- if (utf8file)
- {
- fseek(utf8file, 0, SEEK_END);
- int length = ftell(utf8file);
- if (length > 0)
- {
- fseek(utf8file, 0, SEEK_SET);
- char *bufferUtf8 = (char *)malloc(length + 1);
- memset(bufferUtf8, 0, length + 1);
- fread(bufferUtf8, length, 1, utf8file);
- char *bufferGbk = (char *)malloc(length * 2 + 1);
- u2g(bufferUtf8, length + 1, bufferGbk, length * 2 + 1);
- printf("%s\n\n%s\n\n", bufferUtf8, bufferGbk);
- //FILE *gbkfile = fopen(TEMP_OUT_SMBCLIENT, "w");
- FILE *gbkfile = fopen(TEMP_OUT_SMBCLIENT_UTF8, "w");
- if (gbkfile)
- {
- fwrite(bufferGbk, strlen(bufferGbk), 1, gbkfile);
- fclose(gbkfile);
- }
- free(bufferGbk);
- free(bufferUtf8);
- }
- else
- {
- printf("can not open pvrNeighborhood to write\n");
- return -1;
- }
- fclose(utf8file);
- }
- else
- {
- printf("can not open pvrNeighborhoodutf8 to read\n");
- return -1;
- }
-
- //pfile = fopen(TEMP_OUT_SMBCLIENT, "r");
- pfile = fopen(TEMP_OUT_SMBCLIENT_UTF8, "r");
- if(!pfile){
- printf("open tmpfile error\n");
- return -1;
- }
- sprintf(connection_error_message, "Connection to %s failed", ipaddr);
- while(count < SMB_MAX && fgets(stream, MAX_FILE_READ_BUFFER_SIZE, pfile)){
- fprintf(stderr,"smbclient: %s\n",stream);
- if (NULL != strstr(stream, connection_error_message)){
- fprintf(stderr, "liuqi????%s\n", connection_error_message);
- fclose(pfile);
- return -1;
- }
- if (NULL != strstr(stream, "NT_STATUS_ACCESS_DENIED")
- || NULL != strstr(stream, "NT_STATUS_LOGON_FAILURE") ){
- fclose(pfile);
- fprintf(stderr," NT_STATUS_LOGON_FAILURE smbclient: %s\n",stream);
- return -1;
- }
- if (NULL != strstr(stream, "NT_STATUS_LOGON_FAILURE")){
- fclose(pfile);
- return -1;
- }
- }
- fclose(pfile);
- //pfile = fopen(TEMP_OUT_SMBCLIENT, "r");
- pfile = fopen(TEMP_OUT_SMBCLIENT_UTF8, "r");
- if(NULL == pfile)
- {
- //fprintf(stderr, "open %s error\n", TEMP_OUT_SMBCLIENT);
- fprintf(stderr, "open %s error\n", TEMP_OUT_SMBCLIENT_UTF8);
- return -1;
- }
- while (fgets(stream, MAX_FILE_READ_BUFFER_SIZE, pfile)){
- if (0 == strncmp(stream, "\t", strlen("\t"))){
- memset(path[count], 0 , MAX_FILE_READ_BUFFER_SIZE);
- memset(diskShareTypeChr, 0, sizeof(diskShareTypeChr));
- strcpy(diskShareTypeChr, "Disk");
- strcat(diskShareTypeChr, " ");
- pType = strstr(stream, diskShareTypeChr);
- if (NULL == pType){
- continue;
- }
- strncpy(shareName, stream +1, pType - stream -2);
- shareName[pType- stream - 2] = '\0';
- char *pEndOfShareName = shareName + strlen(shareName) -1;
- while('\40' == *pEndOfShareName)
- pEndOfShareName --;
- *(pEndOfShareName + 1) = '\0';
- fprintf(stderr, " share name = |%s|\n", shareName);
- if( '我几年前写的代码,参考一下吧,肯定好用,而且产品化过,现在还在跑着呢 == shareName[strlen(shareName)-1])
- {
- continue;
- }
- strcpy(path[count], DEFAULT_NETWORK_NEIGHBORHOOD_MOUNTPOINT);
- strcat(path[count], "/");
- strcat(path[count], ipaddr);
- tmp_dir = opendir(path[count]);
- if(!tmp_dir)
- if (0 != mkdir(path[count], 0777)){
- fprintf(stderr, "liuqi<<<<Make %s error\n", path[count]);
- }
- strcat(path[count], "/");
- strcat(path[count], shareName);
- fprintf(stderr, "liuqi>>>>%s\n", path[count]);
- if(smb_attache_mounted(ipaddr, shareName)){
- char umount_cmd[64] = { 0 };
- sprintf(umount_cmd, "umount %s", path[count]);
- printf("umount_cmd:%s\n",umount_cmd);
- system(umount_cmd);
- }
-
-
- tmp_dir = opendir(path[count]);
- if(!tmp_dir){
- if (0 != mkdir(path[count], 0777)){
- fprintf(stderr, "liuqi####Make %s error\n", path[count]);
- }
- }
- closedir(tmp_dir);
- sprintf(srcPath, "\\\\%s\\%s", ipaddr, shareName);
- fprintf(stderr, "liuqi<%s>\n", path[count]);
- if(username == NULL){
- sprintf(data, "user=root,iocharset=cp936");
- fprintf(stderr,"liuqi#$%s#%s\n", srcPath, data);
- tmp_dir = opendir(path[count]);
- fprintf(stderr, "open dir %d\n", tmp_dir);
- if (mount(srcPath, path[count], "cifs", 0, data) != 0 )
- {
- fprintf(stderr, "Mount %s on %s error\n", srcPath, path[count]);
- if (errno == EACCES)
- {
- fprintf(stderr, "Mount Failed %s on %s \n", srcPath, path[count]);
- // rmdir(path[count]);
- fclose(pfile);
- return 0; //return need password
- }
- else
- {
- fprintf(stderr, "mount error\n");
- ;
- // rmdir(path[count]);
- }
- }
- else{
- ret_path[count]=path[count];
- fprintf(stderr, "(%s) |(%s)\n", ret_path[count] , path[count]);
- count++;
- }
- }
- else{
- sprintf(data, "user=%s,pass=%s,iocharset=cp936",
- username, password);
- fprintf(stderr, "|iiiii|%s|\n", data);
- if(mount(srcPath, path[count], "cifs", 0, data) != 0 )
- {
- fprintf(stderr, "Mount Failed %s on %s \n", srcPath, path[count]);
- if (errno == EACCES)
- {
- fprintf(stderr, stderr, "Mount %s on %s error\n", srcPath, path[count]);
- // rmdir(path[count]);
- fclose(pfile);
- return 0; //return need password
- }
- else{
- ;
- // rmdir(path[count]);
- }
- }
- else{
- ret_path[count] = path[count];
- fprintf(stderr, "(%s) |(%s)\n", ret_path[count] , path[count]);
- count++;
- }
- }
- }
- }
- fclose(pfile);
- path_count = count;
- fprintf(stderr, "#%s#\n", path[count]);
- return count == 0 ? 0 : -1; //if count == 0, return no shared file
- }
- extern int covert(char *desc, char *src, char *input, size_t ilen, char *output, size_t olen);
- int smb_get_path_count(int* a_count)
- {
- * a_count = path_count;
- return 0;
- }
- int smb_mount_path_get(int i,char *string_path)
- {
- char tmpStr[128];
- memset(tmpStr, 0, sizeof(char)*128);
- memcpy(tmpStr, path[i], strlen(path[i]));
- covert("utf-8", "gb18030", tmpStr, strlen(tmpStr), string_path, sizeof(char)*MAX_FILE_READ_BUFFER_SIZE-1);
- //string_path = utf8path[i];
- fprintf(stderr, "#%s#\n", path[i]);
- fprintf(stderr, "========%s=======\n", string_path);
- return 0;
- }
- int get_smb_tree_host(int num, char *host_name_init[])
- {
- int i = num * MAX_PER_PAGE;
- int count = 0;
- fprintf(stderr, "number = %d, host_name_init[count] = %s\n", number, host_name[i]);
- for( count =0 ; i < number && count < 6; i++)
- {
- fprintf(stderr, "host_name_init[count] = %s\n", host_name[i]);
- host_name_init[count] = host_name[i];
- count++;
- }
- fprintf(stderr, " liuqi >>>> count = %d\n", count);
- return count;
- }
- int smb_tree_init()
- {
- char tempFilename[] = TEMP_NEIGHBORHOOD_SEARCH_FILE;
- char sharedName[MAX_NAME_LEN];
- char path[MAX_NAME_LEN];
- int i = 0;
- memset(computerName, 0 , 256*MAX_NAME_LEN);
- memset(sharedName, 0, MAX_NAME_LEN);
- pid_t pid= fork();
- if (pid == 0)
- {
- int writeFile= open(tempFilename, O_CREAT | O_RDWR | O_TRUNC,
- S_IREAD | S_IWRITE);
- close(1);
- dup2(writeFile, 1);
- execlp("smbtree", "smbtree", "-S" , "-N","-s", "/home/work/samba/lib/smb.conf", NULL);
- perror("smbtree");
- return 0;
- }
- else if (pid >0)
- {
- int status;
- while (1){
- pid_t ptw = waitpid(pid, NULL, WNOHANG);
- if (ptw == pid)
- break;
- usleep(500000);
- }
- FILE *pfile;
- pfile = fopen(tempFilename, "r");
- char stream[MAX_NAME_LEN];
- if(!pfile){
- fprintf(stderr, "Can't mount network directory!\n");
- return;
- }
- while (fgets(stream, MAX_NAME_LEN, pfile)){
- char *c;
- if(strstr(stream, "smbtree: not found"))
- {
- return 0;
- }
- if( !strncmp(stream, "\t\\\\", strlen("\t\\\\"))){
- sscanf(stream, "\t%[^\40\t]", path);
- c = strrchr(path, '\\');
- if(NULL == c)
- continue;
- c++;
- memset(computerName[i], 0, MAX_NAME_LEN);
- strcpy(computerName[i], c);
- host_name[i] = computerName[i];
- fprintf(stderr, "liuqi>>|%s|\n", computerName[i]);
- i++;
- continue;
- }
- }
- fclose(pfile);
- }
- fprintf(stderr, "i = %d\n", i);
- number = i;
- return i;
- }
- char *get_host_ipaddr(char *name)
- {
- int writeFile;
- memset(pcIpAddress, 0, MAX_NAME_LEN);
- pid_t pid= fork();
- if (pid == 0)
- {
- writeFile = open(TEMP_NMBLOOKUP_FILE, O_CREAT | O_RDWR | O_TRUNC, S_IREAD | S_IWRITE);
- close(1);
- dup2(writeFile, 1);
- execlp("nmblookup", "nmblookup", name, NULL);
- return NULL;
- }
- else if (pid >0){
- while(1){
- pid_t ptw = waitpid(pid, NULL, WNOHANG);
- if (ptw == pid)
- break;
- usleep(50000);
- }
- FILE *pfile;
- pfile = fopen(TEMP_NMBLOOKUP_FILE, "r");
- char stream[MAX_NAME_LEN];
- if(!pfile){
- fprintf(stderr, "Can't mount network directory!\n");
- return NULL;
- }
- while (fgets(stream, MAX_NAME_LEN, pfile)){
- char *c;
- char temp[MAX_NAME_LEN];
- sscanf(stream, "%s %s", pcIpAddress, temp);
- c = strchr(pcIpAddress, '.');
- if(NULL == c)
- continue;
- c++;
- c = strchr(pcIpAddress, '.');
- if(NULL == c)
- continue;
- c++;
- c = strchr(pcIpAddress, '.');
- if(NULL == c)
- continue;
- if(pcIpAddress[0]>'0'&&pcIpAddress[0]<'9')
- return pcIpAddress;
- }
- fclose(pfile);
- }
- return pcIpAddress;
- }
- smb_umount()
- {
- int i = 0;
- for(i =0; i < path_count; i++){
- char umount_cmd[1024] = { 0 };
- fprintf(umount_cmd, "umount %s", path[i]);
- system(umount_cmd);
- }
- path_count = 0;
- }
- #if 0
- int main(int argc, char *argv[])
- {
- int ret=0;
- char *str[10];
- if(argc == 4)
- ret = smb_mount(argv[1], argv[2], argv[3], str);
- else
- ret = smb_mount(argv[1], NULL, NULL, str);
- int i=0;
- for(i=0; i < ret; i++)
- fprintf(stderr, "!%s!\n", str[i]);
- return 0;
- }
- #endif
复制代码 我几年前写的代码,参考一下吧,肯定好用,而且产品化过,现在还在跑着呢 |
|