Skip to content

Commit 83a827f

Browse files
committed
initial commit
1 parent f46fb09 commit 83a827f

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

68 files changed

+8348
-2
lines changed

.appveyor.yml

+48
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,48 @@
1+
2+
environment:
3+
matrix:
4+
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
5+
BNAME: "VS2015 x86"
6+
MATRIX_EVAL: "\"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat\" x86"
7+
CC: "cl.exe"
8+
platform: x86
9+
10+
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
11+
BNAME: "MinGW"
12+
MATRIX_EVAL: "set PATH=C:\\MinGW\\msys\\1.0\\local\\bin;C:\\MinGW\\msys\\1.0\\mingw\\bin;C:\\MinGW\\msys\\1.0\\bin;C:\\MinGW\\bin"
13+
CC: "gcc"
14+
CEXTRA: "-liphlpapi -lws2_32"
15+
platform: x86
16+
17+
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
18+
BNAME: "Clang"
19+
MATRIX_EVAL: "set PATH=C:\\Program Files\\LLVM\\bin"
20+
CC: "clang"
21+
CEXTRA: "-liphlpapi -lws2_32"
22+
platform: x86
23+
24+
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2015
25+
BNAME: "VS2015 x64"
26+
MATRIX_EVAL: "\"C:\\Program Files (x86)\\Microsoft Visual Studio 14.0\\VC\\vcvarsall.bat\" x64"
27+
CC: "cl.exe"
28+
platform: x64
29+
30+
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
31+
BNAME: "VS2017 x86"
32+
MATRIX_EVAL: "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars32.bat\""
33+
CC: "cl.exe"
34+
platform: x86
35+
36+
- APPVEYOR_BUILD_WORKER_IMAGE: Visual Studio 2017
37+
BNAME: "VS2017 x64"
38+
MATRIX_EVAL: "\"C:\\Program Files (x86)\\Microsoft Visual Studio\\2017\\Community\\VC\\Auxiliary\\Build\\vcvars64.bat\""
39+
CC: "cl.exe"
40+
platform: x64
41+
42+
43+
before_build:
44+
- echo "%MATRIX_EVAL%"
45+
- "%MATRIX_EVAL%"
46+
47+
build_script:
48+
- ./test/build.bat

.travis.yml

+78
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
language: c
2+
3+
matrix:
4+
include:
5+
- os: linux
6+
name: "linux gcc"
7+
compiler: gcc
8+
9+
- os: linux
10+
name: "linux gcc-4.9"
11+
addons:
12+
apt:
13+
sources:
14+
- ubuntu-toolchain-r-test
15+
packages:
16+
- g++-4.9
17+
env:
18+
- MATRIX_EVAL="CC=gcc-4.9 && CXX=g++-4.9"
19+
20+
- os: linux
21+
name: "linux gcc-5"
22+
addons:
23+
apt:
24+
sources:
25+
- ubuntu-toolchain-r-test
26+
packages:
27+
- g++-5
28+
env:
29+
- MATRIX_EVAL="CC=gcc-5 && CXX=g++-5"
30+
31+
- os: linux
32+
name: "linux gcc-6"
33+
addons:
34+
apt:
35+
sources:
36+
- ubuntu-toolchain-r-test
37+
packages:
38+
- g++-6
39+
env:
40+
- MATRIX_EVAL="CC=gcc-6 && CXX=g++-6"
41+
42+
- os: linux
43+
name: "linux gcc-7"
44+
addons:
45+
apt:
46+
sources:
47+
- ubuntu-toolchain-r-test
48+
packages:
49+
- g++-7
50+
env:
51+
- MATRIX_EVAL="CC=gcc-7 && CXX=g++-7"
52+
53+
- os: osx
54+
name: "osx gcc-4.9"
55+
osx_image: xcode8
56+
env:
57+
- MATRIX_EVAL="CC=gcc-4.9 && CXX=g++-4.9"
58+
59+
- os: osx
60+
name: "osx clang"
61+
compiler: clang
62+
env:
63+
- MATRIX_EVAL=""
64+
65+
- os: linux
66+
name: "linux clang"
67+
compiler: clang
68+
env:
69+
- MATRIX_EVAL=""
70+
71+
72+
73+
before_install:
74+
- eval "${MATRIX_EVAL}"
75+
- ${CC} --version
76+
77+
script:
78+
- ./test/build.sh

LICENSE

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
MIT License
22

3-
Copyright (c) 2018 Packt
3+
Copyright (c) 2018 Lewis Van Winkle
44

55
Permission is hereby granted, free of charge, to any person obtaining a copy
66
of this software and associated documentation files (the "Software"), to deal

README.md

+110-1
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,111 @@
1+
[![Build Status](https://travis-ci.org/codeplea/Hands-On-Network-Programming-with-C.svg?branch=master)](https://travis-ci.org/codeplea/Hands-On-Network-Programming-with-C)
2+
[![Build status](https://ci.appveyor.com/api/projects/status/yarb3kmmn5db21vn?svg=true)](https://ci.appveyor.com/project/codeplea/hands-on-network-programming-with-c)
3+
14
# Hands-On-Network-Programming-with-C
2-
Hands-On Network Programming with C, published by Packt
5+
6+
This repo contains the code which accompanies the book "Hands-On Network
7+
Programming with C", written by Lewis Van Winkle. The code is released under
8+
the MIT license.
9+
10+
Unless otherwise noted, all programs will compile cleanly on Windows, Linux,
11+
and macOS, and as C or C++. Code is tested with MinGW and Visual Studio on
12+
Windows, gcc and clang on Linux and macOS.
13+
14+
15+
## Chapter 1
16+
17+
* **[chap01/win_init.c](chap01/win_init.c)** Example code to initialize Winsock. (Windows only)
18+
* **[chap01/win_list.c](chap01/win_list.c)** List all local IP addresses. (Windows only)
19+
* **[chap01/unix_list.c](chap01/unix_list.c)** List all local IP addresses. (Linux and macOS only)
20+
21+
## Chapter 2
22+
23+
* **[chap02/sock_init.c](chap02/sock_init.c)** Example program to include all needed headers and initialize.
24+
* **[chap02/time_console.c](chap02/time_console.c)** Prints to console the current date and time.
25+
* **[chap02/time_server.c](chap02/time_server.c)** Serves a web page giving current date and time.
26+
* **[chap02/time_server_ipv6.c](chap02/time_server_ipv6.c)** As above, but listening for IPv6 connections.
27+
* **[chap02/time_server_dual.c](chap02/time_server_dual.c)** As above, but listening for IPv6/IPv4 dual stack connections.
28+
29+
## Chapter 3
30+
31+
* **[chap03/tcp_client.c](chap03/tcp_client.c)** Establish TCP connection and send/receive data from the console.
32+
* **[chap03/tcp_serve_toupper.c](chap03/tcp_serve_toupper.c)** TCP server servicing multiple connections using `select()`. Echos received data back to client in all upper-case.
33+
* **[chap03/tcp_serve_toupper_fork.c](chap03/tcp_serve_toupper_fork.c)** As above, but uses `fork()` instead of `select()`. (Linux and macOS only)
34+
* **[chap03/tcp_serve_chat.c](chap03/tcp_serve_chat.c)** TCP server which relays received data to every other connected client.
35+
36+
## Chapter 4
37+
38+
* **[chap04/udp_client.c](chap04/udp_client.c)** Send/receive UDP data from the console.
39+
* **[chap04/udp_recvfrom.c](chap04/udp_recvfrom.c)** Uses `recvfrom()` to receive one UDP packet.
40+
* **[chap04/udp_sendto.c](chap04/udp_sendto.c)** Uses `sendto()` to send one UDP packet.
41+
* **[chap04/udp_serve_toupper.c](chap04/udp_serve_toupper.c)** Receives UDP data, and echos it back to the sender in all upper-case.
42+
* **[chap04/udp_serve_toupper_simple.c](chap04/udp_serve_toupper_simple.c)** As above, but doesn't use `select()`.
43+
44+
## Chapter 5
45+
46+
* **[chap05/lookup.c](chap05/lookup.c)** Uses `getaddrinfo()` to lookup addresses for a given hostname.
47+
* **[chap05/dns_query.c](chap05/dns_query.c)** Encodes and sends a DNS query, then decodes the response.
48+
49+
## Chapter 6
50+
51+
* **[chap06/web_get.c](chap06/web_get.c)** A minimal HTTP client which will download a web resource from a given URL.
52+
53+
## Chapter 7
54+
55+
* **[chap07/web_server.c](chap07/web_server.c)** A minimal web server.
56+
* **[chap07/web_server2.c](chap07/web_server2.c)** A minimal web server (no globals).
57+
58+
## Chapter 8
59+
60+
* **[chap08/smtp_send.c](chap08/smtp_send.c)** A simple email sender.
61+
62+
## Chapter 9
63+
64+
The examples in this chapter use OpenSSL. Be sure to link against the OpenSSL
65+
libraries when compiling (`-lssl -lcrypto`).
66+
67+
* **[chap09/openssl_version.c](chap09/openssl_version.c)** A program to report the installed OpenSSL version.
68+
* **[chap09/https_simple.c](chap09/https_simple.c)** A minimal program that requests a web page using HTTPS.
69+
* **[chap09/https_get.c](chap09/https_get.c)** The HTTP client of chapter 6 modified to use HTTPS.
70+
* **[chap09/tls_client.c](chap09/tls_client.c)** The TCP client program of chapter 3 modified to use TLS/SSL.
71+
* **[chap09/tls_get_cert.c](chap09/tls_get_cert.c)** Prints a certificate from a TLS/SSL server.
72+
73+
## Chapter 10
74+
75+
The examples in this chapter use OpenSSL. Be sure to link against the OpenSSL
76+
libraries when compiling (`-lssl -lcrypto`).
77+
78+
* **[chap10/tls_time_server.c](chap10/tls_time_server.c)** The time server of chapter 2 modified to use HTTPS.
79+
* **[chap10/https_server.c](chap10/https_server.c)** The web server of chapter 7 modified to use HTTPS.
80+
81+
## Chapter 11
82+
83+
The examples in this chapter use libssh. Be sure to link against the libssh libraries when compiling (`-lssh`).
84+
85+
* **[chap11/ssh_version.c](chap11/ssh_version.c)** A program to report the libssh version.
86+
* **[chap11/ssh_connect.c](chap11/ssh_connect.c)** A minimal client that establishes a ssh connection.
87+
* **[chap11/ssh_auth.c](chap11/ssh_auth.c)** A client which attempts authentication.
88+
* **[chap11/ssh_command.c](chap11/ssh_command.c)** A client that runs a single command over ssh.
89+
* **[chap11/ssh_download.c](chap11/ssh_download.c)** A client that downloads a file over ssh.
90+
91+
92+
## Chapter 12
93+
94+
This chapter doesn't include any example programs.
95+
96+
## Chapter 13
97+
98+
* **[chap13/connect_timeout.c](chap13/connect_timeout.c)** Shows how to timeout a `connect()` call early.
99+
* **[chap13/connect_blocking.c](chap13/connect_blocking.c)** For comparison with **connect_timeout.c**.
100+
* **[chap13/server_reuse.c](chap13/server_reuse.c)** Demonstrates the use of `SO_REUSEADDR`.
101+
* **[chap13/server_noreuse.c](chap13/server_noreuse.c)** For comparison with **server_reuse.c**.
102+
* **[chap13/server_crash.c](chap13/server_crash.c)** This server purposefully writes to a TCP socket after the client disconnects.
103+
* **[chap13/error_text.c](chap13/error_text.c)** Shows how to obtain error code descriptions.
104+
* **[chap13/big_send.c](chap13/big_send.c)** TCP client. Sends lots of data after connecting. Used to show blocking behaviour of `send()`.
105+
* **[chap13/server_ignore.c](chap13/server_ignore.c)** TCP server. Accepts connections, then simply ignores them. Used to show blocking behaviour of `send()`.
106+
* **[chap13/setsize.c](chap13/setsize.c)** Shows max number of sockets `select()` can handle.
107+
108+
## Chapter 14
109+
110+
This chapter doesn't include any example programs.
111+

chap01/unix_list.c

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Lewis Van Winkle
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <sys/socket.h>
26+
#include <netdb.h>
27+
#include <ifaddrs.h>
28+
#include <stdio.h>
29+
#include <stdlib.h>
30+
31+
32+
int main() {
33+
34+
struct ifaddrs *addresses;
35+
36+
if (getifaddrs(&addresses) == -1) {
37+
printf("getifaddrs call failed\n");
38+
return -1;
39+
}
40+
41+
struct ifaddrs *address = addresses;
42+
while(address) {
43+
int family = address->ifa_addr->sa_family;
44+
if (family == AF_INET || family == AF_INET6) {
45+
46+
printf("%s\t", address->ifa_name);
47+
printf("%s\t", family == AF_INET ? "IPv4" : "IPv6");
48+
49+
char ap[100];
50+
const int family_size = family == AF_INET ?
51+
sizeof(struct sockaddr_in) : sizeof(struct sockaddr_in6);
52+
getnameinfo(address->ifa_addr,
53+
family_size, ap, sizeof(ap), 0, 0, NI_NUMERICHOST);
54+
printf("\t%s\n", ap);
55+
56+
}
57+
address = address->ifa_next;
58+
}
59+
60+
61+
freeifaddrs(addresses);
62+
return 0;
63+
}

chap01/win_init.c

+43
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
/*
2+
* MIT License
3+
*
4+
* Copyright (c) 2018 Lewis Van Winkle
5+
*
6+
* Permission is hereby granted, free of charge, to any person obtaining a copy
7+
* of this software and associated documentation files (the "Software"), to deal
8+
* in the Software without restriction, including without limitation the rights
9+
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
* copies of the Software, and to permit persons to whom the Software is
11+
* furnished to do so, subject to the following conditions:
12+
*
13+
* The above copyright notice and this permission notice shall be included in all
14+
* copies or substantial portions of the Software.
15+
*
16+
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
* SOFTWARE.
23+
*/
24+
25+
#include <stdio.h>
26+
#include <winsock2.h>
27+
28+
#pragma comment(lib, "ws2_32.lib")
29+
30+
int main() {
31+
WSADATA d;
32+
33+
if (WSAStartup(MAKEWORD(2, 2), &d)) {
34+
printf("Failed to initialize.\n");
35+
return -1;
36+
}
37+
38+
WSACleanup();
39+
40+
printf("Ok.\n");
41+
42+
return 0;
43+
}

0 commit comments

Comments
 (0)