3 Version 0.03 07.02.2002
8 #include <sys/socket.h>
10 #include <netinet/in.h>
16 #include "mg_net_api.h"
20 static int mg_s; /* socket descriptor */
21 static struct sockaddr_in mg_myaddr_in; /* for local socket address */
22 static struct sockaddr_in mg_servaddr_in; /* for server socket address */
23 static char mg_password[8]; /* passwd for udp-requsts */
28 * This routine is the signal handler for the alarm signal.
29 * It simply re-installs itself as the handler and returns.
33 signal(SIGALRM, handler);
36 /* Send the request to the server. */
37 /* and wait for response */
38 static int send_rq(MG_api_request *rq)
42 strncpy(rq->passwd,mg_password,8);
44 if (sendto (mg_s, rq, sizeof(*rq), 0, (struct sockaddr *) &mg_servaddr_in,
45 sizeof(struct sockaddr_in)) == -1) {
50 /* Set up alarm signal handler. */
51 signal(SIGALRM, handler);
53 /* Set up a timeout so I don't hang in case the packet
54 * gets lost. After all, UDP does not guarantee
59 /* Wait for the reply to come in. We assume that
60 * no messages will come from any other source,
61 * so that we do not need to do a recvfrom nor
62 * check the responder's address.
65 /* RTH FIXME: TIMEOUT DONT WORK !! */
66 ret = recv (mg_s, rq, sizeof(*rq), 0);
69 /* Alarm went off and aborted the receive. */
71 return(MG_ERROR_TIMEOUT);
96 int mg_version(MG_rq_version *version)
109 memcpy(version, rq.buffer, sizeof(*version));
114 int mg_identify(MG_rq_identify *ident)
119 rq.cmd = MG_IDENTIFY;
127 memcpy(ident, rq.buffer, sizeof(*ident));
132 int mg_memorystatus(MG_rq_memorystatus *stats)
137 rq.cmd = MG_MEMORYSTATUS;
138 memcpy(rq.buffer, stats, sizeof(*stats));
146 memcpy(stats, rq.buffer, sizeof(*stats));
151 int mg_get_memory_location(MG_rq_phonebook_location *rph)
156 rq.cmd = MG_GET_MEMORY_LOCATION;
157 memcpy(rq.buffer, rph, sizeof(*rph));
165 memcpy(rph, rq.buffer, sizeof(*rph));
170 int mg_write_phonebook_location(MG_rq_phonebook_location *wph)
175 rq.cmd = MG_WRITE_PHONEBOOK_LOCATION;
176 memcpy(rq.buffer, wph, sizeof(*wph));
184 memcpy(wph, rq.buffer, sizeof(*wph));
189 int mg_delete_phonebook_location(MG_rq_phonebook_location *dph)
194 rq.cmd = MG_DELETE_PHONEBOOK_LOCATION;
195 memcpy(rq.buffer, dph, sizeof(*dph));
203 memcpy(dph, rq.buffer, sizeof(*dph));
208 int mg_send_sms(MG_rq_send_sms *sms)
213 rq.cmd = MG_SEND_SMS;
214 memcpy(rq.buffer, sms, sizeof(*sms));
222 memcpy(sms, rq.buffer, sizeof(*sms));
230 Input: Hostname of mygnokii-server
231 Output: connected udp-port on success, else -1
233 int mg_init(char *hostname, char *passwd)
236 struct hostent *hp; /* pointer to host info for nameserver host */
237 struct servent *sp; /* pointer to service information */
240 strncpy(mg_password,passwd,8);
242 /* clear out address structures */
243 memset ((char *)&mg_myaddr_in, 0, sizeof(struct sockaddr_in));
244 memset ((char *)&mg_servaddr_in, 0, sizeof(struct sockaddr_in));
246 /* Set up the server address. */
247 mg_servaddr_in.sin_family = AF_INET;
249 /* Get the host information for the server's hostname that the
252 hp = gethostbyname (hostname);
257 mg_servaddr_in.sin_addr.s_addr = ((struct in_addr *)(hp->h_addr))->s_addr;
258 /* Find the information for the "example" server
259 * in order to get the needed port number.
261 sp = getservbyname ("mygnokii", "udp");
263 /* not found -> use default port 50963 */
264 mg_servaddr_in.sin_port = htons(50963);
266 else mg_servaddr_in.sin_port = sp->s_port;
268 /* Create the socket. */
269 mg_s = socket (AF_INET, SOCK_DGRAM, 0);
274 /* Bind socket to some local address so that the
275 * server can send the reply back. A port number
276 * of zero will be used so that the system will
277 * assign any available port number. An address
278 * of INADDR_ANY will be used so we do not have to
279 * look up the internet address of the local host.
281 mg_myaddr_in.sin_family = AF_INET;
282 mg_myaddr_in.sin_port = 0;
283 mg_myaddr_in.sin_addr.s_addr = INADDR_ANY;
284 if (bind(mg_s, (struct sockaddr *) &mg_myaddr_in, sizeof(struct sockaddr_in)) == -1) {
288 /* return portnumber */
289 return(mg_servaddr_in.sin_port);