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. Andrewfus:

    Thanks. I value it!

    viagra professonal real viagra overnight
    viagra generic
    viagra effectiveness – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  2. Andrewfus:

    You stated it terrifically.

    viagra in teens for hypertension first time viagra use
    viagra online
    viagra lost erection – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  3. Andrewfus:

    You actually suggested this well.

    compare levetra cialis viagra cough cialas viagra
    cheap viagra
    song written about viagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  4. Andrewfus:

    Very good data, Thanks.

    i took too much viagra does health insurance cover viagra
    buy generic viagra
    when viagra patent expire – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  5. Andrewfus:

    Seriously a good deal of valuable facts!

    men’s experience with viagra men using viagra
    generic viagra 100mg
    compare viagra and levitra – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  6. Andrewfus:

    Whoa a good deal of excellent advice.

    moms boyfriend on viagra viagra mexico
    generic viagra online
    district of columbia viagra flomax interaction – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  7. Andrewfus:

    Wonderful data. Many thanks!

    instructions on using viagra viagra and affiliate
    viagra without a doctors prescription
    adovart viagra – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  8. Andrewfus:

    Amazing facts. Kudos.

    least expensive generic viagra viagra elvis commercial
    viagra for sale uk
    buy viagra ship fedex – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  9. Andrewfus:

    You actually suggested that superbly!

    making viagra last berman sister female viagra study
    viagra for women
    trans viagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  10. Andrewfus:

    Many thanks, Numerous tips!

    will viagra guide with performance anxiety poppers as viagra
    viagra pills
    viagra mp3 – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  11. Andrewfus:

    Awesome information. Thanks!

    weight loss and viagra uk viagra most recognized brand
    viagra generic
    arginine nitric oxide viagra levitra cialis – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  12. Andrewfus:

    Awesome content. Thanks!

    cheap viagra search generic mdma viagra combo on the street
    buy viagra online
    quick forum readtopic viagra none search – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  13. Andrewfus:

    Wonderful info, Thanks a lot.

    viagra orgasim viagra find viagra sites search buy
    viagra without a doctor prescription
    6 free sample viagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  14. Andrewfus:

    Info very well used!!

    men and ther comments on viagra using viagra on house plants
    generic viagra 100mg
    alternate viagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  15. Andrewfus:

    You actually explained this very well.

    mixing zanidip and viagra viagra tv commericals
    buy viagra online
    viagra sex domination – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  16. Andrewfus:

    Whoa lots of terrific facts!

    re viagra nonprescription viagra au
    generic viagra online
    viagra cautions – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  17. Andrewfus:

    Lovely postings, Cheers!

    using viagra after prostate surgery imformation on viagra
    cheap viagra
    viagra cialis – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  18. Andrewfus:

    Regards, Quite a lot of tips.

    viagra ejaculations viagra reports
    viagra generic
    online generic viagra sales – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  19. Andrewfus:

    Very well spoken without a doubt. .

    viagra cialis and relationships harvard study viagra after dinner
    viagra online
    oysters spiked viagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  20. Andrewfus:

    You said it adequately.!

    viagra label buying viagra online title object object
    generic viagra
    brand name authentic viagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  21. Andrewfus:

    You actually stated that effectively!

    viagra with mastercard viagra chat
    generic viagra
    cheap sale viagra – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  22. Andrewfus:

    Helpful advice. Thanks a lot!

    oragel viagra sex viagra cactus pcture
    generic viagra 100mg
    trans viagra – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  23. Andrewfus:

    You actually explained this adequately!

    heart burn and viagra can viagra cause a detached retina
    cheap viagra
    taking viagra and tagamet – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  24. Andrewfus:

    Cheers. Good stuff!

    does viagra work buy overseas viagra
    viagra without a doctors prescription
    generic viagra with free shipping – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  25. Andrewfus:

    With thanks, Quite a lot of stuff.

    discount canadian pharmacy viagra cealis chasity and viagra
    viagra online
    best online viagra – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  26. Andrewfus:

    Many thanks! Numerous posts.

    generic viagra india review viagra crohn’s
    viagra pills
    viagra and unicure pharmacy – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  27. Andrewfus:

    Seriously lots of excellent material.

    uprima viagra cialis viagra alternive
    viagra pills
    buying viagra on line – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  28. Andrewfus:

    Regards, Numerous posts.

    what enzyme is inhibited by viagra edinburgh uk pages viagra find search
    viagra for sale uk
    connecticut viagra caverta generic veega – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  29. Andrewfus:

    Many thanks, I value it!

    directions for viagra use extended use viagra
    viagra 100mg
    buy discount viagra on the internet – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  30. Andrewfus:

    Nicely put, Thanks!

    deaths contributed to viagra premature ejaculation viagra help
    generic viagra 100mg
    anxiety relief online prescription medicine viagra – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  31. Andrewfus:

    Nicely put, Cheers!

    blue vision with viagra viagra safe for ladies
    viagra pills
    store online viagra – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  32. Andrewfus:

    Great info. Regards!

    viagra enhanced threesome buy viagra now
    viagra 100mg
    viagra benefits side effects – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  33. Andrewfus:

    Thank you, Good stuff!

    saturday night live viagra commercial dog eats viagra
    viagra online
    viagra facts – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  34. Andrewfus:

    Terrific data. Kudos.

    scam indian viagra dubai stress and viagra
    viagra without a doctors prescription
    compare viagra levitra – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  35. Andrewfus:

    Many thanks! Numerous data.

    civil suits against phizer viagra dirrections for use of viagra
    cheap viagra
    pro viagra – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  36. Andrewfus:

    Fantastic tips. Cheers!

    viagra as a diet pill best buy deal online viagra viagra
    generic viagra 100mg
    viagra and vascular disease – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  37. Andrewfus:

    With thanks! Ample postings.

    insurance companies covering viagra not birthcontrol viagra label picture
    cheap viagra
    cheap viagra online a href iframe – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  38. Andrewfus:

    Valuable knowledge. Thanks a lot!

    does viagra extend performance vitamin e viagra
    viagra 100mg
    non prescription type viagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  39. Andrewfus:

    Thanks, Great information!

    compro viagra does viagra help stress induced ed
    buy generic viagra
    super duper viagra stiffy – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  40. Andrewfus:

    Many thanks! A good amount of information!

    viagra onlone viagra at tesco
    generic viagra
    recommended dosage of viagra – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  41. Andrewfus:

    You revealed that fantastically.

    christians and viagra viagra vs cialas
    generic viagra 100mg
    break 25 mg viagra – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  42. Andrewfus:

    You said it perfectly.!

    roy d mercer dog viagra hugh hefners viagra supply
    buy generic viagra
    cheapest viagra – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  43. Andrewfus:

    You actually said this adequately!

    best deals on viagra effects of expired viagra
    buy viagra
    viagra prices united states – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  44. Andrewfus:

    Cheers, Wonderful information!

    viagra opens blood brain barrier viagra subsitute
    viagra generic
    viagra bournemouth ads kamagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  45. Andrewfus:

    You actually expressed it effectively!

    edinburgh uk viagra cfm moo tid chinese viagra 5
    generic viagra
    weight control and viagra – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  46. Andrewfus:

    Info effectively applied.!

    walmart female viagra can a man take womens viagra
    viagra online
    neonatal viagra vs nitric oxide – https://genericviagramsl.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  47. Andrewfus:

    Great content. Appreciate it.

    viagra forum viagra super p force
    viagra for sale uk
    viagra india mail order – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  48. Andrewfus:

    With thanks. Loads of content!

    buy viagra caverta viagra work for women
    generic viagra 100mg
    food viagra – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  49. Andrewfus:

    Nicely put. Cheers!

    how viagra saved chrismas viagra express
    viagra generic
    viagra diary joke – https://genericviagrantx.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.
  50. Andrewfus:

    Thanks, A good amount of material!

    search viagra free sites find computer viagra 34434 buy cheap viagra online
    buy viagra
    mccain avoids viagra cnn – https://genericviagrarcp.com

    Davidtaw
    TerenceWipsy
    Victorjar

    Your comment is awaiting moderation.

Leave a response: