wiringpi-ruby 2.0.0 → 2.1.3
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- checksums.yaml +4 -4
- data/ext/wiringpi/WiringPi/devLib/ds1302.c +240 -0
- data/ext/wiringpi/WiringPi/devLib/ds1302.h +44 -0
- data/ext/wiringpi/WiringPi/devLib/font.h +2577 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.c +164 -0
- data/ext/wiringpi/WiringPi/devLib/gertboard.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.c +495 -0
- data/ext/wiringpi/WiringPi/devLib/lcd.h +52 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.c +673 -0
- data/ext/wiringpi/WiringPi/devLib/lcd128x64.h +39 -0
- data/ext/wiringpi/WiringPi/devLib/maxdetect.c +238 -0
- data/ext/wiringpi/WiringPi/devLib/maxdetect.h +40 -0
- data/ext/wiringpi/WiringPi/devLib/piFace.c +112 -0
- data/ext/wiringpi/WiringPi/devLib/piFace.h +32 -0
- data/ext/wiringpi/WiringPi/devLib/piFaceOld.c +177 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.c +118 -0
- data/ext/wiringpi/WiringPi/devLib/piGlow.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.c +113 -0
- data/ext/wiringpi/WiringPi/devLib/piNes.h +45 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhat.c +430 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhat.h +39 -0
- data/ext/wiringpi/WiringPi/devLib/scrollPhatFont.h +544 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/7segments.c +221 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/buttons.c +83 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/gertboard.c +96 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/record.c +60 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/temperature.c +78 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/voltmeter.c +73 -0
- data/ext/wiringpi/WiringPi/examples/Gertboard/vumeter.c +152 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/blink.c +59 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/buttons.c +103 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/ladder.c +337 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/metro.c +111 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/motor.c +120 -0
- data/ext/wiringpi/WiringPi/examples/PiFace/reaction.c +194 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow0.c +51 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piGlow1.c +258 -0
- data/ext/wiringpi/WiringPi/examples/PiGlow/piglow.c +176 -0
- data/ext/wiringpi/WiringPi/examples/blink-thread.c +61 -0
- data/ext/wiringpi/WiringPi/examples/blink.c +48 -0
- data/ext/wiringpi/WiringPi/examples/blink12.c +111 -0
- data/ext/wiringpi/WiringPi/examples/blink12drcs.c +125 -0
- data/ext/wiringpi/WiringPi/examples/blink6drcs.c +115 -0
- data/ext/wiringpi/WiringPi/examples/blink8-drcn.c +61 -0
- data/ext/wiringpi/WiringPi/examples/blink8.c +57 -0
- data/ext/wiringpi/WiringPi/examples/clock.c +201 -0
- data/ext/wiringpi/WiringPi/examples/delayTest.c +102 -0
- data/ext/wiringpi/WiringPi/examples/ds1302.c +238 -0
- data/ext/wiringpi/WiringPi/examples/header.h +23 -0
- data/ext/wiringpi/WiringPi/examples/isr-osc.c +118 -0
- data/ext/wiringpi/WiringPi/examples/isr.c +110 -0
- data/ext/wiringpi/WiringPi/examples/lcd-adafruit.c +347 -0
- data/ext/wiringpi/WiringPi/examples/lcd.c +286 -0
- data/ext/wiringpi/WiringPi/examples/lowPower.c +68 -0
- data/ext/wiringpi/WiringPi/examples/max31855.c +60 -0
- data/ext/wiringpi/WiringPi/examples/nes.c +67 -0
- data/ext/wiringpi/WiringPi/examples/okLed.c +82 -0
- data/ext/wiringpi/WiringPi/examples/pwm.c +58 -0
- data/ext/wiringpi/WiringPi/examples/q2w/binary.c +79 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink-io.c +61 -0
- data/ext/wiringpi/WiringPi/examples/q2w/blink.c +50 -0
- data/ext/wiringpi/WiringPi/examples/q2w/bright.c +59 -0
- data/ext/wiringpi/WiringPi/examples/q2w/button.c +63 -0
- data/ext/wiringpi/WiringPi/examples/q2w/volts.c +62 -0
- data/ext/wiringpi/WiringPi/examples/rht03.c +86 -0
- data/ext/wiringpi/WiringPi/examples/scrollPhat/scphat.c +230 -0
- data/ext/wiringpi/WiringPi/examples/scrollPhat/test.c +115 -0
- data/ext/wiringpi/WiringPi/examples/serialRead.c +48 -0
- data/ext/wiringpi/WiringPi/examples/serialTest.c +75 -0
- data/ext/wiringpi/WiringPi/examples/servo.c +57 -0
- data/ext/wiringpi/WiringPi/examples/softPwm.c +89 -0
- data/ext/wiringpi/WiringPi/examples/softTone.c +54 -0
- data/ext/wiringpi/WiringPi/examples/speed.c +95 -0
- data/ext/wiringpi/WiringPi/examples/spiSpeed.c +118 -0
- data/ext/wiringpi/WiringPi/examples/wfi.c +161 -0
- data/ext/wiringpi/WiringPi/gpio/gpio.c +1538 -0
- data/ext/wiringpi/WiringPi/gpio/readall.c +403 -0
- data/ext/wiringpi/WiringPi/version.h +3 -0
- data/ext/wiringpi/WiringPi/wiringPi/ads1115.c +293 -0
- data/ext/wiringpi/WiringPi/wiringPi/ads1115.h +55 -0
- data/ext/wiringpi/WiringPi/wiringPi/bmp180.c +237 -0
- data/ext/wiringpi/WiringPi/wiringPi/bmp180.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcNet.c +405 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcNet.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.c +196 -0
- data/ext/wiringpi/WiringPi/wiringPi/drcSerial.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/ds18b20.c +146 -0
- data/ext/wiringpi/WiringPi/wiringPi/ds18b20.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/htu21d.c +150 -0
- data/ext/wiringpi/WiringPi/wiringPi/htu21d.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.c +99 -0
- data/ext/wiringpi/WiringPi/wiringPi/max31855.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.c +84 -0
- data/ext/wiringpi/WiringPi/wiringPi/max5322.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.c +149 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23008.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.c +164 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23016reg.h +48 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.c +195 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23017.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.c +188 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s08.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.c +235 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23s17.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x08.h +73 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp23x0817.h +87 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3002.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3004.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.c +125 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp3422.h +43 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.c +76 -0
- data/ext/wiringpi/WiringPi/wiringPi/mcp4802.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.c +126 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8574.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.c +90 -0
- data/ext/wiringpi/WiringPi/wiringPi/pcf8591.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/piHiPri.c +51 -0
- data/ext/wiringpi/WiringPi/wiringPi/piThread.c +63 -0
- data/ext/wiringpi/WiringPi/wiringPi/pseudoPins.c +95 -0
- data/ext/wiringpi/WiringPi/wiringPi/pseudoPins.h +26 -0
- data/ext/wiringpi/WiringPi/wiringPi/rht03.c +252 -0
- data/ext/wiringpi/WiringPi/wiringPi/rht03.h +25 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.c +75 -0
- data/ext/wiringpi/WiringPi/wiringPi/sn3218.h +33 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.c +186 -0
- data/ext/wiringpi/WiringPi/wiringPi/softPwm.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.c +211 -0
- data/ext/wiringpi/WiringPi/wiringPi/softServo.h +35 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.c +150 -0
- data/ext/wiringpi/WiringPi/wiringPi/softTone.h +39 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.c +109 -0
- data/ext/wiringpi/WiringPi/wiringPi/sr595.h +34 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.c +2507 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPi.h +269 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.c +233 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiI2C.h +42 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.c +144 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringPiSPI.h +36 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.c +225 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringSerial.h +38 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringShift.c +83 -0
- data/ext/wiringpi/WiringPi/wiringPi/wiringShift.h +41 -0
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.c +928 -0
- data/ext/wiringpi/WiringPi/wiringPi/wpiExtensions.h +26 -0
- data/ext/wiringpi/WiringPi/wiringPiD/daemonise.c +82 -0
- data/ext/wiringpi/WiringPi/wiringPiD/daemonise.h +9 -0
- data/ext/wiringpi/WiringPi/wiringPiD/drcNetCmd.h +43 -0
- data/ext/wiringpi/WiringPi/wiringPiD/network.c +330 -0
- data/ext/wiringpi/WiringPi/wiringPiD/network.h +31 -0
- data/ext/wiringpi/WiringPi/wiringPiD/runRemote.c +126 -0
- data/ext/wiringpi/WiringPi/wiringPiD/runRemote.h +29 -0
- data/ext/wiringpi/WiringPi/wiringPiD/wiringpid.c +382 -0
- data/ext/wiringpi/wiringpi_wrap.c +636 -219
- data/lib/wiringpi.rb +25 -12
- data/lib/wiringpi/gpio.rb +90 -83
- data/lib/wiringpi/i2c.rb +29 -22
- data/lib/wiringpi/modules/mcp23017.rb +15 -0
- data/lib/wiringpi/modules/mcp23s17.rb +16 -0
- data/lib/wiringpi/scrollphat.rb +14 -14
- data/lib/wiringpi/serial.rb +8 -18
- data/lib/wiringpi/spi.rb +14 -11
- data/lib/wiringpi/watch.rb +24 -0
- metadata +160 -4
- data/lib/wiringpi/event.rb +0 -21
- data/lib/wiringpi/mcp23x17.rb +0 -31
@@ -0,0 +1,26 @@
|
|
1
|
+
/*
|
2
|
+
* extensions.h:
|
3
|
+
* Part of the GPIO program to test, peek, poke and otherwise
|
4
|
+
* noodle with the GPIO hardware on the Raspberry Pi.
|
5
|
+
* Copyright (c) 2012-2015 Gordon Henderson
|
6
|
+
***********************************************************************
|
7
|
+
* This file is part of wiringPi:
|
8
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
9
|
+
*
|
10
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
11
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
12
|
+
* the Free Software Foundation, either version 3 of the License, or
|
13
|
+
* (at your option) any later version.
|
14
|
+
*
|
15
|
+
* wiringPi is distributed in the hope that it will be useful,
|
16
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
17
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
18
|
+
* GNU Lesser General Public License for more details.
|
19
|
+
*
|
20
|
+
* You should have received a copy of the GNU Lesser General Public License
|
21
|
+
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
22
|
+
***********************************************************************
|
23
|
+
*/
|
24
|
+
|
25
|
+
|
26
|
+
extern int loadWPiExtension (char *progName, char *extensionData, int verbose) ;
|
@@ -0,0 +1,82 @@
|
|
1
|
+
/*
|
2
|
+
* daemonise.c:
|
3
|
+
* Fairly generic "Turn the current process into a daemon" code.
|
4
|
+
*
|
5
|
+
* Copyright (c) 2016-2017 Gordon Henderson.
|
6
|
+
*********************************************************************************
|
7
|
+
*/
|
8
|
+
|
9
|
+
#include <stdio.h>
|
10
|
+
#include <stdlib.h>
|
11
|
+
#include <unistd.h>
|
12
|
+
#include <syslog.h>
|
13
|
+
#include <signal.h>
|
14
|
+
#include <sys/stat.h>
|
15
|
+
|
16
|
+
#include "daemonise.h"
|
17
|
+
|
18
|
+
void daemonise (const char *pidFile)
|
19
|
+
{
|
20
|
+
pid_t pid ;
|
21
|
+
int i ;
|
22
|
+
FILE *fd ;
|
23
|
+
|
24
|
+
syslog (LOG_DAEMON | LOG_INFO, "Becoming daemon") ;
|
25
|
+
|
26
|
+
// Fork from the parent
|
27
|
+
|
28
|
+
if ((pid = fork ()) < 0)
|
29
|
+
{
|
30
|
+
syslog (LOG_DAEMON | LOG_ALERT, "Fork no. 1 failed: %m") ;
|
31
|
+
exit (EXIT_FAILURE) ;
|
32
|
+
}
|
33
|
+
|
34
|
+
if (pid > 0) // Parent - terminate
|
35
|
+
exit (EXIT_SUCCESS) ;
|
36
|
+
|
37
|
+
// Now running on the child - become session leader
|
38
|
+
|
39
|
+
if (setsid() < 0)
|
40
|
+
{
|
41
|
+
syslog (LOG_DAEMON | LOG_ALERT, "setsid failed: %m") ;
|
42
|
+
exit (EXIT_FAILURE) ;
|
43
|
+
}
|
44
|
+
|
45
|
+
// Ignore a few signals
|
46
|
+
|
47
|
+
signal (SIGCHLD, SIG_IGN) ;
|
48
|
+
signal (SIGHUP, SIG_IGN) ;
|
49
|
+
|
50
|
+
// Fork again
|
51
|
+
|
52
|
+
if ((pid = fork ()) < 0)
|
53
|
+
{
|
54
|
+
syslog (LOG_DAEMON | LOG_ALERT, "Fork no. 2 failed: %m") ;
|
55
|
+
exit (EXIT_FAILURE) ;
|
56
|
+
}
|
57
|
+
|
58
|
+
if (pid > 0) // parent - terminate
|
59
|
+
exit (EXIT_SUCCESS) ;
|
60
|
+
|
61
|
+
// Tidying up - reset umask, change to / and close all files
|
62
|
+
|
63
|
+
umask (0) ;
|
64
|
+
chdir ("/") ;
|
65
|
+
|
66
|
+
for (i = 0 ; i < sysconf (_SC_OPEN_MAX) ; ++i)
|
67
|
+
close (i) ;
|
68
|
+
|
69
|
+
// Write PID into /var/run
|
70
|
+
|
71
|
+
if (pidFile != NULL)
|
72
|
+
{
|
73
|
+
if ((fd = fopen (pidFile, "w")) == NULL)
|
74
|
+
{
|
75
|
+
syslog (LOG_DAEMON | LOG_ALERT, "Unable to write PID file: %m") ;
|
76
|
+
exit (EXIT_FAILURE) ;
|
77
|
+
}
|
78
|
+
|
79
|
+
fprintf (fd, "%d\n", getpid ()) ;
|
80
|
+
fclose (fd) ;
|
81
|
+
}
|
82
|
+
}
|
@@ -0,0 +1,9 @@
|
|
1
|
+
/*
|
2
|
+
* daemonise.h:
|
3
|
+
* Fairly generic "Turn the current process into a daemon" code.
|
4
|
+
*
|
5
|
+
* Copyright (c) 2016-2017 Gordon Henderson.
|
6
|
+
*********************************************************************************
|
7
|
+
*/
|
8
|
+
|
9
|
+
extern void daemonise (const char *pidFile) ;
|
@@ -0,0 +1,43 @@
|
|
1
|
+
/*
|
2
|
+
* drcNetCmd.c:
|
3
|
+
* Copyright (c) 2012-2017 Gordon Henderson
|
4
|
+
***********************************************************************
|
5
|
+
* This file is part of wiringPi:
|
6
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
7
|
+
*
|
8
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
9
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
10
|
+
* the Free Software Foundation, either version 3 of the License, or
|
11
|
+
* (at your option) any later version.
|
12
|
+
*
|
13
|
+
* wiringPi is distributed in the hope that it will be useful,
|
14
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
15
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
16
|
+
* GNU Lesser General Public License for more details.
|
17
|
+
*
|
18
|
+
* You should have received a copy of the GNU Lesser General Public License
|
19
|
+
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
20
|
+
***********************************************************************
|
21
|
+
*/
|
22
|
+
|
23
|
+
#define DEFAULT_SERVER_PORT 6124
|
24
|
+
|
25
|
+
#define DRCN_PIN_MODE 1
|
26
|
+
#define DRCN_PULL_UP_DN 2
|
27
|
+
|
28
|
+
#define DRCN_DIGITAL_WRITE 3
|
29
|
+
#define DRCN_DIGITAL_WRITE8 4
|
30
|
+
#define DRCN_ANALOG_WRITE 5
|
31
|
+
#define DRCN_PWM_WRITE 6
|
32
|
+
|
33
|
+
#define DRCN_DIGITAL_READ 7
|
34
|
+
#define DRCN_DIGITAL_READ8 8
|
35
|
+
#define DRCN_ANALOG_READ 9
|
36
|
+
|
37
|
+
|
38
|
+
struct drcNetComStruct
|
39
|
+
{
|
40
|
+
uint32_t pin ;
|
41
|
+
uint32_t cmd ;
|
42
|
+
uint32_t data ;
|
43
|
+
};
|
@@ -0,0 +1,330 @@
|
|
1
|
+
/*
|
2
|
+
* network.c:
|
3
|
+
* Part of wiringPiD
|
4
|
+
* Copyright (c) 2012-2017 Gordon Henderson
|
5
|
+
***********************************************************************
|
6
|
+
* This file is part of wiringPi:
|
7
|
+
* https://projects.drogon.net/raspberry-pi/wiringpi/
|
8
|
+
*
|
9
|
+
* wiringPi is free software: you can redistribute it and/or modify
|
10
|
+
* it under the terms of the GNU Lesser General Public License as published by
|
11
|
+
* the Free Software Foundation, either version 3 of the License, or
|
12
|
+
* (at your option) any later version.
|
13
|
+
*
|
14
|
+
* wiringPi is distributed in the hope that it will be useful,
|
15
|
+
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
16
|
+
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
17
|
+
* GNU Lesser General Public License for more details.
|
18
|
+
*
|
19
|
+
* You should have received a copy of the GNU Lesser General Public License
|
20
|
+
* along with wiringPi. If not, see <http://www.gnu.org/licenses/>.
|
21
|
+
***********************************************************************
|
22
|
+
*/
|
23
|
+
|
24
|
+
#include <sys/socket.h>
|
25
|
+
#include <netinet/in.h>
|
26
|
+
#include <arpa/inet.h>
|
27
|
+
#include <stdio.h>
|
28
|
+
#include <stdlib.h>
|
29
|
+
#include <unistd.h>
|
30
|
+
#include <string.h>
|
31
|
+
#include <stdarg.h>
|
32
|
+
#include <malloc.h>
|
33
|
+
|
34
|
+
#include <fcntl.h>
|
35
|
+
#include <crypt.h>
|
36
|
+
|
37
|
+
#include "network.h"
|
38
|
+
|
39
|
+
#define TRUE (1==1)
|
40
|
+
#define FALSE (!TRUE)
|
41
|
+
|
42
|
+
// Local data
|
43
|
+
|
44
|
+
#define SALT_LEN 16
|
45
|
+
|
46
|
+
static char salt [SALT_LEN + 1] ;
|
47
|
+
static char *returnedHash = NULL ;
|
48
|
+
static int serverFd = -1 ;
|
49
|
+
|
50
|
+
// Union for the server Socket Address
|
51
|
+
|
52
|
+
static union
|
53
|
+
{
|
54
|
+
struct sockaddr_in sin ;
|
55
|
+
struct sockaddr_in6 sin6 ;
|
56
|
+
} serverSockAddr ;
|
57
|
+
|
58
|
+
// and client address
|
59
|
+
|
60
|
+
static union
|
61
|
+
{
|
62
|
+
struct sockaddr_in sin ;
|
63
|
+
struct sockaddr_in6 sin6 ;
|
64
|
+
} clientSockAddr ;
|
65
|
+
|
66
|
+
|
67
|
+
/*
|
68
|
+
* getClientIP:
|
69
|
+
* Returns a pointer to a static string containing the clients IP address
|
70
|
+
*********************************************************************************
|
71
|
+
*/
|
72
|
+
|
73
|
+
char *getClientIP (void)
|
74
|
+
{
|
75
|
+
char buf [INET6_ADDRSTRLEN] ;
|
76
|
+
static char ipAddress [1024] ;
|
77
|
+
|
78
|
+
if (clientSockAddr.sin.sin_family == AF_INET) // IPv4
|
79
|
+
{
|
80
|
+
if (snprintf (ipAddress, 1024, "IPv4: %s",
|
81
|
+
inet_ntop (clientSockAddr.sin.sin_family, (void *)&clientSockAddr.sin.sin_addr, buf, sizeof (buf))) == 1024)
|
82
|
+
strcpy (ipAddress, "Too long") ;
|
83
|
+
}
|
84
|
+
else // IPv6
|
85
|
+
{
|
86
|
+
if (clientSockAddr.sin.sin_family == AF_INET6 && IN6_IS_ADDR_V4MAPPED (&clientSockAddr.sin6.sin6_addr))
|
87
|
+
{
|
88
|
+
if (snprintf (ipAddress, 1024, "IPv4in6: %s",
|
89
|
+
inet_ntop (clientSockAddr.sin.sin_family, (char *)&clientSockAddr.sin6.sin6_addr, buf, sizeof(buf))) == 1024)
|
90
|
+
strcpy (ipAddress, "Too long") ;
|
91
|
+
}
|
92
|
+
else
|
93
|
+
{
|
94
|
+
if (snprintf (ipAddress, 1024, "IPv6: %s",
|
95
|
+
inet_ntop (clientSockAddr.sin.sin_family, (char *)&clientSockAddr.sin6.sin6_addr, buf, sizeof(buf))) == 1024)
|
96
|
+
strcpy (ipAddress, "Too long") ;
|
97
|
+
}
|
98
|
+
}
|
99
|
+
|
100
|
+
return ipAddress ;
|
101
|
+
}
|
102
|
+
|
103
|
+
|
104
|
+
|
105
|
+
/*
|
106
|
+
* clientPstr: clientPrintf:
|
107
|
+
* Print over a network socket
|
108
|
+
*********************************************************************************
|
109
|
+
*/
|
110
|
+
|
111
|
+
static int clientPstr (int fd, char *s)
|
112
|
+
{
|
113
|
+
int len = strlen (s) ;
|
114
|
+
return (write (fd, s, len) == len) ? 0 : -1 ;
|
115
|
+
}
|
116
|
+
|
117
|
+
static int clientPrintf (const int fd, const char *message, ...)
|
118
|
+
{
|
119
|
+
va_list argp ;
|
120
|
+
char buffer [1024] ;
|
121
|
+
|
122
|
+
va_start (argp, message) ;
|
123
|
+
vsnprintf (buffer, 1023, message, argp) ;
|
124
|
+
va_end (argp) ;
|
125
|
+
|
126
|
+
return clientPstr (fd, buffer) ;
|
127
|
+
}
|
128
|
+
|
129
|
+
|
130
|
+
/*
|
131
|
+
* sendGreeting:
|
132
|
+
* Send some text to the client device
|
133
|
+
*********************************************************************************
|
134
|
+
*/
|
135
|
+
|
136
|
+
int sendGreeting (int clientFd)
|
137
|
+
{
|
138
|
+
if (clientPrintf (clientFd, "200 Welcome to wiringPiD - http://wiringpi.com/\n") < 0)
|
139
|
+
return -1 ;
|
140
|
+
|
141
|
+
return clientPrintf (clientFd, "200 Connecting from: %s\n", getClientIP ()) ;
|
142
|
+
}
|
143
|
+
|
144
|
+
|
145
|
+
/*
|
146
|
+
* getSalt:
|
147
|
+
* Create a random 'salt' value for the password encryption process
|
148
|
+
*********************************************************************************
|
149
|
+
*/
|
150
|
+
|
151
|
+
static int getSalt (char drySalt [])
|
152
|
+
{
|
153
|
+
static const char *seaDog = "abcdefghijklmnopqrstuvwxyz"
|
154
|
+
"ABCDEFGHIJKLMNOPQRSTUVWXYZ"
|
155
|
+
"0123456789/." ;
|
156
|
+
|
157
|
+
unsigned char wetSalt [SALT_LEN] ;
|
158
|
+
int i, fd ;
|
159
|
+
|
160
|
+
if ((fd = open ("/dev/urandom", O_RDONLY)) < 0)
|
161
|
+
return fd ;
|
162
|
+
|
163
|
+
if (read (fd, wetSalt, SALT_LEN) != SALT_LEN)
|
164
|
+
return -1 ;
|
165
|
+
|
166
|
+
close (fd) ;
|
167
|
+
|
168
|
+
for (i = 0 ; i < SALT_LEN ; ++i)
|
169
|
+
drySalt [i] = seaDog [wetSalt [i] & 63] ;
|
170
|
+
|
171
|
+
drySalt [SALT_LEN] = 0 ;
|
172
|
+
|
173
|
+
return 0 ;
|
174
|
+
}
|
175
|
+
|
176
|
+
|
177
|
+
/*
|
178
|
+
* sendChallenge:
|
179
|
+
* Create and send our salt (aka nonce) to the remote device
|
180
|
+
*********************************************************************************
|
181
|
+
*/
|
182
|
+
|
183
|
+
int sendChallenge (int clientFd)
|
184
|
+
{
|
185
|
+
if (getSalt (salt) < 0)
|
186
|
+
return -1 ;
|
187
|
+
|
188
|
+
return clientPrintf (clientFd, "Challenge %s\n", salt) ;
|
189
|
+
}
|
190
|
+
|
191
|
+
|
192
|
+
/*
|
193
|
+
* getResponse:
|
194
|
+
* Read the encrypted password from the remote device.
|
195
|
+
*********************************************************************************
|
196
|
+
*/
|
197
|
+
|
198
|
+
|
199
|
+
int getResponse (int clientFd)
|
200
|
+
{
|
201
|
+
char reply [1024] ;
|
202
|
+
int len ;
|
203
|
+
|
204
|
+
// Being sort of lazy about this. I'm expecting an SHA-512 hash back and these
|
205
|
+
// are exactly 86 characters long, so no reason not to, I guess...
|
206
|
+
|
207
|
+
len = 86 ;
|
208
|
+
|
209
|
+
if (setsockopt (clientFd, SOL_SOCKET, SO_RCVLOWAT, (void *)&len, sizeof (len)) < 0)
|
210
|
+
return -1 ;
|
211
|
+
|
212
|
+
len = recv (clientFd, reply, 86, 0) ;
|
213
|
+
if (len != 86)
|
214
|
+
return -1 ;
|
215
|
+
|
216
|
+
reply [len] = 0 ;
|
217
|
+
|
218
|
+
if ((returnedHash = malloc (len + 1)) == NULL)
|
219
|
+
return -1 ;
|
220
|
+
|
221
|
+
strcpy (returnedHash, reply) ;
|
222
|
+
|
223
|
+
return 0 ;
|
224
|
+
}
|
225
|
+
|
226
|
+
|
227
|
+
/*
|
228
|
+
* passwordMatch:
|
229
|
+
* See if there's a match. If not, we simply dump them.
|
230
|
+
*********************************************************************************
|
231
|
+
*/
|
232
|
+
|
233
|
+
int passwordMatch (const char *password)
|
234
|
+
{
|
235
|
+
char *encrypted ;
|
236
|
+
char salted [1024] ;
|
237
|
+
|
238
|
+
sprintf (salted, "$6$%s$", salt) ;
|
239
|
+
|
240
|
+
encrypted = crypt (password, salted) ;
|
241
|
+
|
242
|
+
// 20: $6$ then 16 characters of salt, then $
|
243
|
+
// 86 is the length of an SHA-512 hash
|
244
|
+
|
245
|
+
return strncmp (encrypted + 20, returnedHash, 86) == 0 ;
|
246
|
+
}
|
247
|
+
|
248
|
+
|
249
|
+
/*
|
250
|
+
* setupServer:
|
251
|
+
* Do what's needed to create a local server socket instance that can listen
|
252
|
+
* on both IPv4 and IPv6 interfaces.
|
253
|
+
*********************************************************************************
|
254
|
+
*/
|
255
|
+
|
256
|
+
int setupServer (int serverPort)
|
257
|
+
{
|
258
|
+
socklen_t clientSockAddrSize = sizeof (clientSockAddr) ;
|
259
|
+
|
260
|
+
int on = 1 ;
|
261
|
+
int family ;
|
262
|
+
socklen_t serverSockAddrSize ;
|
263
|
+
int clientFd ;
|
264
|
+
|
265
|
+
// Try to create an IPv6 socket
|
266
|
+
|
267
|
+
serverFd = socket (PF_INET6, SOCK_STREAM, 0) ;
|
268
|
+
|
269
|
+
// If it didn't work, then fall-back to IPv4.
|
270
|
+
|
271
|
+
if (serverFd < 0)
|
272
|
+
{
|
273
|
+
if ((serverFd = socket (PF_INET, SOCK_STREAM, 0)) < 0)
|
274
|
+
return -1 ;
|
275
|
+
|
276
|
+
family = AF_INET ;
|
277
|
+
serverSockAddrSize = sizeof (struct sockaddr_in) ;
|
278
|
+
}
|
279
|
+
else // We got an IPv6 socket
|
280
|
+
{
|
281
|
+
family = AF_INET6 ;
|
282
|
+
serverSockAddrSize = sizeof (struct sockaddr_in6) ;
|
283
|
+
}
|
284
|
+
|
285
|
+
if (setsockopt (serverFd, SOL_SOCKET, SO_REUSEADDR, &on, sizeof (on)) < 0)
|
286
|
+
return -1 ;
|
287
|
+
|
288
|
+
// Setup the servers socket address - cope with IPv4 and v6.
|
289
|
+
|
290
|
+
memset (&serverSockAddr, 0, sizeof (serverSockAddr)) ;
|
291
|
+
switch (family)
|
292
|
+
{
|
293
|
+
case AF_INET:
|
294
|
+
serverSockAddr.sin.sin_family = AF_INET ;
|
295
|
+
serverSockAddr.sin.sin_addr.s_addr = htonl (INADDR_ANY) ;
|
296
|
+
serverSockAddr.sin.sin_port = htons (serverPort) ;
|
297
|
+
break;
|
298
|
+
|
299
|
+
case AF_INET6:
|
300
|
+
serverSockAddr.sin6.sin6_family = AF_INET6 ;
|
301
|
+
serverSockAddr.sin6.sin6_addr = in6addr_any ;
|
302
|
+
serverSockAddr.sin6.sin6_port = htons (serverPort) ;
|
303
|
+
}
|
304
|
+
|
305
|
+
// Bind, listen and accept
|
306
|
+
|
307
|
+
if (bind (serverFd, (struct sockaddr *)&serverSockAddr, serverSockAddrSize) < 0)
|
308
|
+
return -1 ;
|
309
|
+
|
310
|
+
if (listen (serverFd, 4) < 0) // Really only going to talk to one client at a time...
|
311
|
+
return -1 ;
|
312
|
+
|
313
|
+
if ((clientFd = accept (serverFd, (struct sockaddr *)&clientSockAddr, &clientSockAddrSize)) < 0)
|
314
|
+
return -1 ;
|
315
|
+
|
316
|
+
return clientFd ;
|
317
|
+
}
|
318
|
+
|
319
|
+
|
320
|
+
/*
|
321
|
+
* closeServer:
|
322
|
+
*********************************************************************************
|
323
|
+
*/
|
324
|
+
|
325
|
+
void closeServer (int clientFd)
|
326
|
+
{
|
327
|
+
if (serverFd != -1) close (serverFd) ;
|
328
|
+
if (clientFd != -1) close (clientFd) ;
|
329
|
+
serverFd = clientFd = -1 ;
|
330
|
+
}
|