RouterOS API library for C

Sometimes I get these ideas in my head that I just have to write out.

librouteros is one of those.

If you need to check data or do periodic updates, or similar stuff to a RouterOS device from a script or program, you should use the RouterOS API. This is an open API that you can read about on Mikrotiks wiki pages.

Earlier I have made a RouterOS API implementation in Flash ActionScript3. You can find it on the wiki page I just linked to. This time I felt that all C implementations of the routeros-api has been either over complicated, or too simple. And by complicated, I don’t mean in a good way. They are just hard to use without any special gain for being complicated.

I set up to create a simple API to talk to the router. At first I just created a “proof of concept” with blocking sockets. But my initial idea was to create a library that supported “asyncronous” data from the devices using the “.tag” support in the API. So now, the current version of my library supports using callbacks to handle scheduled commands. This means that you can send “monitor” commands, and automatically receive updates from the routeros device just when data changes.

Since I made this in two stages, I decided to leave the support for the “simple” interface which uses blocking sockets, for those who just want to send a simple command and read the response. But for the more advanced user, you can use the non-blocking interface where you define callbacks for the commands you send, and handle them asynchronously inside your own run-loop.

int main(int argc, char **argv) {
	struct sockaddr_in address;
	int len;
 
	if (argc < 4) {
		fprintf(stderr, "Usage: %s   \n", argv[0]);
		return 1;
	}
 
	conn = ros_connect(argv[1], ROS_PORT); 
	if (conn == NULL) {
		fprintf(stderr, "Error connecting to %s: %s\n", argv[1], strerror(errno));
		return 1;
	}
 
	if (ros_login(conn, argv[2], argv[3])) {
		struct ros_result *res;
 
		printf("Interfaces:\n");
 
		res = ros_send_command_wait(conn, "/interface/print", "=stats", ".tag=kake", NULL);
		while (res && res->re) {
 
			printf("  %20s  %20s  %20s  %20s\n", ros_get(res, "=name"), ros_get(res, "=type"), ros_get(res, "=rx-byte"), ros_get(res, "=tx-byte"));			
 
			ros_result_free(res);
			res = ros_read_packet(conn);
		}
		ros_result_free(res);
 
		ros_disconnect(conn);
	} else {
		fprintf(stderr, "Error logging in\n");
		return 1;
	}
 
	return 0;
}

Above you see a simple example of how to fetch info from the /interface/print command. This is using the simple blocking portion of the library.

For the non-blocking, main-loop based example of fetching the same information, there is a lot more code, so instead of putting it inline here, you can check the example file in the git repository. This example is a bit larger because I am also setting up the mainloop using select(), etc. But if you really go through the code, it’s not that much more code. It’s quite simple to use.
The callback “handleInterface” gets called when each “sentence” is returned from the command. I have added a “task” counter, to exit the program immediatly after all the interesting data is retrieved.

I have also created two ways of generating a “sentence” to send to the router-os device. As you see in the Mikrotik API specification, you need to generate a “sentence” using a series of “words” (read: key value pairs).

The simplest way, when you have a static amount of words you want to send, you can use the simple variable-parameter function ros_send_command_cb(), specifying all your words in a row, finishing with the last parameter to the command being NULL.

ros_send_command_cb(conn, handleInterface, "/interface/print", "=stats", NULL);

But if the amount of parameters is dynamic, you should rather build the sentence one word at a time, like this:

/* Build the sentence */
sentence = ros_sentence_new();
ros_sentence_add(sentence, "/system/resource/print");
ros_sentence_add(sentence, "=.proplist=uptime,cpu-load");
 
/* Send the sentence */
ros_send_sentence_cb(conn, handleUptime, sentence);
/* Free the sentence */
ros_sentence_free(sentence);

In this example, you could easily have a if() scope around the last word (the one with .proplist), making it optional, etc. But remember to free the sentence using ros_sentence_free(sentence) after you have sent the sentence. Look at the example, or any of the other example files, if you don’t understand my poor English.

I have tested this library with valgrind all the way under development and it is clean from memory leaks as far as valgrind knows. And it should be completely thread safe. I have also compiled and tested the library on Windows XP, Windows CE, Pocket PC, Mac OSX (console and app) and iPhone IOS. There is no need for modifications of the code base.

Please leave a comment or mail if you like this library, or decide to use it in a project.

Last commits on librouteros-api on GitHub

Posted December 22nd, 2012 in Uncategorized.

:

  1. ErnestoEping:

    Nicely put. Thanks!

    mix tadalafil and generic viagra wife sold ski doo viagra
    generic viagra
    viagra edinburgh pages find free information – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  2. ErnestoEping:

    Thank you, I enjoy it!

    free sample viagra walmart pharmacy viagra cost
    viagra 100mg
    find viagra free sites computer edinburgh – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  3. ErnestoEping:

    Information well taken.!

    most reliable generic viagra over the counter viagra in montreal
    viagra for women
    female women viagra – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  4. ErnestoEping:

    Regards! Wonderful information!

    viagra for paraplegic viagra pregnancy fetus
    generic viagra online
    is generic viagra legal in canada – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  5. ErnestoEping:

    Thanks a lot! Helpful stuff.

    levitra and viagra vipps approved viagra
    generic viagra 100mg
    phone consulation viagra – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  6. ErnestoEping:

    Valuable advice. Appreciate it.

    viagra like drugs viagra jokes funny pictures
    viagra online
    viagra fucker – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  7. ErnestoEping:

    Beneficial information. Thanks!

    viagra time sensitive how long does viagra last
    viagra without a doctor prescription
    viagra doctor consultation and shipping free – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  8. ErnestoEping:

    Thanks. Very good stuff.

    soma viagra interaction 491 viagra heart 708
    viagra without a doctor prescription
    pfizer viagra discount – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  9. ErnestoEping:

    Thanks. Helpful stuff!

    viagra insurance medben articles on viagra
    generic viagra
    jokes viagra – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  10. ErnestoEping:

    Information effectively used..

    wal-mart china viagra viagra sports
    viagra pills
    everyday viagra – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  11. ErnestoEping:

    Nicely put, Appreciate it.

    woodoo viagra viagra water
    viagra generic
    generic mexican viagra – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  12. ErnestoEping:

    Regards, Lots of facts.

    subaction showcomments viagra thanks newest viagra available in toronto
    buy viagra online
    viagra cambridge – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  13. ErnestoEping:

    You revealed this really well!

    viagra in india by mail order macular degeneration viagra
    viagra pills
    kamagra verschil viagra – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  14. ErnestoEping:

    Amazing loads of useful facts.

    viagra and female libido free viagra samples
    viagra without a doctors prescription
    india drugs scoria viagra – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  15. ErnestoEping:

    Nicely spoken certainly! !

    viagra and pulmonary hypertension genuine viagra manufacturers
    generic viagra
    find search viagra edinburgh pages online – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  16. ErnestoEping:

    Fantastic material. Thanks!

    when will viagra become generic viagra explained
    generic viagra 100mg
    peyronies disease and viagra – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  17. ErnestoEping:

    Beneficial content. Thanks.

    clearchannel radio and viagra founder what is the formula for viagra
    viagra for sale uk
    viagra headquarters picture – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  18. ErnestoEping:

    Thank you! An abundance of stuff!

    what demographic uses viagra make viagra work faster
    generic viagra online
    stuff like viagra – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  19. ErnestoEping:

    Reliable information. Thank you.

    generic viagra and pay pal viagra racing heart
    viagra for sale uk
    viagra find viagra edinburgh search – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  20. ErnestoEping:

    Terrific tips. Thanks a lot.

    generic viagra generic cialis pills viagra swallow whole or let disolve
    viagra generic
    can drug happened it miracle viagra – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  21. ErnestoEping:

    Nicely put, Many thanks!

    alternatives to viagra compare viagra prices
    viagra 100mg
    novocaine viagra – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  22. ErnestoEping:

    Information clearly used!.

    long term effect of viagra brand name authentic viagra
    cheap viagra
    viagra side effect insomnia – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  23. ErnestoEping:

    Thank you! Plenty of data!

    levitra verses viagra who manufacturers generic viagra
    buy viagra online
    canyon view lodge viagra commercial – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  24. ErnestoEping:

    Superb tips. Thanks.

    cardiazem with viagra testimonies of viagra use
    viagra 100mg
    search viagra viagra find edinburgh free – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  25. ErnestoEping:

    Very good postings. Cheers.

    drug interactions flomax viagra ciallis or viagra
    generic viagra online
    viagra and pulmonary hypertension canine – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  26. ErnestoEping:

    Thank you! Awesome stuff.

    viagra for multible climaxes no orgasm with viagra
    generic viagra 100mg
    subaction showcomments viagra start from newest – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  27. ErnestoEping:

    You actually said this exceptionally well.

    viagra sexual enhancements folgers coffee viagra
    generic viagra 100mg
    is generic viagra the real thing – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  28. ErnestoEping:

    You actually stated it wonderfully.

    viagra mcaps buy viagra and overseas
    buy viagra
    an alternative to viagra – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  29. ErnestoEping:

    Many thanks, Terrific information.

    polcythemia viagra interaction cheap viagra uks
    viagra without a doctors prescription
    viagra 50mg tablet cost cvs – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  30. ErnestoEping:

    Wonderful content. Kudos!

    best buy on line viagra viagra cream
    viagra for women
    subaction showcomments viagra sale posted – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  31. ErnestoEping:

    Great postings. Thanks!

    costof viagra lowest viagra prices from us pharmacy
    viagra generic
    taking viagra and cialis together – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  32. ErnestoEping:

    You said it adequately.!

    viagra wallpaper viagra license
    generic viagra
    nitroglycerin ointment viagra – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  33. ErnestoEping:

    Amazing forum posts. Cheers.

    viagra wife joke caterpillar fungus indias viagra
    viagra generic
    how does viagra wook – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  34. ErnestoEping:

    Helpful forum posts. Thank you.

    does viagra keep erection after ejaculation viagra causes blindness and hearing loss
    viagra generic
    viagra cialis from canada – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  35. ErnestoEping:

    Thank you. Great information.

    does viagra increaese size how to break a viagra pill
    viagra generic
    viagra for doggies – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  36. ErnestoEping:

    Amazing plenty of terrific advice.

    children taking viagra generic viagra capsules
    buy viagra
    lowest price viagra check money order – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  37. ErnestoEping:

    You made your point very clearly!.

    il viagra viagra newsletter sign up canadian pharmacy
    viagra without a doctor prescription
    viagra levitra safe sites – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  38. ErnestoEping:

    Thanks a lot, Numerous forum posts!

    honest place to buy viagra viagra equivalent for women
    buy viagra online
    us patent viagra – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  39. ErnestoEping:

    You actually stated it wonderfully.

    viagra awful online medicines rx cialis viagra order
    buy viagra
    viagra online issues – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  40. ErnestoEping:

    Great tips. With thanks.

    viagra vision viagra on web
    viagra generic
    viagra without prescription in vietnam – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  41. ErnestoEping:

    You suggested this terrifically.

    online viagra purchasing by viagra in uk
    buy generic viagra
    viagra free sites computer find online – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  42. ErnestoEping:

    Wow tons of excellent material.

    viagra 24 hour delivery uk pfizer viagra sales 2011
    viagra online
    comprar viagra – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  43. ErnestoEping:

    Thanks, A good amount of posts.

    wellman clinic viagra 3generic meltabs viagra
    generic viagra
    levitra viagra comparison – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  44. ErnestoEping:

    You made your point pretty nicely!.

    case law regarding viagra viagra verwendung
    viagra without a doctors prescription
    viagra cheap no prescrip – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  45. ErnestoEping:

    Incredible many of excellent information!

    ordering viagra to canada free viagra 2007 jelsoft enterprises ltd
    viagra 100mg
    superdrol with viagra – https://genericviagramsl.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  46. ErnestoEping:

    Whoa all kinds of wonderful data.

    viagra pharmacokinetics when does viagra patent end
    buy generic viagra
    buy viagra without prescription in canada – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  47. ErnestoEping:

    Good material, Regards!

    when women take viagra viagra by perscription
    viagra without a doctors prescription
    dangers of using expired viagra – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  48. ErnestoEping:

    Thanks! Useful stuff.

    viagra genric viagra without prescription au
    viagra without a doctors prescription
    civil suits against phizer viagra – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  49. ErnestoEping:

    Thank you! Terrific information!

    the affects of viagra viagra lavetra cialis
    buy viagra online
    wallgreens viagra cost – https://genericviagrarcp.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.
  50. ErnestoEping:

    Kudos, I appreciate this!

    new female viagra colbert report white rabbit and viagra
    buy viagra
    fine viagra – https://genericviagrantx.com

    KeithNep
    RichardTum
    ErnestoEping

    Your comment is awaiting moderation.

Leave a response: