2013-11-02 11:31:14 +01:00
|
|
|
#include <stdlib.h>
|
2013-11-03 16:06:36 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdbool.h>
|
2013-11-02 18:55:43 +01:00
|
|
|
|
|
|
|
#include <unistd.h> // ?
|
|
|
|
#include <errno.h> // geguttenbergt aus bejees networking guide
|
|
|
|
#include <string.h> // ?
|
|
|
|
|
|
|
|
// networking
|
2013-11-02 11:31:14 +01:00
|
|
|
#include <sys/types.h>
|
|
|
|
#include <sys/socket.h>
|
|
|
|
#include <netinet/in.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
#include <netdb.h>
|
|
|
|
|
2013-11-02 18:55:43 +01:00
|
|
|
#include <time.h> // nanosleep
|
2013-11-02 12:47:17 +01:00
|
|
|
|
2013-11-02 19:21:09 +01:00
|
|
|
#include "net.h"
|
2013-11-02 12:47:17 +01:00
|
|
|
|
2013-11-02 19:21:09 +01:00
|
|
|
|
2013-11-03 13:14:25 +01:00
|
|
|
// little helper
|
|
|
|
static void *get_in_addr(struct sockaddr *sa) {
|
|
|
|
switch (sa->sa_family) {
|
|
|
|
case AF_INET:
|
|
|
|
return &(((struct sockaddr_in*)sa)->sin_addr);
|
|
|
|
break;
|
|
|
|
case AF_INET6:
|
|
|
|
return &(((struct sockaddr_in6*)sa)->sin6_addr);
|
|
|
|
break;
|
|
|
|
case AF_UNSPEC:
|
|
|
|
default:
|
|
|
|
return NULL;
|
2013-11-02 21:16:12 +01:00
|
|
|
}
|
2013-11-02 11:31:14 +01:00
|
|
|
}
|
|
|
|
|
2013-11-02 19:21:09 +01:00
|
|
|
|
|
|
|
// server mode
|
|
|
|
// pumpt im for(;;) den status ins eth
|
|
|
|
//
|
2013-11-02 22:54:28 +01:00
|
|
|
int run_server(const struct prog_info *pinfo, char *img, int w, int h) {
|
2013-11-02 14:04:43 +01:00
|
|
|
struct addrinfo hints, *servinfo, *p;
|
2013-11-02 13:31:04 +01:00
|
|
|
int ret;
|
2013-11-02 14:04:43 +01:00
|
|
|
int sockfd;
|
2013-11-02 13:31:04 +01:00
|
|
|
char portbuf[6];
|
2013-11-03 16:06:36 +01:00
|
|
|
|
2013-11-02 13:31:04 +01:00
|
|
|
memset(&hints, 0, sizeof(hints));
|
|
|
|
hints.ai_family = AF_UNSPEC;
|
|
|
|
hints.ai_socktype = SOCK_DGRAM;
|
|
|
|
|
|
|
|
sprintf(portbuf, "%d", pinfo->port);
|
2013-11-03 16:06:36 +01:00
|
|
|
if ((ret = getaddrinfo("255.255.255.255", portbuf, &hints, &servinfo)) != 0) {
|
2013-11-02 14:04:43 +01:00
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(ret));
|
2013-11-03 13:14:25 +01:00
|
|
|
return 1;
|
2013-11-02 13:31:04 +01:00
|
|
|
}
|
|
|
|
|
2013-11-03 13:14:25 +01:00
|
|
|
for (p = servinfo; p != NULL; p = p->ai_next) {
|
2013-11-02 14:04:43 +01:00
|
|
|
if ((sockfd = socket(p->ai_family, p->ai_socktype, p->ai_protocol)) == -1) {
|
2013-11-02 21:16:12 +01:00
|
|
|
perror("talker: socket");
|
|
|
|
continue;
|
|
|
|
}
|
2013-11-02 14:04:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (p == NULL) {
|
|
|
|
fprintf(stderr, "talker: failed to bind socket\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
|
|
|
|
struct timespec tim;
|
|
|
|
tim.tv_sec = 0;
|
2013-11-02 20:10:57 +01:00
|
|
|
tim.tv_nsec = 1000000000 / pinfo->fps;
|
2013-11-02 14:04:43 +01:00
|
|
|
|
2013-11-02 19:21:09 +01:00
|
|
|
// Einem Socket muss das Broadcasting explizit erlaubt werden:
|
2013-11-02 15:01:40 +01:00
|
|
|
int broadcastPermission = 1;
|
2013-11-03 16:06:36 +01:00
|
|
|
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission, sizeof(broadcastPermission)) < 0){
|
2013-11-02 15:01:40 +01:00
|
|
|
fprintf(stderr, "setsockopt error");
|
|
|
|
exit(1);
|
|
|
|
}
|
|
|
|
|
2013-11-02 19:21:09 +01:00
|
|
|
//
|
2013-11-02 19:45:52 +01:00
|
|
|
int t = 0;
|
2013-11-02 14:04:43 +01:00
|
|
|
for (;;) {
|
2013-11-02 19:45:52 +01:00
|
|
|
//printf("sending...\n");
|
2013-11-02 14:04:43 +01:00
|
|
|
int numbytes;
|
2013-11-02 19:45:52 +01:00
|
|
|
|
|
|
|
t++;
|
2013-11-02 22:54:28 +01:00
|
|
|
t %= pinfo->width;
|
2013-11-03 16:06:36 +01:00
|
|
|
struct message *outmsg = (struct message *) malloc(sizeof(struct message));
|
2013-11-02 19:45:52 +01:00
|
|
|
outmsg->timestamp = (uint32_t)t; //(uint32_t)time(NULL);
|
2013-11-03 16:06:36 +01:00
|
|
|
// send image every 100 frames
|
|
|
|
bool sendimg = (t % 100) == 0;
|
|
|
|
outmsg->width = sendimg ? w : 0;
|
|
|
|
outmsg->height = sendimg ? h : 0;
|
|
|
|
outmsg->image = sendimg ? img : NULL;
|
|
|
|
|
2013-11-02 19:34:21 +01:00
|
|
|
int buflen = getBufferSize(outmsg);
|
2013-11-03 16:06:36 +01:00
|
|
|
char *outbuf = (char *) malloc(buflen);
|
2013-11-02 19:34:21 +01:00
|
|
|
serialize(outbuf, outmsg);
|
|
|
|
|
|
|
|
if ((numbytes = sendto(sockfd, outbuf, buflen, 0, p->ai_addr, p->ai_addrlen)) == -1) {
|
2013-11-02 14:04:43 +01:00
|
|
|
perror("error sending");
|
2013-11-03 13:13:48 +01:00
|
|
|
return -42;
|
2013-11-02 14:04:43 +01:00
|
|
|
}
|
|
|
|
nanosleep(&tim, NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
freeaddrinfo(servinfo);
|
|
|
|
|
|
|
|
close(sockfd);
|
|
|
|
|
2013-11-02 13:31:04 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2013-11-02 19:21:09 +01:00
|
|
|
|
|
|
|
// lauschangriff:
|
|
|
|
// einfach mal in den äther horchen und alle messages rausnehmen die wo da gibt.
|
|
|
|
//
|
2013-11-02 20:57:08 +01:00
|
|
|
int run_client(const struct prog_info *pinfo, void (*framecallback)(const struct message *, const struct prog_info *) ) {
|
2013-11-02 11:31:14 +01:00
|
|
|
|
|
|
|
struct addrinfo hints, *servinfo;
|
2013-11-02 12:47:17 +01:00
|
|
|
char portbuf[6];
|
2013-11-02 11:31:14 +01:00
|
|
|
|
|
|
|
memset(&hints, 0, sizeof(hints));
|
2013-11-02 21:16:12 +01:00
|
|
|
hints.ai_family = AF_INET; // IPv4
|
|
|
|
hints.ai_socktype = SOCK_DGRAM; // UDP
|
2013-11-02 11:31:14 +01:00
|
|
|
hints.ai_flags = AI_PASSIVE;
|
|
|
|
|
2013-11-02 12:47:17 +01:00
|
|
|
sprintf(portbuf, "%d", pinfo->port);
|
|
|
|
|
2013-11-02 11:31:14 +01:00
|
|
|
int rv;
|
2013-11-02 12:47:17 +01:00
|
|
|
if ((rv = getaddrinfo(NULL, portbuf, &hints, &servinfo)) != 0) {
|
2013-11-02 11:31:14 +01:00
|
|
|
fprintf(stderr, "getaddrinfo: %s\n", gai_strerror(rv));
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
printf("got local address\n");
|
|
|
|
|
|
|
|
struct addrinfo *info;
|
|
|
|
int sockfd;
|
2013-11-02 19:21:09 +01:00
|
|
|
// mal drin lassen: falls die ip der gegenstelle relevant werden sollte: char s[INET6_ADDRSTRLEN];
|
2013-11-02 11:31:14 +01:00
|
|
|
for (info = servinfo; info != NULL; info = info->ai_next) {
|
|
|
|
if ((sockfd = socket(info->ai_family, info->ai_socktype, info->ai_protocol)) == -1) {
|
|
|
|
perror("sock");
|
2013-11-03 16:06:36 +01:00
|
|
|
continue;
|
2013-11-02 11:31:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (bind(sockfd, info->ai_addr, info->ai_addrlen) == -1) {
|
|
|
|
perror("bind");
|
2013-11-03 16:06:36 +01:00
|
|
|
continue;
|
2013-11-02 11:31:14 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (!info) {
|
|
|
|
fprintf(stderr, "unbound\n");
|
|
|
|
return 2;
|
|
|
|
}
|
|
|
|
printf("check!\n");
|
|
|
|
|
|
|
|
freeaddrinfo(servinfo); // free whole list
|
2013-11-02 21:55:33 +01:00
|
|
|
|
2013-11-02 11:31:14 +01:00
|
|
|
struct sockaddr_storage their_addr;
|
|
|
|
socklen_t addr_len = sizeof their_addr;
|
|
|
|
int numbytes;
|
|
|
|
char buf[10000];
|
|
|
|
do {
|
|
|
|
if ((numbytes = recvfrom(sockfd, buf, 9999 , 0, (struct sockaddr *)&their_addr, &addr_len)) == -1) {
|
2013-11-02 21:16:12 +01:00
|
|
|
perror("recvfrom");
|
|
|
|
exit(1);
|
2013-11-02 11:31:14 +01:00
|
|
|
}
|
|
|
|
|
2013-11-02 15:01:40 +01:00
|
|
|
//printf("listener: got packet from %s\n", inet_ntop(their_addr.ss_family, get_in_addr((struct sockaddr *)&their_addr), s, sizeof s));
|
|
|
|
//printf("listener: packet is %d bytes long\n", numbytes);
|
2013-11-02 19:34:21 +01:00
|
|
|
//buf[numbytes] = '\0';
|
2013-11-02 15:01:40 +01:00
|
|
|
//printf("listener: packet contains \"%s\"\n", buf);
|
2013-11-02 14:04:43 +01:00
|
|
|
|
2013-11-02 19:21:09 +01:00
|
|
|
struct message *msg = (struct message *)malloc(sizeof(struct message));
|
|
|
|
deserialize(msg, buf);
|
|
|
|
|
2013-11-02 20:57:08 +01:00
|
|
|
framecallback(msg, pinfo);
|
2013-11-02 14:04:43 +01:00
|
|
|
|
2013-11-02 11:31:14 +01:00
|
|
|
} while (strncmp(buf, "exit", 10000));
|
2013-11-02 14:04:43 +01:00
|
|
|
|
2013-11-02 11:31:14 +01:00
|
|
|
close(sockfd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|