# Solved: Ethernet UDP packets not being seen.



## PeterRobertson (Oct 9, 2012)

(I should note that my areas of expertise are not internet-related)

I have a DSP connected directly to a W7 laptop by Ethernet. All firewalls on the laptop are disabled.

IPCONFIG on the laptop gives:

Ethernet adapter Local Area Connection:

Connection-specific DNS Suffix . :
Description . . . . . . . . . . . : Realtek PCIe GBE Family Controller
Physical Address. . . . . . . . . : 00-1E-8C-1F-B7-66
DHCP Enabled. . . . . . . . . . . : No
Autoconfiguration Enabled . . . . : Yes
IPv4 Address. . . . . . . . . . . : 192.168.2.100(Preferred)
Subnet Mask . . . . . . . . . . . : 255.255.255.0
Default Gateway . . . . . . . . . : 0.0.0.0
DNS Servers . . . . . . . . . . . : 192.168.2.101
NetBIOS over Tcpip. . . . . . . . : Enabled

The DSP is sending small (122 byte) packets on the Ethernet at about one per second.
They are all of the form:

src mac: 00-01-02-03-04-05 (the DSP)
dst mac: 00-1E-8C-1F-B7-66 (W7)
source: 192.168.2.100
dest: 192.168.2.102
UDP
src port: 30326 
dst port: 30583
+ 80 bytes of data

Wireshark running on the laptop sees all of the packets arriving.
It does not indicate any errors (checksums or format).

netstat shows received packets delivered going up by the correct number. There are no unknown or discarded packets reported.

I am running a program on the laptop to read the packets, and it is essentially this:

 
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) complain();

in.sin_family = AF_INET;
in.sin_port = htons(30583);
in.sin_addr.s_addr = INADDR_ANY;

if (bind(sock, (struct sockaddr *)&in, sizeof(in) ) < 0) Complain();

for (; ; ) {
 printf("Receiving...\n");
recvfrom(sock, p, 1000, ...
printf("Received\n");​}


Nothing is ever seen. It gets into recvfrom and never leaves, even though everything else indicates that the packets are being continuously delivered.

I have tried changing IP addresses and ports but nothing works.

Any suggestions?


----------



## Ent (Apr 11, 2009)

It's not very easy to help since 
A) I don't know what language you're using
B) You've provided an incomplete code fragment {recvfrom(sock, p, 1000, ...}, also we can't see where you've set most of your variables for example.


----------



## PeterRobertson (Oct 9, 2012)

Fairly obviously C++:

SOCKET sock;
struct sockaddr_in sock_in;
WORD VersionRequested = MAKEWORD(1, 1);
WSADATA Data;
SOCKADDR_IN sender;
int size;
char p[1500];
if (WSAStartup(VersionRequested, &Data)) Fail("unable to initialize WinSock for host info");
sock = socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP);
if (sock < 0) Fail("create socket fails (%d)", WSAGetLastError());

sock_in.sin_family = AF_INET;
sock_in.sin_addr.s_addr = INADDR_ANY;
sock_in.sin_port = htons(30583);
if (bind(sock, (struct sockaddr *)&sock_in, sizeof(sock_in)) < 0) Fail("bind failed (%d)", WSAGetLastError());

for ( ; ; ) {
 printf("Receiving...\n");
size = sizeof(p);
int ret = recvfrom(sock, p, 1500, 0, (SOCKADDR *)&sender, &size);
if (ret ==SOCKET_ERROR) Fail("recvfrom failed (%d)", WSAGetLastError());
printf("Received\n");​}


----------



## Ent (Apr 11, 2009)

The first peculiar thing I notice is that your laptop has the *.*.*.100 address according to ipconfig, but wireshark gives the source as 100 and the destination as 102. You're also listening to the destination port (30583) instead of the source port; I'd think that one should be correct.

The second peculiar thing I notice is that your p buffer is set to get 1500 bytes, even though each packet only provides 122 of them.

I don't use C++ so I can't really comment on the code itself.


----------



## PeterRobertson (Oct 9, 2012)

I see your confusion: the description of the packets was as seen on the laptop by Wireshark.
This means that the laptop is seeing a packet sent to 192.168.2.100:30583 (the laptop) from 192.168.2.102:30326 (the DSP). This is why I am listening to port 30583.

(as this problem is driving me mad, I even tried all four possibilities of swopping src/dst IP and port;
none of them works.)

The third parameter of recvfrom is the actual size of the buffer, not the amount of data expected (which you can't know _ab initio_). The size of data received should be less than or equal to the buffer size and is returned as the result of the function.


----------



## Ent (Apr 11, 2009)

PeterRobertson said:


> I see your confusion: the description of the packets was as seen on the laptop by Wireshark.
> This means that the laptop is seeing a packet sent to 192.168.2.100:30583 (the laptop) from 192.168.2.102:30326 (the DSP). This is why I am listening to port 30583.


My confusion lies in the inconsistency, the *destination *port is the 30583 while the *source *IP is the ...100 one (the laptop). 192.168.2.100:30583 seems to be grabbing one of each.



> (as this problem is driving me mad, I even tried all four possibilities of swopping src/dst IP and port;
> none of them works.)


I know exactly how you feel.



> The third parameter of recvfrom is the actual size of the buffer, not the amount of data expected (which you can't know _ab initio_).
> 
> The size of data received should be less than or equal to the buffer size and is returned as the result of the function.


I thought you had 1500 twice, once overtly in the third parameter and again in the last parameter based on the variable named size. (I'm not quite sure why you're using the & there; I thought that would be equivalent within local scope anyway.)
As best I could work out that last parameter is intended to specify the port buffer size, not the size of your own "p" buffer array.


----------



## PeterRobertson (Oct 9, 2012)

Ent said:


> (I'm not quite sure why you're using the & there; I thought that would be equivalent within local scope anyway.)


As you said you are not familiar with C/C++:

The type of *sock_in* is *(**struct sockaddr_in)* so the type of *&**sock_in* is (*struct sockaddr_in *)*.
This has to be cast to *(struct sockaddr *)* which is the type expected by the function
(note the difference between sockaddr and sockaddr_in).

So, still no further forward on why this trivial thing isn't working.


----------



## Ent (Apr 11, 2009)

No, I meant for &size. I see why now (having re-read the documentation). 
I'd still wonder about that though; should it also be 1500? Or would it be better left as default?


----------



## PeterRobertson (Oct 9, 2012)

Your observation is correct.
 size = sizeof(p);
should be
size = sizeof(sender);

However, that isn't the problem. The code still never manages to read a packet.
I assume that parameter is used to prevent overrunning a tiny buffer with the source address
once a packet has been read, but as nothing is ever read, it's irrelevant.


----------



## PeterRobertson (Oct 9, 2012)

I have found the problem.

The UDP packets that were being sent were computing the UDP checksum incorrectly and this
was causing the packets to be discarded by Windows.

This problem was hidden because Wireshark was claiming that the checksum was being ignored, so I hadn't
investigated it.


----------



## Ent (Apr 11, 2009)

So it wasn't a problem in your code at all?
That sounds, pretty typical actually. Have you managed to fix the sent packets?


----------



## PeterRobertson (Oct 9, 2012)

Yes. The code has been working perfectly since the packet checksums were set correctly.


----------

