fruitfly

a technical blog

Windows Filesharing Library for iOS

with 34 comments

It has been some time since my first post when I promised to myself to be more active in the iOS dev community. Well, the problem was a project that was not very fun…

For a private project I needed a simple iOS library to access a windows network share and download a file. No such free library seemed available and I did not want to dig into Samba and port it to iOS. So, I considered to write such a library myself. I was under the impression it could not be sohard to get the basic functionality I needed done.

Guess what: I was wrong! This protocol is a complete mess and over the years (new windows releases… new required features) turned into a badly documented monster (actually did not work very much time on the library, but whenever i resumed work, I got frustrated soon and called it a day). For details and if you want to get into it, here are my main resources:

I can’t possibly describe you what amazing work the SAMBA guys are doing… Only a quote from the above SMB protocol introduction might give you a feeling:

Remember earlier when we talked about SMB messages as if we were dissecting some strange, new species of multi-legged critter? Well, we’ve moved beyond Entomology, Invertebrate Zoology, Taxonomy, and such. We’re now studying really complex stuff like Sociology, Psychology, and Numismatics, and we get to put the little critters into Skinner boxes and see how they react to various stimuli. It’s important research, and there are all sorts of interesting things to discover.

Enough with that. At the moment the library is tested on iOS 4.2 and with a Windows XP share. It is able to connect to a protected share and list and browse files and directories. I don’t think I will have the power to maintain the library what is beyond my exact needs (I will have to add reading and writing of files as final features), but you are free to do what you like with it. As it its a quiet small library, it might help someone get started with the protocol.

You can find the librarie’s source over at github under the BSD license: tango

Below, two screenshots from the test-app:

Written by 38leinad

March 23, 2011 at 6:09 pm

Posted in ios, library/framework

Tagged with , , ,

34 Responses

Subscribe to comments with RSS.

  1. Hi Daniel,

    I was trying some iOS development around Samba and found your uploads on GitHub. Nice work!

    However, i came across some errors while trying to connect a network drive. And it gave me this “_tango_evaluate_response_header(): NT_STATUS Level is 0003 with value 006d.”

    Appreciate if you could help me out here.

    Cheers
    Ben

    Ben

    July 29, 2011 at 5:42 am

    • Hi Ben,
      Can you send the me the whole log? Maybe create a bug-report on github.
      I assume you are using windows vista or 7? I only used/tested it with windows xp and have heard that the authetication method that i implemented is no longer support in widows vista/7. I still need to have a look at this…

      cheers,
      daniel

      38leinad

      July 30, 2011 at 12:13 pm

      • Hi, I also hit this error (_tango_evaluate_response_header(): NT_STATUS Level is 0003 with value 000d) on linux server with samba service turned on. The weird thing is that I only got this error for some directories while others just work. I open a bug report on github. Can you take a look ?

        qiulang

        November 21, 2011 at 8:04 am

      • I also encountered the same issue when trying with linux samba. Any way to handle this?

        alex

        December 19, 2011 at 6:13 am

  2. Thank you for posting the source. I can’t say I will do anything with it, but it is a nice start.

    Matt

    August 3, 2011 at 10:56 pm

  3. Hi Daniel,

    sorry for the late reply. was involved with something else….T_T

    anyway, I have attempted connecting to share drive on Win7/WinServer2008R2 and some NAS. In the end, I got the same error result like below..

    on the console:
    2011-08-06 16:59:24.623 tango[1673:207] APPLICATION STARTED
    2011-08-06 17:00:08.036 tango[1673:207] tango connecting
    _tango_send(): Bytes to send (including nbt-header!): 51
    _tango_send(): Sent 51 bytes.
    _tango_send_and_receive(): Received 4 bytes.
    _tango_send_and_receive(): Received 117 bytes.
    _tango_NEGOTIATE_PROTOCOL(): Received response
    _tango_NEGOTIATE_PROTOCOL(): Server Capabilities 0x1e3fc
    _tango_NEGOTIATE_PROTOCOL(): – CAP_LARGE_READX
    _tango_send(): Bytes to send (including nbt-header!): 106
    _tango_send(): Sent 106 bytes.
    _tango_send_and_receive(): Received 4 bytes.
    _tango_send_and_receive(): Received 35 bytes.
    _tango_SESSION_SETUP(): Received response
    _tango_evaluate_response_header(): NT_STATUS Level is 0003 with value 006d.

    on the iphone:
    it shows “Error. unable to login”

    I will create a bug report on github as well.

    Thanks for your help, Daniel.
    Regards
    Ben

    Benyong Shi

    August 6, 2011 at 9:15 am

  4. Hey Daniel,

    it works well with Buffalo Ministation NAS. The only issue unicode, which is not supported. Are there any plans to fix this?

    Thanks for sharing this!
    Best regards,
    Sergej

    Sergej

    September 9, 2011 at 8:31 am

  5. […] found this library but it has a problem with Windows 7 (and I suppose with Windows server 2008): it doesn’t […]

  6. Hi I am trying to fix the problem I found with no avail. And I have another question now. I find that you set the information level for TRANS2_FIND_FIRST2 to 0x01, but when I used wireshark to capture the value other samba client set, they seem to set to 0x0104 most of time, I tried to set to that value (I think it repents SMB_FIND_FILE_BOTH_DIRECTORY_INFO, right), but don’t know how to parse the response then. So why do you set information value to 0x01 ?

    BTW, thanks for paying attention to my comments

    qiulang

    November 23, 2011 at 7:18 am

    • Hi qiulang,
      I also was trying to learn the basics of the library and thus just used whatever seemed simplest and worked at the time. So, no special intention to use 0x01.
      On page 82 of this document (http://networks.cs.ucdavis.edu/~zhuk/research/sans/documents/CIFS-TR-1p00_FINAL.pdf), you can find the list of possible values and yours actually is another alternative; you only seem to get back the found files; the 0x1 i tried also hands back some creation/modif dates of the file.

      You can try and modify it in https://github.com/38leinaD/tango/blob/master/Classes/Tango_Find2First.c
      Maybe yours is a simpler/better approach.

      I also see that in line 407 i put in a padding that I am not sure why. Because it is nowhere described in the spec but was required for my windows share at the time. maybe this messes something up for you.

      this protocol is a mess… 😦

      cheers,
      daniel

      38leinad

      November 23, 2011 at 3:07 pm

      • So did you get a chance to debug why your codes only work for the directory name whose length is odd number ?

        qiulang

        November 23, 2011 at 3:30 pm

  7. Hi I found a way to fix the problem although I don’t know the root cause of the problem!

    So as I found current codes only work for the folder whose name’s length is odd number. Then I further found that it may be because for even number folder name, the data size we set in _tango_TRANS2_FIND_FIRST2 is incorrect!

    Say I have a folder called “abc” and another folder called “abcd”, when I turn on the macro VERY_VERBOSE I find that they both print out log “Bytes to send (including nbt-header!): 89”, apparently for “abcd” folder the bytes to send should be 90. So I add this line to correct data size and codes work now!

    if (data_offset % 2 == 0) _tango_smb_setDataSize(smb, data_offset+1);

    But I examine your code carefully and can’t find out why we need to +1 for even number.

    So any idea?

    qiulang

    November 24, 2011 at 8:44 am

  8. Hi I have another question about extended_security in _tango_NEGOTIATE_PROTOCOL.c

    So you write:

    connection->extended_security = capabilities & CAP_EXTENDED_SECURITY;

    if (connection->extended_security) {
    error(“_tango_NEGOTIATE_PROTOCOL(): Extended Security unsupported.\n”);
    _tango_set_error(connection, kTangoErrorUnsupported, “Unsupported security mode”);
    goto bailout;
    }

    But doesn’t this check actually do the opposite work it was supposed to do ?
    if (connection->extended_security) …

    And the weird thing is that if I use iOS device (iphone or ipad) to access my samba folder, I always get capabilities 0x0080f3fd, which means extended_security is not supported. But if I used my mac to access the same samba folder, I will get 0x8080f3fd (as it should)

    qiulang

    November 24, 2011 at 10:48 am

    • Hi qiulang,
      Ok, the wording here is a little bit confusing: the tango library does not support extended security. So, the functions returns an error because it does not support the capability of the server (the server wants to use some extended security features; the server supports it; we don’t).

      The reason why the server is sending different responses for the call via tango and a regular samba client might be that in the negotiate-protocol request we already provide a dialect we support. i guess the server adjusts its capabilities based on what “capabilities” the client has.

      extended security is a topic i am planing to integrate in the library for some time now because vistas and windows 7 only use these security mechanisms; but did not find the motivation yet. You can find more information on this in here:
      http://ubiqx.org/cifs/SMB.html
      section 2.8.6

      I hope this help.

      Cheers,
      Daniel

      38leinad

      November 25, 2011 at 8:27 pm

  9. Hi I have trying to add unicode support to your library and find that it is too difficult. Sorry.

    Now I am studying jcifs codes to see if I can port it to object c.

    qiulang

    November 30, 2011 at 10:52 am

  10. Hi I am using GCDAsyncSocket and have already implemented the function of browsing files and directories (with unicode support).

    It is not that hard actually. So my next step is to add reading and writing of files support (as you did before :))

    qiulang

    December 9, 2011 at 9:34 am

  11. Hi, qiulang, did you have any success with implementing reading and writing of files on Windows network shares using objective-c?

    Dmitry Nadezhdin

    January 13, 2012 at 2:43 am

    • Yes, with the help of wireshark I have managed to do that.

      qiulang

      January 13, 2012 at 6:33 am

      • Great, this is the exact functionality I’m looking for.
        I am currently developing a small in-house iPad productivity application at a small Australian company. The app would copy job data to servicemen’s iPads daily.

        Would you be interested in sharing a part of your resulting code for a fee? Please e-mail me: dmitry AT puratap DOT com or reply here.

        Dmitry Nadezhdin

        January 13, 2012 at 7:19 am

  12. Hi I’m trying to use your library to connect a network share drive in iPad. Is it ok to use your library to creat apps and submit to apple for review. I afraid they will reject it due to private API. Please advise. Thanks so much :).

    Brian Chung

    January 13, 2012 at 12:14 pm

    • Have anyone tried it on window vista? When I login my share folder. The system returns
      _tango_SESSION_SETUP(): Received response
      _tango_evaluate_response_header(): NT_STATUS Level is 0003 with value 006d.

      Does anyone know how to solve it >_<?

      Brian Chung

      January 13, 2012 at 2:50 pm

      • HI Brian,
        I myself only tested the library on Win XP. I heard from other people that there are problems in Win Vista and 7 because the authentication-mechanism has changed.

        Currently, I have no time to get this fixed though.

        Regarding your initial question: there should be no reason why apple should reject your app because of this libarry. There are no private APIs used.

        Cheers,
        Daniel

        38leinad

        January 13, 2012 at 11:58 pm

      • Hi Daniel,

        Thanks for your reply. Then I have to modify the authentication-mechanism part to support Window Vista/ 7. Is it diffiuclt to do that? I have no knowledge on smb protocol haha….. By the way, do you have any website for reference? Thanks again!

        Brian Chung

        January 14, 2012 at 2:57 am

  13. I also used no extended security negotiation, NTLM v1 and I was able to login window xp and window 7. So I suspected tango implementation for authentication-mechanism has some problem.

    qiulang

    January 15, 2012 at 2:22 pm

    • Hi, quilang,

      Was the Windows 7 machine you connected to in a network domain or in a workgroup?

      Dmitry Nadezhdin

      January 15, 2012 at 10:01 pm

      • Actually I do not know. I just need its IP address to connect to (of course it is inside our corporate intranet).

        qiulang

        January 16, 2012 at 3:26 am

    • Hi qiulang,

      So it is possible to skip the extended security negotiation and connect to Window Vista / Window 7. Could you please tell me how to modify the tango library to do it? Many thanks!

      Brian Chung

      January 16, 2012 at 8:42 am

      • No I wrote my own impletation instead of modifying tango

        qiulang

        January 16, 2012 at 1:27 pm

  14. Any chance that this supports IPv6?

    Dick Visser

    January 16, 2012 at 9:38 pm

  15. Thanks for the library. I have one problem. I have code from GitHub. I tried the sample project. But its not showing me shared folders or files. This is my console log

    _tango_send(): Bytes to send (including nbt-header!): 51
    _tango_send(): Sent 51 bytes.
    _tango_send_and_receive(): Received 4 bytes.
    _tango_send_and_receive(): Received 111 bytes.
    _tango_NEGOTIATE_PROTOCOL(): Received response
    _tango_NEGOTIATE_PROTOCOL(): Server Capabilities 0xe3fd
    _tango_NEGOTIATE_PROTOCOL(): – CAP_LARGE_READX
    _tango_send(): Bytes to send (including nbt-header!): 107
    _tango_send(): Sent 107 bytes.
    _tango_send_and_receive(): Received 4 bytes.
    _tango_send_and_receive(): Received 88 bytes.
    _tango_SESSION_SETUP(): Received response
    _tango_SESSION_SETUP(): Only logged in anonymously.
    _tango_send(): Bytes to send (including nbt-header!): 64
    _tango_send(): Sent 64 bytes.
    _tango_send_and_receive(): Received 4 bytes.
    _tango_send_and_receive(): Received 49 bytes.
    _tango_TREE_CONNECT_ANDX(): Received response

    After this nothing happens. Can you tell me whats going wrong? Thanks

    appDev

    March 27, 2012 at 5:03 am

  16. […] tango This entry was posted in memo by zuk. Bookmark the permalink. […]

  17. Hi Ben,
    Nice work done. Can u please help me on how to get access of shared folder on mac system ?

    Paresh Vaghela

    November 17, 2012 at 8:24 am

    • I have the sam e problem. Has anyone managed to get the authentification done for windows 7?

      Adrian

      July 22, 2013 at 10:31 am

  18. For a more up to date library that support both SMB and SMB2 contact talw@visualitynq.com

    Tal Widerman

    July 9, 2015 at 11:59 am


Leave a reply to Paresh Vaghela Cancel reply