Subversion Repositories Spectranet

[/] [trunk/] [utils/] [configserv.c] - Blame information for rev 495

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 423 winston
/*
2
;The MIT License
3
;
4
;Copyright (c) 2011 Dylan Smith
5
;
6
;Permission is hereby granted, free of charge, to any person obtaining a copy
7
;of this software and associated documentation files (the "Software"), to deal
8
;in the Software without restriction, including without limitation the rights
9
;to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10
;copies of the Software, and to permit persons to whom the Software is
11
;furnished to do so, subject to the following conditions:
12
;
13
;The above copyright notice and this permission notice shall be included in
14
;all copies or substantial portions of the Software.
15
;
16
;THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17
;IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18
;FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19
;AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20
;LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21
;OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22
;THE SOFTWARE.
23
*/
24
 
25
/* Configuration server for initial Spectranet setup.
26
 * It is very simple, it merely sends a MAC address and hostname with
27
 * each connection, then disconnects.
28
 * Listens on tcp/2001
29
 *
30
 * During initial configuration the Spectrum has a hard-coded MAC
31
 * address and IP for the purposes of querying this server. */
32
 
33
#include <stdio.h>
34
#include <sys/types.h>
35
#include <unistd.h>
36
#include <stdlib.h>
37
#include <string.h>
38
 
39 495 winston
#ifdef WIN32
40
#include <winsock.h>
41
#else
42
#include <sys/socket.h>
43
#include <netinet/in.h>
44
#endif
45
 
46 423 winston
#define DATASZ  24
47
 
48 426 winston
void server(char *file, int port);
49
void getdata(char *file, char *data);
50 423 winston
 
51
int main(int argc, char **argv) {
52 426 winston
        if(argc < 2) {
53
                fprintf(stderr, "Usage: %s <serialfile>\n", argv[0]);
54
                exit(-1);
55
        }
56
        server(argv[1], 2001);
57 423 winston
        return 0;
58
}
59
 
60 426 winston
void server(char *file, int port) {
61 423 winston
        int sockfd, connfd, bytes;
62
        struct sockaddr_in my_addr;
63
        char cfgdata[DATASZ];
64
 
65
        sockfd=socket(AF_INET, SOCK_STREAM, 0);
66
        if(sockfd<0) {
67
                perror("socket");
68
                exit(-1);
69
        }
70
 
71
        memset(&my_addr, 0, sizeof(my_addr));
72
        my_addr.sin_family=AF_INET;
73
        my_addr.sin_port=htons(port);
74
        if(bind(sockfd, (struct sockaddr *)&my_addr, sizeof(my_addr)) < 0) {
75
                perror("bind");
76
                exit(-1);
77
        }
78
 
79
        if(listen(sockfd, 1) < 0) {
80
                perror("listen");
81
                exit(-1);
82
        }
83
 
84
        while((connfd=accept(sockfd, NULL, NULL)) > 0) {
85 426 winston
                getdata(file, cfgdata);
86 423 winston
                bytes=send(connfd, cfgdata, DATASZ, 0);
87
                if(bytes < DATASZ) {
88
                        fprintf(stderr,
89
                        "warning: sent fewer bytes than expected");
90
                }
91
                close(connfd);
92
        }
93
}
94
 
95 426 winston
void getdata(char *file, char *data) {
96 423 winston
        FILE *stream;
97
        long mac;
98
        int serno;
99
 
100 426 winston
        if((stream=fopen(file, "rb")) != NULL) {
101 423 winston
                fscanf(stream, "%d", &serno);
102
                fclose(stream);
103 426 winston
                if((stream=fopen(file, "wb")) != NULL) {
104 423 winston
                        fprintf(stream, "%d", serno+1);
105
                        fclose(stream);
106
                } else {
107
                        perror("writing serial number");
108
                        exit(-1);
109
                }
110
        } else {
111
                perror("reading serial number");
112
                exit(-1);
113
        }
114
 
115 495 winston
        mac=rand();
116 423 winston
 
117
        data[0]=0x00;
118
        data[1]=0xAA;
119
        data[2]=mac & 0xFF;
120
        data[3]=(mac >> 8) & 0xFF;
121
        data[4]=(mac >> 16) & 0xFF;
122
        data[5]=(mac >> 24) & 0xFF;
123
        sprintf(&data[6], "spectranet%d", serno);
124
}
125
 
126