Submitted By: Mario Fetka (mario dot fetka at gmail dot com) Date: 2008-08-06 Initial Package Version: 5.11.04 Origin: http://irtrans.de irserver package 5.09.07 Upstream Status: unknown Description: readd ip_assign to the irclient package diff -Naur irclient-5.11.04.orig/ip_assign.c irclient-5.11.04/ip_assign.c --- irclient-5.11.04.orig/ip_assign.c 1970-01-01 00:00:00.000000000 +0000 +++ irclient-5.11.04/ip_assign.c 2007-08-19 21:03:25.000000000 +0000 @@ -0,0 +1,601 @@ +/* + * Copyright (c) 2007, IRTrans GmbH + * All rights reserved. + * + * Redistribution and use in source and binary forms, with or without + * modification, are permitted provided that the following conditions are met: + * * Redistributions of source code must retain the above copyright + * notice, this list of conditions and the following disclaimer. + * * Redistributions in binary form must reproduce the above copyright + * notice, this list of conditions and the following disclaimer in the + * documentation and/or other materials provided with the distribution. + * * Neither the name of IRTrans GmbH nor the + * names of its contributors may be used to endorse or promote products + * derived from this software without specific prior written permission. + * + * THIS SOFTWARE IS PROVIDED BY IRTrans GmbH ``AS IS'' AND ANY + * EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED + * WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE + * DISCLAIMED. IN NO EVENT SHALL IRTrans GmbH BE LIABLE FOR ANY + * DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES + * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; + * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND + * ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT + * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF + * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. + */ + + + +#ifdef WIN32 + +#include +#include + +#else + +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + + +typedef int SOCKET; +typedef void* WSAEVENT; +#define closesocket close +#endif + +#include + +typedef unsigned char byte; + +#ifdef WIN32 +typedef unsigned int uint; +typedef unsigned short ushort; +#endif + +typedef struct { + byte op; + byte htype; + byte hlen; + byte hops; + uint xid; + ushort secs; + ushort flags; + uint client_ip; + uint own_ip; + uint server_ip; + uint relay_ip; + byte client_mac[16]; + byte server_name[64]; + byte file[128]; + byte opt_header[13]; + byte options[299]; +} DHCP_REQUEST; + + +typedef struct { + uint ip; + uint netmask; + uint gateway; + char password[30]; + byte dhcp_flag; +} IP_PARAM; + +typedef struct { + uint ip; + byte mac[6]; + char firmware[10]; + char ir_firmware[10]; + byte dhcp_flag; +} DEVICE_ENTRY; + + + +#ifdef WIN32 +WSAEVENT IrtLanEvent; +#endif + + +SOCKET snd_socket[32],rcv_socket; + +int open_sockets (uint ip[],int cnt); +void new_devicequery (byte msg,uint srvadr,DHCP_REQUEST *req); +void new_dhcpset (byte msg,uint srvadr,byte mac[],char password[],DHCP_REQUEST *req); +void new_ipset (byte msg,uint srvadr,byte mac[],uint ip,uint nm,uint gw,char password[],DHCP_REQUEST *req); +void SendQuery (uint ips[],int cnt); +int ReadDevices (DEVICE_ENTRY *dev,int cnt); +int StoreDevice (DEVICE_ENTRY *dev,DHCP_REQUEST *req); +void read_ip_parameter (IP_PARAM *ip); +void SetIP (uint ips[],int cnt,DEVICE_ENTRY *dev,IP_PARAM *ip); +int GetOwnIP (uint ips[]); +int GetInterfaces (uint ips[]); + + +int main (int argc,char *argv[]) + +{ + int res,i,choice; + char nm[100]; + struct in_addr iadr; + IP_PARAM ip; + DEVICE_ENTRY irtrans[256]; + int device_count; + uint ip_adr[32]; + int ip_count = 0; + + +#ifdef WIN32 + int err; + WORD wVersionRequired; + WSADATA wsaData; + wVersionRequired = MAKEWORD(2,2); + err = WSAStartup(wVersionRequired, &wsaData); + if (err != 0) return (1); +#endif + + argc++; + for (;--argc > 2;argv++) { // Process all Command Line Arguments + if (!strcmp (argv[1],"-interface")) { + argc--; + argv++; + ip_adr[0] = inet_addr (argv[1]); + ip_count = 1; + continue; + } + + } + + + if (!ip_count) { + ip_count = GetOwnIP (ip_adr); + } + + for (i=0;i < ip_count;i++) { + iadr.s_addr = ip_adr[i]; + if (!i) printf ("\nOwn Interfaces / IPs: %s\n",inet_ntoa(iadr)); + else printf (" %s\n",inet_ntoa(iadr)); + } + + printf ("\n"); + res = open_sockets (ip_adr,ip_count); + + + if (res) { + printf ("Open socket error: %d [%d]\n",res,errno); + perror (NULL); + exit (-1); + } + + while (1) { + SendQuery (ip_adr,ip_count); + + memset (irtrans,0,sizeof (irtrans)); + device_count = ReadDevices (irtrans,ip_count); + + if (!device_count) { + printf ("No IRTrans Ethernet device(s) found\n"); + exit (0); + } + + + printf ("\n%2d IRTrans Ethernet device(s) found:\n",device_count); + printf ("----------------------------------------------------------------\n"); + + for (i=0;i < device_count;i++) { + iadr.s_addr = irtrans[i].ip; + + if (irtrans[i].dhcp_flag == 1) strcpy (nm,"DHCP"); + else nm[0] = 0; + printf ("%2d %02x-%02x-%02x-%02x-%02x-%02x %-15s %s %8s %s\n",i+1,irtrans[i].mac[0],irtrans[i].mac[1],irtrans[i].mac[2], + irtrans[i].mac[3],irtrans[i].mac[4],irtrans[i].mac[5],inet_ntoa(iadr),irtrans[i].firmware,irtrans[i].ir_firmware,nm); + } + printf ("----------------------------------------------------------------\n"); + + + printf ("Please select device to use (0 = Exit) "); + fflush (stdout); + do { + fgets (nm,sizeof (nm),stdin); + choice = atoi (nm); + } while (nm[0] < '0'); + + if (!choice) exit (0); + + read_ip_parameter (&ip); + + SetIP (ip_adr,ip_count,irtrans + choice-1,&ip); + } + + + return (0); +} + + +int GetOwnIP (uint ips[]) +{ + char nm[100]; + int count = 0; + +#ifdef WIN32 + struct hostent *host; + + gethostname (nm,100); + + host = gethostbyname (nm); + + while (host->h_addr_list[count]) { + if (host->h_addr_list[count][0] != 127) break; + } + ips[0] = *((uint *)host->h_addr_list[count]); + count = 1; +#else + count = GetInterfaces (ips); +#endif + return (count); +} + + +#ifndef WIN32 +int GetInterfaces (uint ips[]) +{ + int i,j,cnt; + FILE *fp; + char *pnt,ln[256]; + struct sockaddr_in *sinp; + struct ifreq ifr; + int s; /* Socket */ + char local_ip_addr[16]; + + fp = fopen ("/proc/net/dev","r"); + if (!fp) return (0); + s = socket(AF_INET, SOCK_DGRAM, 0); + + cnt = 0; + pnt = fgets (ln,sizeof (ln),fp); + while (pnt) { + i = 0; + while (ln[i] == ' ') i++; + if (!memcmp (ln+i,"eth",3)) { + j = i; + while ((ln[j] >= '0' && ln[j] <= '9') || (ln[j] >= 'a' && ln[j] <= 'z') || (ln[j] >= 'A' && ln[j] <= 'Z')) j++; + ln[j] = 0; + memset (&ifr,0,sizeof (ifr)); + strcpy(ifr.ifr_name, ln+i); + ioctl(s, SIOCGIFADDR, &ifr); + sinp = (struct sockaddr_in*)&ifr.ifr_addr; + ips[cnt++] = sinp->sin_addr.s_addr; + } + pnt = fgets (ln,sizeof (ln),fp); + } + + close (s); + fclose (fp); + + return (cnt); +} +#endif + + +void SetIP (uint server_ip[],int cnt,DEVICE_ENTRY *dev,IP_PARAM *ip) +{ + int i; + DHCP_REQUEST req; + + for (i=0;i < cnt;i++) { + if (ip->dhcp_flag) new_dhcpset (190,server_ip[i],dev->mac,ip->password,&req); + + else new_ipset (189,server_ip[i],dev->mac,ip->ip,ip->netmask,ip->gateway,ip->password,&req); + + send (snd_socket[i],(char *)&req,sizeof (DHCP_REQUEST),0); + } +} + +void read_ip_parameter (IP_PARAM *ip) +{ + char nm[100]; + + memset (ip,0,sizeof (IP_PARAM)); + + printf ("\n\nConfigure device for DHCP (Y/N) ? "); + fflush (stdout); + fgets (nm,sizeof (nm),stdin); + printf ("\n"); + + if (*nm == 'Y' || *nm == 'y') ip->dhcp_flag = 1; + else { + do { + printf ("\nEnter IP Address: "); + fflush (stdout); + fgets (nm,sizeof (nm),stdin); + ip->ip = inet_addr (nm); + } while (ip->ip == INADDR_NONE); + do { + printf ("\nEnter Netmask : "); + fflush (stdout); + fgets (nm,sizeof (nm),stdin); + ip->netmask = inet_addr (nm); + } while (ip->netmask == INADDR_NONE); + do { + printf ("\nDefault Gateway : "); + fflush (stdout); + fgets (nm,sizeof (nm),stdin); + ip->gateway = inet_addr (nm); + } while (ip->gateway == INADDR_NONE); + } + + printf ("\n\nEnter device password: "); + fflush (stdout); + fgets (ip->password,8,stdin); + + printf ("\n"); +} + +void SendQuery (uint ip_adr[],int cnt) +{ + int i; + DHCP_REQUEST req; + + for (i=0;i < cnt;i++) { + new_devicequery (187,ip_adr[i],&req); + send (snd_socket[i],(char *)&req,sizeof (DHCP_REQUEST),0); + } +} + + +int ReadDevices (DEVICE_ENTRY *dev,int ipcnt) +{ + int i; + DHCP_REQUEST req; + int res,cnt = 0; + +#ifndef WIN32 + fd_set events; + int maxfd,wait; + struct timeval tv; +#endif + +#ifdef WIN32 + IrtLanEvent = WSACreateEvent (); + WSAEventSelect (rcv_socket, IrtLanEvent,FD_READ); +#endif + + while (1) { + +#ifdef WIN32 + res = WaitForSingleObject (IrtLanEvent,5000); + if (res == WAIT_TIMEOUT) break; + ResetEvent (IrtLanEvent); + +#else + + FD_ZERO (&events); + + FD_SET (rcv_socket,&events); + maxfd = rcv_socket + 1; + + tv.tv_sec = 1; + tv.tv_usec = 0; + + wait = select (maxfd,&events,NULL,NULL,&tv); + if (!wait) break; +#endif + res = recv (rcv_socket,(char *)&req,sizeof (req),0); + cnt += StoreDevice (dev + cnt,&req); + i = 0; + while (i < (cnt - 1)) { + if (!memcmp (dev[i].mac,dev[cnt-1].mac,6)) break; + i++; + } + if (i < (cnt - 1)) cnt--; + + } + return (cnt); +} + + +int StoreDevice (DEVICE_ENTRY *dev,DHCP_REQUEST *req) +{ + if (req -> op != 1) return (0); + + if (req -> opt_header[0] != 99 || req -> opt_header[1] != 130 || req -> opt_header[2] != 83 || req -> opt_header[3] != 99) return (0); + if (req -> opt_header[4] != 53 || req -> opt_header[5] != 1 || req -> opt_header[6] != 188 || req -> opt_header[7] != 0xe9) return (0); + + if (req -> opt_header[9] != 0x78 || req -> opt_header[10] != 0xa3 || req -> opt_header[11] != 0x8b || req -> opt_header[12] != 0x84) return (0); + + + if (req -> client_mac[0] != 0 || req -> client_mac[1] != 0x50 || req -> client_mac[2] != 0xC2 || + req -> client_mac[3] != 0x52 || (req -> client_mac[4] & 0xf0) != 0x70) return (0); + + + dev->ip = req -> client_ip; + memcpy (dev->mac,req -> client_mac,6); + dev->dhcp_flag = req->options[0]; + memcpy (dev->firmware,req->options+1,8); + if (req -> opt_header[8] == 23) memcpy (dev->ir_firmware,req->options+9,8); + + return (1); +} + + +void new_dhcpset (byte msg,uint srvadr,byte mac[],char password[],DHCP_REQUEST *req) +{ + int i,pos; + + memset (req,0,sizeof (DHCP_REQUEST)); + + req->op = 2; + req->htype = 1; + req->hlen = 6; + req->hops = 0; + req->xid = htonl(0xaabbccdd); + req->secs = 0; + req->flags = htons (0x8000); + req->server_ip = srvadr; + memcpy (req->client_mac,mac,6); + + req->opt_header[0] = 99; + req->opt_header[1] = 130; + req->opt_header[2] = 83; + req->opt_header[3] = 99; + req->opt_header[4] = 53; + req->opt_header[5] = 1; + req->opt_header[6] = msg; + req->opt_header[7] = 233; + req->opt_header[8] = 4; + req->opt_header[9] = 0xac; + req->opt_header[10] = 0x75; + req->opt_header[11] = 0x20; + req->opt_header[12] = 0xbf; + + pos = 0; + req->options[pos++] = 234; + req->options[pos++] = strlen (password) + 1; + + for (i=0;i < (int)strlen (password);i++) req->options[pos++] = password[i]; + req->options[pos++] = 0; + req->options[pos++] = 255; +} + +void new_ipset (byte msg,uint srvadr,byte mac[],uint ip,uint nm,uint gw,char password[],DHCP_REQUEST *req) +{ + int i,pos; + + memset (req,0,sizeof (DHCP_REQUEST)); + + req->op = 2; + req->htype = 1; + req->hlen = 6; + req->hops = 0; + req->xid = htonl(0xaabbccdd); + req->secs = 0; + req->flags = htons (0x8000); + req->server_ip = srvadr; + req->own_ip = ip; + memcpy (req->client_mac,mac,6); + + req->opt_header[0] = 99; + req->opt_header[1] = 130; + req->opt_header[2] = 83; + req->opt_header[3] = 99; + req->opt_header[4] = 53; + req->opt_header[5] = 1; + req->opt_header[6] = msg; + req->opt_header[7] = 233; + req->opt_header[8] = 4; + req->opt_header[9] = 0xac; + req->opt_header[10] = 0x75; + req->opt_header[11] = 0x20; + req->opt_header[12] = 0xbf; + + pos = 0; + req->options[pos++] = 1; // Subnetmask + req->options[pos++] = 4; + memcpy (req->options+pos,&nm,4); + pos += 4; + + req->options[pos++] = 3; // Default Gateway + req->options[pos++] = 4; + memcpy (req->options+pos,&gw,4); + pos += 4; + + req->options[pos++] = 234; + req->options[pos++] = strlen (password) + 1; + + for (i=0;i < (int)strlen (password);i++) req->options[pos++] = password[i]; + req->options[pos++] = 0; + req->options[pos++] = 255; + +} + +void new_devicequery (byte msg,uint srvadr,DHCP_REQUEST *req) +{ + memset (req,0,sizeof (DHCP_REQUEST)); + + req->op = 2; + req->htype = 1; + req->hlen = 6; + req->hops = 0; + req->xid = htonl(0xaabbccdd); + req->secs = 0; + req->flags = htons (0x8000); + req->server_ip = srvadr; + req->client_mac[0] = 0xff; + req->client_mac[1] = 0xff; + req->client_mac[2] = 0xff; + req->client_mac[3] = 0xff; + req->client_mac[4] = 0xff; + req->client_mac[5] = 0xff; + + req->opt_header[0] = 99; + req->opt_header[1] = 130; + req->opt_header[2] = 83; + req->opt_header[3] = 99; + req->opt_header[4] = 53; + req->opt_header[5] = 1; + req->opt_header[6] = msg; + req->opt_header[7] = 233; + req->opt_header[8] = 4; + req->opt_header[9] = 0x78; + req->opt_header[10] = 0xa3; + req->opt_header[11] = 0x8b; + req->opt_header[12] = 0x84; + + req->options[0] = 255; +} + +int open_sockets (uint ip[],int cnt) +{ + int res,i; + struct sockaddr_in iadr; + struct sockaddr_in serv_addr; + + rcv_socket = socket (PF_INET,SOCK_DGRAM,0); + if (rcv_socket < 0) return (2); + + memset (&serv_addr,0,sizeof (serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = INADDR_ANY; + serv_addr.sin_port = htons (67); + + res = bind (rcv_socket,(struct sockaddr *)&serv_addr,sizeof (serv_addr)); + if (res) return (3); + + + for (i=0;i < cnt;i++) { + snd_socket[i] = socket (PF_INET,SOCK_DGRAM,0); + if (snd_socket[i] < 0) return (4); + res = 1; + setsockopt (snd_socket[i],SOL_SOCKET,SO_BROADCAST,(char *)&res,sizeof (int)); + + +#ifndef WIN32 + memset (&serv_addr,0,sizeof (serv_addr)); + serv_addr.sin_family = AF_INET; + serv_addr.sin_addr.s_addr = ip[i]; + serv_addr.sin_port = 0; + + res = bind (snd_socket[i],(struct sockaddr *)&serv_addr,sizeof (serv_addr)); + if (res) return (6); +#endif + + memset (&iadr,0,sizeof (struct sockaddr)); + iadr.sin_family = AF_INET; + iadr.sin_addr.s_addr = INADDR_BROADCAST; + iadr.sin_port = htons (68); + + if (connect (snd_socket[i],(struct sockaddr *)&iadr,sizeof (struct sockaddr_in)) < 0) return (5); + } + + return (0); +} + diff -Naur irclient-5.11.04.orig/makefile irclient-5.11.04/makefile --- irclient-5.11.04.orig/makefile 2008-08-06 20:11:16.000000000 +0000 +++ irclient-5.11.04/makefile 2008-08-06 20:27:31.000000000 +0000 @@ -15,6 +15,11 @@ OBJS64 = $(patsubst %,$(ODIR64)/%,$(_OBJS)) OBJSARM = $(patsubst %,$(ODIRARM)/%,$(_OBJS)) +_OBJS_IP = ip_assign.o +OBJS_IP = $(patsubst %,$(ODIR)/%,$(_OBJS_IP)) +OBJS64_IP = $(patsubst %,$(ODIR64)/%,$(_OBJS_IP)) +OBJSARM_IP = $(patsubst %,$(ODIRARM)/%,$(_OBJS_IP)) + irclient: $(OBJS) $(CC) $(CFLAGS) $(OBJS) -m32 -o irclient $(LDFLAGS) @@ -26,15 +31,29 @@ $(CC) $(CFLAGS) $(OBJSARM) -o irclient $(LDFLAGS) -all: irclient irclient64 +ip_assign: $(OBJS_IP) + $(CC) $(CFLAGS) $(OBJS_IP) -m32 -o ip_assign $(LDFLAGS) + +ip_assign64: $(OBJS64_IP) + $(CC) $(CFLAGS) -DX64 $(OBJS64_IP) -m64 -o ip_assign64 $(LDFLAGS) + +ip_assign_arm: $(OBJSARM_IP) + $(CC) $(CFLAGS) $(OBJSARM_IP) -o ip_assign $(LDFLAGS) -arm: irclient_arm + +all: irclient irclient64 ip_assign ip_assign64 + +arm: irclient_arm ip_assign_arm clean: - -rm $(OBJS) - -rm $(OBJS64) - -rm $(OBJSARM) + -rm -f $(OBJS) + -rm -f $(OBJS64) + -rm -f $(OBJSARM) + + -rm -f $(OBJS_IP) + -rm -f $(OBJS64_IP) + -rm -f $(OBJSARM_IP) $(ODIR)/%.o: %.c flash.h errcode.h network.h remote.h makefile