gosl/src/net.c

151 lines
3.3 KiB
C
Raw Normal View History

2013-11-02 11:31:14 +01:00
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <netdb.h>
2013-11-02 12:47:17 +01:00
// usleep support
#include <time.h>
#include "misc.h"
2013-11-02 18:51:10 +01:00
#include "msg.h"
2013-11-02 12:47:17 +01:00
2013-11-02 11:31:14 +01:00
void *get_in_addr(struct sockaddr *sa)
{
if (sa->sa_family == AF_INET) {
return &(((struct sockaddr_in*)sa)->sin_addr);
}
return &(((struct sockaddr_in6*)sa)->sin6_addr);
}
2013-11-02 12:47:17 +01:00
int run_server(const struct prog_info *pinfo) {
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];
memset(&hints, 0, sizeof(hints));
hints.ai_family = AF_UNSPEC;
hints.ai_socktype = SOCK_DGRAM;
sprintf(portbuf, "%d", pinfo->port);
2013-11-02 15:01:40 +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-02 13:31:04 +01:00
return 1;
}
2013-11-02 17:20:34 +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) {
perror("talker: socket");
continue;
}
break;
}
if (p == NULL) {
fprintf(stderr, "talker: failed to bind socket\n");
return 2;
}
struct timespec tim;
tim.tv_sec = 0;
tim.tv_nsec = 500000000;
2013-11-02 15:01:40 +01:00
int broadcastPermission = 1;
if (setsockopt(sockfd, SOL_SOCKET, SO_BROADCAST, (void *) &broadcastPermission,sizeof(broadcastPermission)) < 0){
fprintf(stderr, "setsockopt error");
exit(1);
}
2013-11-02 14:04:43 +01:00
for (;;) {
printf("sending...\n");
int numbytes;
if ((numbytes = sendto(sockfd, "bla", 3, 0, p->ai_addr, p->ai_addrlen)) == -1) {
perror("error sending");
return -44;
}
nanosleep(&tim, NULL);
}
freeaddrinfo(servinfo);
close(sockfd);
2013-11-02 13:31:04 +01:00
return 0;
}
2013-11-02 14:04:43 +01:00
int run_client(const struct prog_info *pinfo, void (*framecallback)(long) ) {
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));
hints.ai_family = AF_INET; // IPv4
hints.ai_socktype = SOCK_DGRAM; // UDP
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 16:14:50 +01:00
//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");
continue;
}
if (bind(sockfd, info->ai_addr, info->ai_addrlen) == -1) {
perror("bind");
continue;
}
break;
}
if (!info) {
fprintf(stderr, "unbound\n");
return 2;
}
printf("check!\n");
freeaddrinfo(servinfo); // free whole list
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) {
perror("recvfrom");
exit(1);
}
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 11:31:14 +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
framecallback(123);
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;
}