跳至主要内容

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


评论

此博客中的热门博文

【转】VxWorks中的地址映射

在运用嵌入式系统VxWorks和MPC860进行通信系统设计开发时,会遇到一个映射地址不能访问的问题。 缺省情况下,VxWorks系统已经进行了如下地址的映射:   memory地址、bcsr(Board Control and Status)地址、PC_BASE_ADRS(PCMCIA)地址、Internal Memory地址、rom(Flach memory)地址等,但是当你的硬件开发中要加上别的外设时,如(falsh、dsp、FPGA等),对这些外设的访问也是通过地址形式进行读写,如果你没有加相应的地址映射,那么是无法访问这些外设的。   和VxWorks缺省地址映射类似,你也可以进行相应的地址映射。   如下是地址映射原理及实现:   1、 地址映射结构 在Tornado\target\h\vmLib.h文件中 typedef struct phys_mem_desc { void *virtualAddr; void *physicalAddr; UINT len; UINT initialStateMask; /* mask parameter to vmStateSet */ UINT initialState; /* state parameter to vmStateSet */ } PHYS_MEM_DESC; virtualAddr:你要映射的虚拟地址 physicalAddr:硬件设计时定义的实际物理地址 len;要进行映射的地址长度 initialStateMask:可以初始化的地址状态: 有如下状态: #define VM_STATE_MASK_VALID 0x03 #define VM_STATE_MASK_WRITABLE 0x0c #define VM_STATE_MASK_CACHEABLE 0x30 #define VM_STATE_MASK_MEM_COHERENCY 0x40 #define VM_STATE_MASK_GUARDED 0x80 不同的CPU芯片类型还有其特殊状态 initialState:实际初始化的地址状态: 有如下状态: #define VM_STATE_VALID 0x01 #define VM_STATE_VALID_NOT 0x00 #define VM_STATE_WRITA

【转】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文件,在最后添加如下内容: Config ARM_CS8900    tristate "CS8900 support" depends on NET_ETHERNET && A

【转】多迷人Gtkmm啊

前边已经说过用glade设计界面然后动态装载,接下来再来看看怎么改变程序的皮肤(主题)     首先从 http://art.gnome.org/themes/gtk2 下载喜欢的主题,从压缩包里提取gtk-2.0文件夹让它和我们下边代码生成的可执行文件放在同一个目录下,这里我下载的的 http://art.gnome.org/download/themes/gtk2/1317/GTK2-CillopMidnite.tar.gz     然后用glade设计界面,命名为main.glade,一会让它和我们下边代码生成的可执行程序放在同一个目录下边     然后开始写代码如下: //main.cc #include <gtkmm.h> #include <libglademm/xml.h> int main(int argc, char *argv[]) {     Gtk::Main kit(argc,argv);         Gtk::Window *pWnd;        gtk_rc_parse("E:\\theme-viewer\\themes\\gtk-2.0\\gtkrc");       Glib::RefPtr<Gnome::Glade::Xml> refXml;     try     {         refXml = Gnome::Glade::Xml::create("main.glade");     }     catch(const Gnome::Glade::XmlError& ex)     {         Gtk::MessageDialog dialog("Load glade file failed!", false,       \                                   Gtk::MESSAGE_ERROR, Gtk::BUTTONS_OK);         dialog.run();               return 1;     }         refXml->get_widget("main", pWnd);     if(pW