Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
41 changes: 24 additions & 17 deletions utils/listener.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,39 +7,46 @@
// Changes:
// * Compiles for Linux
// * Takes the port and group on the command line
// * adopted c++ idiomatic practices
//

#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <time.h>

#include <string.h>
#include <stdio.h>
#include <stdlib.h>
#include <cstring>
#include <iostream>

#define MSGBUFSIZE 256
constexpr size_t MSGBUFSIZE=256;

int main(int argc, char *argv[]) {
if (argc < 3) {
printf("Command line args should be multicast group and port\n");
printf("(e.g. for SSDP, `listener 239.255.255.250 1900`)\n");
std::cout << "Command line args should be multicast group and port\n";
std::cout << "e.g. for SSDP, `listener 239.255.255.250 1900`)\n";
return 1;
}

char *group = argv[1]; // e.g. 239.255.255.250 for SSDP
int port = atoi(argv[2]); // 0 if error, which is an invalid port
int port;
try {
port = std::stoi(argv[2]);
if ( port < 1 ) throw std::out_of_range("invalid port number");
}
catch (...) {
std::cerr << "invalid port number\n";
return 1;
}

char *filterstr = NULL;
char *filterstr = nullptr;

if (argc == 4) filterstr = argv[3];

// create what looks like an ordinary UDP socket
//
int fd = socket(AF_INET, SOCK_DGRAM, 0);
if (fd < 0) {
perror("socket");
std::cerr << "socket: " << std::strerror(errno) << "\n";
return 1;
}

Expand All @@ -51,7 +58,7 @@ int main(int argc, char *argv[]) {
fd, SOL_SOCKET, SO_REUSEADDR, (char *) &yes, sizeof(yes)
) < 0
) {
perror("Reusing ADDR failed");
std::cerr << "Reusing ADDR: " << std::strerror(errno) << "\n";
return 1;
}

Expand All @@ -66,7 +73,7 @@ int main(int argc, char *argv[]) {
// bind to receive address
//
if (bind(fd, (struct sockaddr *) &addr, sizeof(addr)) < 0) {
perror("bind");
std::cerr << "bind: " << std::strerror(errno) << "\n";
return 1;
}

Expand All @@ -80,7 +87,7 @@ int main(int argc, char *argv[]) {
fd, IPPROTO_IP, IP_ADD_MEMBERSHIP, (char *) &mreq, sizeof(mreq)
) < 0
) {
perror("setsockopt");
std::cerr << "setsockopt: " << std::strerror(errno) << "\n";
return 1;
}

Expand All @@ -98,13 +105,13 @@ int main(int argc, char *argv[]) {
&addrlen
);
if (nbytes < 0) {
perror("recvfrom");
std::cerr << "recvfrom: " << std::strerror(errno) << "\n";
return 1;
}
msgbuf[nbytes] = '\0';
if (filterstr != NULL) {
if (strstr(msgbuf, filterstr) != NULL) puts(msgbuf);
} else puts(msgbuf);
if (filterstr != nullptr) {
if (strstr(msgbuf, filterstr) != nullptr) std::cout << msgbuf << "\n";
} else std::cout << msgbuf << "\n";
}

return 0;
Expand Down
41 changes: 24 additions & 17 deletions utils/md5.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -9,29 +9,36 @@
* This implementation uses little endian byte order.
*
* obtained from https://github.com/B-Con/crypto-algorithms
* adopted c++ idiomataic practices, David Hale <dhale@asto.caltech.edu>
*********************************************************************/

/*************************** HEADER FILES ***************************/
#include <stdlib.h>
#include <memory.h>
#include <cstdlib>
#include <cstring>
#include "md5.h"

/****************************** MACROS ******************************/
#define ROTLEFT(a,b) ((a << b) | (a >> (32-b)))

#define F(x,y,z) ((x & y) | (~x & z))
#define G(x,y,z) ((x & z) | (y & ~z))
#define H(x,y,z) (x ^ y ^ z)
#define I(x,y,z) (y ^ (x | ~z))

#define FF(a,b,c,d,m,s,t) { a += F(b,c,d) + m + t; \
a = b + ROTLEFT(a,s); }
#define GG(a,b,c,d,m,s,t) { a += G(b,c,d) + m + t; \
a = b + ROTLEFT(a,s); }
#define HH(a,b,c,d,m,s,t) { a += H(b,c,d) + m + t; \
a = b + ROTLEFT(a,s); }
#define II(a,b,c,d,m,s,t) { a += I(b,c,d) + m + t; \
a = b + ROTLEFT(a,s); }
constexpr uint32_t ROTLEFT(uint32_t a, uint32_t b) { return (a << b) | (a >> (32 - b)); }
constexpr uint32_t F(uint32_t x, uint32_t y, uint32_t z) { return (x & y ) | ( ~x & z ); }
constexpr uint32_t G(uint32_t x, uint32_t y, uint32_t z) { return (x & z) | ( y & ~z ); }
constexpr uint32_t H(uint32_t x, uint32_t y, uint32_t z) { return x ^ y ^ z; }
constexpr uint32_t I(uint32_t x, uint32_t y, uint32_t z) { return y ^ ( x | ~z ); }
constexpr void FF(uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t m, uint32_t s, uint32_t t) {
a += F(b, c, d) + m + t;
a = b + ROTLEFT(a, s);
}
constexpr void GG(uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t m, uint32_t s, uint32_t t) {
a += G(b, c, d) + m + t;
a = b + ROTLEFT(a, s);
}
constexpr void HH(uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t m, uint32_t s, uint32_t t) {
a += H(b, c, d) + m + t;
a = b + ROTLEFT(a, s);
}
constexpr void II(uint32_t &a, uint32_t b, uint32_t c, uint32_t d, uint32_t m, uint32_t s, uint32_t t) {
a += I(b, c, d) + m + t;
a = b + ROTLEFT(a, s);
}

/*********************** FUNCTION DEFINITIONS ***********************/
void md5_transform(MD5_CTX *ctx, const BYTE data[]) {
Expand Down
5 changes: 3 additions & 2 deletions utils/md5.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
#pragma once

/*************************** HEADER FILES ***************************/
#include <stddef.h>
#include <cstddef>
#include <cstdint>

/****************************** MACROS ******************************/
#define MD5_BLOCK_SIZE 16 // MD5 outputs a 16 byte digest
constexpr size_t MD5_BLOCK_SIZE=16; // MD5 outputs a 16 byte digest

/**************************** DATA TYPES ****************************/
typedef unsigned char BYTE; // 8-bit byte
Expand Down
Loading