39 #include <sys/ioctl.h> 40 #include <netinet/in.h> 44 #include <net/if_arp.h> 67 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
68 perror(
"wge100ArpAdd can't create socket");
72 wge100_debug(
"Registering ARP info for S/N %d \n", camInfo->
serial);
75 ((
struct sockaddr_in*)&arp.arp_pa)->sin_family = AF_INET;
76 memcpy(&((
struct sockaddr_in*)&arp.arp_pa)->sin_addr, &camInfo->
ip,
sizeof(
struct in_addr));
79 arp.arp_flags = ATF_PERM;
81 arp.arp_ha.sa_family = ARPHRD_ETHER;
82 memcpy(&arp.arp_ha.sa_data, camInfo->
mac, 6);
84 strncpy(arp.arp_dev, camInfo->
ifName,
sizeof(arp.arp_dev));
86 if( ioctl(s, SIOCSARP, &arp) == -1 ) {
91 wge100_debug(
"Camera %u successfully configured\n", camInfo->
serial);
113 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
114 perror(
"wge100ArpDel can't create socket");
118 wge100_debug(
"Removing ARP info for S/N %d \n", camInfo->
serial);
121 ((
struct sockaddr_in*)&arp.arp_pa)->sin_family = AF_INET;
122 memcpy(&((
struct sockaddr_in*)&arp.arp_pa)->sin_addr, &camInfo->
ip,
sizeof(
struct in_addr));
127 arp.arp_ha.sa_family = ARPHRD_ETHER;
128 memcpy(&arp.arp_ha.sa_data, camInfo->
mac, 6);
130 strncpy(arp.arp_dev, camInfo->
ifName,
sizeof(arp.arp_dev));
133 if( ioctl(s, SIOCDARP, &arp) == -1 ) {
134 perror(
"Warning, was unable to remove ARP entry");
138 wge100_debug(
"Camera %u successfully removed from ARP table\n", camInfo->
serial);
159 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
160 perror(
"wge100EthGetLocalMac can't create socket");
165 strncpy(ifr.ifr_name,ifName,
sizeof(ifr.ifr_name)-1);
166 ifr.ifr_name[
sizeof(ifr.ifr_name)-1]=
'\0';
169 if( ioctl(s, SIOCGIFHWADDR, &ifr) == -1 ) {
170 fprintf(stderr,
"On interface '%s': ", ifName);
171 perror(
"wge100EthGetLocalMac ioctl failed");
177 memcpy(macAddr, &ifr.ifr_addr,
sizeof(
struct sockaddr));
198 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
199 perror(
"wge100IpGetLocalBcast can't create socket");
204 strncpy(ifr.ifr_name,ifName,
sizeof(ifr.ifr_name)-1);
205 ifr.ifr_name[
sizeof(ifr.ifr_name)-1]=
'\0';
208 if( ioctl(s,SIOCGIFBRDADDR , &ifr) == -1 ) {
216 memcpy(&(bcast->s_addr), &((
struct sockaddr_in *)(&ifr.ifr_broadaddr))->sin_addr,
sizeof(
struct in_addr));
236 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
237 perror(
"wge100IpGetLocalAddr can't create socket");
242 strncpy(ifr.ifr_name,ifName,
sizeof(ifr.ifr_name)-1);
243 ifr.ifr_name[
sizeof(ifr.ifr_name)-1]=
'\0';
246 if( ioctl(s,SIOCGIFADDR , &ifr) == -1 ) {
253 memcpy(&(addr->s_addr), &((
struct sockaddr_in *)(&ifr.ifr_broadaddr))->sin_addr,
sizeof(
struct in_addr));
274 if ((s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP))==-1) {
275 perror(
"wge100IpGetLocalAddr can't create socket");
280 strncpy(ifr.ifr_name,ifName,
sizeof(ifr.ifr_name)-1);
281 ifr.ifr_name[
sizeof(ifr.ifr_name)-1]=
'\0';
284 if( ioctl(s,SIOCGIFNETMASK , &ifr) == -1 ) {
291 memcpy(&(addr->s_addr), &((
struct sockaddr_in *)(&ifr.ifr_broadaddr))->sin_addr,
sizeof(
struct in_addr));
310 if ( (s=socket(AF_INET, SOCK_DGRAM, IPPROTO_UDP)) == -1 ) {
311 perror(
"wge100SocketCreate can't create socket");
316 struct sockaddr_in si_host;
317 memset( (uint8_t*)&si_host, 0,
sizeof(si_host) );
318 si_host.sin_family = AF_INET;
319 memcpy(&si_host.sin_addr, addr,
sizeof(
struct in_addr));
320 si_host.sin_port = htons(port);
324 if( bind(s, (
struct sockaddr *)&si_host,
sizeof(si_host)) == -1 ) {
334 if( setsockopt(s, SOL_SOCKET,SO_BROADCAST, &flags,
sizeof(flags)) == -1) {
335 perror(
"wge100SocketCreate unable to set broadcast socket option");
355 struct sockaddr_in camIP;
362 if (*ip == 0x0100007F)
365 camIP.sin_family = AF_INET;
367 camIP.sin_addr.s_addr=*ip;
369 if( connect(s, (
struct sockaddr *)&camIP,
sizeof(camIP)) == -1 ) {
370 perror(
"Could not connect datagram socket");
389 struct in_addr host_addr;
400 if(localHost != NULL) {
401 struct sockaddr_in socketAddr;
402 socklen_t socketAddrSize =
sizeof(socketAddr);
403 if( getsockname(s, (
struct sockaddr *)&socketAddr, &socketAddrSize) == -1) {
404 perror(
"wge100SocketToNetHost Could not get socket name");
409 struct sockaddr macAddr;
415 memcpy(localHost->
mac, macAddr.sa_data,
sizeof(localHost->
mac));
416 localHost->
addr = socketAddr.sin_addr.s_addr;
417 localHost->
port = socketAddr.sin_port;
437 struct sockaddr_in si_cam;
438 memset( (uint8_t *)&si_cam, 0,
sizeof(si_cam) );
439 si_cam.sin_family = AF_INET;
441 si_cam.sin_addr.s_addr = *ip;
444 if( sendto(s, data, dataSize, 0, (
struct sockaddr*)&si_cam,
sizeof(si_cam)) == -1 ) {
445 perror(
"wge100SendUDP unable to send packet");
466 struct in_addr bcastIP;
496 struct timeval timeout;
497 timeout.tv_sec = *wait_us / 1000000UL;
498 timeout.tv_usec = *wait_us % 1000000UL;
502 struct timeval timestarted;
503 struct timeval timenow;
504 gettimeofday(×tarted, NULL);
505 gettimeofday(&timenow, NULL);
506 timeradd( &timeout, ×tarted, &timeout );
508 struct timeval looptimeout;
510 while( timercmp( &timeout, &timenow, >= ) ) {
515 timersub(&timeout, ×tarted, &looptimeout);
518 for (i = 0; i < nums; i++)
526 if( select(maxs+1, &
set, NULL, NULL, &looptimeout) == -1 ) {
527 perror(
"wge100WaitForPacket select failed");
531 for (i = 0; i < nums; i++) {
533 if( FD_ISSET(s[i], &
set) ) {
537 if( (r=recvfrom( s[i], &gPkt,
sizeof(
PacketGeneric), MSG_PEEK|MSG_TRUNC, NULL, NULL )) == -1 ) {
538 perror(
"wge100WaitForPacket unable to receive from socket");
545 if( ((
unsigned int) r < pktLen) ||
547 gPkt.
type != htonl(type) ) {
548 wge100_debug(
"Dropping packet with magic #%08X, type 0x%02X (looking for 0x%02X), length %d (looking for %d)\n", ntohl(gPkt.
magic_no), ntohl(gPkt.
type), type, r, pktLen);
550 if( recvfrom( s[i], &gPkt,
sizeof(
PacketGeneric), 0, NULL, NULL ) == -1 ) {
551 perror(
"wge100WaitForPacket unable to receive from socket");
557 struct timeval timeleft;
558 gettimeofday(&timenow, NULL);
559 timersub(&timeout, &timenow, &timeleft);
561 if (timeleft.tv_sec < 0)
565 *wait_us = timeleft.tv_usec+timeleft.tv_sec*1000000UL;
571 gettimeofday(&timenow, NULL);
uint32_t type
The packet type (see list of packet types, above)
int wge100ArpDel(IpCamList *camInfo)
int wge100IpGetLocalBcast(const char *ifName, struct in_addr *bcast)
int wge100SendUDPBcast(int s, const char *ifName, const void *data, size_t dataSize)
int wge100SendUDP(int s, const IPAddress *ip, const void *data, size_t dataSize)
int wge100SocketConnect(int s, const IPAddress *ip)
int wge100IpGetLocalAddr(const char *ifName, struct in_addr *addr)
int wge100WaitForPacket(int *s, int nums, uint32_t type, size_t pktLen, uint32_t *wait_us)
int wge100IpGetLocalNetmask(const char *ifName, struct in_addr *addr)
int wge100SocketCreate(const struct in_addr *addr, uint16_t port)
int wge100ArpAdd(IpCamList *camInfo)
uint32_t magic_no
The Willow Garage Magic number (always WG_MAGIC_NO)
int wge100CmdSocketCreate(const char *ifName, NetHost *localHost)
int wge100EthGetLocalMac(const char *ifName, struct sockaddr *macAddr)