C.5 A Simple SMB URL Parser

Listing C.1 provides a simple, and not entirely robust, SMB URL parser. A better parser would consume a much larger portion of the time-space continuum than is available for example code. This one is a good place to start.

Listing C.1a A simple SMB URL parser: SMB_URL.h
 /* Typedefs.  */ typedef struct   {   uchar *ntdomain;   uchar *user;   uchar *server;   uchar *port;   uchar *share;   uchar *path;   uchar *context;   } smb_url; /* Function prototypes.  */ smb_url *smb_urlParse( char *src, smb_url *url );   /* ---------------------------------------------------- **    * Parse an SMB URL string into an smb_url structure.    * The function returns NULL on error.    * ---------------------------------------------------- **    */ void smb_urlContent( smb_url *url );   /* ---------------------------------------------------- **    * Dump the contents of an smb_url structure,    * representing a parsed SMB URL string.    * ---------------------------------------------------- **    */ /* ------------------------------------------------------ */ 
Listing C.1b A simple SMB URL parser: SMB_URL.c
 #include <stdio.h> #include <string.h> #include "smb_url.h" smb_url *smb_urlParse( char *src, smb_url *url )   /* ---------------------------------------------------- **    * Parse an SMB URL string into an smb_url structure.    *    * This is a very, very simplistic URL parser... just    * enough for demonstration purposes.    * It does not handle the full syntax of SMB URLs.    * It only handles absolute URLs, and does not do    * enough error checking.  You can certainly do better,    * and superior examples can be found on on the web.    *    * The function returns NULL on error.    * ---------------------------------------------------- **    */   {   int    pos;   uchar *p;   /* Clear the smb_url structure first. */   (void)memset( url, 0, sizeof( smb_url ) );   /* Check for a correct prefix. */   pos = 0;   if( 0 == strncasecmp( "smb://", src, 6 ) )     pos = 6;   else     if( 0 == strncasecmp( "cifs://", src, 7 ) )       pos = 7;     else       return( NULL );   /* Check for an empty URL ("smb://"). */   if( ' 
 #include <stdio.h> #include <string.h> #include "smb_url.h" smb_url *smb_urlParse( char *src, smb_url *url ) /* ---------------------------------------------------- ** * Parse an SMB URL string into an smb_url structure. * * This is a very, very simplistic URL parser... just * enough for demonstration purposes. * It does not handle the full syntax of SMB URLs. * It only handles absolute URLs, and does not do * enough error checking. You can certainly do better, * and superior examples can be found on on the web. * * The function returns NULL on error. * ---------------------------------------------------- ** */ { int pos; uchar *p; /* Clear the smb_url structure first. */ (void) memset ( url, 0, sizeof( smb_url ) ); /* Check for a correct prefix. */ pos = 0; if( 0 == strncasecmp( "smb://", src, 6 ) ) pos = 6; else if( 0 == strncasecmp( "cifs://", src, 7 ) ) pos = 7; else return( NULL ); /* Check for an empty URL ("smb://"). */ if( '\0' == src[pos] ) return( url ); /* Copy the original string so that we can carve it up. */ src = strdup( &src[pos] ); /* Separate the server, share, path, and context * components . */ url->server = src; /* Look for context. */ p = strrchr ( src, '?' ); if( NULL != p ) { *p = '\0'; url->context = ++p; } /* Share part next . */ p = strchr( src, '/' ); if( NULL != p ) { *p = '\0'; url->share = ++p; /* path part. */ p = strchr( p, '/' ); if( NULL != p ) { *p = '\0'; url->path = ++p; } } /* Look for the ntdomain & username subfields * in the server string (the Authority field). */ p = strchr( url->server, '@' ); if( NULL != p ) { *p = '\0'; url->user = url->server; url->server = ++p; /* Split the user field into ntdomain;user */ p = strchr( url->user, ';' ); if( NULL != p ) { *p = '\0'; url->ntdomain = url->user; url->user = ++p; } } /* Look for a port number in the server string. */ p = strchr ( url->server, ':' ); if( NULL != p ) { *p = '\0'; url->port = ++p; } return( url ); } /* smb_urlParse */ void smb_urlContent( smb_url *url ) /* ---------------------------------------------------- ** * Dump the contents of an smb_url structure, * representing a parsed SMB URL string. * ---------------------------------------------------- ** */ { if( url->ntdomain ) (void)printf( "ntdomain: %s\n", url->ntdomain ); if( url->user ) (void)printf( " user: %s\n", url->user ); if( url->server ) (void)printf( " server: %s\n", url->server ); if( url->port ) (void)printf( " port: %s\n", url->port ); if( url->share ) (void)printf( " share: %s\n", url->share ); if( url->path ) (void)printf( " path: %s\n", url->path ); if( url->context ) (void)printf( " context: %s\n", url->context ); } /* smb_urlContent */ /* ------------------------------------------------------ */ 
' == src[pos] ) return( url ); /* Copy the original string so that we can carve it up. */ src = strdup( &src[pos] ); /* Separate the server, share, path, and context * components. */ url->server = src; /* Look for context. */ p = strrchr( src, '?' ); if( NULL != p ) { *p = '
 #include <stdio.h> #include <string.h> #include "smb_url.h" smb_url *smb_urlParse( char *src, smb_url *url ) /* ---------------------------------------------------- ** * Parse an SMB URL string into an smb_url structure. * * This is a very, very simplistic URL parser... just * enough for demonstration purposes. * It does not handle the full syntax of SMB URLs. * It only handles absolute URLs, and does not do * enough error checking. You can certainly do better, * and superior examples can be found on on the web. * * The function returns NULL on error. * ---------------------------------------------------- ** */ { int pos; uchar *p; /* Clear the smb_url structure first. */ (void) memset ( url, 0, sizeof( smb_url ) ); /* Check for a correct prefix. */ pos = 0; if( 0 == strncasecmp( "smb://", src, 6 ) ) pos = 6; else if( 0 == strncasecmp( "cifs://", src, 7 ) ) pos = 7; else return( NULL ); /* Check for an empty URL ("smb://"). */ if( '\0' == src[pos] ) return( url ); /* Copy the original string so that we can carve it up. */ src = strdup( &src[pos] ); /* Separate the server, share, path, and context * components . */ url->server = src; /* Look for context. */ p = strrchr ( src, '?' ); if( NULL != p ) { *p = '\0'; url->context = ++p; } /* Share part next . */ p = strchr( src, '/' ); if( NULL != p ) { *p = '\0'; url->share = ++p; /* path part. */ p = strchr( p, '/' ); if( NULL != p ) { *p = '\0'; url->path = ++p; } } /* Look for the ntdomain & username subfields * in the server string (the Authority field). */ p = strchr( url->server, '@' ); if( NULL != p ) { *p = '\0'; url->user = url->server; url->server = ++p; /* Split the user field into ntdomain;user */ p = strchr( url->user, ';' ); if( NULL != p ) { *p = '\0'; url->ntdomain = url->user; url->user = ++p; } } /* Look for a port number in the server string. */ p = strchr ( url->server, ':' ); if( NULL != p ) { *p = '\0'; url->port = ++p; } return( url ); } /* smb_urlParse */ void smb_urlContent( smb_url *url ) /* ---------------------------------------------------- ** * Dump the contents of an smb_url structure, * representing a parsed SMB URL string. * ---------------------------------------------------- ** */ { if( url->ntdomain ) (void)printf( "ntdomain: %s\n", url->ntdomain ); if( url->user ) (void)printf( " user: %s\n", url->user ); if( url->server ) (void)printf( " server: %s\n", url->server ); if( url->port ) (void)printf( " port: %s\n", url->port ); if( url->share ) (void)printf( " share: %s\n", url->share ); if( url->path ) (void)printf( " path: %s\n", url->path ); if( url->context ) (void)printf( " context: %s\n", url->context ); } /* smb_urlContent */ /* ------------------------------------------------------ */ 
'; url->context = ++p; } /* Share part next. */ p = strchr( src, '/' ); if( NULL != p ) { *p = '
 #include <stdio.h> #include <string.h> #include "smb_url.h" smb_url *smb_urlParse( char *src, smb_url *url ) /* ---------------------------------------------------- ** * Parse an SMB URL string into an smb_url structure. * * This is a very, very simplistic URL parser... just * enough for demonstration purposes. * It does not handle the full syntax of SMB URLs. * It only handles absolute URLs, and does not do * enough error checking. You can certainly do better, * and superior examples can be found on on the web. * * The function returns NULL on error. * ---------------------------------------------------- ** */ { int pos; uchar *p; /* Clear the smb_url structure first. */ (void) memset ( url, 0, sizeof( smb_url ) ); /* Check for a correct prefix. */ pos = 0; if( 0 == strncasecmp( "smb://", src, 6 ) ) pos = 6; else if( 0 == strncasecmp( "cifs://", src, 7 ) ) pos = 7; else return( NULL ); /* Check for an empty URL ("smb://"). */ if( '\0' == src[pos] ) return( url ); /* Copy the original string so that we can carve it up. */ src = strdup( &src[pos] ); /* Separate the server, share, path, and context * components . */ url->server = src; /* Look for context. */ p = strrchr ( src, '?' ); if( NULL != p ) { *p = '\0'; url->context = ++p; } /* Share part next . */ p = strchr( src, '/' ); if( NULL != p ) { *p = '\0'; url->share = ++p; /* path part. */ p = strchr( p, '/' ); if( NULL != p ) { *p = '\0'; url->path = ++p; } } /* Look for the ntdomain & username subfields * in the server string (the Authority field). */ p = strchr( url->server, '@' ); if( NULL != p ) { *p = '\0'; url->user = url->server; url->server = ++p; /* Split the user field into ntdomain;user */ p = strchr( url->user, ';' ); if( NULL != p ) { *p = '\0'; url->ntdomain = url->user; url->user = ++p; } } /* Look for a port number in the server string. */ p = strchr ( url->server, ':' ); if( NULL != p ) { *p = '\0'; url->port = ++p; } return( url ); } /* smb_urlParse */ void smb_urlContent( smb_url *url ) /* ---------------------------------------------------- ** * Dump the contents of an smb_url structure, * representing a parsed SMB URL string. * ---------------------------------------------------- ** */ { if( url->ntdomain ) (void)printf( "ntdomain: %s\n", url->ntdomain ); if( url->user ) (void)printf( " user: %s\n", url->user ); if( url->server ) (void)printf( " server: %s\n", url->server ); if( url->port ) (void)printf( " port: %s\n", url->port ); if( url->share ) (void)printf( " share: %s\n", url->share ); if( url->path ) (void)printf( " path: %s\n", url->path ); if( url->context ) (void)printf( " context: %s\n", url->context ); } /* smb_urlContent */ /* ------------------------------------------------------ */ 
'; url->share = ++p; /* path part. */ p = strchr( p, '/' ); if( NULL != p ) { *p = '
 #include <stdio.h> #include <string.h> #include "smb_url.h" smb_url *smb_urlParse( char *src, smb_url *url ) /* ---------------------------------------------------- ** * Parse an SMB URL string into an smb_url structure. * * This is a very, very simplistic URL parser... just * enough for demonstration purposes. * It does not handle the full syntax of SMB URLs. * It only handles absolute URLs, and does not do * enough error checking. You can certainly do better, * and superior examples can be found on on the web. * * The function returns NULL on error. * ---------------------------------------------------- ** */ { int pos; uchar *p; /* Clear the smb_url structure first. */ (void) memset ( url, 0, sizeof( smb_url ) ); /* Check for a correct prefix. */ pos = 0; if( 0 == strncasecmp( "smb://", src, 6 ) ) pos = 6; else if( 0 == strncasecmp( "cifs://", src, 7 ) ) pos = 7; else return( NULL ); /* Check for an empty URL ("smb://"). */ if( '\0' == src[pos] ) return( url ); /* Copy the original string so that we can carve it up. */ src = strdup( &src[pos] ); /* Separate the server, share, path, and context * components . */ url->server = src; /* Look for context. */ p = strrchr ( src, '?' ); if( NULL != p ) { *p = '\0'; url->context = ++p; } /* Share part next . */ p = strchr( src, '/' ); if( NULL != p ) { *p = '\0'; url->share = ++p; /* path part. */ p = strchr( p, '/' ); if( NULL != p ) { *p = '\0'; url->path = ++p; } } /* Look for the ntdomain & username subfields * in the server string (the Authority field). */ p = strchr( url->server, '@' ); if( NULL != p ) { *p = '\0'; url->user = url->server; url->server = ++p; /* Split the user field into ntdomain;user */ p = strchr( url->user, ';' ); if( NULL != p ) { *p = '\0'; url->ntdomain = url->user; url->user = ++p; } } /* Look for a port number in the server string. */ p = strchr ( url->server, ':' ); if( NULL != p ) { *p = '\0'; url->port = ++p; } return( url ); } /* smb_urlParse */ void smb_urlContent( smb_url *url ) /* ---------------------------------------------------- ** * Dump the contents of an smb_url structure, * representing a parsed SMB URL string. * ---------------------------------------------------- ** */ { if( url->ntdomain ) (void)printf( "ntdomain: %s\n", url->ntdomain ); if( url->user ) (void)printf( " user: %s\n", url->user ); if( url->server ) (void)printf( " server: %s\n", url->server ); if( url->port ) (void)printf( " port: %s\n", url->port ); if( url->share ) (void)printf( " share: %s\n", url->share ); if( url->path ) (void)printf( " path: %s\n", url->path ); if( url->context ) (void)printf( " context: %s\n", url->context ); } /* smb_urlContent */ /* ------------------------------------------------------ */ 
'; url->path = ++p; } } /* Look for the ntdomain & username subfields * in the server string (the Authority field). */ p = strchr( url->server, '@' ); if( NULL != p ) { *p = '
 #include <stdio.h> #include <string.h> #include "smb_url.h" smb_url *smb_urlParse( char *src, smb_url *url ) /* ---------------------------------------------------- ** * Parse an SMB URL string into an smb_url structure. * * This is a very, very simplistic URL parser... just * enough for demonstration purposes. * It does not handle the full syntax of SMB URLs. * It only handles absolute URLs, and does not do * enough error checking. You can certainly do better, * and superior examples can be found on on the web. * * The function returns NULL on error. * ---------------------------------------------------- ** */ { int pos; uchar *p; /* Clear the smb_url structure first. */ (void) memset ( url, 0, sizeof( smb_url ) ); /* Check for a correct prefix. */ pos = 0; if( 0 == strncasecmp( "smb://", src, 6 ) ) pos = 6; else if( 0 == strncasecmp( "cifs://", src, 7 ) ) pos = 7; else return( NULL ); /* Check for an empty URL ("smb://"). */ if( '\0' == src[pos] ) return( url ); /* Copy the original string so that we can carve it up. */ src = strdup( &src[pos] ); /* Separate the server, share, path, and context * components . */ url->server = src; /* Look for context. */ p = strrchr ( src, '?' ); if( NULL != p ) { *p = '\0'; url->context = ++p; } /* Share part next . */ p = strchr( src, '/' ); if( NULL != p ) { *p = '\0'; url->share = ++p; /* path part. */ p = strchr( p, '/' ); if( NULL != p ) { *p = '\0'; url->path = ++p; } } /* Look for the ntdomain & username subfields * in the server string (the Authority field). */ p = strchr( url->server, '@' ); if( NULL != p ) { *p = '\0'; url->user = url->server; url->server = ++p; /* Split the user field into ntdomain;user */ p = strchr( url->user, ';' ); if( NULL != p ) { *p = '\0'; url->ntdomain = url->user; url->user = ++p; } } /* Look for a port number in the server string. */ p = strchr ( url->server, ':' ); if( NULL != p ) { *p = '\0'; url->port = ++p; } return( url ); } /* smb_urlParse */ void smb_urlContent( smb_url *url ) /* ---------------------------------------------------- ** * Dump the contents of an smb_url structure, * representing a parsed SMB URL string. * ---------------------------------------------------- ** */ { if( url->ntdomain ) (void)printf( "ntdomain: %s\n", url->ntdomain ); if( url->user ) (void)printf( " user: %s\n", url->user ); if( url->server ) (void)printf( " server: %s\n", url->server ); if( url->port ) (void)printf( " port: %s\n", url->port ); if( url->share ) (void)printf( " share: %s\n", url->share ); if( url->path ) (void)printf( " path: %s\n", url->path ); if( url->context ) (void)printf( " context: %s\n", url->context ); } /* smb_urlContent */ /* ------------------------------------------------------ */ 
'; url->user = url->server; url->server = ++p; /* Split the user field into ntdomain;user */ p = strchr( url->user, ';' ); if( NULL != p ) { *p = '
 #include <stdio.h> #include <string.h> #include "smb_url.h" smb_url *smb_urlParse( char *src, smb_url *url ) /* ---------------------------------------------------- ** * Parse an SMB URL string into an smb_url structure. * * This is a very, very simplistic URL parser... just * enough for demonstration purposes. * It does not handle the full syntax of SMB URLs. * It only handles absolute URLs, and does not do * enough error checking. You can certainly do better, * and superior examples can be found on on the web. * * The function returns NULL on error. * ---------------------------------------------------- ** */ { int pos; uchar *p; /* Clear the smb_url structure first. */ (void) memset ( url, 0, sizeof( smb_url ) ); /* Check for a correct prefix. */ pos = 0; if( 0 == strncasecmp( "smb://", src, 6 ) ) pos = 6; else if( 0 == strncasecmp( "cifs://", src, 7 ) ) pos = 7; else return( NULL ); /* Check for an empty URL ("smb://"). */ if( '\0' == src[pos] ) return( url ); /* Copy the original string so that we can carve it up. */ src = strdup( &src[pos] ); /* Separate the server, share, path, and context * components . */ url->server = src; /* Look for context. */ p = strrchr ( src, '?' ); if( NULL != p ) { *p = '\0'; url->context = ++p; } /* Share part next . */ p = strchr( src, '/' ); if( NULL != p ) { *p = '\0'; url->share = ++p; /* path part. */ p = strchr( p, '/' ); if( NULL != p ) { *p = '\0'; url->path = ++p; } } /* Look for the ntdomain & username subfields * in the server string (the Authority field). */ p = strchr( url->server, '@' ); if( NULL != p ) { *p = '\0'; url->user = url->server; url->server = ++p; /* Split the user field into ntdomain;user */ p = strchr( url->user, ';' ); if( NULL != p ) { *p = '\0'; url->ntdomain = url->user; url->user = ++p; } } /* Look for a port number in the server string. */ p = strchr ( url->server, ':' ); if( NULL != p ) { *p = '\0'; url->port = ++p; } return( url ); } /* smb_urlParse */ void smb_urlContent( smb_url *url ) /* ---------------------------------------------------- ** * Dump the contents of an smb_url structure, * representing a parsed SMB URL string. * ---------------------------------------------------- ** */ { if( url->ntdomain ) (void)printf( "ntdomain: %s\n", url->ntdomain ); if( url->user ) (void)printf( " user: %s\n", url->user ); if( url->server ) (void)printf( " server: %s\n", url->server ); if( url->port ) (void)printf( " port: %s\n", url->port ); if( url->share ) (void)printf( " share: %s\n", url->share ); if( url->path ) (void)printf( " path: %s\n", url->path ); if( url->context ) (void)printf( " context: %s\n", url->context ); } /* smb_urlContent */ /* ------------------------------------------------------ */ 
'; url->ntdomain = url->user; url->user = ++p; } } /* Look for a port number in the server string. */ p = strchr( url->server, ':' ); if( NULL != p ) { *p = '
 #include <stdio.h> #include <string.h> #include "smb_url.h" smb_url *smb_urlParse( char *src, smb_url *url ) /* ---------------------------------------------------- ** * Parse an SMB URL string into an smb_url structure. * * This is a very, very simplistic URL parser... just * enough for demonstration purposes. * It does not handle the full syntax of SMB URLs. * It only handles absolute URLs, and does not do * enough error checking. You can certainly do better, * and superior examples can be found on on the web. * * The function returns NULL on error. * ---------------------------------------------------- ** */ { int pos; uchar *p; /* Clear the smb_url structure first. */ (void) memset ( url, 0, sizeof( smb_url ) ); /* Check for a correct prefix. */ pos = 0; if( 0 == strncasecmp( "smb://", src, 6 ) ) pos = 6; else if( 0 == strncasecmp( "cifs://", src, 7 ) ) pos = 7; else return( NULL ); /* Check for an empty URL ("smb://"). */ if( '\0' == src[pos] ) return( url ); /* Copy the original string so that we can carve it up. */ src = strdup( &src[pos] ); /* Separate the server, share, path, and context * components . */ url->server = src; /* Look for context. */ p = strrchr ( src, '?' ); if( NULL != p ) { *p = '\0'; url->context = ++p; } /* Share part next . */ p = strchr( src, '/' ); if( NULL != p ) { *p = '\0'; url->share = ++p; /* path part. */ p = strchr( p, '/' ); if( NULL != p ) { *p = '\0'; url->path = ++p; } } /* Look for the ntdomain & username subfields * in the server string (the Authority field). */ p = strchr( url->server, '@' ); if( NULL != p ) { *p = '\0'; url->user = url->server; url->server = ++p; /* Split the user field into ntdomain;user */ p = strchr( url->user, ';' ); if( NULL != p ) { *p = '\0'; url->ntdomain = url->user; url->user = ++p; } } /* Look for a port number in the server string. */ p = strchr ( url->server, ':' ); if( NULL != p ) { *p = '\0'; url->port = ++p; } return( url ); } /* smb_urlParse */ void smb_urlContent( smb_url *url ) /* ---------------------------------------------------- ** * Dump the contents of an smb_url structure, * representing a parsed SMB URL string. * ---------------------------------------------------- ** */ { if( url->ntdomain ) (void)printf( "ntdomain: %s\n", url->ntdomain ); if( url->user ) (void)printf( " user: %s\n", url->user ); if( url->server ) (void)printf( " server: %s\n", url->server ); if( url->port ) (void)printf( " port: %s\n", url->port ); if( url->share ) (void)printf( " share: %s\n", url->share ); if( url->path ) (void)printf( " path: %s\n", url->path ); if( url->context ) (void)printf( " context: %s\n", url->context ); } /* smb_urlContent */ /* ------------------------------------------------------ */ 
'; url->port = ++p; } return( url ); } /* smb_urlParse */ void smb_urlContent( smb_url *url ) /* ---------------------------------------------------- ** * Dump the contents of an smb_url structure, * representing a parsed SMB URL string. * ---------------------------------------------------- ** */ { if( url->ntdomain ) (void)printf( "ntdomain: %s\n", url->ntdomain ); if( url->user ) (void)printf( " user: %s\n", url->user ); if( url->server ) (void)printf( " server: %s\n", url->server ); if( url->port ) (void)printf( " port: %s\n", url->port ); if( url->share ) (void)printf( " share: %s\n", url->share ); if( url->path ) (void)printf( " path: %s\n", url->path ); if( url->context ) (void)printf( " context: %s\n", url->context ); } /* smb_urlContent */ /* ------------------------------------------------------ */


Implementing CIFS. The Common Internet File System
Implementing CIFS: The Common Internet File System
ISBN: 013047116X
EAN: 2147483647
Year: 2002
Pages: 210

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net