[CDRIVER-108] getaddrinfo not working Created: 18/Jan/12  Updated: 19/Oct/16  Resolved: 20/Jan/12

Status: Closed
Project: C Driver
Component/s: None
Affects Version/s: 0.4
Fix Version/s: 0.5

Type: Bug Priority: Major - P3
Reporter: natalie po Assignee: Kyle Banker
Resolution: Done Votes: 0
Labels: mongolab
Remaining Estimate: Not Specified
Time Spent: Not Specified
Original Estimate: Not Specified
Environment:

C, Cocoa/iOS



 Description   

Hi there,

I'd like to use the mongo-c-driver in a new iOS project, and I'm following the tutorial (http://api.mongodb.org/c/current/tutorial.html), but I can't successfully connect to a mongolab.com database of my creation. I can connect to the mongolab db at the command line ("mongo ds029807.mongolab.com:29807/starwatch_1", for example), so I know it exists. I've tried several variations of ip and port declarations such as the following (in objective-C) and it compiles and runs successfully, but it does not connect to my mongolab db:

mongo conn[1];

NSString * ip = @"ds029807.mongolab.com";
int port = 1234; // dummy port number for now

int status = mongo_connect(conn, (char*)ip, port);

if( status != MONGO_OK )

{ ... }

... more code below

The conn->err value is always MONGO_CONN_FAIL, even if I append the ":1234" port number at the end of the ip string, or other ways.

Have you connected to a mongolab collection with this library before? Could you give me guidance on how the syntax would work, or if I'm barking up the wrong tree? Googling the topic didn't shed any light on the subject. I've worked successfully with perl and python mongo libraries with mongolab, and I'm hoping I can do the same in C/Objective-C.

Thank you!
Natalie



 Comments   
Comment by Kyle Banker [ 20/Jan/12 ]

You're welcome!

Comment by natalie po [ 19/Jan/12 ]

Great! I've confirmed that this works for me in a simple C script. Now I just need to work on linking the right files in my iOS project. Thanks very much, Kyle!

Comment by Kyle Banker [ 19/Jan/12 ]

Yes. Here's a basic test script:

https://gist.github.com/55ed6c59d9deb3a7f49b

Here's how I compile the library:
scons --use-platform=LINUX

Here's how I compile the test script:
gcc --std=c99 test.c libmongoc.a

Comment by natalie po [ 19/Jan/12 ]

Thanks, Kyle, for taking a look. I'm still not having luck connecting to my mongolab db. As a sanity check, is this along the same lines of the syntax you're using?

mongo conn[1];
int port_num = 5678;
char * ip = "subdomain1234.mongolab.com";

int status = mongo_connect(conn, ip, port_num);

My connection result still results in failure (-1).

Thanks again for helping with this!

Comment by Kyle Banker [ 19/Jan/12 ]

I've just pushed a small fix, and I tested it with a MongoLab database, and it worked! Please let me know if you have any problems with it.

Comment by Kyle Banker [ 19/Jan/12 ]

Thanks, Natalie. Will commit a fix later this afternoon.

Comment by natalie po [ 18/Jan/12 ]

Thanks, Kyle, for the fast response!

I've got the code from the latest release (commit: 879b67c40a6d2ee284c7a99b13ded8b1b6535109), and I ran scons to rebuild the files. My project now includes the net.* files that include comments about the Linux version, and I removed the other net.* files from my project (since they borked on methods being declared twice, as expected).

I'm stepping through the following call in my project's debugger:

int status = mongo_connect(conn, "ds029807.mongolab.com", 29807);

and here's my rough stack trace of where it's failing (adding arrows for hopefully easier indentations and reading):

mongo.c - mongo_connect():
>> if (mongo_socket_connect(conn, host, port) != MONGO_OK)
>>>> //net.c - mongo_socket_connect():
>>>>>> // it creates a socket using mongo_create_socket(conn) just fine
>>>>>> // then it initializes the 'sa' struct
>>>>>> if (connect (conn->sock, (struct sockaddr*)&sa, addressSize) == -1)
>>>>>> ^^^ This is where it's failing. The port value is correct here, but the sa.sin_addr.s_addr value doesnt match my host string, but I'd expect it to be shifted like the port was, so it may be okay, or it might be the resolving host name issue that you mentioned.

Thanks again for taking a close look at this!
Natalie

Comment by Kyle Banker [ 18/Jan/12 ]

I need to make a new release, but you're probably running into an issue resolving the host name. Please checkout from head (i.e., the latest commit). Then compile like so:

scons --use-platform=LINUX

You'll then have the libraries you need. Do no append the port number to the host name.

Generated at Wed Feb 07 21:08:29 UTC 2024 using Jira 9.7.1#970001-sha1:2222b88b221c4928ef0de3161136cc90c8356a66.