Skip to content

Commit

Permalink
initial commit
Browse files Browse the repository at this point in the history
  • Loading branch information
pbrisbin committed Jan 8, 2011
0 parents commit bfae985
Show file tree
Hide file tree
Showing 5 changed files with 128 additions and 0 deletions.
3 changes: 3 additions & 0 deletions .gitignore
@@ -0,0 +1,3 @@
*.out
*.o
android-receiver
20 changes: 20 additions & 0 deletions Makefile
@@ -0,0 +1,20 @@

include config.mk

SRC = mail-query.c
OBJ = ${SRC:.c=.o}

all: android-receiver

.c.o:
${CC} -c ${CFLAGS} $<

${OBJ}: config.mk
mail-query: ${OBJ}
${CC} -o $@ ${OBJ} ${LDFLAGS}


clean:
rm -f android-receiver ${OBJ}

.PHONY: all clean
10 changes: 10 additions & 0 deletions README.md
@@ -0,0 +1,10 @@
# Android Receiver

Listen for broadcast messages from
[android-notifer](http://code.google.com/p/android-notifier/) coming
over a specific port.

Format that message (todo) and pass it off to a handling application
like notify-osd, dzen2, or some other script.

Done mostly as a learning exercise in C.
82 changes: 82 additions & 0 deletions android-receiver.c
@@ -0,0 +1,82 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <netinet/in.h>
#include <sys/socket.h>

int execvp(const char *file, char *const argv[]);
void bzero(void *s, size_t n);
pid_t fork(void);

void error(char *msg);
void handle_message(char *msg);

/* error and die */
void error(char *msg)
{
perror(msg);
exit(EXIT_FAILURE);
}

/* for now we just hand off to my existing bash script */
void handle_message(char *msg)
{
char *app = "dzen-handler";
char *flags[] = {app, msg, NULL};

printf("executing handler: %s %s\n", flags[0], flags[1]);
execvp(app, flags);
}

/* connect to <port> and listen for messages */
int main(int argc, char *argv[])
{
unsigned int fromlen;
int sock;
int length;
int n;

struct sockaddr_in server;
struct sockaddr_in from;

char buf[1024];

pid_t pid;

if (argc != 2) {
fprintf(stderr, "argc was %i\n", argc);
fprintf(stderr, "saw %s, %s, %s\n", argv[0], argv[1], argv[2]);
fprintf(stderr, "usage: %s <port>\n", argv[0]);
return(EXIT_FAILURE);
}

sock = socket(AF_INET, SOCK_DGRAM, 0);

if (sock < 0)
error("Opening socket");

length = sizeof(server);

bzero(&server,length);

server.sin_family = AF_INET;
server.sin_addr.s_addr = INADDR_ANY;
server.sin_port = htons(atoi(argv[1]));

if (bind(sock, (struct sockaddr *)&server, length) < 0)
error("binding");

fromlen = sizeof(struct sockaddr_in);

while (1) {
n = recvfrom(sock, buf, 1024, 0, (struct sockaddr * restrict)&from, &fromlen);

if (n < 0)
error("recvfrom");

pid = fork();

if (pid == 0)
handle_message(buf);
}
}
13 changes: 13 additions & 0 deletions config.mk
@@ -0,0 +1,13 @@
# version
VERSION = $(shell git describe)

# paths
PREFIX ?= /usr/local
MANPREFIX ?= ${PREFIX}/share/man

# compiler flags
CC = c99
CPPFLAGS =
CFLAGS += -g -pedantic -Wall -Wextra ${CPPFLAGS}
LDFLAGS =

0 comments on commit bfae985

Please sign in to comment.