跳至主要内容

【转】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;
}


评论

此博客中的热门博文

【转】嵌入式开发中使用DDD进行调试

 在嵌入式程序开发过程中,程序员要进行大量的调试,以此验证程序的正确性,修改潜在的错误。调试器对于程序员来说是不可或缺的必备工具。在Linux环境中,有很多调试工具和调试辅助工具,例如GDB、XXGDB、RHIDE、XWPE、GVD和DDD等。其中,DDD是命令行调试器的图形前端,除了一般的程序调试功能以外,还具有交互式图形数据显示的功能。它在嵌入式应用开发中也十分出色。本文主要讲述DDD(Data Display Debugger)的使用方法。      1.认识DDD     GNU DDD是命令行调试程序,如GDB、DBX、WDB、Ladebug、JDB、XDB、Perl Debugger或Python Debugger的可视化图形前端。它特有的图形数据显示功能(Graphical Data Display)可以把数据结构按照图形的方式显示出来。DDD最初源于1990年Andreas Zeller编写的VSL结构化语言,后来经过一些程序员的努力,演化成今天的模样。DDD的功能非常强大,可以调试用C\C++、Ada、 Fortran、Pascal、Modula-2和Modula-3编写的程序;可以超文本方式浏览源代码;能够进行断点设置、回溯调试和历史纪录编辑;具有程序在终端运行的仿真窗口,并在远程主机上进行调试的能力;图形数据显示功能(Graphical Data Display)是创建该调试器的初衷之一,能够显示各种数据结构之间的关系,并将数据结构以图形化形式显示;具有GDB/DBX/XDB的命令行界面,包括完全的文本编辑、历史纪录、搜寻引擎。     DDD是开源软件,用户可以去 http://www.cs.tubs.de/softech/ddd/ 下载.rpm格式的DDD源码文件。      图1显示的是DDD的主窗口。它主要由选单栏、工具条、数据窗口、源文件窗口、机器码窗口、控制台和命令工具窗口等几部分组成。其中,数据窗口用于观察复杂的数据结构,在删除数据之后,显示仍然有效;源文件窗口显示源代码、断点和当前执行到达的位置,选择该窗口中的"Display"项,可以显示任意表达式的值;机...

【转】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文件,在最后添加如...

【转】U-boot代码分析

/* * S3C44B0 CPU启动代码。 * 蔡军生 2005/07/17 * */ /* * 包含配置文件。 */ #include <config.h> /* 添加版本说明. 2005/07/16 */ #include <version.h> /* * 中断向量表. */ /* × 声明标量是全局函数,CPU加电启动后,就从这里执行代码。 */ .globl _start _start:        b       reset                   /* 跳到复位中断开始位置。 */         add        pc, pc, #0x0c000000         add        pc, pc, #0x0c000000         add        pc, pc, #0x0c000000         add        pc, pc, #0x0c000000         add        pc, pc, #0x0c000000     ...