98
98
#include < net/if.h>
99
99
#include < netinet/in.h>
100
100
#include < unistd.h>
101
+ #include < ifaddrs.h>
101
102
102
103
103
104
@@ -116,7 +117,8 @@ public:
116
117
static const KUInt16 TCPFlagFIN = 0x0001 ;
117
118
118
119
/* * Used in GetType(). */
119
- static const KUInt16 NetTypeIP = 0x0800 ;
120
+ static const KUInt16 NetTypeIP = 0x0800 ;
121
+ static const KUInt16 NetTypeARP = 0x0806 ;
120
122
121
123
/* * Used in GetIPProtocol() */
122
124
static const KUInt8 IPProtocolTCP = 6 ;
@@ -139,8 +141,13 @@ public:
139
141
}
140
142
mCopy = 1 ;
141
143
} else {
142
- mData = data;
143
- mCopy = 0 ;
144
+ if (data) {
145
+ mData = data;
146
+ mCopy = 0 ;
147
+ } else {
148
+ mData = (KUInt8*)calloc (size, 1 );
149
+ mCopy = 1 ;
150
+ }
144
151
}
145
152
mSize = size;
146
153
}
@@ -179,13 +186,15 @@ public:
179
186
mData [i+3 ] = v>>16 ;
180
187
mData [i+4 ] = v>>8 ;
181
188
mData [i+5 ] = v; }
189
+
182
190
/* * Get a 32-bit word in network order. */
183
191
KUInt32 Get32 (KUInt32 i) { return (mData [i]<<24 )|(mData [i+1 ]<<16 )|(mData [i+2 ]<<8 )|(mData [i+3 ]); }
184
192
void Set32 (KUInt32 i, KUInt32 v) {
185
193
mData [i+0 ] = v>>24 ;
186
194
mData [i+1 ] = v>>16 ;
187
195
mData [i+2 ] = v>>8 ;
188
196
mData [i+3 ] = v; }
197
+
189
198
/* * Get a 16-bit word in network order. */
190
199
KUInt16 Get16 (KUInt32 i) { return (mData [i]<<8 )|(mData [i+1 ]); }
191
200
void Set16 (KUInt32 i, KUInt16 v) {
@@ -232,6 +241,16 @@ public:
232
241
KUInt8 *GetUDPPayloadStart (){ return mData + 42 ; }
233
242
KUInt32 GetUDPPayloadSize () { return mSize - 42 ; }
234
243
244
+ KUInt16 GetARPHType () { return Get16 (14 ); }
245
+ KUInt16 GetARPPType () { return Get16 (16 ); }
246
+ KUInt8 GetARPHLen () { return mData [18 ]; }
247
+ KUInt8 GetARPPLen () { return mData [19 ]; }
248
+ KUInt16 GetARPOp () { return Get16 (20 ); }
249
+ KUInt64 GetARPSHA () { return Get48 (22 ); }
250
+ KUInt32 GetARPSPA () { return Get32 (28 ); }
251
+ KUInt64 GetARPTHA () { return Get48 (32 ); }
252
+ KUInt32 GetARPTPA () { return Get32 (38 ); }
253
+
235
254
236
255
void SetDstMAC (KUInt64 v) { Set48 (0 , v); }
237
256
void SetSrcMAC (KUInt64 v) { Set48 (6 , v); }
@@ -267,6 +286,16 @@ public:
267
286
void SetUDPChecksum (KUInt16 v) { Set16 (40 , v); }
268
287
void SetUDPPayload (KUInt8 *, KUInt32);
269
288
289
+ void SetARPHType (KUInt16 v) { Set16 (14 , v); }
290
+ void SetARPPType (KUInt16 v) { Set16 (16 , v); }
291
+ void SetARPHLen (KUInt8 v) { mData [18 ] = v; }
292
+ void SetARPPLen (KUInt8 v) { mData [19 ] = v; }
293
+ void SetARPOp (KUInt16 v) { Set16 (20 , v); }
294
+ void SetARPSHA (KUInt64 v) { Set48 (22 , v); }
295
+ void SetARPSPA (KUInt32 v) { Set32 (28 , v); }
296
+ void SetARPTHA (KUInt64 v) { Set48 (32 , v); }
297
+ void SetARPTPA (KUInt32 v) { Set32 (38 , v); }
298
+
270
299
Packet *prev, *next;
271
300
272
301
private:
@@ -628,12 +657,12 @@ public:
628
657
* Create a UDP packet handler.
629
658
*/
630
659
UDPPacketHandler (TUsermodeNetwork *h, Packet &packet) :
631
- PacketHandler (h),
632
- myMAC (0 ), theirMAC(0 ),
633
- myIP (0 ), theirIP(0 ),
634
- myPort (0 ), theirPort(0 ),
635
- theirID (0 ),
636
- mSocket (-1 )
660
+ PacketHandler (h),
661
+ myMAC (0 ), theirMAC(0 ),
662
+ myIP (0 ), theirIP(0 ),
663
+ myPort (0 ), theirPort(0 ),
664
+ theirID (0 ),
665
+ mSocket (-1 )
637
666
{
638
667
myMAC = packet.GetSrcMAC ();
639
668
myIP = packet.GetIPSrcIP ();
@@ -691,7 +720,7 @@ public:
691
720
net->SetIPv4Checksum (p->Data (), p->Size ());
692
721
net->SetUDPChecksum (p->Data (), p->Size ());
693
722
}
694
-
723
+
695
724
/* *
696
725
* Send a Newton packet to the outside world.
697
726
* \param packet send this packet
@@ -720,8 +749,8 @@ public:
720
749
theirSockAddr.sin_port = htons (theirPort);
721
750
}
722
751
int ret =
723
- sendto (mSocket , packet.GetUDPPayloadStart (), packet.GetUDPPayloadSize (),
724
- 0 , (struct sockaddr *)&theirSockAddr, sizeof (theirSockAddr));
752
+ sendto (mSocket , packet.GetUDPPayloadStart (), packet.GetUDPPayloadSize (),
753
+ 0 , (struct sockaddr *)&theirSockAddr, sizeof (theirSockAddr));
725
754
if (ret==-1 )
726
755
return -1 ;
727
756
return 1 ;
@@ -740,7 +769,7 @@ public:
740
769
if (mSocket ==-1 )
741
770
return ;
742
771
int avail =
743
- recvfrom (mSocket , buf, sizeof (buf), 0 , (struct sockaddr *)&theirSockAddr, &addrLen);
772
+ recvfrom (mSocket , buf, sizeof (buf), 0 , (struct sockaddr *)&theirSockAddr, &addrLen);
744
773
if (avail<1 )
745
774
return ;
746
775
@@ -781,6 +810,55 @@ public:
781
810
782
811
783
812
813
+ /* *
814
+ * This class handles UDP packets.
815
+ */
816
+ class ARPPacketHandler : public PacketHandler
817
+ {
818
+ public:
819
+
820
+ /* *
821
+ * Can we handle the given package?
822
+ * \param packet the Newton packet that could be sent
823
+ * \param n the network interface
824
+ * \return 0 if we can not handle this packet
825
+ * \return 1 if we can handled it and it need not to be propagated any further
826
+ * \return -1 if an error occurred an no other handler should handle this packet
827
+ */
828
+ static int canHandle (Packet &packet, TUsermodeNetwork *net) {
829
+ if ( packet.GetType () != Packet::NetTypeARP )
830
+ return 0 ;
831
+ printf (" ---> Newt wants to send some ARP request.\n " );
832
+ if ( packet.GetARPOp () != 1 ) // is this a request?
833
+ return 0 ;
834
+ if ( packet.GetARPHType () != 1 ) // is this an Ethernet MAC address?
835
+ return 0 ;
836
+ if ( packet.GetARPPType () != 0x0800 ) // Which upper layer protocol? IP?
837
+ return 0 ;
838
+ if ( packet.GetARPHLen () != 6 ) // Length of MAC Address?
839
+ return 0 ;
840
+ if ( packet.GetARPPLen () != 4 ) // Length of IP Address?
841
+ return 0 ;
842
+ Packet *reply = new Packet (0L , 42 );
843
+ reply->SetSrcMAC ( packet.GetDstMAC () );
844
+ reply->SetDstMAC ( packet.GetSrcMAC () );
845
+ reply->SetType ( Packet::NetTypeARP );
846
+ reply->SetARPHType ( 1 );
847
+ reply->SetARPPType ( 0x0800 );
848
+ reply->SetARPHLen ( 6 );
849
+ reply->SetARPPLen ( 4 );
850
+ reply->SetARPOp ( 2 ); // reply
851
+ reply->SetARPSHA ( 0x000000fa00000000ULL | packet.GetARPTPA () ); // faking a MAC address
852
+ reply->SetARPSPA ( packet.GetARPTPA () );
853
+ reply->SetARPTHA ( packet.GetARPSHA () );
854
+ reply->SetARPTPA ( packet.GetARPSPA () );
855
+ net->Enqueue (reply);
856
+ return 1 ;
857
+ }
858
+ };
859
+
860
+
861
+
784
862
/* *
785
863
* Create an interface betweenthe Newton network driver and Einstein.
786
864
*/
@@ -790,6 +868,23 @@ TUsermodeNetwork::TUsermodeNetwork(TLog* inLog) :
790
868
mFirstPacket( 0L ),
791
869
mLastPacket( 0L )
792
870
{
871
+ // http://www.developerweb.net/forum/showthread.php?t=5085
872
+ /*
873
+ struct ifaddrs *interfaces;
874
+ int ret = getifaddrs(&interfaces);
875
+ if (ret==0) {
876
+ struct ifaddrs *ifa = interfaces;
877
+ while (ifa) {
878
+ if (ifa->ifa_name && strcmp(ifa->ifa_name, "en1")==0) {
879
+ printf("Network adapter found\n");
880
+ struct if_data *ifd = (struct if_data *)ifa->ifa_data;
881
+ int x = ifd->ifi_obytes;
882
+ }
883
+ ifa = ifa->ifa_next;
884
+ }
885
+ freeifaddrs(interfaces);
886
+ }
887
+ */
793
888
}
794
889
795
890
@@ -840,6 +935,11 @@ int TUsermodeNetwork::SendPacket(KUInt8 *data, KUInt32 size)
840
935
case 1 : return 0 ; // packet was handled successfuly, leave
841
936
case 0 : break ; // another handler should deal with this packet
842
937
}
938
+ switch ( ARPPacketHandler::canHandle (packet, this ) ) {
939
+ case -1 : return -1 ; // an error occured, packet must not be handled
940
+ case 1 : return 0 ; // packet was handled successfuly, leave
941
+ case 0 : break ; // another handler should deal with this packet
942
+ }
843
943
844
944
// FIXME: simulate the ARP request for now
845
945
// see: man 3 getaddrinfo()
0 commit comments