Wednesday, February 16, 2011

Explode (Part One)

I have always been a fan of PHP's explode (quite opposed to a lot of other parts of that language) which made me want to recreate it in C. Thinking about the problem i came with a non-equivalent function that i liked better which i thought i should share.  It takes a string and a separator on which to split the string up on.  The function returns an array of strings that can (and has to be freed) with a single free. This is achieved by reorganizing the array in front of the resulting strings as one continous piece.
When thinking about it I decided to factor out multiple occurrences of the separator instead of inserting Null Strings, which may or may not be desired. One could improve this in several ways like several possible separator chars, a separator of more than one char, a limiting argument,...  of course, none of which i need right now. I am planning to write up a couple of other possible implementations over the course of time and discuss their respective merits and benchmark them.

char **explode(const char * source, char sep){
  int i =0, j =0;
  const char * runner=NULL, *oldrunner=NULL;
  int length = strlen(source)+1;
  int rlength=0;
  char ** resArr = NULL;
  char * finalArr = NULL;

  if (!(*source)){
    return NULL;
  if (! (resArr = malloc(sizeof(char *) * length)) )
    return NULL;
  memset(resArr,'\0', length*sizeof(char*));

  runner = oldrunner = source;

  while (*runner){
    if (*runner == sep){
      if ( (length = runner - oldrunner) ){
        resArr[i++] = (char *) oldrunner;
      oldrunner = runner+1;
  resArr[i++] = (char *) oldrunner;
  rlength += runner - oldrunner+1;

  /* this part would be optional if i malloc'ed and strcpy'ed
   * above but that way the user only needs to free once*/
  if  ( !(finalArr = realloc(resArr, sizeof(char *)*(i+1) + rlength)) ){
    free (resArr);
    return NULL;
  resArr = (char **) finalArr;
  finalArr = (char *) (resArr + ((i+1)*sizeof(char *)));
  j = i;

  for (i=0; i
    /* this of course only works because they are contigous */
    length = resArr[i+1] - resArr[i]-1;
    strncpy( finalArr, resArr[i], length);
    finalArr[length] = '\0';
    resArr[i] = finalArr;
    finalArr += length+1;
  strcpy(finalArr, resArr[j-1]);
  resArr[j-1] = finalArr;
  return resArr;

Tuesday, February 15, 2011

Shine a little light

It stood there all alone in the dark. Freezing cold as it was I could not help but emphasize with the poor thing. Of course it was a rather daunting atmosphere; a new moon had just begun its course and the stars were rather dim, the air was cold, the wind was stiff. So there it stood trying to hold its ground alone in the darkness. First I wasn't sure what the flickering light meant but then I came closer. The bulb was starting to show it's age, long hours of service had made it weary and it was gathering the last bit of life in it in a futile attempt to fulfill its duty. It was close to dying but still mustered the strength to try everything and perform honourably during its last fight.

Compassion filled me and I was trying to remember when i had last seen as much grandeur in my own life, let alone on myself. I was moved and wanted to help, but what could a simple person like me do? Still I moved on seeing I was not worthy staying in its presence about it.

To my surprise it was still fighting the next time I came to this place two days later and I could all but stop and wonder. It's pride and endurance were beyond my mental grasp i must say. I wished it well, moved on thinking about it for a very long time.

The next time i got there it was replaced by a younger recruit so it must have died in the meantime, but i am sure it was with honour. I thus vowed to pay my respects to it by trying to take it as an example and I encourage everybody to do the same!

Tuesday, February 8, 2011

Setting the MacOSX volume really low

I just realized it took me insanely long to figure this out and never put it up here in case someone else had the same problem. I have this theory after using original Apple earphones once that they use a preresistor that is just way to big and once you plug something decent in then you just go deaf even on the lowest level of the volume slider.

I then quickly found out about option-shift- but that only gets you down to 2.5% of the volume which is still quite loud in a quiet place. The lowest i have found out how to get to is 1% (and even that is too loud in the library sometimes) and this is how you do it:

osascript -e "set Volume 0.01"

Wednesday, February 2, 2011

ipv4 exhaustion

As you are hopefully aware IPv4 is on its way out and we reached a major milestone yesterday. Up until monday IANA had seven /8 subnets(that's a a bit under 17 million adresses each) left. On Tuesday APNIC requested another two, which let the number drop to the treshold agreed upon in advance on which the remaining five subnets will be distributed, one to each of the RIR's(Regional Internet Registries). That means yesterday every RIR got one last /8 Block, APNIC got three and that's it. Every IPv4 address that willl be requested once these blocks have been used up will have to come from some previous allotment.

How long will this last on the RIR level?

First you have to look at the rate with which the addresses were given out until now. As you can see the rate was about 10 /8's per year, of which only a neglegible amount was used by AFRINIC(Africa) and LACNIC(South America) (overestimate with 10%). On average RIPE(Europe) and ARIN(North America) use about equal amounts and together about as much as APNIC(Asia and Pacific) does. So that means APNIC, RIPE and ARIN will need about 4.5, 2.25 and 2.25 /8 networks over the course of next year if demand stays exactly the same.

Given that the good is becoming rarer this is unlikely as consumers will probably start hoarding. So given this increase in demand and estimating RIPE and ARIN to have a spare of about 1 /8 left over that would mean APNIC will be the first RIR to run out for good until September at the latest, shortly followed by RIPE and ARIN. LACNIC will likely run out early 2012 and AFRINIC probably will last longest given their low local demand.

What happens then?

Most likely it will be an ugly patchwork of a routing mess and a rather small bazaar for the unused parts of previous allocation that were simply to big for the organization getting them to be used up sensibly. I don't think this will scale to very small subnets since this will put too much demand on the routing infrastructure, so this market is a lot smaller than a lot of people estimate right now, although it will surely be amusing.

Apart from that there will be a rush to get IPv6 out to the public which will fail in a lot of entertaining ways and i am certain will be blamed on the short time frame that this had to be done in (a ludicrous claim that will be uttered for sure). A minor number of people will sit in the dark and wonder why a few websites won't work but overall won't notice anything wrong.

What does it mean?

PARTY! The dog days are over. Well at least to some degree I hope. I am really looking forward to what will hopefully be a global shared address space with hopefully less routing issues, NATing, masquerading and all that other fun stuff. I am sure someone will come up with some unbelievable abomination to ruin the fun but let's enjoy utopia while it lasts

If now my dorm and my work place (a scientific institution) would consider running ipv6 I'll be overjoyed.