C - UDP sink example

8. March 2012 20:12

 

This is a short example in C on linux which will open a socket and listen on a udp port for data.

 

#include <stdio.h>
#include <stdlib.h>
#include <stdarg.h>
#include <string.h>
#include <unistd.h>
#include <errno.h>

#include <arpa/inet.h>
#include <sys/socket.h>

int verbose = 0;

void print_usage(FILE *fp, char *app) {
	fprintf(fp, "Usage: %s <port>\n", app);
	fprintf(fp, "\n");
	fprintf(fp, "  -h        Print this help\n");
	fprintf(fp, "\n");
	fprintf(fp, "Example: %s 5000\n", app);
	fprintf(fp, "\n");
}

int main(int argc, char **argv) {
	int c;
	int sockfd;
	struct sockaddr_in in;
	socklen_t in_size = sizeof(in);
	char buf[65535];
	int port = 0;
	
	while(( c = getopt(argc, argv, "hv")) != -1) {
		switch(c) {
			case 'h':
				print_usage(stdout, argv[0]);
				exit(EXIT_FAILURE);
				break;
			case 'v':
				verbose++;
				break;
			default:
				break;
		}
	}
	
	if (optind < argc) {
		port = atoi(argv[optind]);
		optind++;
	} else {
		print_usage(stderr, argv[0]);
		exit(EXIT_FAILURE);
	}

	if (port == 0 || port > 65535) {
		fprintf(stderr, "%d is an invalid port\n", port);
		exit(EXIT_FAILURE);
	}

	sockfd = socket(AF_INET, SOCK_DGRAM, 0);
	if (sockfd < 0) {
		perror("socket");
		exit(EXIT_FAILURE);
	}

	in.sin_family = AF_INET;
	in.sin_port = htons(port);
	in.sin_addr.s_addr = INADDR_ANY;

	if (bind(sockfd, (struct sockaddr *) &in, sizeof(in)) < 0) {
		perror("bind");
		exit(EXIT_FAILURE);
	}

	while(1) {
		int ret = recvfrom(sockfd, buf, sizeof(buf), 0, (struct sockaddr *) &in, &in_size);

		if (ret < 0) {
			perror("recvfrom");
			continue;
		}
		if (verbose)
			printf("Recived: %d Bytes From %s:%d\n", ret, inet_ntoa(in.sin_addr), ntohs(in.sin_port));

	}

	return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink


Python - Doesn't do real for loops

24. February 2012 07:00

 

Previous I wrote about python and its weakness for 2d arrays. This has now got me thinking a little more out of the box.

 

I have also discovered that python doesn't actually support the tradition for loop. It supports a for each loop instead which has issues when attempts to run quite large loops. This is a simple example of one of these issues which you can only ever discover when it is too late normally resulting in a crash of some kind.

 

To start with lets look at the simple loop.

 

>>> for i in range(0, 10):
...  print i
...
0
[cut]
9
>>>

 

 

It does of course print 0 - 9 as output. But look at how it is constructed. It uses the range function and range does't produce a number range. It produces a list of numbers from the specified paramaters on the function. So whats the big deal?

 

Well if you need to run a "big" for loop in python you simply cannot use it ... Well not without using lots and lots of memory. Not to mention that the entire list has to be created before the first for loop runs. Which is just going to make it dog slow to begin with.

 

So to get python to use around 170MB of memory (in a for loop) simply write

 

>>> for i in range(0, 10000000):
...     print i

 

 

But wait... 10 million? That a very big number. Or is it? It is not uncommon for large data sets to grow to 10 millions records. This would be exactly the sort of place you might want to use python as well.

 

So I guess the alternative to this is to role your own for loop using a while loop. Which I have had todo for various other things for variable stride lengths across array's. Though it kinda defets the entire purpose of there being a for loop in the first place.

 

As a bonus surprise while playing with this feature it probably also open's up a whole new range of security holes if a loop counter is supplied from end user input. This would lead to using massive amounts of memory. The real surprise I was greated with the following from the linux kernel who's out of memory killer could have also picked apache or a database engine to kill in its place.

 

 

[5537286.542945] Out of memory: kill process 14166 (python) score 230114 or a child
[5537286.543168] Killed process 14166 (python)

 

 

But I guess thats python's way of doing things!

E-mail Kick it! DZone it! del.icio.us Permalink


Python - 2d Array's don't work.

22. February 2012 20:25

 

If you have been working with python you will notice that 2d array's just don't work. This might come as a surprise since almost every other modern programming language supports 2d array's. What might even come as a bigger surprise is that pythin doesn't support array's at all in its language. It uses lists to support this functionality. It might look like an array but it is actually a list.

 

This can be showen with the following. Note the error about the list.

 

 

arr = []
arr[0] = 1


Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IndexError: list assignment index out of range

 

 

So once you stop thinking about trying to create and array in pythin (which is easy since they don't exist) and start thinking about creating a list of items things become much easyier to understand why your 2d arrays of arr = [][] just doesnt work. As you have just attempts to create 2 lists and set a variable with them.

 

So to create a single array list is easy and we can do so using the following code.

 

>>> arr = range(0,5)
>>> print arr
[0, 1, 2, 3, 4]

>>> arr[0] = 1
>>> print arr
[1, 1, 2, 3, 4]

 

 

I actually find this really ugly. Since we just created a list of incrementing number and the array still needs to be set to zero. So to create a real array preset to zero you need to use the following.

 

 

>>> arr = []
>>> for i in range(0, 5):
...  arr.append(0)
...
>>> print arr
[0, 0, 0, 0, 0]

 

 

So moving onto the 2d array and you do exactly the same thing. Except you create a list and put it in the list item. Like this

 

 

>>> arr = []
>>> for i in range(0, 5):
...  x = []
...  for j in range(0, 5):
...   x.append(0)
...  arr.append(x)
...
>>> print arr
[
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0],
[0, 0, 0, 0, 0]
]

 

Now you can read / write to the items by using an array like syntax or arr[0][0] .. arr[4][4].

 

There is another alternative support to using 2d arrays in python by using a single list and then calulating the offsets. However for the particular problem I was trying to solve it really didn't help since python doesn't appear to support proper for loops either!

 

I guess thats what you get with python!

E-mail Kick it! DZone it! del.icio.us Permalink


C - get home dir location in linux

20. February 2012 08:00

 

This is a short example of how to get the home directory location in linux in a C program. There is really a few choices here and depending on what your requirements are you might want to use one or other methods. The first method can be changed by the user. The second method cannot. However if the first method fails you should drop faill back on the other method.

 

The simple method is to pull the enviroment variable "HOME"

 

The slightly more complex method is to read it from the system user database.

 

 

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <pwd.h>
#include <sys/types.h>


int main(int argc, char **argv) {


        char *homedir = getenv("HOME");

        if (homedir != NULL) {
                printf("Home dir in enviroment");
                printf("%s\n", homedir);
        }

        uid_t uid = getuid();
        struct passwd *pw = getpwuid(uid);

        if (pw == NULL) {
                printf("Failed\n");
                exit(EXIT_FAILURE);
        }

        printf("%s\n", pw->pw_dir);

        return 0;
}

 

E-mail Kick it! DZone it! del.icio.us Permalink


C - ip address validation

17. February 2012 08:00

 

A short example to show how to check and ip address is actually an ip address in C. I have seen a number of time that people provide solution using reg'ex and various other methods to attempt to do this. However there is a standard function that will test for you.

 

The function is called inet_pton will be able to tell you if a string is acutally an ip address or not. Typically the function also has another use which is to convert an ip address into its network byte ordered integer format.

 

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

#include <arpa/inet.h>

int main(int argc, char **argv) {
    unsigned long ip = 0;

    if (argc < 2) {
        printf("Usage: %s <ipv4>\n", argv[0]);
        exit(EXIT_FAILURE);
    }

    if (0 == inet_pton(AF_INET, argv[1], &ip)) {
        printf("Failed\n");
        exit(EXIT_FAILURE);
    }

    printf("IP: %ld\n", ip);
    return 0;
}

E-mail Kick it! DZone it! del.icio.us Permalink