For some reason, it seems like I keep getting 10-Byte 802.11 MAC headers from pcap in c/c++ and I don't know why.
Some intro details:
Yes, I'm in monitor mode
Yes, I'm using wlan1mon
I checked that pcap_open_live returned non-null
I checked that pcap_datalink returned 127 (802.11 w/ radiotap header)
I have had a really hard time finding a good reference for the 802.11 MAC header. The Ethernet header, IPv4 header, etc have all had really good references which went into all the necessary detail about every field and how you know if it is/isn't present and/or valid... but nobody even says whether addr4 is omitted entirely if unnecessary or if it's just 0-filled/unfilled. Or what the arrangement of the header is given the different types/subtypes (one site suggested that, sometimes, the frame is just the control, duration, and MAC for acknowledgements, but no other site I've found says the same).
A quick reference for code segments below: I made a macro Assert (which I usually give longer names that conform to the spec, but for now it's just that) which has a condition as the first argument and, if it fails, it uses a stringstream to construct a string and throws a runtime_error if it's false. This lets me make very descriptive error messages which include local variable values when necessary.
Ok, here're where I currently am. Note that this is my first program with pcap and I'm writing it entirely on a Raspberry Pi in vim over ssh from git-bash from Windows, so I'm not exactly in ideal circumstances for formatting. Also things get messy when I try to make the stupid thing not
namespace
{
struct ieee80211_radiotap_header {
u_int8_t it_version; /* set to 0 */
u_int8_t it_pad;
u_int16_t it_len; /* entire length */
u_int32_t it_present; /* fields present */
} __attribute__((__packed__));
static_assert ( sizeof(ieee80211_radiotap_header)==8 , "Bad packed structure" ) ;
/* Presence bits */
enum {
RADIOTAP_TSFT = 0 ,
RADIOTAP_FLAGS = 1 ,
RADIOTAP_RATE = 2 ,
RADIOTAP_CHANNEL = 3 ,
RADIOTAP_FHSS = 4 ,
RADIOTAP_ANTENNA_SIGNAL = 5 ,
RADIOTAP_ANTENNA_NOISE = 6 ,
RADIOTAP_LOCK_QUALITY = 7 ,
RADIOTAP_TX_ATTENUATION = 8 ,
RADIOTAP_DB_TX_ATTENUATION = 9 ,
RADIOTAP_DBM_TX_POWER = 10 ,
RADIOTAP_ANTENNA = 11 ,
RADIOTAP_DB_ANTENNA_SIGNAL = 12 ,
} ;
typedef array<uint8_t,6> MAC ;
static_assert ( is_pod<MAC>::value , "MAC is not a POD type" ) ;
static_assert ( sizeof(MAC)==6 , "MAC is not 6-Bytes" ) ;
string MAC2String ( MAC const& m ) {
string rval = "__:__:__:__" ;
for ( auto iByte(0) ; iByte<6 ; ++iByte ) {
static char const * const hex = "0123456789abcdef" ;
rval[3*iByte] = hex [ ( m[iByte] & 0xF0 ) >> 4 ] ;
rval[3*iByte+1] = hex [ m[iByte] & 0x0F ] ;
}
return rval ;
}
void handlePacket ( u_char * args , pcap_pkthdr const * header , u_char const * packet ) {
static_assert ( sizeof(u_char)==1 , "Huh?" ) ;
//cout << "Packet; " << header->caplen << " of " << header->len << " captured" << endl ;
size_t expectedSize = sizeof(ieee80211_radiotap_header) ;
Assert ( header->caplen>=expectedSize , "Capture is not big enough; expecting " << expectedSize << " so far, but only have " << header->caplen ) ;
uint8_t const* radioHeader = packet ;
auto rx = reinterpret_cast<ieee80211_radiotap_header const*> ( radioHeader ) ;
expectedSize += rx->it_len - sizeof(ieee80211_radiotap_header) ; // add the radiotap body length
Assert ( header->caplen>=expectedSize , "Capture is not big enough; expecting " << expectedSize << " so far, but only have " << header->caplen ) ;
// Look at the 802.11 Radiotap Header
if ( header->caplen == expectedSize ) {
cout << "Packet contains ONLY " << expectedSize << "-Byte RadioTap header" << endl ;
return ;
}
// From this point forward, all error messages should subtract rx->it_len so that the error reflects the 802.11 frame onward
// and does not include the radiotap header.
// Look at the 802.11 MAC Header
expectedSize += 2+2+2+4 ; // everything but the four addresses
Assert ( header->caplen>=expectedSize , "Frame is not big enough; expecting " << expectedSize-rx->it_len << " so far, but only have " << header->caplen-rx->it_len ) ;
uint8_t const* frameHeader = radioHeader + rx->it_len ;
uint8_t version = frameHeader[0] & 3 ;
Assert ( version==0 , "Bad 802.11 MAC version: " << int(version) ) ;
uint8_t frameType = ( frameHeader[0] >> 2 ) & 0x03 ;
uint8_t frameSubtype= ( frameHeader[0] >> 4 ) & 0x0F ;
bool toDS = frameHeader[1] & 0x01 ;
bool fromDS = frameHeader[1] & 0x02 ;
bool isWEP = frameHeader[1] & (1<<6) ;
MAC const* addr1 = reinterpret_cast<MAC const*> ( frameHeader + 4 ) ;
MAC const* addr2 = reinterpret_cast<MAC const*> ( frameHeader + 10 ) ;
MAC const* addr3 = reinterpret_cast<MAC const*> ( frameHeader + 16 ) ;
MAC const* addr4 = reinterpret_cast<MAC const*> ( frameHeader + 24 ) ;
MAC const* bssid (0) ;
MAC const* da ;
MAC const* sa ;
MAC const* ra ;
MAC const* ta ;
char const* desc ;
// Table found here: https://www.rfwireless-world.com/Articles/WLAN-MAC-layer-protocol.html
if ( !toDS && !fromDS ) {
desc = "STA->STA" ;
da = addr1 ;
sa = addr2 ;
bssid= addr3 ;
// inferred
ta = sa ;
ra = da ;
expectedSize += 6+6+6 ;
} else if ( !toDS && fromDS ) {
desc = "Base->STA" ;
da = addr1 ;
bssid= addr2 ;
sa = addr3 ;
// inferred
ta = bssid ;
ra = da ;
expectedSize += 6+6+6 ;
} else if ( toDS && !fromDS ) {
desc = "STA->Base" ;
bssid= addr1 ;
sa = addr2 ;
da = addr3 ;
// inferred
ta = sa ;
ra = bssid ;
expectedSize += 6+6+6 ;
} else if ( toDS && fromDS ) {
desc = "Base->Base" ;
ra = addr1 ;
ta = addr2 ;
da = addr3 ;
sa = addr4 ;
expectedSize += 6+6+6+6 ;
}
Assert ( header->caplen>=expectedSize , "Frame is not big enough; expecting " << expectedSize-rx->it_len << " so far, but only have " << header->caplen-rx->it_len << " for a " << desc << " frame of type " << int(frameType) << '/' << int(frameSubtype) ) ;
cout << desc << "\t" << MAC2String(*ta) << '/' << MAC2String(*sa) << " --> " << MAC2String(*ra) << '/' << MAC2String(*da) << endl ;
//cout << MAC2String(addr1) << " / " << MAC2String(addr2) << " --> " << MAC2String(addr3) << " / " << MAC2String(addr4) << endl ;
//cout << " " << int(frameType) << " / " << int(frameSubtype) << endl ;
}
}
int main ( int , char const* * )
{
cout << "Hello, world" << endl ;
auto devName = "wlan1mon" ;
char errBuf[PCAP_ERRBUF_SIZE] ;
auto maxTime = 0 ; // ms
auto maxPacketCount = 0 ;
auto dev = pcap_open_live ( devName , BUFSIZ , maxPacketCount , maxTime , errBuf ) ;
Assert ( dev!=0 , "Failed to open pcap: " << errBuf ) ;
auto linkType = pcap_datalink ( dev ) ;
// IEEE 802.11 link type is 105, from tcpdump.org/linktypes.html
// 127 is the 802.11 radiotap which is even better
Assert ( linkType==127 , "Link type was " << linkType << ", but expected " << 127 ) ;
pcap_loop ( dev , maxPacketCount , handlePacket , /*user*/0 ) ;
cout << "Exiting" << endl ;
return 0 ;
}
But what I get very quickly is an error that the 802.11 frame (after the radiotap) is 10-Bytes long (this is from the last Assert in the code, after the to/from DS).
I've tried a few combinations of reading the Frame Control (whole little-endian uint16_t at a time, or byte-by-byte, b0-first or b0-last, etc) because there seems to be no good spec on how it is arranged. I do run into STA->STA frames most often (as detected/reported by this code), which seems unlikely given my environment.
What am I missing? Clearly I am missing something.
For reference, the radiotap it_len is always either 18 Bytes or 21 Bytes as reported by this code, which also seems odd.
I have had a really hard time finding a good reference for the 802.11 MAC header.
If by "good" you mean "simple", unfortunately that's impossible, because the header isn't simple the way the 802.3 Ethernet header is. :-)
There's always IEEE Std 802.11-2016; see section 9.2 "MAC frame formats".
(one site suggested that, sometimes, the frame is just the control, duration, and MAC for acknowledgements, but no other site I've found says the same).
Frame control, duration, receiver address, and CRC. That's it - and that's 14 octets, with the CRC being the last 4 octets, so if the CRC isn't present in the packet, that would be 10 octets.
I'm trying to extract the 3x3 rotation matrix from the 3x4 pose matrix I have. However, two values are differing even though I have very simple code setting one to the other. I'm banging my head against the wall because I have no idea why this is happening. Here is the code:
std::cout << "Camera pose matrix from optical flow homography" << std::endl;
for (int e = 0; e < pose.rows; e++) {
for (int f = 0; f < pose.cols; f++) {
std::cout << pose.at<double>(e,f) << " " << e << " " << f;
std::cout << " ";
}
std::cout << "\n" << std::endl;
}
std::cout << "Creating rotation matrix" << std::endl;
Mat rotvec = Mat::eye(3, 3, CV_32FC1);
for (int s = 0; s < pose.rows; s++) {
for (int g = 0; g < pose.cols-1; g++) {
rotvec.at<double>(s, g) = pose.at<double>(s,g);
std::cout << rotvec.at<double>(s,g) << " " << s << " " << g;
std::cout << " ";
}
std::cout << "\n" << std::endl;
}
std::cout << "Rotation matrix" << std::endl;
for (int e = 0; e < pose.rows; e++) {
for (int f = 0; f < pose.cols-1; f++) {
std::cout << rotvec.at<double>(e,f) << " " << e << " " << f;
std::cout << " ";
std::cout << pose.at<double>(e,f) << " " << e << " " << f;
std::cout << " ";
}
std::cout << "\n" << std::endl;
}
Here is the output:
Camera pose matrix from optical flow homography
5.26354e-315 0 0 0 0 1 0.0078125 0 2 0 0 3
0.0078125 1 0 0 1 1 0 1 2 5.26354e-315 1 3
0 2 0 5.26354e-315 2 1 1.97626e-323 2 2 7.64868e-309 2 3
Creating rotation matrix
5.26354e-315 0 0 0 0 1 0.0078125 0 2
0.0078125 1 0 0 1 1 0 1 2
0 2 0 5.26354e-315 2 1 1.97626e-323 2 2
Rotation matrix
5.26354e-315 0 0 5.26354e-315 0 0 0 0 1 0 0 1 5.26354e-315 0 2 0.0078125 0 2
0.0078125 1 0 0.0078125 1 0 0 1 1 0 1 1 0.0078125 1 2 0 1 2
0 2 0 0 2 0 5.26354e-315 2 1 5.26354e-315 2 1 1.97626e-323 2 2 1.97626e-323 2 2
Here you can see I'm trying to save the first three columns of pose into the rotvec matrix. When I actually set the rotation matrix equal to the pose for those three columns, I get the correct matrix, as the second matrix is equal to the first three columns of the first matrix. However, when I check the rotation matrix once again, (third matrix) it is not the same as the output I require on coordinates (0, 2) and (1, 2). (I outputted the rotvec matrix number next to the pose matrix number, and you can see at these coordinates the numbers do not match). I am not sure why this is happening, could someone please help me out?
Solved my problem for anyone else who stumbles upon this later: I just changed the Mat type to CV_64F (to make it double) for both rotvec and pose, and used all to display. Creds to berak for pointing me in the right direction.
Something go wrong and I don't know what, because I comprate with tool and the result is the same. The Tools and my code return the same Authorization Header , but twitter said: Failed to validate oauth signature and token
http://nouncer.com/oauth/authentication.html
http://nouncer.com/oauth/signature.html
<script language="javascript" type="text/javascript" runat="server">
/*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS PUB 180-1
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
*/
/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_sha1(s) { return binb2hex(core_sha1(str2binb(s), s.length * chrsz)); }
function b64_sha1(s) { return binb2b64(core_sha1(str2binb(s), s.length * chrsz)); }
function str_sha1(s) { return binb2str(core_sha1(str2binb(s), s.length * chrsz)); }
function hex_hmac_sha1(key, data) { return binb2hex(core_hmac_sha1(key, data)); }
function b64_hmac_sha1(key, data) { return binb2b64(core_hmac_sha1(key, data)); }
function str_hmac_sha1(key, data) { return binb2str(core_hmac_sha1(key, data)); }
/*
* Perform a simple self-test to see if the VM is working
*/
function sha1_vm_test() {
return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
}
/*
* Calculate the SHA-1 of an array of big-endian words, and a bit length
*/
function core_sha1(x, len) {
/* append padding */
x[len >> 5] |= 0x80 << (24 - len % 32);
x[((len + 64 >> 9) << 4) + 15] = len;
var w = Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j < 80; j++) {
if (j < 16) w[j] = x[i + j];
else w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return Array(a, b, c, d, e);
}
/*
* Perform the appropriate triplet combination function for the current
* iteration
*/
function sha1_ft(t, b, c, d) {
if (t < 20) return (b & c) | ((~b) & d);
if (t < 40) return b ^ c ^ d;
if (t < 60) return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
/*
* Determine the appropriate additive constant for the current iteration
*/
function sha1_kt(t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514;
}
/*
* Calculate the HMAC-SHA1 of a key and some data
*/
function core_hmac_sha1(key, data) {
var bkey = str2binb(key);
if (bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16);
for (var i = 0; i < 16; i++) {
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
return core_sha1(opad.concat(hash), 512 + 160);
}
/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
/*
* Bitwise rotate a 32-bit number to the left.
*/
function rol(num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
/*
* Convert an 8-bit or 16-bit string to an array of big-endian words
* In 8-bit function, characters >255 have their hi-byte silently ignored.
*/
function str2binb(str) {
var bin = Array();
var mask = (1 << chrsz) - 1;
for (var i = 0; i < str.length * chrsz; i += chrsz)
bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i % 32);
return bin;
}
/*
* Convert an array of big-endian words to a string
*/
function binb2str(bin) {
var str = "";
var mask = (1 << chrsz) - 1;
for (var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i >> 5] >>> (32 - chrsz - i % 32)) & mask);
return str;
}
/*
* Convert an array of big-endian words to a hex string.
*/
function binb2hex(binarray) {
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for (var i = 0; i < binarray.length * 4; i++) {
str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +
hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
}
return str;
}
/*
* Convert an array of big-endian words to a base-64 string
*/
function binb2b64(binarray) {
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for (var i = 0; i < binarray.length * 4; i += 3) {
var triplet = (((binarray[i >> 2] >> 8 * (3 - i % 4)) & 0xFF) << 16)
| (((binarray[i + 1 >> 2] >> 8 * (3 - (i + 1) % 4)) & 0xFF) << 8)
| ((binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xFF);
for (var j = 0; j < 4; j++) {
if (i * 8 + j * 6 > binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F);
}
}
return str;
}
</script>
<%
function normalizar(cadena)
normalizar=replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(replace(cadena,"^","%5E"),"&","%26"),"`","%60"),"{","%7B"),"}","%7D"),"|","%7C"),"]","%5D"),"[","%5B"),"""","%22"),"<","%3C"),">","%3E"),"\","%5C"),":","%3A"),"/","%2F"),"=","%3D")
end function
url = "https://api.twitter.com/oauth/request_token"
web_ajax="http://localhost/cet/urbannews/modulo/twitter/ajax.asp"
m_strUserAgent="Test app"
m_strHost="api.twitter.com"
oauth_consumer_key = "fAEFB4ZX4fmBSAIkxLnkG6R0q"
oauth_consumer_sec = "a9Yi1C3Pfj6od5fee3GdII46nNqDQ9mDzpk4bqoGrumjisMWis"
oauth_nonce = Year(now) & Month(now) & Day(now) & Hour(now) & Minute(now) & Second(now) & Replace(Request.ServerVariables("REMOTE_ADDR"),".","")
oauth_signature_method = "HMAC-SHA1"
oauth_timestamp = DateDiff("s", "01/01/1970 00:00:00", Now())
oauth_version = "1.0"
oauth_sign = "oauth_callback=" & replace(Server.urlencode(normalizar(web_ajax)),"%2E",".") & "&oauth_consumer_key=" & oauth_consumer_key & "&oauth_nonce=" & oauth_nonce & "&oauth_signature_method=" & oauth_signature_method & "&oauth_timestamp=" & oauth_timestamp & "&oauth_token=&oauth_version=" & oauth_version
response.Write oauth_sign & "<br /><br />"
oauth_sign =replace(replace(oauth_sign,"&","%26"),"=","%3D")
response.Write oauth_sign & "<br /><br />"
response.Write "POST&" & normalizar(url) & "&" & normalizar(oauth_sign) & "<br /><br />"
oauth_signature = b64_hmac_sha1(oauth_consumer_sec&"&", "POST&" & normalizar(url) & "&" & normalizar(oauth_sign))
response.Write oauth_consumer_key & "<br /><br />"
response.Write oauth_nonce & "<br />"
response.Write oauth_timestamp & "<br /><br />"
response.Write oauth_signature & "<br /><br />"
PARAM_AUTH="OAuth realm="""&url&""", oauth_callback=" & Server.urlencode(web_ajax) & ", oauth_consumer_key=""" & oauth_consumer_key & """, oauth_token="""", oauth_nonce=""" & oauth_nonce & """, oauth_timestamp=""" & oauth_timestamp & """, oauth_signature_method=""HMAC-SHA1"", oauth_version=""1.0"", oauth_signature=""" & normalizar(oauth_signature) & """"
response.write PARAM_AUTH& "<br /><br />"
Set objSrvHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP")
objSrvHTTP.setTimeouts 10000, 10000, 15000, 15000
objSrvHTTP.Open "POST", url & "?oauth_callback=" & Server.urlencode(normalizar(web_ajax)), False
objSrvHTTP.setRequestHeader "Authorization", PARAM_AUTH
objSrvHTTP.setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
objSrvHTTP.SetRequestHeader "User-Agent", m_strUserAgent
objSrvHTTP.SetRequestHeader "Host", m_strHost
objSrvHTTP.Send
response.Write objSrvHTTP.status & " - " & objSrvHTTP.statusText & " - " & url & "<br /><br />"
response.Write objSrvHTTP.responseText
Set objSrvHTTP = Nothing
%>
I find the problem. Is my normalice function and other encode string. Now with that code work.
<script language="javascript" type="text/javascript" runat="server">
/*
* A JavaScript implementation of the Secure Hash Algorithm, SHA-1, as defined
* in FIPS PUB 180-1
* Version 2.1a Copyright Paul Johnston 2000 - 2002.
* Other contributors: Greg Holt, Andrew Kepert, Ydnar, Lostinet
* Distributed under the BSD License
* See http://pajhome.org.uk/crypt/md5 for details.
*/
/*
* Configurable variables. You may need to tweak these to be compatible with
* the server-side, but the defaults work in most cases.
*/
var hexcase = 0; /* hex output format. 0 - lowercase; 1 - uppercase */
var b64pad = "="; /* base-64 pad character. "=" for strict RFC compliance */
var chrsz = 8; /* bits per input character. 8 - ASCII; 16 - Unicode */
/*
* These are the functions you'll usually want to call
* They take string arguments and return either hex or base-64 encoded strings
*/
function hex_sha1(s) { return binb2hex(core_sha1(str2binb(s), s.length * chrsz)); }
function b64_sha1(s) { return binb2b64(core_sha1(str2binb(s), s.length * chrsz)); }
function str_sha1(s) { return binb2str(core_sha1(str2binb(s), s.length * chrsz)); }
function hex_hmac_sha1(key, data) { return binb2hex(core_hmac_sha1(key, data)); }
function b64_hmac_sha1(key, data) { return binb2b64(core_hmac_sha1(key, data)); }
function str_hmac_sha1(key, data) { return binb2str(core_hmac_sha1(key, data)); }
/*
* Perform a simple self-test to see if the VM is working
*/
function sha1_vm_test() {
return hex_sha1("abc") == "a9993e364706816aba3e25717850c26c9cd0d89d";
}
/*
* Calculate the SHA-1 of an array of big-endian words, and a bit length
*/
function core_sha1(x, len) {
/* append padding */
x[len >> 5] |= 0x80 << (24 - len % 32);
x[((len + 64 >> 9) << 4) + 15] = len;
var w = Array(80);
var a = 1732584193;
var b = -271733879;
var c = -1732584194;
var d = 271733878;
var e = -1009589776;
for (var i = 0; i < x.length; i += 16) {
var olda = a;
var oldb = b;
var oldc = c;
var oldd = d;
var olde = e;
for (var j = 0; j < 80; j++) {
if (j < 16) w[j] = x[i + j];
else w[j] = rol(w[j - 3] ^ w[j - 8] ^ w[j - 14] ^ w[j - 16], 1);
var t = safe_add(safe_add(rol(a, 5), sha1_ft(j, b, c, d)),
safe_add(safe_add(e, w[j]), sha1_kt(j)));
e = d;
d = c;
c = rol(b, 30);
b = a;
a = t;
}
a = safe_add(a, olda);
b = safe_add(b, oldb);
c = safe_add(c, oldc);
d = safe_add(d, oldd);
e = safe_add(e, olde);
}
return Array(a, b, c, d, e);
}
/*
* Perform the appropriate triplet combination function for the current
* iteration
*/
function sha1_ft(t, b, c, d) {
if (t < 20) return (b & c) | ((~b) & d);
if (t < 40) return b ^ c ^ d;
if (t < 60) return (b & c) | (b & d) | (c & d);
return b ^ c ^ d;
}
/*
* Determine the appropriate additive constant for the current iteration
*/
function sha1_kt(t) {
return (t < 20) ? 1518500249 : (t < 40) ? 1859775393 :
(t < 60) ? -1894007588 : -899497514;
}
/*
* Calculate the HMAC-SHA1 of a key and some data
*/
function core_hmac_sha1(key, data) {
var bkey = str2binb(key);
if (bkey.length > 16) bkey = core_sha1(bkey, key.length * chrsz);
var ipad = Array(16), opad = Array(16);
for (var i = 0; i < 16; i++) {
ipad[i] = bkey[i] ^ 0x36363636;
opad[i] = bkey[i] ^ 0x5C5C5C5C;
}
var hash = core_sha1(ipad.concat(str2binb(data)), 512 + data.length * chrsz);
return core_sha1(opad.concat(hash), 512 + 160);
}
/*
* Add integers, wrapping at 2^32. This uses 16-bit operations internally
* to work around bugs in some JS interpreters.
*/
function safe_add(x, y) {
var lsw = (x & 0xFFFF) + (y & 0xFFFF);
var msw = (x >> 16) + (y >> 16) + (lsw >> 16);
return (msw << 16) | (lsw & 0xFFFF);
}
/*
* Bitwise rotate a 32-bit number to the left.
*/
function rol(num, cnt) {
return (num << cnt) | (num >>> (32 - cnt));
}
/*
* Convert an 8-bit or 16-bit string to an array of big-endian words
* In 8-bit function, characters >255 have their hi-byte silently ignored.
*/
function str2binb(str) {
var bin = Array();
var mask = (1 << chrsz) - 1;
for (var i = 0; i < str.length * chrsz; i += chrsz)
bin[i >> 5] |= (str.charCodeAt(i / chrsz) & mask) << (32 - chrsz - i % 32);
return bin;
}
/*
* Convert an array of big-endian words to a string
*/
function binb2str(bin) {
var str = "";
var mask = (1 << chrsz) - 1;
for (var i = 0; i < bin.length * 32; i += chrsz)
str += String.fromCharCode((bin[i >> 5] >>> (32 - chrsz - i % 32)) & mask);
return str;
}
/*
* Convert an array of big-endian words to a hex string.
*/
function binb2hex(binarray) {
var hex_tab = hexcase ? "0123456789ABCDEF" : "0123456789abcdef";
var str = "";
for (var i = 0; i < binarray.length * 4; i++) {
str += hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8 + 4)) & 0xF) +
hex_tab.charAt((binarray[i >> 2] >> ((3 - i % 4) * 8)) & 0xF);
}
return str;
}
/*
* Convert an array of big-endian words to a base-64 string
*/
function binb2b64(binarray) {
var tab = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";
var str = "";
for (var i = 0; i < binarray.length * 4; i += 3) {
var triplet = (((binarray[i >> 2] >> 8 * (3 - i % 4)) & 0xFF) << 16)
| (((binarray[i + 1 >> 2] >> 8 * (3 - (i + 1) % 4)) & 0xFF) << 8)
| ((binarray[i + 2 >> 2] >> 8 * (3 - (i + 2) % 4)) & 0xFF);
for (var j = 0; j < 4; j++) {
if (i * 8 + j * 6 > binarray.length * 32) str += b64pad;
else str += tab.charAt((triplet >> 6 * (3 - j)) & 0x3F);
}
}
return str;
}
</script>
<%
Response.CharSet = "utf-8"
Public Function EncodeString(s)
Dim strTmpVal : strTmpVal = s
Dim strRetVal : strRetVal = ""
Dim intAsc : intAsc = 0
Dim strHex : strHex = ""
Const OAUTH_UNRESERVED = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789-_.~"
Dim i, strChr : For i = 1 To Len(strTmpVal)
strChr = Mid(strTmpVal, i, 1)
If InStr(1, OAUTH_UNRESERVED, strChr) = 0 Then
intAsc = Asc(strChr)
If intAsc < 32 Or intAsc > 126 Then
'strHex = encodeURIComponent(strChr)
Else
strHex = "%" & Hex(intAsc)
End If
strRetVal = strRetVal & strHex
Else
strRetVal = strRetVal & strChr
End If
Next
EncodeString = strRetVal
End Function
url = "https://api.twitter.com/oauth/request_token"
web_ajax="https://www.mydomain.com/my-callback.html"
m_strUserAgent="Test app asp"
m_strHost="api.twitter.com"
m_metodo="POST"
oauth_consumer_key = "fAEFB4ZX4fmBSAIkxLnkG6R0q"
oauth_consumer_sec = "a9Yi1C3Pfj6od5fee3GdII46nNqDQ9mDzpk4bqoGrumjisMWis"
oauth_nonce = Year(now) & Month(now) & Day(now) & Hour(now) & Minute(now) & Second(now) & Replace(Request.ServerVariables("REMOTE_ADDR"),".","")
oauth_signature_method = "HMAC-SHA1"
oauth_timestamp = DateDiff("s", "01/01/1970 00:00:00", Now())
oauth_version = "1.0"
oauth_sign = "oauth_callback=" & EncodeString(web_ajax) & "&oauth_consumer_key=" & oauth_consumer_key & "&oauth_nonce=" & oauth_nonce & "&oauth_signature_method=" & oauth_signature_method & "&oauth_timestamp=" & oauth_timestamp & "&oauth_version=" & oauth_version
response.Write oauth_sign & "<br /><br />"
response.Write m_metodo & "&" & EncodeString(url) & "&" & EncodeString(oauth_sign) & "<br /><br />"
oauth_signature = b64_hmac_sha1(oauth_consumer_sec&"&", m_metodo & "&" & EncodeString(url) & "&" & EncodeString(oauth_sign))
'response.Write oauth_consumer_key & "<br /><br />"
response.Write oauth_nonce & "<br />"
response.Write oauth_timestamp & "<br /><br />"
response.Write oauth_signature & "<br /><br />"
PARAM_AUTH="oauth_callback=" & EncodeString(web_ajax) & "&oauth_consumer_key=" & oauth_consumer_key & "&oauth_nonce=" & oauth_nonce & "&oauth_signature_method=HMAC-SHA1&oauth_timestamp=" & oauth_timestamp & "&oauth_version="&oauth_version&"&oauth_signature=" & EncodeString(oauth_signature)
response.write PARAM_AUTH& "<br /><br />"
response.write "<br /><br />INFO POST<br /><br />"
requesturl=url & "?" & PARAM_AUTH
response.write "<br /><b>URL: </b>" & url
response.write "<br /><br /><b>Request URL: </b>" & requesturl
response.write "<br /><br /><b>Content-Type: </b>" & "application/x-www-form-urlencoded"
response.write "<br /><br /><b>Authorization: </b>" & PARAM_AUTH
response.write "<br /><br /><b>User-Agent: </b>" & m_strUserAgent
response.write "<br /><br /><b>Host: </b>" & m_strHost
response.write "<br />RESPONSE: <br /><br />"
Set objSrvHTTP = Server.CreateObject("Msxml2.ServerXMLHTTP")
'objSrvHTTP.setTimeouts 10000, 10000, 15000, 15000
objSrvHTTP.Open m_metodo, requesturl, False
objSrvHTTP.setOption(2)=13056
objSrvHTTP.SetRequestHeader "Content-Type","application/x-www-form-urlencoded"
objSrvHTTP.SetRequestHeader "User-Agent", m_strUserAgent
objSrvHTTP.SetRequestHeader "Host", m_strHost
objSrvHTTP.Send
response.Write objSrvHTTP.status & " - " & objSrvHTTP.statusText & "<br />"
response.Write objSrvHTTP.responseText
Set objSrvHTTP = Nothing
%>
I have a USB printer device. I want to send file data to the USB printer from Linux. I am using libUsb for my code. I am getting timeout (libusb return value -7) always while sending. But I can able to send data in Windows for the same printer. What went wrong ? It seems ehci or uhci is not sending data to the printer. Please Help .
OS : Ubuntu 12.04 (32 Bit)
The below is my code snippet.
dev_handle = libusb_open_device_with_vid_pid(ctx, PRINTER_VID, PRINTER_PID);
if (dev_handle == NULL)
{
cout << "Cannot open device" << endl;
libusb_free_device_list(devs, 1); //free the list, unref the devices in it
return;
}
else
{
cout << "Device Opened" << endl;
}
if (libusb_kernel_driver_active(dev_handle, 0) == 1) //find out if kernel driver is attached
{
cout << "Kernel Driver Active" << endl;
if (libusb_detach_kernel_driver(dev_handle, 0) == 0) //detach it
cout << "Kernel Driver Detached!" << endl;
}
r = libusb_claim_interface(dev_handle, 0);
if (r < 0)
{
cout << "Cannot Claim Interface" << endl;
return 1;
}
cout << "Claimed Interface" << endl;
cout << "Writing Data..." << endl;
memset(data_buffer,0,64);
while(fgets((char *)data_buffer,64,fp))
{
errno = 0;
r = libusb_bulk_transfer(dev_handle,0x081 | LIBUSB_ENDPOINT_OUT, data_buffer, 64,&actual, 10);
cout<<"The return value of r is "<<r<< "::::" << actual << endl ;
memset(data_buffer,0,64);
}
The output is
The return value of r is -7::::0
The return value of r is -7::::0
Try to execute following comamnd from superuser and then reconnect device.
echo 0 > /sys/bus/usb/drivers_autoprobe
It helped me with some devices.