#include <omnetpp.h>
#include <cstdlib>
#include <unistd.h>
#include <sys/socket.h>
#include <linux/netlink.h>
#include <string.h>
#include <string>
#include <iostream>
#include <stdio.h>
#include <arpa/inet.h>
#include <signal.h>
#include <time.h>
#include <sstream>
#include <vector>
#include <sys/time.h>
#include <bitset>
#include <limits.h>
Defines | |
#define | PUBLISH_SCOPE 0 |
#define | PUBLISH_INFO 1 |
#define | UNPUBLISH_SCOPE 2 |
#define | UNPUBLISH_INFO 3 |
#define | SUBSCRIBE_SCOPE 4 |
#define | SUBSCRIBE_INFO 5 |
#define | UNSUBSCRIBE_SCOPE 6 |
#define | UNSUBSCRIBE_INFO 7 |
#define | PUBLISH_DATA 8 |
#define | DISCONNECT 13 |
#define | PURSUIT_ID_LEN 8 |
#define | NODEID_LEN PURSUIT_ID_LEN |
#define | FID_LEN 8 |
#define | MAC_LEN 6 |
#define | NODE_LOCAL 0 |
Urb : Strategies. | |
#define | LINK_LOCAL 1 |
#define | DOMAIN_LOCAL 2 |
#define | PUBLISH_NOW 3 |
#define | SUBSCRIBE_LOCALLY 4 |
#define | START_PUBLISH 100 |
Urb : Events. | |
#define | STOP_PUBLISH 101 |
#define | SCOPE_PUBLISHED 102 |
#define | SCOPE_UNPUBLISHED 103 |
#define | PUBLISHED_DATA 104 |
#define | MATCH_PUB_SUBS 105 |
#define | PUB_METADATA 106 |
#define | QoS_REPORT 107 |
#define | NETLINK_BADDER 20 |
Other consts. | |
#define | NETLINK_BADDER_PID 9999 |
#define | TM_RSP_SCOPE "fffffffffffffffd" |
Scopes. | |
#define | TM_REQ_SCOPE "fffffffffffffffe" |
#define | TM_LSM_SCOPE "fffffffffffffffb" |
#define | TM_META_SCOPE "fffffffffffffffa" |
#define | RV_SCOPE "ffffffffffffffff" |
Typedefs | |
typedef struct sockaddr_nl | nladdr |
Functions | |
string | chararray_to_hex (const string &str) |
string | hex_to_chararray (string const &hexstr) |
string | bin_to_chararray (string const &binstr) |
string | chararray_to_bin (string const &str) |
string | reverseByteOrder (string str) |
string | reverseStrBits (string str) |
string | reverseEachByte (string str) |
string | lidOR (string lid1, string lid2) |
string | lidAND (string lid1, string lid2) |
vector< string > | split (const char *str, char c= ' ') |
int | getEvent (int sock_fd) |
void | create_and_send_buffers (int sock_fd, nladdr d_nladdr, unsigned char type, string &id, string &prefix_id, char strategy, string LID) |
void | disconnect (int sock_fd, nladdr d_nladdr) |
void | publish_scope (int sock_fd, nladdr d_nladdr, string &id, string &prefix_id, char strategy, string LID) |
void | publish_info (int sock_fd, nladdr d_nladdr, string id, string prefix_id, char strategy, string LID) |
void | unpublish_scope (int sock_fd, nladdr d_nladdr, string id, string prefix_id, char strategy, string LID) |
void | unpublish_info (int sock_fd, nladdr d_nladdr, string id, string prefix_id, char strategy, string LID) |
void | subscribe_scope (int sock_fd, nladdr d_nladdr, string id, string prefix_id, char strategy, string LID) |
void | subscribe_info (int sock_fd, nladdr d_nladdr, string id, string prefix_id, char strategy, string LID) |
void | unsubscribe_scope (int sock_fd, nladdr d_nladdr, string id, string prefix_id, char strategy, string LID) |
void | unsubscribe_info (int sock_fd, nladdr d_nladdr, string id, string prefix_id, char strategy, string LID) |
void | publish_data (int sock_fd, nladdr d_nladdr, string id, unsigned char strategy, string LID, char *data, int data_len) |
#define DISCONNECT 13 |
#define DOMAIN_LOCAL 2 |
#define FID_LEN 8 |
#define LINK_LOCAL 1 |
#define MAC_LEN 6 |
#define MATCH_PUB_SUBS 105 |
#define NETLINK_BADDER 20 |
Other consts.
#define NETLINK_BADDER_PID 9999 |
#define NODE_LOCAL 0 |
Urb
: Strategies.
#define NODEID_LEN PURSUIT_ID_LEN |
#define PUB_METADATA 106 |
#define PUBLISH_DATA 8 |
#define PUBLISH_INFO 1 |
#define PUBLISH_NOW 3 |
#define PUBLISH_SCOPE 0 |
#define PUBLISHED_DATA 104 |
#define PURSUIT_ID_LEN 8 |
#define QoS_REPORT 107 |
#define RV_SCOPE "ffffffffffffffff" |
#define SCOPE_PUBLISHED 102 |
#define SCOPE_UNPUBLISHED 103 |
#define START_PUBLISH 100 |
Urb
: Events.
#define STOP_PUBLISH 101 |
#define SUBSCRIBE_INFO 5 |
#define SUBSCRIBE_LOCALLY 4 |
#define SUBSCRIBE_SCOPE 4 |
#define TM_LSM_SCOPE "fffffffffffffffb" |
#define TM_META_SCOPE "fffffffffffffffa" |
#define TM_REQ_SCOPE "fffffffffffffffe" |
#define TM_RSP_SCOPE "fffffffffffffffd" |
Scopes.
#define UNPUBLISH_INFO 3 |
#define UNPUBLISH_SCOPE 2 |
#define UNSUBSCRIBE_INFO 7 |
#define UNSUBSCRIBE_SCOPE 6 |
typedef struct sockaddr_nl nladdr |
string bin_to_chararray | ( | string const & | binstr | ) |
Convert 0000010101010101010010101 -> char *
00070 { 00071 vector<unsigned char> bytes = vector<unsigned char>(); 00072 for (string::size_type i = 0; i < binstr.size() / 8; ++i) { 00073 bitset<8> set(binstr.substr(i * 8, 8)); 00074 bytes.push_back(static_cast<unsigned char> (set.to_ulong())); 00075 } 00076 bytes.data(); 00077 string result = string((const char *) bytes.data(), bytes.size()); 00078 return result; 00079 }
string chararray_to_bin | ( | string const & | str | ) |
string chararray_to_hex | ( | const string & | str | ) |
Convert 010AFFE0 -> char *
00054 { 00055 ostringstream oss; 00056 for (string::size_type i = 0; i < str.size(); ++i) { 00057 if ((unsigned short) (unsigned char) str[i] > 15) { 00058 oss << hex << (unsigned short) (unsigned char) str[i]; 00059 //oss << " 0x" << (unsigned short) (unsigned char) str[i]; 00060 } else { 00061 oss << hex << '0'; 00062 //oss << " 0x" << '0'; 00063 oss << hex << (unsigned short) (unsigned char) str[i]; 00064 //oss << " 0x" << (unsigned short) (unsigned char) str[i]; 00065 } 00066 } 00067 return oss.str(); 00068 }
void create_and_send_buffers | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
unsigned char | type, | |||
string & | id, | |||
string & | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00151 { 00152 struct msghdr msg; 00153 struct iovec *iov; 00154 unsigned char id_len = id.length() / PURSUIT_ID_LEN; 00155 unsigned char prefix_id_len = prefix_id.length() / PURSUIT_ID_LEN; 00156 struct nlmsghdr *nlh = (struct nlmsghdr *) malloc(sizeof (struct nlmsghdr)); 00157 00158 00159 if (LID.compare("") == 0) { 00160 iov = (struct iovec *) calloc(sizeof (struct iovec), 7); 00161 /* Fill the netlink message header */ 00162 nlh->nlmsg_len = sizeof (struct nlmsghdr) + 1 /*type*/ + 1 /*for id length*/ + id.length() + 1 /*for prefix_id length*/ + prefix_id.length() + 1 /*strategy*/; 00163 nlh->nlmsg_pid = getpid(); 00164 nlh->nlmsg_flags = 1; 00165 nlh->nlmsg_type = 0; 00166 } else { 00167 iov = (struct iovec *) calloc(sizeof (struct iovec), 8); 00168 /* Fill the netlink message header */ 00169 nlh->nlmsg_len = sizeof (struct nlmsghdr) + 1 /*type*/ + 1 /*for id length*/ + id.length() + 1 /*for prefix_id length*/ + prefix_id.length() + 1 /*strategy*/ + FID_LEN /*optional LID*/; 00170 nlh->nlmsg_pid = getpid(); 00171 nlh->nlmsg_flags = 1; 00172 nlh->nlmsg_type = 0; 00173 } 00174 iov[0].iov_base = nlh; 00175 iov[0].iov_len = sizeof (struct nlmsghdr); 00176 iov[1].iov_base = &type; 00177 iov[1].iov_len = sizeof (type); 00178 iov[2].iov_base = &id_len; 00179 iov[2].iov_len = sizeof (id_len); 00180 iov[3].iov_base = (void *) id.c_str(); 00181 iov[3].iov_len = id.length(); 00182 iov[4].iov_base = &prefix_id_len; 00183 iov[4].iov_len = sizeof (prefix_id_len); 00184 iov[5].iov_base = (void *) prefix_id.c_str(); 00185 iov[5].iov_len = prefix_id.length(); 00186 iov[6].iov_base = &strategy; 00187 iov[6].iov_len = sizeof (strategy); 00188 if (LID.compare("") == 0) { 00189 memset(&msg, 0, sizeof (msg)); 00190 msg.msg_name = (void *) &d_nladdr; 00191 msg.msg_namelen = sizeof (d_nladdr); 00192 msg.msg_iov = iov; 00193 msg.msg_iovlen = 7; 00194 sendmsg(sock_fd, &msg, 0); 00195 } else { 00196 00197 iov[7].iov_base = (void *) LID.c_str(); 00198 iov[7].iov_len = FID_LEN; 00199 00200 00201 memset(&msg, 0, sizeof (msg)); 00202 msg.msg_name = (void *) &d_nladdr; 00203 msg.msg_namelen = sizeof (d_nladdr); 00204 msg.msg_iov = iov; 00205 msg.msg_iovlen = 8; 00206 sendmsg(sock_fd, &msg, 0); 00207 } 00208 free(iov); 00209 free(nlh); 00210 }
void disconnect | ( | int | sock_fd, | |
nladdr | d_nladdr | |||
) |
00212 { 00213 struct msghdr msg; 00214 struct iovec *iov; 00215 struct nlmsghdr *nlh = (struct nlmsghdr *) malloc(sizeof (struct nlmsghdr)); 00216 unsigned char type = DISCONNECT; 00217 /* Fill the netlink message header */ 00218 nlh->nlmsg_len = sizeof (struct nlmsghdr) + 1 /*type*/; 00219 nlh->nlmsg_pid = getpid(); 00220 nlh->nlmsg_flags = 1; 00221 nlh->nlmsg_type = 0; 00222 iov = new iovec[2]; 00223 iov[0].iov_base = nlh; 00224 iov[0].iov_len = sizeof (struct nlmsghdr); 00225 iov[1].iov_base = &type; 00226 iov[1].iov_len = sizeof (type); 00227 memset(&msg, 0, sizeof (msg)); 00228 msg.msg_name = (void *) &d_nladdr; 00229 msg.msg_namelen = sizeof (d_nladdr); 00230 msg.msg_iov = iov; 00231 msg.msg_iovlen = 2; 00232 sendmsg(sock_fd, &msg, 0); 00233 free(nlh); 00234 delete [] iov; 00235 }
int getEvent | ( | int | sock_fd | ) |
00329 { 00330 int total_buf_size; 00331 int bytes_read; 00332 unsigned char type; 00333 unsigned char id_len; 00334 char *data; 00335 00336 char *buf; 00337 char *fake_buf; 00338 struct nlmsghdr *nlh = (struct nlmsghdr *) malloc(sizeof (struct nlmsghdr)); 00339 fake_buf = (char *) malloc(1); 00340 total_buf_size = recv(sock_fd, fake_buf, 1, MSG_PEEK | MSG_TRUNC | MSG_WAITALL); 00341 if (total_buf_size < 0) { 00342 return 0; 00343 } 00344 buf = (char *) malloc(total_buf_size); 00345 bytes_read = recv(sock_fd, buf, total_buf_size, MSG_WAITALL); 00346 00347 //cout << "bytes_read: " << bytes_read << endl; 00348 type = *(buf + sizeof (struct nlmsghdr)); 00349 id_len = *(buf + sizeof (struct nlmsghdr) + sizeof (unsigned char)); 00350 string str_id(buf + sizeof (struct nlmsghdr) + sizeof (unsigned char) + sizeof (unsigned char), ((int) id_len) * PURSUIT_ID_LEN); 00351 00352 if (type == PUBLISHED_DATA) { 00353 data = buf + sizeof (struct nlmsghdr) + sizeof (unsigned char) + sizeof (unsigned char) + ((int) id_len) * PURSUIT_ID_LEN; 00354 //cout<<"received data of size: "<< (total_buf_size - (sizeof (struct nlmsghdr) + sizeof (unsigned char) + sizeof (unsigned char) + ((int) id_len) * PURSUIT_ID_LEN)) << endl; 00355 if (data[0] == 'A') { 00356 00357 //cout << "received published data for ID " << chararray_to_hex(str_id) << "!! size: " << (total_buf_size - (sizeof (struct nlmsghdr) + sizeof (unsigned char) + sizeof (unsigned char) +str_id.size())) << endl; 00358 free(buf); 00359 free(fake_buf); 00360 free(nlh); 00361 } 00362 if (data[0] == 'B') { 00363 00364 free(buf); 00365 free(fake_buf); 00366 free(nlh); 00367 return -1; 00368 } 00369 } else if (type == SCOPE_PUBLISHED) { 00370 cout<<"Scope "<<str_id.c_str()<<" has been published"<<endl; 00371 } 00372 return 0; 00373 }
string hex_to_chararray | ( | string const & | hexstr | ) |
00041 { 00042 vector<unsigned char> bytes = vector<unsigned char>(); 00043 for (string::size_type i = 0; i < hexstr.size() / 2; ++i) { 00044 istringstream iss(hexstr.substr(i * 2, 2)); 00045 unsigned int n; 00046 iss >> hex >> n; 00047 bytes.push_back(static_cast<unsigned char> (n)); 00048 } 00049 bytes.data(); 00050 string result = string((const char *) bytes.data(), bytes.size()); 00051 return result; 00052 }
string lidAND | ( | string | lid1, | |
string | lid2 | |||
) |
string lidOR | ( | string | lid1, | |
string | lid2 | |||
) |
OR 2 LIDs. If one is empty and the other is not the result is the non-empty one!
00008 { 00009 00010 if (lid1.size()==0 && lid2.size()>0) return lid2; 00011 if (lid2.size()==0 && lid1.size()>0) return lid1; 00012 00013 // Error if not the same and NOT EPMTY 00014 if (lid1.size() != lid2.size()) return ""; 00015 00016 // OR them 00017 string res=lid1; 00018 for (uint i=0; i<lid2.size(); ++i) 00019 res[i]|=lid2[i]; 00020 00021 return res; 00022 00023 00024 }
void publish_data | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string | id, | |||
unsigned char | strategy, | |||
string | LID, | |||
char * | data, | |||
int | data_len | |||
) |
00269 { 00270 struct msghdr msg; 00271 struct iovec *iov; 00272 unsigned char id_len; 00273 unsigned char type = PUBLISH_DATA; 00274 id_len = (unsigned char) id.length() / PURSUIT_ID_LEN; 00275 00276 // cout<<"Publish_Data BA_API: "; 00277 // for (int i=0; i<data_len; i++){ 00278 // cout<<hex<<(int)data[i]<<" "; 00279 // } 00280 // cout<<endl; 00281 struct nlmsghdr *nlh = (struct nlmsghdr *) malloc(sizeof (struct nlmsghdr)); 00282 /* Fill the netlink message header */ 00283 nlh->nlmsg_len = sizeof (struct nlmsghdr) + 1 /*type*/ + 1 /*for id length*/ + id.length() + sizeof (strategy) + FID_LEN + data_len; 00284 nlh->nlmsg_pid = getpid(); 00285 nlh->nlmsg_flags = 1; 00286 nlh->nlmsg_type = 0; 00287 00288 if (LID.compare("") == 0) { 00289 iov = (struct iovec *) calloc(sizeof (struct iovec), 6); 00290 } else { 00291 iov = (struct iovec *) calloc(sizeof (struct iovec), 7); 00292 } 00293 iov[0].iov_base = nlh; 00294 iov[0].iov_len = sizeof (struct nlmsghdr); 00295 iov[1].iov_base = &type; 00296 iov[1].iov_len = sizeof (type); 00297 iov[2].iov_base = &id_len; 00298 iov[2].iov_len = sizeof (id_len); 00299 iov[3].iov_base = (void *) id.c_str(); 00300 iov[3].iov_len = id.length(); 00301 iov[4].iov_base = (void *) &strategy; 00302 iov[4].iov_len = sizeof (unsigned char); 00303 if (LID.compare("") == 0) { 00304 iov[5].iov_base = (void *) data; 00305 iov[5].iov_len = data_len; 00306 memset(&msg, 0, sizeof (msg)); 00307 msg.msg_name = (void *) &d_nladdr; 00308 msg.msg_namelen = sizeof (d_nladdr); 00309 msg.msg_iov = iov; 00310 msg.msg_iovlen = 6; 00311 sendmsg(sock_fd, &msg, 0); 00312 } else { 00313 iov[5].iov_base = (void *) LID.c_str(); 00314 iov[5].iov_len = FID_LEN; 00315 iov[6].iov_base = (void *) data; 00316 iov[6].iov_len = data_len; 00317 memset(&msg, 0, sizeof (msg)); 00318 msg.msg_name = (void *) &d_nladdr; 00319 msg.msg_namelen = sizeof (d_nladdr); 00320 msg.msg_iov = iov; 00321 msg.msg_iovlen = 7; 00322 sendmsg(sock_fd, &msg, 0); 00323 } 00324 00325 free(nlh); 00326 delete [] iov; 00327 }
void publish_info | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string | id, | |||
string | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00241 { 00242 create_and_send_buffers(sock_fd, d_nladdr,PUBLISH_INFO, id, prefix_id, strategy, LID); 00243 }
void publish_scope | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string & | id, | |||
string & | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00237 { 00238 create_and_send_buffers(sock_fd, d_nladdr,PUBLISH_SCOPE, id, prefix_id, strategy, LID); 00239 }
string reverseByteOrder | ( | string | str | ) |
BA Configuration uses reverse byte order on LIDs and FIDs ie. 0x60 0x03 0x02 0x01 should be sent as -> 0x01 0x02 0x03 0x60.
It is better for the TM to do all the calculations on reverse order. That way reversing is needed _only_ on configuration loading.
string reverseEachByte | ( | string | str | ) |
Reverse the bits in EACH BYTE ONLY
00126 { 00127 string res=str; 00128 int len=str.length(); 00129 for (int i=0; i<len; i++) 00130 res[i] = reverseCharBits(str[i]); 00131 00132 return res; 00133 }
string reverseStrBits | ( | string | str | ) |
Reverse ALL the bits from begining to the end...
00117 { 00118 string res=str; 00119 int len=str.length(); 00120 for (int i=0; i<len; i++) 00121 res[len-1-i] = reverseCharBits(str[i]); 00122 00123 return res; 00124 }
vector<string> split | ( | const char * | str, | |
char | c = ' ' | |||
) |
void subscribe_info | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string | id, | |||
string | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00257 { 00258 create_and_send_buffers(sock_fd, d_nladdr,SUBSCRIBE_INFO, id, prefix_id, strategy, LID); 00259 }
void subscribe_scope | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string | id, | |||
string | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00253 { 00254 create_and_send_buffers(sock_fd, d_nladdr,SUBSCRIBE_SCOPE, id, prefix_id, strategy, LID); 00255 }
void unpublish_info | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string | id, | |||
string | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00249 { 00250 create_and_send_buffers(sock_fd, d_nladdr,UNPUBLISH_INFO, id, prefix_id, strategy, LID); 00251 }
void unpublish_scope | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string | id, | |||
string | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00245 { 00246 create_and_send_buffers(sock_fd, d_nladdr,UNPUBLISH_SCOPE, id, prefix_id, strategy, LID); 00247 }
void unsubscribe_info | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string | id, | |||
string | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00265 { 00266 create_and_send_buffers(sock_fd, d_nladdr,UNSUBSCRIBE_INFO, id, prefix_id, strategy, LID); 00267 }
void unsubscribe_scope | ( | int | sock_fd, | |
nladdr | d_nladdr, | |||
string | id, | |||
string | prefix_id, | |||
char | strategy, | |||
string | LID | |||
) |
00261 { 00262 create_and_send_buffers(sock_fd,d_nladdr,UNSUBSCRIBE_SCOPE, id, prefix_id, strategy, LID); 00263 }