Skip to content

Instantly share code, notes, and snippets.

@xopr
Created July 23, 2021 15:13
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save xopr/669844a23ee061ca5d7a49fb7521e948 to your computer and use it in GitHub Desktop.
Save xopr/669844a23ee061ca5d7a49fb7521e948 to your computer and use it in GitHub Desktop.
// # compile this with
// ./openwrt-sdk-19.07.7-ramips-mt76x8_gcc-7.5.0_musl.Linux-x86_64/staging_dir/toolchain-mipsel_24kc_gcc-7.5.0_musl/bin/mipsel-openwrt-linux-gcc fartnet.c -o fartnet
// # copy over the file:
// # at the pc, run:
// nc -l 9999 -w0 < fartnet
// # at the router, run:
// nc 192.168.2.116 9999 > fartnet
// chmod +x fartnet
// # next, kill everything that accesses the device node:
// kill -9 `ps w|grep watchdog_loop|grep -v grep|awk '{ print $1 }'`
// kill -9 `ps w|grep autopubmsg|grep -v grep|awk '{ print $1 }'`
// killall -9 pubmsg
// and run it!
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <arpa/inet.h>
#include <netinet/in.h>
#define PORT 6454
#define MAXLINE 1024
int main()
{
int sockfd;
char buffer[MAXLINE];
struct sockaddr_in servaddr, cliaddr;
// Creating socket file descriptor
if ( (sockfd = socket(AF_INET, SOCK_DGRAM, 0)) < 0 )
{
perror( "socket creation failed" );
exit( EXIT_FAILURE );
}
memset( &servaddr, 0, sizeof(servaddr) );
memset( &cliaddr, 0, sizeof(cliaddr) );
// Server connection information
servaddr.sin_family = AF_INET; // IPv4
servaddr.sin_addr.s_addr = INADDR_ANY;
servaddr.sin_port = htons( PORT );
// Bind socket
if ( bind(sockfd, (const struct sockaddr *)&servaddr, sizeof(servaddr)) < 0 )
{
perror("bind failed");
exit(EXIT_FAILURE);
}
int len, n;
FILE * fp;
len = sizeof( cliaddr );
// Serve forever
while ( 1 )
{
n = recvfrom(
sockfd,
(char* )buffer,
MAXLINE,
MSG_WAITALL,
( struct sockaddr* ) &cliaddr,
&len
);
if ( n > 18 )
{
buffer[n] = '\0';
fp = fopen("/dev/ws2812", "wb");
if( fp == NULL )
{
printf("Unable to open ws2812 device node.\n");
exit(EXIT_FAILURE);
}
fwrite( ((char* )buffer)+18, sizeof( char ), n - 18, fp);
fclose( fp );
}
}
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment