|
The basic approach to this is to SetCapture() on the window you want to stay in. Then scroll it when the mouse moves outside its boundaries. Remember to ReleaseCapture() when you are finished
|
|
|
|
|
Can someone please explain why my code isn't working???
I'm trying to send a raw UDP packet with a self constructed header.
I posted something like this earlier but i investigated the code since then and changed a bit cause it seemed to contain some bugs. BUT the problem remains the same, the code fails at runtime because the sendto() function returns 10049
WSAEADDRNOTAVAIL: Cannot assign requested address. This can also result from connect, sendto!!!, WSAConnect, WSAJoinLeaf, or WSASendTo when the remote address or port is not valid for a remote computer (for example, address or port 0).
Does this mean something might be wrong with my sin structure???? I don't understand, how can it be wrong, i mean, why would it even be USED as it is a raw packet and i constructed the header with the destination ip and port myself!!?? I really don't understand why it won't work, and its been bugging me for several days now so if anyone knows why it won't work, please let me know.
Here's the code:
<code>
#pragma comment( lib, "Ws2_32.lib" )
#define WIN32_LEAN_AND_MEAN
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <conio.h>
#define IP "172.27.2.15"
#define PORT 123
#define IP_DEST "192.168.200.9"
#define PORT_DEST 123
#define SIO_RCVALL _WSAIOW(IOC_VENDOR,1)
#define MAX_ADDR_LEN 16
#define MAX_HOSTNAME_LAN 255
USHORT checksum(USHORT *buffer, int size);
void checkPacket(char * pacote);
struct tcpheader {
unsigned short int th_sport;
unsigned short int th_dport;
unsigned int th_seq;
unsigned int th_ack;
unsigned char th_x2:4;
unsigned char th_off:4;
unsigned char th_flags;
unsigned short int th_win;
unsigned short int th_sum;
unsigned short int th_urp;
};
struct ipheader {
unsigned char ip_hl:4, ip_v:4;
unsigned char ip_tos;
unsigned short int ip_len;
unsigned short int ip_id;
unsigned short int ip_off;
unsigned char ip_ttl;
unsigned char ip_proto;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
};
struct udpheader {
unsigned short int uh_sport;
unsigned short int uh_dport;
unsigned short int uh_len;
unsigned short int uh_sum;
};
int main (void)
{
WSADATA wsd;
char datagram[5000];
unsigned int bOpt=1;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0){
printf("WSAStartup() failed: %d\n", GetLastError());
return -1;
}
SOCKET s = WSASocket(AF_INET, SOCK_RAW, IPPROTO_UDP, NULL, 0,0);
if (s == INVALID_SOCKET){
printf("WSASocket() failed: %d\n", WSAGetLastError());
return -1;
}
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt)) == SOCKET_ERROR){
printf("setsockopt(IP_HDRINCL) failed: %d\n", WSAGetLastError());
return -1;
}
struct ipheader *iph = (struct ipheader *) datagram;
struct udpheader *udph = (struct udpheader *) (datagram + 20);
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons (PORT_DEST);
sin.sin_addr.s_addr = inet_addr (IP_DEST);
memset (datagram, 0, sizeof(datagram));
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ipheader) + sizeof (struct udpheader);
iph->ip_id = 1;
iph->ip_off = 0;
iph->ip_ttl = 100;
iph->ip_proto = 17;
iph->ip_sum = 0;
iph->ip_src = inet_addr(IP);
iph->ip_dst = inet_addr(IP_DEST);
udph->uh_sport = htons (PORT);
udph->uh_dport = htons (PORT_DEST);
udph->uh_len = sizeof (struct ipheader) + sizeof (struct udpheader);
udph->uh_sum = 0;
strcpy(datagram+28,"Teste");
datagram[5000] = NULL;
iph->ip_sum = checksum((unsigned short *)&iph, sizeof(ipheader));
udph->uh_sum = checksum((unsigned short *)&udph, sizeof(udpheader));
checkPacket(datagram);
if (sendto(s, datagram, strlen(datagram), 0, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("sendto() failed: %d\n", WSAGetLastError());
return -1;
}
else{
printf("OK\n");
}
getch();
return 0;
}
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
void checkPacket(char * buff)
{
ipheader* pIpheader;
tcpheader* pTcpheader;
udpheader* pUdpheader;
SOCKADDR_IN saSource, saDest;
int iNextheader;
char * packetData;
pIpheader = (ipheader *) buff;
printf("IP Header \n");
printf("IHL: %d\n",(int)pIpheader->ip_hl);
printf("VERSION: %d\n",pIpheader->ip_v);
printf("TYPE OF SERVICE: %d\n",(int)pIpheader->ip_tos);
printf("TOTAL LENGTH: %d\n",pIpheader->ip_len);
printf("IDENTIFICATION: %d\n",pIpheader->ip_id);
printf("FRAGMENT OFFSET: %d\n",pIpheader->ip_off);
printf("TTL: %d\n",(int)pIpheader->ip_ttl);
printf("PROTOCOL: %d\n", (int)pIpheader->ip_proto);
printf("CHECKSUM: %d\n",pIpheader->ip_sum);
saSource.sin_addr.s_addr = pIpheader->ip_src;
printf("SOURCE ADDRESS: %s\n",inet_ntoa(saSource.sin_addr));
saDest.sin_addr.s_addr = pIpheader->ip_dst;
printf("DESTINATION ADDRESS: %s\n",inet_ntoa(saDest.sin_addr));
iNextheader = (20);
if ((int)pIpheader->ip_proto == 6)
{
pTcpheader = (tcpheader *) (buff + iNextheader);
printf("\nTCP Header\n");
saSource.sin_port = pTcpheader->th_sport;
printf ("SOURCE PORT: %d\n", htons(saSource.sin_port));
saDest.sin_port = pTcpheader->th_dport;
printf ("DESTINATION PORT: %d\n", htons(saDest.sin_port));
printf ("SEQUENCE: %d\n", pTcpheader->th_seq);
printf ("ACK: %d\n", pTcpheader->th_ack);
printf ("X2: %d\n", (int)pTcpheader->th_x2);
printf ("OFF SET: %d\n", (int)pTcpheader->th_off);
printf ("FLAGS: %d\n", (int)pTcpheader->th_flags);
printf ("WINDOW: %d\n", pTcpheader->th_win);
printf ("CHECKSUM: %d\n", pTcpheader->th_sum);
printf ("URP: %d\n", pTcpheader->th_urp);
printf("\nDATA\n");
iNextheader = (pIpheader->ip_hl * 4 + sizeof(tcpheader));
packetData = (char *) (buff + iNextheader);
printf ("DATA: %s\n", packetData);
}
if ((int)pIpheader->ip_proto == 17)
{
printf("\nUDP Header\n");
pUdpheader = (udpheader *) (buff + iNextheader);
saSource.sin_port = pUdpheader->uh_sport;
printf ("SOURCE PORT: %d\n", htons(saSource.sin_port));
saDest.sin_port = pUdpheader->uh_dport;
printf ("DESTINATION PORT: %d\n", htons(saDest.sin_port));
printf ("LEN: %d\n", pUdpheader->uh_len);
printf ("CHECKSUM: %d\n", pUdpheader->uh_sum);
printf("\nDATA\n");
iNextheader = (pIpheader->ip_hl * 4 + sizeof(udpheader));
packetData = (char *) (buff + iNextheader);
printf ("DATA: %s\n", packetData);
}
getch();
}
</code>
Thanks
Kuniva
--------------------------------------------
|
|
|
|
|
Kuniva wrote:
datagram[5000] = NULL;
I did only a cursory check so this is probably not your problem but was wondering what you were trying to do here. datagram is an array of 5000 but has elements 0 to 4999, so if you wanted to put a terminating '\0' you are overwriting the bounds of that array. Also, I don't think you can send a packet over ~1594 in size so I don't know why you would try to send 5000 bytes.
Probably a good idea to remove your strlen() call in the sendto(). BTW, you don't have to terminate your data with a trailing zero but I guess it could make it easier on the other side (recv side) if you knew you were handling strings. As you probably already know you would put the trailing zero after "Teste". Thus, the length in sendto() would be 5 or 6 (incl the terminating zero)
I don't know why you get the 10049 error. Perhaps, you are trying to access a reserved or dedicated port. If you are not, then I would choose a port number > 1023.
|
|
|
|
|
erm..
Well first of all thanks for taking a look. I can understand most of the comments but there's two things i don't seem to understand:
1.: Why get rid of strlen() in sendto()? I can understand that i need to replace it with sizeof() but the argument in sendto() requests the size of datagram, which would be the header + the data
2.: So obviously the second thing i don't understand is this:
"Thus, the length in sendto() would be 5 or 6 (incl the terminating zero)"
Isn't my header in there too? and did i not use strcpy(datagram+28,"Teste") ??? to put the "Teste" string behind the ip+udp header? Or am i missing something here?
I realise you only took a quick look, and the code is somewhat chaotic, but this didn't seem to make sense to me.
I'll also try using a different port number.
Thanks
Kuniva
--------------------------------------------
|
|
|
|
|
Well, I've never done raw before but in the past when I used sendto(), I always put the size of the data only. Search on this forum...within the last week or two somebody asked about how to do raw mode and somebody posted a link to a sample program on MSDN. This is where I downloaded this. Hope it helps:
#include <winsock2.h>
#include <windows.h>
#include <ws2tcpip.h>
#include <stdio.h>
#include <conio.h>
#pragma comment(lib, "ws2_32.lib")
#pragma pack(1)
struct tcpheader {
unsigned short int th_sport;
unsigned short int th_dport;
unsigned int th_seq;
unsigned int th_ack;
unsigned char th_x2:4, th_off:4;
unsigned char th_flags;
unsigned short int th_win;
unsigned short int th_sum;
unsigned short int th_urp;
};
struct ipheader {
unsigned char ip_hl:4, ip_v:4;
unsigned char ip_tos;
unsigned short int ip_len;
unsigned short int ip_id;
unsigned short int ip_off;
unsigned char ip_ttl;
unsigned char ip_p;
unsigned short int ip_sum;
unsigned int ip_src;
unsigned int ip_dst;
};
USHORT checksum(USHORT *buffer, int size)
{
unsigned long cksum=0;
while (size > 1)
{
cksum += *buffer++;
size -= sizeof(USHORT);
}
if (size)
{
cksum += *(UCHAR*)buffer;
}
cksum = (cksum >> 16) + (cksum & 0xffff);
cksum += (cksum >>16);
return (USHORT)(~cksum);
}
#define PORT 25
int main (void)
{
WSADATA wsd;
char datagram[4096];
int bOpt = 1;
if (WSAStartup(MAKEWORD(2,2), &wsd) != 0)
{
printf("WSAStartup() failed: %d\n", GetLastError());
return -1;
}
SOCKET s = socket(AF_INET, SOCK_RAW, IPPROTO_RAW);
if (s == INVALID_SOCKET)
{
printf("WSASocket() failed: %d\n", WSAGetLastError());
return -1;
}
struct ipheader *iph = (struct ipheader *) datagram;
struct tcpheader *tcph = (struct tcpheader *) (datagram + sizeof (struct ipheader));
struct sockaddr_in sin;
sin.sin_family = AF_INET;
sin.sin_port = htons (PORT);
sin.sin_addr.s_addr = inet_addr ("2.3.4.5");
memset (datagram, 0, 4096);
iph->ip_hl = 5;
iph->ip_v = 4;
iph->ip_tos = 0;
iph->ip_len = sizeof (struct ipheader) + sizeof (struct tcpheader);
iph->ip_id = 1;
iph->ip_off = 0;
iph->ip_ttl = 255;
iph->ip_p = 6;
iph->ip_sum = 0;
iph->ip_src = inet_addr ("1.2.3.4");
iph->ip_dst = sin.sin_addr.s_addr;
tcph->th_sport = htons (1234);
tcph->th_dport = htons (PORT);
tcph->th_seq = rand();
tcph->th_ack = 0;
tcph->th_x2 = 0;
tcph->th_off = 5;
tcph->th_flags = 2;
tcph->th_win = htons(65535);
tcph->th_sum = 0;
tcph->th_urp = 0;
iph->ip_sum = checksum((unsigned short *)&iph, sizeof(ipheader) >> 1);
if (setsockopt(s, IPPROTO_IP, IP_HDRINCL, (char *)&bOpt, sizeof(bOpt)) == SOCKET_ERROR)
{
printf("setsockopt(IP_HDRINCL) failed: %d\n", WSAGetLastError());
return -1;
}
printf("Started sending of raw data. Hit any key to quit\n");
while (!kbhit())
{
if (sendto(s, datagram, 40, 0, (SOCKADDR *)&sin, sizeof(sin)) == SOCKET_ERROR)
{
printf("sendto() failed: %d\n", WSAGetLastError());
return -1;
}
printf(".");
Sleep(1000);
tcph->th_seq++;
}
return 0;
}
|
|
|
|
|
along with Johnny's great comments, one thing struck me.
Since your tcp/ip/udp headers are "raw" network data -- they should be packed? Or am I wrong?
#pragma pack(push,1)<br />
struct<br />
{<br />
BYTE x;<br />
BYTE y;<br />
};<br />
#pragma pack(pop)
As I recall, structure members are aligned on DWORD boundaries by default. That is, that a struct like the one above would have 3 bytes of unused data between x and y. You can verify by doing a sizeof() on your data.
Packing it forces the structure members to line up one directly following the next (no space between items).
When in doubt, I always force things to pack for creating network packet headers....
Does this make sense? Does this apply? Am I incorrect?
-p
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~<br />
Peter Weyzen<br />
Staff Engineer<br />
Santa Cruz Networks
|
|
|
|
|
I am planning to create a small application that will utilize a database for managing persistant data.
I'd like to use MySQL as the database engine, but am having a difficult time figuring out how I could distribute the app. without requiring the user to have MySQL installed.
Any help at all would be appreciated in helping me figure out what needs to be done to make this a reality.
|
|
|
|
|
Don't tie your application to any database. Instead, have a requirement that the database be installed. Then have a little wizard or setup program that will request the userid and password for the database. Then use SQL statements to create the DB structure.
The only requirement is that your SQL statements work. I have created entire Access databases using SQL because all I had was the ODBC driver.
--
"The money power of the country will endeavor to prolong its rule by preying upon the prejudices of the people until all wealth is concentrated in a few hands and the Republic destroyed."
-- Abraham Lincoln
|
|
|
|
|
Well, the problem with that solution is that a lot of people will be disinterested in the app based upon that alone. I don't want to have to pile on additional requirements beyond basic system requirements.
I want anybody (Your grandma, for instance) to be able to very simply dload and install this app and be up in running quickly and easily.
|
|
|
|
|
Is an Access DB not up to the task? If it is you can setup an ODBC connection during setup and then use SQL to create an Access db.
You can have a setup that has an "Advanced" setup that will allow someone to use an existing db system instead and just use Access for grandma.
I can't remember where I read this or who said it, but it was something like this: "Make a system that any fool can use and only a fool will use it."
--
"The money power of the country will endeavor to prolong its rule by preying upon the prejudices of the people until all wealth is concentrated in a few hands and the Republic destroyed."
-- Abraham Lincoln
|
|
|
|
|
|
Thanks ... this is the kind of thinking I was looking for!
I'll look into this, and see if it fits my needs. In the mean time ... more suggestions please!!
-G
|
|
|
|
|
I keep an eye on small DB engines and I don't think you'll find too much else out there that is any good and well supported, but if you do let me know.
You haven't said what capabilities you are looking for, but an alternative I'd consider is XML.
Neville Franks, Author of ED for Windows. Free Trial at www.getsoft.com
|
|
|
|
|
Hi all
I was trying to find a way how to get control ID (in my case button)
in run time using some Win API function but failed. I was looking for that ID that is hard coded usually in .h files.
The only similar API GetDlgCtrID() I found but it returns value different for each start of application. But I need an identifier for the button that is persisted and is the same along each start of the application. Similar how it works for the MenuItem IDs that are stored in the resource file.
Please let me know if it possible at all or application forgets this ID when it creates the window and identify it using just handler to the window.
Any advices greatly appreciated
Roman
|
|
|
|
|
GetDlgCtrlID() is what you want. Maybe it's being used incorrectly.
|
|
|
|
|
How to highlight an item in a list control in report view?
I'm using SetSelectionMark(), but it doesn't work. What am I missing?
|
|
|
|
|
Try it with SetItemState()
|
|
|
|
|
|
Hello
is it possible to come to the address (URL) of the active internet explorer window? if yes, how? if no, why not? and any other way as by programing? like java script or so...
thanks
|
|
|
|
|
I haven't tried it, but my first guess would be IE Automation.
|
|
|
|
|
|
Hi SledgeHammer
First of all I want to congratulate you for your CLabel 1.6.
I was wandering if it could be possible add to the class a SetToolTip method.
I've seen there is a simple SetToolTip (plus InitToolTip) method in class CButtonST class found here in Codeproject, but trying to add it to CLabel it doesn't work.
Maybe there is a simple reason because is not working.
Could you please tell me if there is a simple way to add ToolTip fonctionality to the CLabel class?
Best Regards
Doc Pi
|
|
|
|
|
I have many applications that I write that need to be multithreaded because I need to do a lot of background processing and the user interface must be as responsive as possible. In the past I have divided the program into different managers. Each manager would be a CThread derived class (has a message queue) that would handle a specific set of functionality and different priorities. Some examples are CArchiveMgr , CDistributionMgr, CScanMgr. CArchive manager handles all loading and saving of images, documents, cases and does background archiving when the system runs low on space. CDristributionMgr handles all the transfer of data from the client computers, along with the all the processing that needs to occur (compression / encryption...) before and after a transfer. CScanMgr connects to the scanner and receives and processes scanned documents. The system works by broadcasting a thread message with a specific message ID (there are over 70 codes) to all the managers (about 10) along with a DWORD code that is a parameter. Each thread receives the message and only threads that are interested in the message will process the data.
I have seen a few thread pool examples lately and I was thinking that it might be a better design in the future to use a thread pool instead, but my thread pool would have to be more complex than the ones I have seen so far. I do need to have priorities and it would be nice to reschedule jobs that are blocked instead of holding the thread while waiting for a shared resource.
My question is do you have any ideas about this? Pros and Cons?
Thanks in Advance,
John
|
|
|
|
|
Hello,
I am working on an application that uses IE 6 to print HTML files through Adobe distiller to produce PDF files.
Before the upgrade to Windows XP we have been using IE5.5, but under IE6 it seems to truncate the file name that distiller is given to use as the output file for the produced PDF and LOG file. This limitation seems to 34 character for the just the file name! So some our code broke waiting for the wrong file name in ther Adobe "PDF Output" directory.
Has anyone seen this? Is there a registry setting to control this? Or is it a known bug of IE6/Distiller?
Jeff Wilson
Fidelity Investments
|
|
|
|
|
No, I don't want a Shell Namespace Extension, I want something similar. What I want to achieve is something (actually, my problem is more complex, but you can use this as a pretty good example) like the Zip folders on Windows XP, but I would like that every Windows application could access it, not only the shell.
I don't know if I'm being clear here, what I want to achieve is to "map" a zip file in a drive letter. The most similar thing to what I'm trying to do is the M:\ drive on the Exchange Server 2000.
So, what do I need to do? A device driver? A filter driver? Can you point me to some example?
Kant wrote:
Actually she replied back to me "You shouldn't fix the bug. You should kill it"
|
|
|
|
|