Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
ONDD UDP inject
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sys/socket.h>
#include <sys/types.h>
#include <sys/un.h>
#include <netdb.h>
#include <netinet/in.h>
#include <unistd.h>
#define SOCKET_NAME "/tmp/run/ondd.data"
#define BUFFER_SIZE 1000
#define UDP_PORT "10000"
int main(int argc, char *argv[]) {
struct sockaddr_un ondd_addr;
int ondd_socket, udp_socket;
char buffer[BUFFER_SIZE];
struct addrinfo hints, *udp_addr;
ssize_t len;
/* Connect to ONDD */
if ((ondd_socket = socket(AF_UNIX, SOCK_SEQPACKET, 0)) == -1) {
perror("Failed to create SOCK_SEQPACKET socket");
exit(EXIT_FAILURE);
}
memset(&ondd_addr, 0, sizeof(ondd_addr));
ondd_addr.sun_family = AF_UNIX;
strncpy(ondd_addr.sun_path, SOCKET_NAME, sizeof(ondd_addr.sun_path) - 1);
if (connect(ondd_socket, (const struct sockaddr *) &ondd_addr,
sizeof(struct sockaddr_un)) == -1) {
perror("Failed to connect to ONDD socket");
exit(EXIT_FAILURE);
}
/* Listen on UDP socket */
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
hints.ai_protocol = 0;
hints.ai_flags=AI_PASSIVE | AI_ADDRCONFIG;
udp_addr = NULL;
if (getaddrinfo(NULL, UDP_PORT, &hints, &udp_addr)) {
perror("Failed to get address info for UDP socket");
exit(EXIT_FAILURE);
}
if ((udp_socket = socket(udp_addr->ai_family, udp_addr->ai_socktype,
udp_addr->ai_protocol)) == -1) {
perror("Failed to create UDP socket");
exit(EXIT_FAILURE);
}
if (bind(udp_socket, udp_addr->ai_addr, udp_addr->ai_addrlen) == -1) {
perror("Failed to bind to UDP socket");
exit(EXIT_FAILURE);
}
freeaddrinfo(udp_addr);
while (1) {
/* Receive from UDP socket */
if ((len = recv(udp_socket, buffer, sizeof(buffer), 0)) == -1) {
perror("Failed to receive from UDP socket");
exit(EXIT_FAILURE);
}
/* Write frame to ONDD */
if (write(ondd_socket, buffer, len) != len) {
/* TODO: hanle partial writes */
perror("write()");
exit(EXIT_FAILURE);
}
}
close(ondd_socket);
close(udp_socket);
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment