跳至主要内容

【转】smb协议栈使用示例

/*
 * * uncdownload.c
 * *
 * * Utility for downloading files from SMB shares using libsmbclient
 * *
 * * Copyright(C) 2006 Sophos Plc, Oxford, England.
 * *
 * * This program is free software; you can redistribute it and/or modify it under the terms of the
 * * GNU General Public License Version 2 as published by the Free Software Foundation.
 * *
 * * This program is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY; without
 * * even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * * See the GNU General Public License for more details.
 * *
 * * You should have received a copy of the GNU General Public License along with this program; if not,
 * * write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
 * *
 * */


#include <sys/types.h>
#include <sys/time.h>
#include <stdio.h>
#include <unistd.h>
#include <string.h>
#include <stdlib.h>
#include <errno.h>
#include <strings.h>

#include <libsmbclient.h>

/*
 * *
 * */

static const char *def_workgroup = "WORKGROUP";

/*
 * * Global variables holding parsed command line options
 * */

static char *opt_workgroup;
static char *opt_username;
static char *opt_password;
static int opt_stdinpwd;
static int opt_stdoutdata = 1;
static char *opt_outfile;
static int opt_outfd;
static char *opt_downloadurl;

/*
 * * Parse command line options
 * */

static int parseopts(int argc, char *argv[])
{
    int arg;
    char *nonoption = NULL;
    char option = 0;
    int param = 0;

    for ( arg = 1; arg < argc; arg++ ) {
        if ( param ) {
            /* Previous arg was an option */
            param = 0;
            switch ( option ) {
                case 'w':
                    opt_workgroup = argv[arg];
                    break;
                case 'u':
                    opt_username = argv[arg];
                    break;
                case 'p':
                    opt_password = argv[arg];
                    if ( !strcmp(opt_password, "-") )
                        opt_stdinpwd = 1;
                    break;
                case 'o':
                    opt_outfile = argv[arg];
                    opt_stdoutdata = 0;
                    break;
                case 'd':
                    opt_outfd = atoi(argv[arg]);
                    opt_stdoutdata = 0;
                    break;
            }
        } else {
            /* Look for an option */
            if ( argv[arg][0] == '-' ) {
                option = argv[arg][1];
                switch ( option ) {
                    case 'w':
                    case 'u':
                    case 'p':
                    case 'o':
                    case 'd':
                        param = 1;
                        break;
                    default:
                        fprintf(stderr, "Unknown option: %c\n", option);
                }
            /* If not an option, store as a non-option argument */
            } else {
                if ( nonoption )
                    fprintf(stderr, "Only first non-option argument considered as download URL!");
                else
                    nonoption = argv[arg];
            }
        }
    }

    if ( param ) {
        fprintf(stderr, "No argument for -%c!\n", option);
        return -1;
    }

    if ( !nonoption ) {
        fprintf(stderr, "No UNC path given!\n");
        return -1;
    }

    opt_downloadurl = nonoption;

    return 0;
}

/*
 * * libsmbclient callback function
 * */

static void get_auth_data_fn(const char *pServer, const char *pShare, char *pWorkgroup, int maxLenWorkgroup, char *pUsername, int maxLenUsername, char *pPassword, int maxLenPassword )
{
    (void)pServer;
    (void)pShare;


    if ( opt_workgroup && opt_workgroup[0] != '\0' )
        strncpy(pWorkgroup, opt_workgroup, maxLenWorkgroup - 1);

    if ( opt_username && opt_username[0] != '\0' )
        strncpy(pUsername, opt_username, maxLenUsername - 1);

    if ( opt_password && opt_password[0] != '\0' )
        strncpy(pPassword, opt_password, maxLenPassword - 1);
}

/*
 * * Convert backslashes to slashes
 * */

static char *bstos(char *str)
{
    char *p = str;

    while ( *p ) {
        if ( *p == '\\' )
            *p = '/';
        p++;
    }

    return str;
}

/*
 * * Creates a nice file URL which libsmbclient understands
 * */

static char *mkurl(char *uncpath)
{
    unsigned int len = strlen(uncpath) + 1;
    char *url;
    int prepend = 0;

    if ( strncasecmp(uncpath, "smb:", 4) ) {
        len += 4;
        prepend = 1;
    }

    if ( !(url = malloc(len)) )
        return NULL;
    else
        *url = 0; /* So that strcat below is safe if not prepending */

    if ( prepend )
        strcpy(url, "smb:");

    strcat(url, uncpath);

    return bstos(url);
}

/*
 * * Null-terminate string on first newline
 * */

static char *ntonl(char *str)
{
    char *p = str;

    while ( *p ) {
        if ( *p == '\n' ) {
            *p = 0;
            break;
        }
        p++;
    }

    return str;
}

/*
 * @return codes:
 * 0 success
 * 1 failed to parse arguments
 * 2 failed mkurl
 * 3 failed to read password
 * 4 failed to open output file
 * 5 failed to open remote file (not permission or existence)
 * 6 failed reading or writing
 * 7 bad authentication
 * 8 no such file
 * 9 invalid argument from smbc_open - may mean missing directory component
 * */

int main(int argc, char * argv[])
{
    char *smbpath;
    char pwdbuf[128];
    int outfd;
    int smbdebug = 0;
    int smbfd;
    int ret;
    char buffer[2048];

    if ( parseopts(argc, argv) ) {
        fprintf(stderr,
"Usage:\n\
\t%s [OPTIONS] DOWNLOAD-URL\n\
Options:\n\
\t-w\tworkgroup (default: %s)\n\
\t-u\tusername\n\
\t-p\tpassword (if - then read from stdin)\n\
\t-o\toutput (if omitted then written to stdout)\n\
\t-d\tdescriptor (if omitted then written to stdout, overrides output file)\n"
,
             argv[0], def_workgroup);
        return 1;
    }

    if ( !(smbpath = mkurl(opt_downloadurl)) ) {
        fprintf(stderr, "Failed to create SMB URL!\n");
        return 2;
    }

    if ( opt_stdinpwd ) {
        if ( !fgets(pwdbuf, sizeof(pwdbuf), stdin) ) {
            fprintf(stderr, "Failed to read password from stdin!\n");
            return 3;
        }
        opt_password = ntonl(pwdbuf);
    }

    if ( opt_outfd ) {
        outfd = opt_outfd;
    } else if ( !opt_stdoutdata ) {
        if ( (outfd = open(opt_outfile, O_WRONLY | O_CREAT | O_TRUNC)) <= 0 ) {
            fprintf(stderr, "Failed to open output file! - %s\n", strerror(errno));
            return 4;
        }
    } else {
        outfd = STDOUT_FILENO;
    }

    smbc_init(get_auth_data_fn, smbdebug);

    if ( (smbfd = smbc_open(smbpath, O_RDONLY, 0)) < 0 ) {
        fprintf(stderr, "Failed to open remote location! - %s(%d)\n", strerror(errno),errno);
        switch(errno)
        {
            case EACCES:
                return 7; /* Bad permissions */
            case ENOENT: /* No such file or directory */
            case EISDIR: /* Is a directory */
            case ENODEV: /* No such share */
            case ENOTDIR: /* Perhaps a directory in the path is in fact a file? */
                return 8; /* Implies: No such file */
            case EINVAL: /* May mean a directory in the path doesn't exist */
                return 9; /* Implies: No such file */
        }
        /* default */
        return 5;
    }

    /* Main loop */
    do {
        ret = smbc_read(smbfd, buffer, sizeof(buffer));
        if ( ret > 0 ) {
            ret = write(outfd, buffer, ret);
            if ( ret < 0 )
                break;
        }
    } while ( ret > 0 );

    if ( ret < 0 ) {
        fprintf(stderr, "Error reading or writting! - %s\n", strerror(errno));
        return 6;
    }

    smbc_close(smbfd);
    free(smbpath); /* Allocated with mkurl */

    return 0;
}


评论

此博客中的热门博文

【转】AMBA、AHB、APB总线简介

AMBA 简介 随着深亚微米工艺技术日益成熟,集成电路芯片的规模越来越大。数字IC从基于时序驱动的设计方法,发展到基于IP复用的设计方法,并在SOC设计中得到了广泛应用。在基于IP复用的SoC设计中,片上总线设计是最关键的问题。为此,业界出现了很多片上总线标准。其中,由ARM公司推出的AMBA片上总线受到了广大IP开发商和SoC系统集成者的青睐,已成为一种流行的工业标准片上结构。AMBA规范主要包括了AHB(Advanced High performance Bus)系统总线和APB(Advanced Peripheral Bus)外围总线。   AMBA 片上总线        AMBA 2.0 规范包括四个部分:AHB、ASB、APB和Test Methodology。AHB的相互连接采用了传统的带有主模块和从模块的共享总线,接口与互连功能分离,这对芯片上模块之间的互连具有重要意义。AMBA已不仅是一种总线,更是一种带有接口模块的互连体系。下面将简要介绍比较重要的AHB和APB总线。 基于 AMBA 的片上系统        一个典型的基于AMBA总线的系统框图如图3所示。        大多数挂在总线上的模块(包括处理器)只是单一属性的功能模块:主模块或者从模块。主模块是向从模块发出读写操作的模块,如CPU,DSP等;从模块是接受命令并做出反应的模块,如片上的RAM,AHB/APB 桥等。另外,还有一些模块同时具有两种属性,例如直接存储器存取(DMA)在被编程时是从模块,但在系统读传输数据时必须是主模块。如果总线上存在多个主模块,就需要仲裁器来决定如何控制各种主模块对总线的访问。虽然仲裁规范是AMBA总线规范中的一部分,但具体使用的算法由RTL设计工程师决定,其中两个最常用的算法是固定优先级算法和循环制算法。AHB总线上最多可以有16个主模块和任意多个从模块,如果主模块数目大于16,则需再加一层结构(具体参阅ARM公司推出的Multi-layer AHB规范)。APB 桥既是APB总线上唯一的主模块,也是AHB系统总线上的从模块。其主要功能是锁存来自AHB系统总...

【转】GPIO编程模拟I2C入门

ARM编程:ARM普通GPIO口线模拟I2C  请教个问题: 因为需要很多EEPROM进行点对点控制,所以我现在要用ARM的GPIO模拟I2C,管脚方向我设 置的是向外的。我用网上的RW24C08的万能程序修改了一下,先进行两根线的模拟,SDA6, SCL6,但是读出来的数不对。我做了一个简单的实验,模拟SDA6,SCL6输出方波,在示波 器上看到正确方波,也就是说,我的输出控制是没问题的。 哪位大哥能指点一下,是否在接收时管脚方向要设为向内?(不过IOPIN不管什么方向都可 以读出当前状态值的阿) 附修改的RW24C08()程序: #define  SomeNOP() delay(300); /**/ /* *********************************  RW24C08   **************************************** */ /**/ /* ----------------------------------------------------------------------------- ---  调用方式:void I2CInit(void)   函数说明:私有函数,I2C专用 ------------------------------------------------------------------------------- -- */ void  I2CInit( void ) ... {  IO0CLR  =  SCL6;      // 初始状态关闭总线  SomeNOP();  // 延时   I2CStop();  // 确保初始化,此时数据线是高电平 }   /**/ /* ---------------------------------------------------------------------------- ----  调用方式:void I2CSta...

【转】cs8900网卡的移植至基于linux2.6内核的s3c2410平台

cs8900网卡的移植至基于linux2.6内核的s3c2410平台(转) 2008-03-11 20:58 硬件环境:SBC-2410X开发板(CPU:S3C2410X) 内核版本:2.6.11.1 运行环境:Debian2.6.8 交叉编译环境:gcc-3.3.4-glibc-2.3.3 第一部分 网卡CS8900A驱动程序的移植 一、从网上将Linux内核源代码下载到本机上,并将其解压: #tar jxf linux-2.6.11.1.tar.bz2 二、打开内核顶层目录中的Makefile文件,这个文件中需要修改的内容包括以下两个方面。 (1)指定目标平台。 移植前:         ARCH?= $(SUBARCH) 移植后: ARCH            :=arm (2)指定交叉编译器。 移植前: CROSS_COMPILE ?= 移植后: CROSS_COMPILE   :=/opt/crosstool/arm-s3c2410-linux-gnu/gcc-3.3.4-glibc-2.3.3/bin/arm-s3c2410-linux-gnu- 注:这里假设编译器就放在本机的那个目录下。 三、添加驱动程序源代码,这涉及到以下几个方面。(1)、从网上下载了cs8900.c和cs8900.h两个针对2.6.7的内核的驱动程序源代码,将其放在drivers/net/arm/目录下面。 #cp cs8900.c ./drivers/net/arm/ #cp cs8900.h ./drivers/net/arm/ 并在cs8900_probe()函数中,memset (&priv,0,sizeof (cs8900_t));函数之后添加如下两条语句: __raw_writel(0x2211d110,S3C2410_BWSCON); __raw_writel(0x1f7c,S3C2410_BANKCON3); 注:其原因在"第二部分"解释。 (2)、修改drivers/net/arm/目录下的Kconfig文件,在最后添加如...