Jay Freeman (’saurik’, http://www.saurik.com) has brought a number of welcome improvements to the iPhone develoment scene. Too many to try to enumerate here, and this isn’t meant as a tribute to him anyway. However, he has done a lot of great work on figuring out the horrible mess that is gethostbyname on the iPhone. My current solution to this, which does the trick provided you’re writing an obj-c app, is written about here. Saurik’s explanation goes to get length to detail exactly what’s going wrong, and records the various attempts to fix this along the way (including some of my attempts). It’s a very interesting read, particularly after having spent as much time fighting with it as I have. Thank you Saurik for your great work!
So I’ve been doing some development for the iPhone–most notably iNewsgroup, the newsgroup client for the iPhone. One of the problems plaguing iPhone development was weird dns issues. Things worked fine if you stayed in their obj-c frameworks, but try to do normal bsd socket things like gethostbyname, and the phone acted really weird.
NerveGas goes into the problems a bit here: here.
Well, iNewsGroup needed DNS. And since the backend was originally tin, it used gethostbyname.
The fix up until now (shudder) was that if a connection attempt failed due to dns resolution failure, we tried to get a http request on the hostname. This was horrible for a number of rather obvious reasons–most notably slow, some newsgroup servers don’t listen on port 80, and is entirely hackish and ugly.
After some poking around, I’ve come up with the following function that fixes the problem:
#include <dns_sd.h> //dns request bool resolveHostname( char * hostname ) { NSString * name = [NSString stringWithFormat: @"%s", hostname ]; NSLog( @"Resolving for %@", name ); DNSServiceErrorType error; DNSServiceRef service; error = DNSServiceQueryRecord( &service, 0 /*no flags*/, 0 /*all network interfaces */, hostname, kDNSServiceType_A, /* we want the ipv4 addy */ kDNSServiceClass_IN, /* internet */ 0, /* no callback */ NULL /* no context */ ); if ( error == kDNSServiceErr_NoError )//good so far... { int dns_sd_fd = DNSServiceRefSockFD(service); int nfds = dns_sd_fd + 1; fd_set readfds; struct timeval tv; FD_ZERO(&readfds); FD_SET(dns_sd_fd, &readfds); tv.tv_sec = DNS_RESOLV_TIMEOUT; tv.tv_usec = 0; bool ret = false; int result = select(nfds, &readfds, (fd_set*)NULL, (fd_set*)NULL, &tv); if (result > 0) { if (FD_ISSET(dns_sd_fd, &readfds)) { //remove this if you want to compile in c, not obj-c NSLog( @"resolved %s to %@", hostname, [ [ NSHost hostWithName: name ] address ] ); ret = true; } } //clean up and return accordingly DNSServiceRefDeallocate( service ); return ret; } //clean up.... DNSServiceRefDeallocate( service ); NSLog( @"dns error: %d", error ); return false; }
This code will compile with minor changes (remove NSLog and the NSHost code) in both c and obj-c.
iNewsGroup now just calls this before doing and normal posix networking calls, and since the A record has already been found, the calls work just fine.
Not perfect, but far and away a better solution.
Man, it feels good to be back :). Sure missed my room, campus… and those noodles of cheesy goodness, which I’m consuming currently. For christmas I got a number of things that made my room a lot nicer:
a new lamp, I brought my keyboard, a vhs/dvd player and a roomba (550). My roommate and myself are still in the process of trying to figure out what to name it. :). Along with the roomba I got a book on common ways to hack it, which should prove rather interesting and fun.
When I got back, I found myself setting up all my stuff again, as usual, unfortunately my TV is a bit of a pain because I can’t stack anything on top of it, due to its design. Faced now with 3 devices that all needed to be together, I needed to come up with a solution.
Here’s the result, my “Media Center”:
Basically we took some cardboard boxes from costco that I used to hold my shit and made layers/shelves out of them. Works rather well, and give the devices space for their vents, and handles the cord clutter rather well.
Maybe we’ll paint it or something eventually.
Anyway, first class tomorrow… at 3pm :). \o/. Better get some sleep…
So today I get home from viv’s, and find myself faced with a cardboard box full of old computer parts and a note written from my dad telling me to take them to the curb unless I have reason to keep any of it.
This is pretty much the end of what has been a long cleaning process, getting rid of all the computer parts that have accumulated over the years–mostly in my closet. Most of the parts went to building computers to give to family, donated to Africa, etc. It’s hard to describe why I find myself so attached to these computer parts. It’s easy to agree that they’re being put to better use–and in particular that I had pretty much no use for them at all.
Maybe it’s my bad memory, but I can’t help but feel that every item I’m throwing away is a memory that’s forever lost. A dream, a lesson–these were all parts of my childhood, and… it’s difficult to see them go. That graphics card that was a big upgrade and allowed me to play starcraft without lagging, that computer I built into a shoebox as a wireless/ethernet bridge, the infared project, all the many many projects that I worked on. My Duron that I overclocked to wazoo–until one late night while working on it, I fell asleep and accidentally knocked a pillow into it, and woke up to a fried computer. Even those ancient ancient computers that I never even saw run, but used to take apart with fascination, just to see what could be learned. They represent the magic that computers were to me…. and now they’re just a bunch of plastic parts… sitting on a curb, soon to be crushed into oblivion.
Time to move on I suppose, but… here’s to you my old hardware! You served me well, and may you be recycled and used in something glorious and linux-based. :-).
Finals are over, huzzah! \o/ Break has been good so far, mostly been sleeping and working on random projects (see other posts for those updates :)).
Christmas was good, it was really nice to have Mark and his kids over, it sure brightened things up.
I’m going to be working from the 2nd->11th, that should be fun. Miss those guys. :-).
Yep, I suppose I’m not much of a chatterbox today, but wanted to get some non-dev post going.
So after much development, iNewsGroup 1.0.1 has been released.
See iNewsGroup for more information regarding the release, but most notably POSTING support, and a much improved UI. The app sure is coming along.
Should be in ste’s repo sometime tonight…. :-).
Also, it’s fun to check http://blog.psmxy.org/pkg-stats/ and see how many people have downloaded iNewsGroup. :-).
Thanks to Matt for helping me test, and everyone else who submitted issues, etc.
Today in IRC GCDS mentioned wanting unrar on the iphone… so I went ahead and got the sources and ported it.
Here it is: http://dtztech.com/iphone/unrar
For now you’ll have to scp it to your phone, no idea how/if I should distribute it.
Anyway, happy archiving ![]()
Released iNewsGroup 0.0.3
More information available here: iNewsGroup Google Code
Still need to implement posting, fix the dns issues.. and.. well a complete up-to-date list of issues here: issues
Ste said he would add it to his repo sometime tomorrow.. can’t wait! ![]()
Additionally, core might help me figure out those dns issues. \o/
Well it’s been fun, and I can’t wait until this project gets all the features it needs and some gloss. ![]()
So I’m working on a project, which for now is called “inewsgroup”. It’s a newsgroup client for the iphone, as the name might suggest. Project site:
I started out wanting to just use an existing command-line newsgroup client to handle the networking, and just write a gui–but that proved much more difficult than it was worth since I couldn’t find an application that would integrate nicely into a frontend other than the one it ships with. I thought I had what I wanted with ‘tin’, but it became quickly apparent that it doesn’t make sense to use some external application to do such calls. So after checking the relevant rfc977(the original NNTP rfc), I thought “hey I can do this”. So I telnet into my local news server here at uiuc, and… immediately I get stuck. WTF is the command to authenticate myself??
Unforunately it doesn’t seem that the original rfc included such a command. x.x. So some more poking around turned up a bunch of rfcs, most notably: rfc2980. This rfc contains common extensions to the nntp protocol, and the list of extensions is at least 3x as long as the original rfc is. :-(. So interested in getting a client out quickly, I decided against implementing nntp myself, and began my search for means of getting nntp support without having to code for every possible extension. I eventually ended up with tin again, being now somewhat familiar with it’s internals, and tried something I hadn’t thought of before–just use their code. Turns out doing so wasn’t nearly as hard as I’d thought it’d be, just a matter of tweaking relavant makefiles, creating the appropriate library archive (and ripping out the main :-)) and linking that into my application. Doing so make my application go from 20KB->900+KB, so perhaps some code cleaning is in order, although I suspect that’s what it takes to implement all the extensions…. (but there is still a lot of UI code in tin that could do to be removed).
It /does/ seem as tin was designed so that this would be possible, but I couldn’t find documentation anywhere indicating such.
Currently it can login to the server, and print the groups out. Sounds trivial, but going from here should be a bit easier.
So anyway, the project is starting, and its future looks promising. Development is coming along nicely and so far it looks like it might be possible. No idea when the first release will be, but I’ll keep posting important updates to my blog.
Posting this from my iPhone, just about to go to bed. It is possible thanks to wphone… I’m rather pleased with it, and wordpress in general. There are some things I love to fight with to get to work, but a blog isn’t one of them. Congrats to all involved. ![]()