codeplea / hands-on-network-programming-with-c Goto Github PK
View Code? Open in Web Editor NEWBook Example Code for Hands-On Network Programming with C
Home Page: https://handsonnetworkprogramming.com/
License: MIT License
Book Example Code for Hands-On Network Programming with C
Home Page: https://handsonnetworkprogramming.com/
License: MIT License
I couldn't successfully compile with various errors on OpenSSL using 1.0.2s installed from brew.
The working code
printf("OpenSSL version: %s\n", SSLeay_version(SSLEAY_VERSION));
Tried running web_server2.c which did not render, I think adding next got it working?
struct client_info *next = client->next;
Have compiled with OpenSSL 1.0.2s result in error, replaced with the sample code from OpenSSL site.
Replace with working code:
SSL_CTX *ctx = SSL_CTX_new(SSLv23_server_method());
Hi Lewis,
I'm running the UDP server code through Coverity analysis and it states then returning here:
means socket_listen leaks resources when it goes out of scope. If you try to CLOSESOCKET(socket_listen);
there, then it complains (rightly so) that:
54 if (!ISVALIDSOCKET(socket_listen)) {
55 fprintf(stderr, "socket() failed. (%d)\n", GETSOCKETERRNO());
>>> CID 364052: Error handling issues (NEGATIVE_RETURNS)
>>> "socket_listen" is passed to a parameter that cannot be negative.
return 1;
what is the recommended technique here to handle that?
when I run the "ssh_download.exe localhost 22 username" command in cmd.exe, I try to download the test.txt that I have created in advance. But ssh_scp_pull_request() failed. Then I try to use the absolute path, it still failed. I promise that the test.txt is existing in that path.
I don't know the reason.
FD_ISSSET -> FD_ISSET
Thank you for such a great book!
When I compile and run the code for time_server.c I get the following:
bind() failed. (98)
Configuring local address...
Creating socket...
Binding socket to local address...
[Finished in 0.0s with exit code 1]
[shell_cmd: gcc "/home/chuck/ContEd/HOCN/SampleCode/chap02/time_server.c" -o "/home/chuck/ContEd/HOCN/SampleCode/chap02/time_server" && "/home/chuck/ContEd/HOCN/SampleCode/chap02/time_server"]
[dir: /home/chuck/ContEd/HOCN/SampleCode/chap02]
[path: /home/chuck/bin:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games:/usr/local/games:/snap/bin]
Just recently started my own open source project called SentryPeer and thanks to your book and example code I have my first UDP server for the project:
https://github.com/SentryPeer/SentryPeer/blob/main/src/sip_daemon.c
Thanks for writing it!
Really, really enjoying your book. Big thanks!
However, doing this would have helped me as port 8080 is in use on my system:
making it or adding another line:
git diff
diff --git a/chap02/time_server.c b/chap02/time_server.c
index b3456cb..50c13f5 100644
--- a/chap02/time_server.c
+++ b/chap02/time_server.c
@@ -97,6 +97,7 @@ int main() {
if (bind(socket_listen,
bind_address->ai_addr, bind_address->ai_addrlen)) {
fprintf(stderr, "bind() failed. (%d)\n", GETSOCKETERRNO());
+ perror("bind() failed");
return 1;
}
freeaddrinfo(bind_address);
which then helps me with:
./time_server
Configuring local address...
Creating socket...
Binding socket to local address...
bind() failed. (98)
bind() failed: Address already in use
Thanks.
Hi!
I found I minor issue with information about domain name lengths in the book and it's reflected int the dns_query.c
code from chapter 5.
Specifically, the book mentions:
It also checks that the hostname isn't more than 255
characters long. Hostnames longer than that aren't allowed by the DNS standard, and
checking it now ensures that we don't need to allocate too much memory.
Which is reflected in the code here:
Hands-On-Network-Programming-with-C/chap05/dns_query.c
Lines 236 to 241 in 54be3ed
But the DNS specification mentions:
To simplify implementations, the total number of octets that represent a
domain name (i.e., the sum of all label octets and label lengths) is
limited to 255.
See https://datatracker.ietf.org/doc/html/rfc1034#section-3.1
That effectively limits readable ASCII domain names to only 253 characters (excluding the optional . at the end).
For example using the dns_query
program from the book as such:
./dns_query 123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789a.xyz a
Leads to a response indicating a format error:
QR = 1 response
OPCODE = 0 standard
AA = 0
TC = 0
RD = 1 recursion desired
RCODE = 1 format error
That is a readable domain name of length 254, which is more than the allowed 253 readable characters. Encoded in the DNS binary format, the total number of octets that it's represented by is 256 which is more than the allowed 255.
So as expected trying with a domain name that's only 253 characters long works just fine (notice the second level domain name is changed from 123456789a
to 123456789
):
./dns_query 123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.123456789.xyz a
QR = 1 response
OPCODE = 0 standard
AA = 0
TC = 0
RD = 1 recursion desired
RCODE = 0 success
QDCOUNT = 1
ANCOUNT = 1
NSCOUNT = 0
ARCOUNT = 0
Reference to a practical explanation: https://devblogs.microsoft.com/oldnewthing/?p=7873
P.S.: I'm not a complete newbie in network programming but I still found your book useful, and I quite like it as a reference material.
I'm working through the SMTP code (chapter 8) under Ubuntu and I wanted to share a tip for folks who cannot use port 25 for whatever reason. Under Linux and MacOS you can set up a local mail server to test against. I found a gist on Github link that has step-by-step instructions for doing so. Not sure about Windows as I haven't worked in that OS for a long time.
The call to setsockopt won't compiler on VC++ compiler since (on Windows headers), setsockopt takes char*
and not void*
. It is true that MS header/implmenetation is faulty as it doesn't make sense to take char*
- but the code won't compile.
int option = 0;
setsockopt(socket_listen, IPPROTO_IPV6, IPV6_V6ONLY,(void*)&option, sizeof(option));
Shouldn't this be *p++, since you are checking if *p == 0 in the look directly below which will always be true since we're setting *p = 0 here, and the loop looking for the hash will never be executed as the while condition is satisfied already? I don' think the # will ever be found and nulled out of the path and therefore is sent to the server with the current code.
I compile it and run it. when it run to _kbhit(), the function return false. Then it's cause program block in SSL_read().
Hi Lewis,
Have you ever done this with a UDP package?
https://stackoverflow.com/questions/5281409/get-destination-address-of-a-received-udp-packet
Looks like you can't use recvfrom and have to switch to recvmsg to use IP_PKTINFO
I'll dig into the book again.
Thanks for reading.
Gavin.
Hi codeplea,
I realized the following behavior: The server closes after connection of the client browser. Closing the browser on the local maschine
leads to the strange state, that the server cannot bind to the 8080 port. This I observed on my local Debian machines.
Is there maybe something not released in the example code. After some time the binding is again possible. I think this is reasoned by TIME_WAIT for old packages. But it needs really minutes that the 8080 port is available again. Is there a way
to programmatically avoid this wait cycle. Maybe to early asked -- started the book reading some minutes ago.
Thanks for info, Stephan
A declarative, efficient, and flexible JavaScript library for building user interfaces.
๐ Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. ๐๐๐
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google โค๏ธ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.