Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Modify log in udpserver.c #1314

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open

Modify log in udpserver.c #1314

wants to merge 1 commit into from

Conversation

KangLin
Copy link
Contributor

@KangLin KangLin commented Nov 10, 2023

  • Modify log in udpserver.c
  • fix memory leak

@KangLin KangLin force-pushed the udpserver branch 8 times, most recently from f9b4cdc to 7859523 Compare November 14, 2023 02:43
@KangLin
Copy link
Contributor Author

KangLin commented Nov 14, 2023

New output contents:

10:42:45: Debugging /home/build-coturn-Desktop_Qt_5_12_12_GCC_64bit-Debug/bin/turnutils_peer -v -L 192.168.11.30
...
0: (13865): INFO: 192.168.11.30:3480 start
0: (13865): INFO: 192.168.11.30:3481 start
7: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3480 from 192.168.11.49:56473. total received: 100 bytes
7: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3480 from 192.168.11.49:56473. total received: 200 bytes
7: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3480 from 192.168.11.49:56473. total received: 300 bytes
7: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3480 from 192.168.11.49:56473. total received: 400 bytes
7: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3480 from 192.168.11.49:56473. total received: 500 bytes
10: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3481 from 192.168.11.49:62882. total received: 100 bytes
10: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3481 from 192.168.11.49:62882. total received: 200 bytes
10: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3481 from 192.168.11.49:62882. total received: 300 bytes
10: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3481 from 192.168.11.49:62882. total received: 400 bytes
10: (13865): DEBUG: Received 100 bytes data 192.168.11.30:3481 from 192.168.11.49:62882. total received: 500 bytes
10:43:02: Debugging of /home/build-coturn-Desktop_Qt_5_12_12_GCC_64bit-Debug/bin/turnutils_peer -v -L 192.168.11.30
has finished with exit code 0.

stun_buffer buffer;
ioa_addr remote_addr;
int slen = get_ioa_addr_len(&listen->addr);
stun_buffer *buffer = (stun_buffer *)malloc(sizeof(stun_buffer));
Copy link
Collaborator

@eakraly eakraly Nov 19, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

What is the reason to use heap instead of stack as before? It does not go ouside of the function and actually released at the end of it?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This size is large. then maybe stack overflow.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Default stack sizes are large - usually 1MB
Here we are talking about packet size - 1500B? Did you have an issue?
Not that it matters much for a test util but I would still not include unexplained changes to code

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I agree with eakraly

the size of a stun packet shouldn't be large enough to justify always mallocing.

If you're concerned about stack size, you could try to support dynamic buffers, with a default buffer of 1500 on the stack, and then larger buffers allocated if necessary because the incoming data is large enough to justify it.

Though given the nature of async code, i'm not sure it would be possible to determine that prior to attempting to read.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To summarize, I have a fairly strong preference that this buffer be on the stack not on the heap, especially since I've spent the last week trying to track down memory leaks in coturn and C-language code is notorious for memory problems in general.

src/apps/peer/udpserver.c Outdated Show resolved Hide resolved
@KangLin
Copy link
Contributor Author

KangLin commented Nov 20, 2023 via email

- fix memory leak
@KangLin
Copy link
Contributor Author

KangLin commented Nov 23, 2023

@eakraly pls merge!

@@ -58,7 +58,7 @@ int main(int argc, char **argv) {
char **local_addr_list = NULL;
size_t las = 0;
int verbose = TURN_VERBOSE_NONE;
int c;
int c = 0;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can int c be moved down in to the while loop instead ?

@@ -98,6 +98,8 @@ int main(int argc, char **argv) {
}

server_type *server = start_udp_server(verbose, ifname, local_addr_list, las, port);
if (!server)
return -3;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can these return codes be made into named constants?

int slen = get_ioa_addr_len(&listen->addr);
stun_buffer *buffer = (stun_buffer *)malloc(sizeof(stun_buffer));
ioa_addr remote_addr = {0};
if (!buffer)
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Braces please


do {
len = recvfrom(fd, buffer.buf, sizeof(buffer.buf) - 1, 0, (struct sockaddr *)&remote_addr, (socklen_t *)&slen);
len = recvfrom(fd, buffer->buf, sizeof(buffer->buf) - 1, 0, (struct sockaddr *)&remote_addr, (socklen_t *)&slen);
} while (len < 0 && socket_eintr());
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is attempting to read until recvfrom stops failing? recvfrom returns a positive number representing how many bytes were read, or a negative number on failure, and 0 on "no messages available".

I find the way this is setup to be really weird, but maybe that's just the design and i'm not familiar enough with the project to understand yet.


if (len >= 0) {
listen->received += len;
if (listen->verbose) {
uint8_t ra[64] = {0}, la[64] = {0};
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

magic numbers. Please use a named constant or some kind of sizeof() calculation.

Also please don't declare multiple variables on the same line, it makes it impossible to understand what's going on

do {
len = sendto(fd, buffer.buf, buffer.len, 0, (const struct sockaddr *)&remote_addr, (socklen_t)slen);
len = sendto(fd, buffer->buf, buffer->len, 0, (const struct sockaddr *)&remote_addr, (socklen_t)slen);
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe i'm not understanding the design properly, but why are we sending the buffer we just read...? Doesn't the buffer need to be checked for contents of any sort?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unless this particular application is simply intended to spit back the exact buffer it received.
If so, that doesn't seem all that useful, but i could use it in specific test situations.

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This app is an implementation of "peer" so that tests could be run without relying on external code/clients. Echoing back the received packets is what it does - for tests exactly @jonesmz

@KangLin
Copy link
Contributor Author

KangLin commented Jan 20, 2024 via email

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

3 participants