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

    Хорошо смотрится реклама, где максимально используется окружающее пространство
    https://clck.ru/LhXJq
    https://clck.ru/LhVoS
    https://clck.ru/LhXaZ
    https://clck.ru/LhZBS
    https://clck.ru/LhWJW
    https://clck.ru/LhY6n
    https://clck.ru/LhUuF
    https://clck.ru/LhXSj
    https://clck.ru/LhXW3
    https://clck.ru/LhUoZ
    https://clck.ru/LhYLQ
    https://clck.ru/LhVFU
    https://clck.ru/LhXZv
    https://clck.ru/LhWGx
    https://clck.ru/LhVXx

    Your comment is awaiting moderation.
  2. Charleslen:

    Хорошо смотрится реклама, где максимально используется окружающее пространство
    https://clck.ru/LhYTR
    https://clck.ru/LhWXV
    https://clck.ru/LhVaw
    https://clck.ru/LhWww
    https://clck.ru/LhVha
    https://clck.ru/LhYen
    https://clck.ru/LhXHv
    https://clck.ru/LhVvH
    https://clck.ru/LhWzn
    https://clck.ru/LhWLp
    https://clck.ru/LhXeu
    https://clck.ru/LhXeV
    https://clck.ru/LhWQ8
    https://clck.ru/LhWvQ
    https://clck.ru/LhVgH

    Your comment is awaiting moderation.
  3. Charleslen:

    Хорошо смотрится реклама, где максимально используется окружающее пространство
    http://oppchamber.com/put-car-mechanic-business-top-gear-wheely-good-tips/?unapproved=4888&moderation-hash=f886f0f9cc465ec75e88be96083e2863#comment-4888
    http://maks12345.biz.ua/forums/theme/horosho-smotritsya-reklama/
    http://napoliproverbitradizioni.altervista.org/portale/?q=node/825&page=3391#comment-286749
    https://sw-studio.ru/plyusy-i-minusy-besplatnogo-sozdaniya-sajta-konstruktory-sajtov/?unapproved=1701&moderation-hash=6377948ca470023fa299a95106846dea#comment-1701
    https://www.semtrainers.com/product/3b-laser-needle-red-laser-light-12×660-nm/?unapproved=951&moderation-hash=100dab1539f21fbee002b762eeb6a75a#comment-951
    http://www.almaoso3a.com/6493/26/11/%D9%85%D8%A7%D9%87%D9%88+%D8%A7%D9%88%D9%84+%D8%AC%D8%A8%D9%84+%D9%86%D8%B5%D8%A8+%D9%81%D9%8A+%D8%A7%D9%84%D8%A7%D8%B1%D8%B6+%D8%9F.html
    http://aiam.7mry.com/showthread.php?p=1110672#post1110672
    http://zooelite.net/the-apprentices-bianca-miller-goes-for-it/?unapproved=179523&moderation-hash=28255b93666c16e361cb41cdc9e4162d#comment-179523
    http://dawndaispeaks.com/vitamin-d-being-pretty-hurts/?unapproved=42676&moderation-hash=d3d53d8f45c19a318b315d52a382ce07#comment-42676
    http://dindafitri.student.umm.ac.id/2017/06/16/kepo-ya-2/?unapproved=59384&moderation-hash=01c9116e91956f429d1e486341dc5a95#comment-59384
    https://strahifobii.ru/zastenchivost-u-detej/?unapproved=27382&moderation-hash=3fca2fe2944edeaad5860edea8a2cf9c#comment-27382
    http://mahkety.ru/2017/06/29/%d1%84%d0%be%d1%80%d1%83%d0%bc-%d1%82%d0%b0%d0%b2%d1%80%d0%b8%d0%b4%d0%b0-%d0%b2%d0%bd%d0%be%d0%b2%d1%8c-%d1%81%d0%be%d0%b1%d0%b5%d1%80%d0%b5%d1%82-%d0%bd%d0%b0-%d0%ba%d1%80%d1%8b%d0%bc/?unapproved=2499&moderation-hash=2f217f96bc100a8baefe532b77f598b5#comment-2499
    https://www.nutritionmagic.com/toxins/know-suffer-toxic-metal-poisoning/?unapproved=25493&moderation-hash=61197c32b7429aa0960cc8a3a094181a#comment-25493
    http://latexinnovations.com/comments/?unapproved=116975&moderation-hash=08de30bbdbdabbd515011b0e68caf070#comment-116975
    https://kindafunnyforums.com/viewtopic.php?f=18&t=4478&p=313318#p313318

    Your comment is awaiting moderation.
  4. Charleslen:

    Хорошо смотрится реклама, где максимально используется окружающее пространство
    http://zamanula.ru/samsung-galaxy-s8-%d0%b8-s8-plus-%d0%bf%d1%80%d0%be%d1%82%d0%b8%d0%b2-iphone-7-%d0%b8-galaxy-s7-%d1%81%d1%80%d0%b0%d0%b2%d0%bd%d0%b5%d0%bd%d0%b8%d0%b5-%d0%b3%d0%b0%d0%b1%d0%b0%d1%80%d0%b8%d1%82%d0%be/comment-page-1/?unapproved=161903&moderation-hash=b963aeb15a6622d333f6ceafbd3f7735#comment-161903
    https://www.itakemeds.com/member/266-charlespeany
    http://fotografia-moreno.com/sesion-fotografica-en-exterior-snaut/?unapproved=40832&moderation-hash=5cbe129f97b32f5c9b74fa7d2b6e7dcd#comment-40832
    http://handball.se-leistungssport.de/viewtopic.php?f=2&t=304235
    http://michelet.basket.free.fr/forum/viewtopic.php?f=4&t=163337
    https://filmydialogues.dontgetserious.com/10-met-mother-quotes-can-relate-real-life/?unapproved=20862&moderation-hash=12b2c64c18b69cf3e4a0d7484e6694da
    http://deandortonafo.com/9716-explore-power-cloud-erp/?unapproved=19318&moderation-hash=787133f955d31935c717597445a8773a#comment-19318
    https://emergency.sandiegocountyplumber.com/privacy-policy/?unapproved=37990&moderation-hash=2be08799a3cbebc945b8e8513be1e9af#comment-37990
    http://digitallibrarianroanokecollege.pages.roanoke.edu/hello-world/?unapproved=20711&moderation-hash=571fa5bcd4e6d3cce726c3aaa684ff45#comment-20711
    http://livingessentially.ca/nhp-natural-health-products/?unapproved=39553&moderation-hash=2dc8fa5aac694420dc4323f90dc859b3#comment-39553
    http://academynlp.com/ru/%d0%bf%d1%81%d0%b8%d1%85%d0%be%d1%82%d0%b5%d1%80%d0%b0%d0%bf%d0%b8%d1%8f-%d0%b6%d0%b8%d0%b7%d0%bd%d1%8c%d1%8e/?unapproved=147&moderation-hash=991f3c5ec91eaedc537dc34966c346de#comment-147
    https://blog.1sivalavie.com/2010/03/mister-jl-navette-b-day/comment-page-1/?unapproved=47185&moderation-hash=3325c52676dd7fc5e3d1f72c7279af2b#comment-47185
    http://joamac.synology.me/wordpress/2018/03/28/hello-world/?unapproved=47889&moderation-hash=2168a1a614da6feaa56a5cfaaf5a135f#comment-47889
    http://2m-mebel.ru/shop/divan-panda/?unapproved=894&moderation-hash=0c9d40c49faf1294965002d2b4fd26ec#comment-894
    http://www.paroquiasantateresinha.com.br/por-que-se-celebra-a-missa-de-7o-dia/?unapproved=73095&moderation-hash=566aecb7ffae18915d125489400bfaa0#comment-73095

    Your comment is awaiting moderation.
  5. Charleslen:

    Хорошо смотрится реклама, где максимально используется окружающее пространство
    http://www.1960beads.com/events/super-duo-bracelet?unapproved=133261&moderation-hash=f6401bd6142852f01728ce31b94feb3a#comment-133261
    http://www.junkycraft.fluctis.com/viewtopic.php?pid=1016974#p1016974
    http://www.bbiphones.com/bbiphone/cyanogenmod-installer-now-available-on-google-play-store-install-custom-rom-now-be-done-with-ease/comment-page-1?unapproved=142043&moderation-hash=3f72defe7aecde7894b4d20037c6e75c#comment-142043
    http://www.iwcr.lv/visual-communication/?unapproved=135735&moderation-hash=52d3a1768ac289b88db55b3f23d4fb91#comment-135735
    https://www.jacksonsmusic.com/2016/08/jacksons-music-adds-online-bandorchestra-instrument-rentals/?unapproved=28&moderation-hash=235a162157ff99a0b5940d67703eaf92#comment-28
    https://www.advancedpmr.com/blog/pelvic-instability/?unapproved=8310&moderation-hash=a618fb82a08906387887dea1d1b9878e#comment-8310
    https://flatmedia.ru/blog/17-vidov-kontenta/?unapproved=15026&moderation-hash=7fc54a172df090d7e5bbcdee89a0f2ce#comment-15026
    http://qgolive.com/qgl-audio-playout/?unapproved=8547&moderation-hash=969168d602f581ecffb59816dafc347b#comment-8547
    http://travel-vesti.ru/turizm-v-yaponii-arhipelag-ryukyu-veryovka-v-otkrytom-more/?unapproved=100218&moderation-hash=dbb93287a9b3bc7847e87d49c1fd23a9#comment-100218
    http://blogveterinaria.net/2017/10/09/cirugia-pez-tumor/?unapproved=200183&moderation-hash=f8aa7f857a46d303e9f8740efdb8a574#comment-200183
    https://m4dtang.com/forums/users/charlesvurdy/
    http://remirough.com/488-public-provocations-at-colab-gallery/?unapproved=34616&moderation-hash=62a1ae3d7e8b5e42782fae63e4dbf66f#comment-34616
    https://zipitopener.com/hard-plastic-package-opener/invisible-ailments/?unapproved=112729&moderation-hash=1ada000240aa58402d2c70f1d799eeb4#comment-112729
    http://xgb.ru/sliderrv/
    https://forme.gr/blog/aloi-i-agapimeni-tis-kosmetologias/?unapproved=63933&moderation-hash=3902c35212f64a1aa5e208a03c5596c3#comment-63933

    Your comment is awaiting moderation.
  6. Charleslen:

    Хорошо смотрится реклама, где максимально используется окружающее пространство
    https://iyinconline.com/testimonial/w-j-k-krienen-griffith-funeral-home/?unapproved=49962&moderation-hash=e6260ca54e3d55cbf2c1920d1ad5fbc1#comment-49962
    http://www.envisol.fr/interview-envisol-actu-cci/?unapproved=40109&moderation-hash=ba620be29741429686d9450b639c8615#comment-40109
    http://kninf.wsei.lublin.pl/viewtopic.php?f=2&t=518055
    http://www.islam2you.com/forums/member.php?u=949
    https://blog.1sivalavie.com/2010/03/mister-jl-navette-b-day/comment-page-1/?unapproved=47185&moderation-hash=3325c52676dd7fc5e3d1f72c7279af2b#comment-47185
    http://www.bubaklife.cz/sapamapa/?unapproved=290820&moderation-hash=f7a615b23f2d63aa0b4b1e8efc8906f0#comment-290820
    https://millybitcoin.com/?p=591&unapproved=2134&moderation-hash=3af45d9c7d02ab405e995a778db36e45#comment-2134
    https://www.rosprirod-tatarstan.spr-kazan.ru/forum/messages/forum13/message25559/595-xxl_massamuscular1pt.eu9mass_extreme.html/?result=reply#message25559
    http://osteopat-matrosov.ru/comment-page-1/?unapproved=185983&moderation-hash=0f620a55b91c1ad055ee84e39a18e5cd#comment-185983
    https://pammfix.ru/forex-news/liderstvo-7-vazhnykh-pravil-kotorye-nast/?unapproved=46442&moderation-hash=95f180bcbb192b6a635f6b98376255f5#comment-46442
    http://blog.agricolum.com/cosechadoras-demoagro-2015/?unapproved=48522&moderation-hash=8af164abd7096996f64b8045e0078e17#comment-48522
    https://wpdeveloper.net/new-wp-scheduled-posts/?unapproved=142329&moderation-hash=a4ce65c2220487c2e2c81285ca7f2acb#comment-142329
    http://boli-v-spine.net/?unapproved=46356&moderation-hash=83816ea74d12b2dc60eb82871458160e#comment-46356
    http://meetme.in.ua/2014/01/13/%d1%82%d0%b8%d0%bf%d1%8b-%d0%bf%d0%bb%d0%b5%d0%bd%d0%ba%d0%b8-%d0%b4%d0%bb%d1%8f-%d1%82%d0%b5%d0%bf%d0%bb%d0%b8%d1%86/comment-page-1/?unapproved=4597&moderation-hash=17eb621198bb790b624fd2a061eb6102#comment-4597
    https://www.bachelorsdegree.org/2011/01/11/20-incredible-ted-talks-for-both-music-students-and-lovers/comment-page-1/?unapproved=44791&moderation-hash=c76ef6e407ddfef32b5d5403bfdc4fe2#comment-44791

    Your comment is awaiting moderation.
  7. Charleslen:

    Хорошо смотрится реклама, где максимально используется окружающее пространство
    http://ersenoy.ru/2019/09/19/%d0%b2%d1%81%d0%b5%d1%80%d0%be%d1%81%d1%81%d0%b8%d0%b9%d1%81%d0%ba%d0%b8%d0%b9-%d1%81%d0%be%d0%b2%d0%b5%d1%82-%d0%bc%d0%b5%d1%81%d1%82%d0%bd%d0%be%d0%b3%d0%be-%d1%81%d0%b0%d0%bc%d0%be%d1%83%d0%bf/?unapproved=857&moderation-hash=59eb1c01d636ec5b9ff311d0bbee6f06#comment-857
    https://www.tatflot.spr-kazan.ru/forum/messages/forum10/message25560/308-a-href_http_berzerker.eu_titans_rage.html_http_www.berzerker.eu_titans_rage.html_a/?result=reply#message25560
    https://americasmemory.com/2014/03/28/afghanistan-wall-fort-hood/?unapproved=19707&moderation-hash=1c7f4b07020aab20891273c9ed2eeb68#comment-19707
    https://parentalcontrolguides.com/free-parental-controls/comment-page-1/?unapproved=79634&moderation-hash=f968509291811697afa3bf6e09687ebd#comment-79634
    https://www.ex-ttcommunity.com/forum/viewtopic.php?f=2&t=1825055
    https://wmtake.ru/qiwi/poluchenie-kredita-na-qiwi-koshelek-onlajn.html?unapproved=836&moderation-hash=b5043c78220257e48261b2731f66a5ae#comment-836
    http://advision.co.in/?unapproved=34371&moderation-hash=8ce35b8161b986c1c57b6bd37fd62662#comment-34371
    http://avhospitals.com/nullam-vitae-nibh-un-odio/?unapproved=61143&moderation-hash=cd60e4ddbafb3125387703853b5d88e6#comment-61143
    http://www.envisol.fr/interview-envisol-actu-cci/?unapproved=40109&moderation-hash=ba620be29741429686d9450b639c8615#comment-40109
    http://blog.bulgarienview.com/archives/151/comment-page-1?unapproved=774567&moderation-hash=860c355592ad77b7a6e3e304a04718de#comment-774567
    https://www.jaigurudeva.it/thecreativenetwork/la-creativita-nellarte-comico-umoristica-_-intervista-ad-arianna-porcelli-safonov/?unapproved=3139&moderation-hash=c48c0c227e0be734205bf9313e273c86#comment-3139
    http://www.shearstoyousalon.com/chemo-wigs/?unapproved=15167&moderation-hash=31d3e37e0620fa9fb6d50d08a84905bc#comment-15167
    https://gunungrizki.com/banking-with-celebrities-featuring-virzha/?unapproved=26570&moderation-hash=cf7d65b95cb4c902943c880a767ebb45#comment-26570
    https://romfour.com/blog/2017/10/27/se-apropie-extragerea-unui-nou-logan/?unapproved=26358&moderation-hash=d9091193cc2a6390f1b5ca92244c95d9#comment-26358
    http://www.donbalon.pl/polkolonie-tenisowe-2016/?unapproved=95800&moderation-hash=51a45734cec8b2b320c28a0e6c71a335#comment-95800

    Your comment is awaiting moderation.
  8. Charleslen:

    Хорошо смотрится реклама, где максимально используется окружающее пространство
    http://konkanmail.com/%e0%a6%aa%e0%a7%82%e0%a6%b0%e0%a7%8d%e0%a6%ac%e0%a6%b8%e0%a7%8d%e0%a6%a5%e0%a6%b2%e0%a7%80%e0%a6%b0-%e0%a6%95%e0%a6%be%e0%a6%b0%e0%a7%8d%e0%a6%a4%e0%a6%bf%e0%a6%95-%e0%a6%b2%e0%a6%a1%e0%a6%bc%e0%a6%be/?unapproved=331&moderation-hash=2bc53f6486604ccf36453a356b507fd7#comment-331
    http://7k.clanweb.eu/2019/10/hello-world/?unapproved=1426&moderation-hash=658a0ba7e8e8916a24f581a673d82705#comment-1426
    https://www.woogy.org/index.php/najti-novykh-druzej-v-nizhnem-novgorode
    https://www.theurbansuites.com/en/2017/05/18/summer-festivals-barcelona/?unapproved=11127&moderation-hash=40a6960fa1c8c7de1d6f908de76fcee6#comment-11127
    http://kiehlmann.co.uk/User:Williamcub
    https://wp-theme.net/blog/2019/03/29/hello-world/?unapproved=185&moderation-hash=6ac7606ffaa8ac5e85a32cbf8a9c618c#comment-185
    http://sirwinstonrealty.com/author/charlestream/
    http://ecoled63.ru/?p=1&unapproved=40041&moderation-hash=b06a91582e8ca630d2e93cc7e649d275#comment-40041
    http://fundacionvisiontotal.com/brigada-villa-jimenez/?unapproved=67230&moderation-hash=5b9eb65e24b663924fd350a025aeaee0#comment-67230
    https://xn—-8sbl6ahclo7c7a.xn--p1ai/product/tkan-h-fantazy-1504-tsv-r-1013-beyaz/?unapproved=366&moderation-hash=c5982536919b64183242a6b39826cba0#comment-366
    https://zoldiallvany.hu/allvanyozas-munkavedelmi-szabalyzatai-izelito/?unapproved=2344&moderation-hash=ef54b57ee76c3a4eccee298032b9318d#comment-2344
    http://holdingofwrist.com/self-harm-awareness-day/comment-page-1/?unapproved=50897&moderation-hash=d7133a4ee88c43c8a930f23931fe169f#comment-50897
    http://www.movieclipnotes.com/movie-posts/crazy-love/?unapproved=116116&moderation-hash=95b938839f4f09ae958faff8867e3633#comment-116116
    http://aromaspa.me/standard-crib-size/?unapproved=337&moderation-hash=61d456814d861dd17767814ba73b17d2#comment-337
    https://maxiotzyv.ru/catalog/horosho-smotritsya-reklama

    Your comment is awaiting moderation.

Leave a response: