From a11df2c963520c64768158c2753237152a1eae3d Mon Sep 17 00:00:00 2001 From: Franciszek Malinka Date: Sat, 17 Apr 2021 17:31:46 +0200 Subject: Update --- Semestr 4/sieci/pracownia2/router.c | 212 ++++++++++++++++++++++++++++++++ Semestr 4/sieci/pracownia2/udp_client.c | 33 +++++ Semestr 4/sieci/pracownia2/udp_server.c | 59 +++++++++ 3 files changed, 304 insertions(+) create mode 100644 Semestr 4/sieci/pracownia2/router.c create mode 100755 Semestr 4/sieci/pracownia2/udp_client.c create mode 100755 Semestr 4/sieci/pracownia2/udp_server.c (limited to 'Semestr 4/sieci/pracownia2') diff --git a/Semestr 4/sieci/pracownia2/router.c b/Semestr 4/sieci/pracownia2/router.c new file mode 100644 index 0000000..79754b3 --- /dev/null +++ b/Semestr 4/sieci/pracownia2/router.c @@ -0,0 +1,212 @@ +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +#define SERVER_PORT 54321 +#define TURN_LEN_S 5 +#define TURN_LEN_MS (1000 * TURN_LEN_S) +#define TURN_LEN_US (1000000 * TURN_LEN_S) +#define NS_TO_MS(X) ((long)(X) / (long)1000000) +#define S_TO_MS(X) ((long)(X) * (long)1000) + +struct router_addr { + struct in_addr addr; + uint16_t distance; + uint8_t netmask; +}; + +struct in_addr get_broadcast_address(struct router_addr ra) { + struct in_addr result = ra.addr; + /* bitshift by more than 31 is UB */ + if (ra.netmask < 32) { + result.s_addr |= ~((1< +#include +#include +#include +#include +#include +#include + +int main() +{ + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + fprintf(stderr, "socket error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + struct sockaddr_in server_address; + bzero (&server_address, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_port = htons(54321); + inet_pton(AF_INET, "192.168.1.2", &server_address.sin_addr); + + char* message = "Hello server!"; + ssize_t message_len = strlen(message); + if (sendto(sockfd, message, message_len, 0, (struct sockaddr*) &server_address, sizeof(server_address)) != message_len) { + fprintf(stderr, "sendto error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + close (sockfd); + return EXIT_SUCCESS; +} + diff --git a/Semestr 4/sieci/pracownia2/udp_server.c b/Semestr 4/sieci/pracownia2/udp_server.c new file mode 100755 index 0000000..b5ae6d1 --- /dev/null +++ b/Semestr 4/sieci/pracownia2/udp_server.c @@ -0,0 +1,59 @@ +#include +#include +#include +#include +#include +#include +#include + +int main() +{ + int sockfd = socket(AF_INET, SOCK_DGRAM, 0); + if (sockfd < 0) { + fprintf(stderr, "socket error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + struct sockaddr_in server_address; + bzero (&server_address, sizeof(server_address)); + server_address.sin_family = AF_INET; + server_address.sin_port = htons(54321); + server_address.sin_addr.s_addr = htonl(INADDR_ANY); + if (bind (sockfd, (struct sockaddr*)&server_address, sizeof(server_address)) < 0) { + fprintf(stderr, "bind error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + for (;;) { + + struct sockaddr_in sender; + socklen_t sender_len = sizeof(sender); + u_int8_t buffer[IP_MAXPACKET+1]; + + ssize_t datagram_len = recvfrom (sockfd, buffer, IP_MAXPACKET, 0, (struct sockaddr*)&sender, &sender_len); + if (datagram_len < 0) { + fprintf(stderr, "recvfrom error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + char sender_ip_str[20]; + inet_ntop(AF_INET, &(sender.sin_addr), sender_ip_str, sizeof(sender_ip_str)); + printf ("Received UDP packet from IP address: %s, port: %d\n", sender_ip_str, ntohs(sender.sin_port)); + + buffer[datagram_len] = 0; + printf ("%ld-byte message: +%s+\n", datagram_len, buffer); + + char* reply = "Thank you!"; + ssize_t reply_len = strlen(reply); + if (sendto(sockfd, reply, reply_len, 0, (struct sockaddr*)&sender, sender_len) != reply_len) { + fprintf(stderr, "sendto error: %s\n", strerror(errno)); + return EXIT_FAILURE; + } + + fflush(stdout); + } + + close (sockfd); + return EXIT_SUCCESS; +} + -- cgit v1.2.3